Skip to content

Commit

Permalink
refactor(generators/with-postgres-typeorm/templates): rename componen…
Browse files Browse the repository at this point in the history
…t model.ts to schema.ts
  • Loading branch information
phatpham9 committed Feb 14, 2021
1 parent e979f99 commit 16ebaea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import { getRepository } from 'typeorm';
import { MyError } from '@boringcodes/utils/error';

import { <%= compNamePascalCase %> } from './types';
import Model from './model';
import Schema from './schema';

const list = async (): Promise<<%= compNamePascalCase %>[]> => {
// list documents
const documents = await getRepository(Model).find();
const documents = await getRepository(Schema).find();

return documents.map(transform);
};

const create = async (object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
// create document
const document = getRepository(Model).create(object);
await getRepository(Model).save(document);
const document = getRepository(Schema).create(object);
await getRepository(Schema).save(document);

return transform(document);
};

const get = async (id: number): Promise<<%= compNamePascalCase %>> => {
// get document
const document = await getRepository(Model).findOne(id);
const document = await getRepository(Schema).findOne(id);
if (document === undefined) {
throw new MyError('Document not found');
}
Expand All @@ -34,33 +34,33 @@ const update = async (
object: Omit<<%= compNamePascalCase %>, 'id'>,
): Promise<<%= compNamePascalCase %>> => {
// get document
const document = await getRepository(Model).findOne(id);
const document = await getRepository(Schema).findOne(id);
if (document === undefined) {
throw new MyError('Document not found');
}

// update document
getRepository(Model).merge(document, object);
await getRepository(Model).save(document);
getRepository(Schema).merge(document, object);
await getRepository(Schema).save(document);

return transform(document);
};

const del = async (id: number): Promise<<%= compNamePascalCase %>> => {
// get document
const document = await getRepository(Model).findOne(id);
const document = await getRepository(Schema).findOne(id);
if (document === undefined) {
throw new MyError('Document not found');
}

// delete document
await getRepository(Model).remove(document);
await getRepository(Schema).remove(document);

return transform(document);
};

// transform document to <%= compNamePascalCase %>
const transform = (document: Model): <%= compNamePascalCase %> => {
const transform = (document: Schema): <%= compNamePascalCase %> => {
return document;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { <%= compNamePascalCase %> } from './types';
import { ENTITY } from './constants';

@Entity(ENTITY)
class Model implements <%= compNamePascalCase %> {
class Schema implements <%= compNamePascalCase %> {
@PrimaryGeneratedColumn()
id!: number;

Expand All @@ -14,4 +14,4 @@ class Model implements <%= compNamePascalCase %> {
// TODO: add more fields
}

export default Model;
export default Schema;

0 comments on commit 16ebaea

Please sign in to comment.