Skip to content

Commit 2cd0c11

Browse files
committed
feat(generators/with-postgres): rename app to with-postgres-sequelize, pluralize model name & RESOURCE const
1 parent 65f3224 commit 2cd0c11

File tree

9 files changed

+100
-86
lines changed

9 files changed

+100
-86
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ $ yo create-service-component
6161
# or :with-mongo
6262
$ yo create-service-component:with-mongo
6363

64-
# or :with-postgres
65-
$ yo create-service-component:with-postgres
64+
# or :with-postgres-sequelize
65+
$ yo create-service-component:with-postgres-sequelize
6666

6767
# or :with-postgres-typeorm
6868
$ yo create-service-component:with-postgres-typeorm
@@ -80,8 +80,8 @@ This scaffolds out:
8080
│ │   │   ├── constants.ts
8181
│ │   │   ├── controller.ts
8282
│ │   │   ├── index.ts
83-
│ │   │   ├── model.ts (:with-mongo/:with-postgres/:with-postgres-typeorm)
84-
│ │   │   ├── repository.ts (:with-mongo/:with-postgres/:with-postgres-typeorm/:with-postgres-prisma)
83+
│ │   │   ├── model.ts (:with-mongo/:with-postgres-sequelize/:with-postgres-typeorm)
84+
│ │   │   ├── repository.ts (:with-mongo/:with-postgres-sequelize/:with-postgres-typeorm/:with-postgres-prisma)
8585
│ │   │   ├── types.ts
8686
│ │   └── ...
8787
│ ├── ...

generators/with-postgres/index.js renamed to generators/with-postgres-sequelize/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,21 @@ module.exports = class extends Generator {
2222
return this.prompt(prompts).then((props) => {
2323
const compNameParamCase = changeCase.paramCase(props.compName);
2424
const compNamePascalCase = changeCase.pascalCase(props.compName);
25+
const compNameCamelCase = changeCase.camelCase(props.compName);
2526

2627
this.props = {
27-
...props,
28+
// param-case
2829
compNameParamCase,
29-
compNamePascalCase,
30+
// param-cases
3031
compNameParamCasePlural: pluralize(compNameParamCase),
32+
// PascalCase
33+
compNamePascalCase,
34+
// PasCalCases
35+
compNamePascalCasePlural: pluralize(compNamePascalCase),
36+
// camelCase
37+
compNameCamelCase,
38+
// camelCases
39+
compNameCamelCasePlural: pluralize(compNameCamelCase),
3140
};
3241
});
3342
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import pluralize from 'pluralize';
2+
13
const ENTITY = '<%= compNameParamCase %>';
2-
const RESOURCE = '<%= compNameParamCasePlural %>';
4+
const RESOURCE = pluralize(ENTITY);
35

46
export { ENTITY, RESOURCE };

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import {
55
ModelCtor,
66
DataTypes,
77
} from 'sequelize';
8+
import pluralize from 'pluralize';
89

910
import { <%= compNamePascalCase %> } from './types';
1011
import { ENTITY } from './constants';
1112

12-
interface Instance extends SequelizeModel<<%= compNamePascalCase %>, Omit<<%= compNamePascalCase %>, 'id'>>, <%= compNamePascalCase %> {}
13-
interface Model extends ModelCtor<Instance> {}
13+
interface Document extends SequelizeModel<<%= compNamePascalCase %>, Omit<<%= compNamePascalCase %>, 'id'>>, <%= compNamePascalCase %> {}
14+
interface Model extends ModelCtor<Document> {}
1415

15-
const name = ENTITY;
16+
const name = pluralize(ENTITY);
1617

17-
const schema: ModelAttributes<Instance> = {
18+
const schema: ModelAttributes<Document> = {
1819
id: {
1920
type: DataTypes.INTEGER,
2021
primaryKey: true,
@@ -24,7 +25,9 @@ const schema: ModelAttributes<Instance> = {
2425
// TODO: add more fields
2526
};
2627

27-
const options: ModelOptions<SequelizeModel> = {};
28+
const options: ModelOptions<SequelizeModel> = {
29+
timestamps: false,
30+
};
2831

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

generators/with-postgres/templates/<%= compNameParamCasePlural %>/types.ts renamed to generators/with-postgres-sequelize/templates/<%= compNameParamCasePlural %>/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
interface <%= compNamePascalCase %> {
22
readonly id: number;
3-
readonly name: string;
3+
readonly name?: string;
44
// TODO: add more props
55
}
66

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)