Skip to content

Commit

Permalink
Hard-delete resources
Browse files Browse the repository at this point in the history
  • Loading branch information
netervati committed Jun 16, 2024
1 parent 8f5b495 commit c492256
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
4 changes: 2 additions & 2 deletions server/routes/apps/[id].delete.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import AppServices from '~~/server/services/appServices';

export default defineEventHandler(async (event) => {
const deleted = await new AppServices(event).delete(
await new AppServices(event).delete(

Check warning on line 4 in server/routes/apps/[id].delete.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Replace `⏎····event.context.params?.id·??·''⏎··` with `event.context.params?.id·??·''`
event.context.params?.id ?? ''
);

return deleted;
return null;
});
4 changes: 2 additions & 2 deletions server/routes/models/[id].delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default defineEventHandler(async (event) => {
const { appId } = await extractAppKey(event, query.apiKey);
const modelId = event.context.params?.id ?? '';

const model = await new ModelServices(event).delete({
await new ModelServices(event).delete({
id: modelId,
appId,
});

return model;
return null;
});
4 changes: 2 additions & 2 deletions server/routes/models/[id]/model-data.delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export default defineEventHandler(async (event) => {

const ids = query.ids.split(',');

const deleted = await new ModelDataServices(event).bulkDelete({
await new ModelDataServices(event).bulkDelete({
ids,
modelId: event.context.params?.id ?? '',
});

return deleted;
return null;
});
12 changes: 3 additions & 9 deletions server/services/appServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@ export default class AppServices extends SupabaseService {
async delete(id: string) {
const apps = await this.client

Check warning on line 23 in server/services/appServices.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Replace `⏎······.from('apps')⏎······.delete()⏎······` with `.from('apps').delete()`
.from('apps')
.update({
deleted_at: new Date().toISOString(),
})
.eq('id', id)
.select('*');
.delete()
.eq('id', id);

if (apps.error !== null) {
throw ErrorResponse.supabase(apps.error);
}

return apps.data[0];
return null;
}

async find(id: string) {
const apps = await this.client
.from('apps')
.select('*')
.is('deleted_at', null)
.eq('id', id)
.eq('user_id', this.user.id);

Expand All @@ -58,7 +54,6 @@ export default class AppServices extends SupabaseService {
const app = await this.client
.from('apps')
.select('*')
.is('deleted_at', null)
.eq('title', title)
.eq('user_id', this.user.id);

Expand All @@ -73,7 +68,6 @@ export default class AppServices extends SupabaseService {
const apps = await this.client
.from('apps')
.select('id, title, description, app_keys(api_key)')
.is('deleted_at', null)
.is('app_keys.deleted_at', null)
.eq('user_id', this.user.id)
.order('created_at', { ascending: false });
Expand Down
16 changes: 9 additions & 7 deletions server/services/modelDataServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ export default class ModelDataService extends SupabaseService {
params.ids.map((id) => {
return this.client
.from('model_data')
.update({
deleted_at: new Date().toISOString(),
})
.delete()
.eq('id', id)
.eq('model_id', params.modelId)
.select('*');
.eq('model_id', params.modelId);
})
);
}
Expand All @@ -35,7 +32,6 @@ export default class ModelDataService extends SupabaseService {
const modelData = await this.client
.from('model_data')
.select('*', { count: 'exact', head: true })
.is('deleted_at', null)
.eq('model_id', modelId)
.order('created_at', { ascending: false });

Expand All @@ -46,11 +42,17 @@ export default class ModelDataService extends SupabaseService {
return modelData.count ?? 0;
}

async deleteByModelId(modelId: string) {
return await this.client
.from('model_data')

Check warning on line 47 in server/services/modelDataServices.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Insert `··`
.delete()

Check warning on line 48 in server/services/modelDataServices.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Insert `··`
.eq('model_id', modelId);

Check warning on line 49 in server/services/modelDataServices.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Insert `··`
}

async list(modelId: string) {
const modelData = await this.client
.from('model_data')
.select('id, schema')
.is('deleted_at', null)
.eq('model_id', modelId)
.order('created_at', { ascending: false });

Expand Down
12 changes: 3 additions & 9 deletions server/services/modelServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,22 @@ export default class ModelServices extends SupabaseService {
async delete(params: { id: string; appId: string }) {
const model = await this.client
.from('models')
.update({
deleted_at: new Date().toISOString(),
})
.delete()
.eq('id', params.id)
.eq('app_id', params.appId)
.eq('user_id', this.user.id)
.select('*');
.eq('user_id', this.user.id);

if (model.error !== null) {
throw ErrorResponse.supabase(model.error);
}

return model.data[0];
return null;
}

async findByName(params: { name: string; appId: string }) {
const models = await this.client
.from('models')
.select('*')
.is('deleted_at', null)
.eq('name', params.name)
.eq('app_id', params.appId)
.eq('user_id', this.user.id);
Expand All @@ -60,7 +56,6 @@ export default class ModelServices extends SupabaseService {
const models = await this.client
.from('models')
.select('*')
.is('deleted_at', null)
.eq('id', id)
.eq('user_id', this.user.id);

Expand All @@ -79,7 +74,6 @@ export default class ModelServices extends SupabaseService {
const models = await this.client
.from('models')
.select('id, name, schema')
.is('deleted_at', null)
.eq('app_id', appId)
.eq('user_id', this.user.id)
.order('created_at', { ascending: false });
Expand Down
3 changes: 3 additions & 0 deletions supabase/migrations/20240616150923_remove_deleted_at.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table models drop column deleted_at;
alter table model_data drop column deleted_at;
alter table apps drop column deleted_at;

0 comments on commit c492256

Please sign in to comment.