|
| 1 | +import { ModelCtor } from 'sequelize'; |
1 | 2 | import { MyError } from '@boringcodes/utils/error'; |
2 | 3 |
|
3 | 4 | import { <%= compNamePascalCase %> } from './types'; |
4 | | -import Model, { Instance } from './model'; |
| 5 | +import model, { Model } from './model'; |
| 6 | +import postgres from '../../db/postgres'; |
| 7 | + |
| 8 | +// get model |
| 9 | +const getModel = (): ModelCtor<Model> => { |
| 10 | + return postgres.getModel<Model>(model); |
| 11 | +}; |
5 | 12 |
|
6 | 13 | const list = async (): Promise<<%= compNamePascalCase %>[]> => { |
7 | | - // list instances |
8 | | - const instances = await Model.findAll(); |
| 14 | + // list documents |
| 15 | + const documents = await getModel().findAll(); |
9 | 16 |
|
10 | | - return instances.map(transform); |
| 17 | + return documents.map(transform); |
11 | 18 | }; |
12 | 19 |
|
13 | 20 | const create = async (object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => { |
14 | | - // create instance |
15 | | - const instance = Model.build(object); |
16 | | - await instance.save(); |
| 21 | + // create document |
| 22 | + const document = getModel().build(object); |
| 23 | + await document.save(); |
17 | 24 |
|
18 | | - return transform(instance); |
| 25 | + return transform(document); |
19 | 26 | }; |
20 | 27 |
|
21 | 28 | const get = async (id: number): Promise<<%= compNamePascalCase %>> => { |
22 | | - // get instance |
23 | | - const instance = await Model.findByPk(id); |
24 | | - if (instance === null) { |
25 | | - throw new MyError('Instance not found'); |
| 29 | + // get document |
| 30 | + const document = await getModel().findByPk(id); |
| 31 | + if (document === null) { |
| 32 | + throw new MyError('Document not found'); |
26 | 33 | } |
27 | 34 |
|
28 | | - return transform(instance); |
| 35 | + return transform(document); |
29 | 36 | }; |
30 | 37 |
|
31 | 38 | const update = async ( |
32 | 39 | id: number, |
33 | 40 | object: Omit<<%= compNamePascalCase %>, 'id'>, |
34 | 41 | ): Promise<<%= compNamePascalCase %>> => { |
35 | | - // get instance |
36 | | - const instance = await Model.findByPk(id); |
37 | | - if (instance === null) { |
38 | | - throw new MyError('Instance not found'); |
| 42 | + // get document |
| 43 | + const document = await getModel().findByPk(id); |
| 44 | + if (document === null) { |
| 45 | + throw new MyError('Document not found'); |
39 | 46 | } |
40 | 47 |
|
41 | | - // update instance |
42 | | - instance.set(object); |
43 | | - await instance.save(); |
| 48 | + // update document |
| 49 | + document.set(object); |
| 50 | + await document.save(); |
44 | 51 |
|
45 | | - return transform(instance); |
| 52 | + return transform(document); |
46 | 53 | }; |
47 | 54 |
|
48 | 55 | const del = async (id: number): Promise<<%= compNamePascalCase %>> => { |
49 | | - // get instance |
50 | | - const instance = await Model.findByPk(id); |
51 | | - if (instance === null) { |
52 | | - throw new MyError('Instance not found'); |
| 56 | + // get document |
| 57 | + const document = await getModel().findByPk(id); |
| 58 | + if (document === null) { |
| 59 | + throw new MyError('Document not found'); |
53 | 60 | } |
54 | 61 |
|
55 | | - // delete instance |
56 | | - await instance.destroy(); |
| 62 | + // delete document |
| 63 | + await document.destroy(); |
57 | 64 |
|
58 | | - return transform(instance); |
| 65 | + return transform(document); |
59 | 66 | }; |
60 | 67 |
|
61 | | -// transform instance to <%= compNamePascalCase %> |
62 | | -const transform = (instance: Instance): <%= compNamePascalCase %> => { |
63 | | - return instance.toJSON() as <%= compNamePascalCase %>; |
| 68 | +// transform document to <%= compNamePascalCase %> |
| 69 | +const transform = (document: Model): <%= compNamePascalCase %> => { |
| 70 | + return document.toJSON() as <%= compNamePascalCase %>; |
64 | 71 | }; |
65 | 72 |
|
66 | 73 | export default { list, create, get, update, del }; |
0 commit comments