Skip to content

Commit

Permalink
feat(generators/with-postgres/templates): refactor repository & model
Browse files Browse the repository at this point in the history
  • Loading branch information
phatpham9 committed Feb 14, 2021
1 parent 2a363d3 commit d7b33ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import {
Model as SequelizeModel,
ModelAttributes,
ModelOptions,
ModelCtor,
DataTypes,
} from 'sequelize';

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

interface Model extends SequelizeModel<<%= compNamePascalCase %>, Omit<<%= compNamePascalCase %>, 'id'>>, <%= compNamePascalCase %> {}
interface Instance extends SequelizeModel<<%= compNamePascalCase %>, Omit<<%= compNamePascalCase %>, 'id'>>, <%= compNamePascalCase %> {}
interface Model extends ModelCtor<Instance> {}

const name = ENTITY;

const attributes: ModelAttributes<Model> = {
const schema: ModelAttributes<Instance> = {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
Expand All @@ -24,5 +26,5 @@ const attributes: ModelAttributes<Model> = {

const options: ModelOptions<SequelizeModel> = {};

export default { name, attributes, options };
export { Model };
export default { name, schema, options };
export { Instance, Model };
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
import { ModelCtor } from 'sequelize';
import { MyError } from '@boringcodes/utils/error';

import { <%= compNamePascalCase %> } from './types';
import model, { Model } from './model';
import postgres from '../../db/postgres';

// get model
const getModel = (): ModelCtor<Model> => {
return postgres.getModel<Model>(model);
};
import { <%= compNamePascalCase %> } from './types';
import model, { Instance, Model } from './model';

const list = async (): Promise<<%= compNamePascalCase %>[]> => {
// list documents
const documents = await getModel().findAll();
// list instances
const instances = await getModel().findAll();

return documents.map(transform);
return instances.map(transform);
};

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

return transform(document);
return transform(instance);
};

const get = async (id: number): Promise<<%= compNamePascalCase %>> => {
// get document
const document = await getModel().findByPk(id);
if (document === null) {
throw new MyError('Document not found');
// get instance
const instance = await getModel().findByPk(id);
if (instance === null) {
throw new MyError('Instance not found');
}

return transform(document);
return transform(instance);
};

const update = async (
id: number,
object: Omit<<%= compNamePascalCase %>, 'id'>,
): Promise<<%= compNamePascalCase %>> => {
// get document
const document = await getModel().findByPk(id);
if (document === null) {
throw new MyError('Document not found');
// get instance
const instance = await getModel().findByPk(id);
if (instance === null) {
throw new MyError('Instance not found');
}

// update document
document.set(object);
await document.save();
// update instance
instance.set(object);
await instance.save();

return transform(document);
return transform(instance);
};

const del = async (id: number): Promise<<%= compNamePascalCase %>> => {
// get document
const document = await getModel().findByPk(id);
if (document === null) {
throw new MyError('Document not found');
// get instance
const instance = await getModel().findByPk(id);
if (instance === null) {
throw new MyError('Instance not found');
}

// delete document
await document.destroy();
// delete instance
await instance.destroy();

return transform(instance);
};

return transform(document);
// get model
const getModel = (): Model => {
return postgres.getModel(model.name) as Model;
};

// transform document to <%= compNamePascalCase %>
const transform = (document: Model): <%= compNamePascalCase %> => {
return document.toJSON() as <%= compNamePascalCase %>;
// transform instance to <%= compNamePascalCase %>
const transform = (instance: Instance): <%= compNamePascalCase %> => {
return instance.toJSON() as <%= compNamePascalCase %>;
};

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

0 comments on commit d7b33ee

Please sign in to comment.