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