-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
54 lines (48 loc) · 1.68 KB
/
index.js
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
49
50
51
52
53
54
class TypeOrmAdapter {
constructor(connection) {
this.connection = connection;
}
build(Model, props) {
const model = new Model(props);
return model;
}
async save(model, Model) {
return this.connection.manager.save(model);
}
async destroy(model, Model) {
const manager = this.connection.manager;
const modelRepo = manager.getRepository(Model);
try {
if (this.connection.options.type === 'sqlite') {
await manager.query('PRAGMA foreign_keys = OFF;');
} else if (this.connection.options.type === 'postgres') {
await manager.query(`BEGIN;
ALTER TABLE "${modelRepo.metadata.tableName}" DISABLE TRIGGER ALL;`);
} else {
await manager.query('SET FOREIGN_KEY_CHECKS=0;');
}
await manager.delete(Model, model.id ? model.id : model.api_id);
if (this.connection.options.type === 'sqlite') {
return manager.query('PRAGMA foreign_keys = ON;');
} else if (this.connection.options.type === 'postgres') {
await manager.query(`
ALTER TABLE "${modelRepo.metadata.tableName}" ENABLE TRIGGER ALL;
COMMIT;`);
} else {
return manager.query('SET FOREIGN_KEY_CHECKS=1;');
}
} catch (err) {
return;
}
}
get(model, attr, Model) {
return model[attr];
}
set(props, model, Model) {
Object.keys(props).forEach((key) => {
model[key] = props[key];
});
return model;
}
}
module.exports = TypeOrmAdapter;