-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.ts
More file actions
48 lines (41 loc) · 1.36 KB
/
delete.ts
File metadata and controls
48 lines (41 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Transaction } from 'sequelize';
import { DeleteInstruction } from '../../types/dml';
import { DMLRepository } from '../../repositories/dml-repository';
import MetadataTableRepository from '../../repositories/metadata-table-repository';
import MetadataColumnRepository from '../../repositories/metadata-column-repository';
import {
parseAndValidateCondition,
validIdentifier,
} from '../../utils/validation';
export class DeleteOperation {
static async execute(
instruction: DeleteInstruction,
transaction: Transaction,
) {
const { table, condition, params } = instruction;
if (!validIdentifier(table))
throw new Error(`Invalid table name: ${table}`);
const metadataTable = await MetadataTableRepository.findOne(
{ table_name: table },
transaction,
);
if (!metadataTable) throw new Error(`Table ${table} does not exist`);
const metadataColumns = await MetadataColumnRepository.findAll(
{ table_id: metadataTable.id },
transaction,
);
const parsedCondition = condition
? parseAndValidateCondition(condition, metadataColumns)
: {};
const result = await DMLRepository.delete(
table,
parsedCondition,
params,
transaction,
);
await transaction.afterCommit(() => {
console.log(`Data deleted from ${table} successfully`);
});
return result;
}
}