Skip to content

Commit

Permalink
refactor: sequelize (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Tran authored Aug 30, 2020
1 parent ff07263 commit 5deb05f
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Hits-of-Code](https://hitsofcode.com/github/103cuong/graphql-kit)](https://hitsofcode.com/view/github/103cuong/graphql-kit)
[![GitHub](https://img.shields.io/github/license/103cuong/graphql-kit.svg)](https://github.com/103cuong/graphql-kit/blob/master/LICENSE)

> 🛸🚀 A Node kit with TypeScript, GraphQL, Sequelize, PostgreSQL and awesome tools.
🍣 A Node kit with TypeScript, GraphQL, Sequelize, PostgreSQL and awesome tools.

## Preparation

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "graphql-kit",
"version": "1.0.0",
"main": "src/index.ts",
"description": "🍣 A Node kit with TypeScript, GraphQL, Sequelize, PostgreSQL and awesome tools.",
"scripts": {
"start:dev": "nodemon --ignore build/ --exec ts-node src/index.ts",
"prebuild": "rm -rf build",
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import path from 'path';
import { logger, globalOptions } from 'juno-js';

import { migrateDB, config } from './components';
import sequelize from './models';
import { associate } from './models/association';
import { sequelize, associate } from './models/sequelize';
import app from './app';

globalOptions.environment = config.nodeEnv;
Expand Down
4 changes: 2 additions & 2 deletions src/migrations/20200605151403_create_category.migration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryInterface, DataTypes } from 'sequelize';

const migration = {
up: (queryInterface: QueryInterface) => queryInterface.sequelize.transaction((t) => queryInterface.createTable('categories', {
up: (queryInterface: QueryInterface) => queryInterface.sequelize.transaction((transaction) => queryInterface.createTable('categories', {
id: {
type: DataTypes.UUID,
primaryKey: true,
Expand All @@ -17,7 +17,7 @@ const migration = {
type: DataTypes.DATE,
},
}, {
transaction: t,
transaction,
})),
};

Expand Down
4 changes: 2 additions & 2 deletions src/migrations/20200605151452_create_cat.migration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryInterface, DataTypes } from 'sequelize';

const migration = {
up: (queryInterface: QueryInterface) => queryInterface.sequelize.transaction((t) => queryInterface.createTable('cats', {
up: (queryInterface: QueryInterface) => queryInterface.sequelize.transaction((transaction) => queryInterface.createTable('cats', {
id: {
type: DataTypes.UUID,
primaryKey: true,
Expand All @@ -28,7 +28,7 @@ const migration = {
type: DataTypes.DATE,
},
}, {
transaction: t,
transaction,
})),
};

Expand Down
20 changes: 0 additions & 20 deletions src/models/association.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/models/cat.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model, DataTypes } from 'sequelize';

import sequelize from '.';
import { sequelize } from './sequelize';
import Category from './category.model';

class Cat extends Model {
Expand Down
2 changes: 1 addition & 1 deletion src/models/category.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model, DataTypes } from 'sequelize';

import sequelize from '.';
import { sequelize } from './sequelize';
import Cat from './cat.model';

class Category extends Model {
Expand Down
20 changes: 19 additions & 1 deletion src/models/index.ts → src/models/sequelize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Sequelize } from 'sequelize';
import { logger } from 'juno-js';
import path from 'path';
import fs from 'fs';

import { config } from '../components';

Expand All @@ -23,4 +25,20 @@ sequelize
logger.error('Sequelize authentication failed: ', e);
});

export default sequelize;
const associate = () => {
const models: { [key: string]: any } = {};
fs
.readdirSync(__dirname)
.filter((fileName: string) => /model.[t|j]s/.test(fileName))
.forEach((fileName) => {
const model = require(path.resolve(__dirname, fileName));
models[model.default.name] = model.default;
});
Object.keys(models).forEach((modelName: string) => {
if ('associate' in models[modelName]) {
models[modelName].associate(models);
}
});
};

export { sequelize, associate };
2 changes: 1 addition & 1 deletion src/services/cat.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sequelize from '../models';
import { sequelize } from '../models/sequelize';
import CatModel from '../models/cat.model';
import Category from '../models/category.model';
import { MutationCreateCatArgs, Cat } from '../types/graphql.type';
Expand Down
2 changes: 1 addition & 1 deletion src/services/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sequelize from '../models';
import { sequelize } from '../models/sequelize';
import Cat from '../models/cat.model';
import CategoryModel from '../models/category.model';
import { MutationCreateCategoryArgs, Category } from '../types/graphql.type';
Expand Down

0 comments on commit 5deb05f

Please sign in to comment.