Skip to content

Commit 8e5e0f9

Browse files
committed
refactor(generators/with-postgres-typeorm): rename schema to model & remove unused repository transform func
1 parent 87952b8 commit 8e5e0f9

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

generators/with-postgres-typeorm/templates/<%= compNameParamCasePlural %>/schema.ts renamed to generators/with-postgres-typeorm/templates/<%= compNameParamCasePlural %>/model.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { <%= compNamePascalCase %> } from './types';
44
import { ENTITY } from './constants';
55

66
@Entity(ENTITY)
7-
class Schema implements <%= compNamePascalCase %> {
7+
class Model implements <%= compNamePascalCase %> {
88
@PrimaryGeneratedColumn()
99
id!: number;
1010

@@ -14,5 +14,4 @@ class Schema implements <%= compNamePascalCase %> {
1414
// TODO: add more fields
1515
}
1616

17-
export default Schema;
18-
export { Schema };
17+
export default Model;

generators/with-postgres-typeorm/templates/<%= compNameParamCasePlural %>/repository.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,54 @@ import { getRepository } from 'typeorm';
22
import { MyError } from '@boringcodes/utils/error';
33

44
import { <%= compNamePascalCase %> } from './types';
5-
import Schema from './schema';
5+
import Model from './model';
66

77
const list = async (): Promise<<%= compNamePascalCase %>[]> => {
88
// list documents
9-
const documents = await getRepository(Schema).find();
10-
11-
return documents.map(transform);
9+
return await getRepository(Model).find();
1210
};
1311

14-
const create = async (object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
12+
const create = async (data: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
1513
// create document
16-
const document = getRepository(Schema).create(object);
17-
await getRepository(Schema).save(document);
18-
19-
return transform(document);
14+
const document = getRepository(Model).create(data);
15+
return await getRepository(Model).save(document);
2016
};
2117

2218
const get = async (id: number): Promise<<%= compNamePascalCase %>> => {
2319
// get document
24-
const document = await getRepository(Schema).findOne(id);
20+
const document = await getRepository(Model).findOne(id);
2521
if (document === undefined) {
2622
throw new MyError('Document not found');
2723
}
2824

29-
return transform(document);
25+
return document;
3026
};
3127

3228
const update = async (
3329
id: number,
34-
object: Omit<<%= compNamePascalCase %>, 'id'>,
30+
data: Omit<<%= compNamePascalCase %>, 'id'>,
3531
): Promise<<%= compNamePascalCase %>> => {
3632
// get document
37-
const document = await getRepository(Schema).findOne(id);
33+
const document = await getRepository(Model).findOne(id);
3834
if (document === undefined) {
3935
throw new MyError('Document not found');
4036
}
4137

4238
// update document
43-
getRepository(Schema).merge(document, object);
44-
await getRepository(Schema).save(document);
39+
getRepository(Model).merge(document, data);
4540

46-
return transform(document);
41+
return await getRepository(Model).save(document);
4742
};
4843

4944
const del = async (id: number): Promise<<%= compNamePascalCase %>> => {
5045
// get document
51-
const document = await getRepository(Schema).findOne(id);
46+
const document = await getRepository(Model).findOne(id);
5247
if (document === undefined) {
5348
throw new MyError('Document not found');
5449
}
5550

5651
// delete document
57-
await getRepository(Schema).remove(document);
58-
59-
return transform(document);
60-
};
61-
62-
// transform document to <%= compNamePascalCase %>
63-
const transform = (document: Schema): <%= compNamePascalCase %> => {
64-
return document;
52+
return await getRepository(Model).remove(document);
6553
};
6654

6755
export default { list, create, get, update, del };

0 commit comments

Comments
 (0)