-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtypeormParams.js
More file actions
77 lines (71 loc) · 1.88 KB
/
typeormParams.js
File metadata and controls
77 lines (71 loc) · 1.88 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { deleteUndefineds } from '../utils/common';
export const typeormParams = (param, req, res) => {
switch (param.type) {
case 'typeorm.findOne': {
const { db } = req.app.locals;
const { entity, ...rest } = param.content;
const where = deleteUndefineds({ ...rest.where });
db.getRepository(entity)
.findOne({
...rest,
where,
})
.then((result) => {
res.json(result);
});
break;
}
case 'typeorm.find': {
const { db } = req.app.locals;
const { entity, ...rest } = param.content;
const where = deleteUndefineds({ ...rest.where });
db.getRepository(entity)
.find({
...rest,
where,
})
.then((result) => {
res.json(result);
});
break;
}
case 'typeorm.create': {
const { db } = req.app.locals;
const repo = db.getRepository(param.content.entity);
const newDocument = param.content.fields(req);
repo.save(newDocument).then((saved) => {
res.json(saved);
});
break;
}
case 'typeorm.delete': {
try {
const { db } = req.app.locals;
const params = param.content.fields(req);
const repo = db.getRepository(param.content.entity);
const where = deleteUndefineds(params);
const isID = !!params.id;
repo
.findOne(isID ? params.id : where)
.then((found) => {
if (!found) {
res.send('Nothing to remove');
return;
}
repo.remove(found).then(() => {
res.json('Removed');
});
})
.catch((err) => {
res.statusCode = 500;
res.end(err.message);
});
} catch (error) {
console.error(error.message);
}
break;
}
default:
break;
}
};