Skip to content

Commit

Permalink
chore: migrations and config from environment file
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Santos committed Jul 25, 2022
1 parent 1b65b77 commit 1b19fc9
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NODE_ENV=development|test|production
DATABASE_DSN=postgres://user:[email protected]:5432/dbname
7 changes: 7 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require('path');
module.exports = {
'config': path.resolve('src', 'config', 'config.js'),
'models-path': path.resolve('src', 'config', 'models'),
'seeders-path': path.resolve('src', 'config', 'seeders'),
'migrations-path': path.resolve('src', 'config', 'migrations'),
};
6 changes: 6 additions & 0 deletions src/config/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default () => ({
pg: {
dsn: process.env.DATABASE_DSN,
dialect: process.env.DATABASE_DSN.split(':')[0],
},
});
16 changes: 16 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
development: {
username: 'postgres',
password: '123456',
database: 'posterr_development',
host: '127.0.0.1',
dialect: 'postgres',
},
production: {
username: 'postgres',
password: '123456',
database: 'posterr',
host: '127.0.0.1',
dialect: 'postgres',
},
};
35 changes: 35 additions & 0 deletions src/config/migrations/20220724224210-create_users_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.createTable('users', {
id: {
type: Sequelize.DataTypes.UUID,
primaryKey: true,
allowNull: false,
},
username: {
type: Sequelize.DataTypes.STRING(255),
},
created_at: {
type: Sequelize.DataTypes.DATE,
},
});
},

async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
await queryInterface.dropTable('users');
},
};
63 changes: 63 additions & 0 deletions src/config/migrations/20220724225213-create_post_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.createTable('posts', {
id: {
type: Sequelize.DataTypes.UUID,
primaryKey: true,
allowNull: false,
},
content: {
type: Sequelize.DataTypes.TEXT,
},
user_id: {
type: Sequelize.DataTypes.UUID,
allowNull: false,
},
created_at: {
type: Sequelize.DataTypes.DATE,
},
is_quote: {
type: Sequelize.DataTypes.BOOLEAN,
allowNull: false,
},
is_repost: {
type: Sequelize.DataTypes.BOOLEAN,
allowNull: false,
},
original_post_id: {
type: Sequelize.DataTypes.UUID,
allowNull: true,
},
original_post_content: {
type: Sequelize.DataTypes.TEXT,
allowNull: true,
},
original_post_user_id: {
type: Sequelize.DataTypes.UUID,
allowNull: true,
},
original_post_screen_name: {
type: Sequelize.DataTypes.STRING(255),
allowNull: true,
},
});
},

async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
await queryInterface.dropTable('posts');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*
*/
await queryInterface.addConstraint('posts', {
fields: ['user_id'],
type: 'foreign key',
name: 'fk_user_id_post',
references: {
table: 'users',
field: 'id',
},
});
},

async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
await queryInterface.removeConstraint('posts', 'fk_user_id_post');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*
*/
await queryInterface.addIndex('users', ['username'], {
name: 'idx_unique_username',
unique: true,
});
},

async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
await queryInterface.removeIndex('users', 'idx_unique_username');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
await queryInterface.addIndex('posts', ['user_id'], {
name: 'idx_user_id',
});
await queryInterface.addIndex('posts', ['created_at'], {
name: 'idx_created_at',
});
await queryInterface.addIndex('posts', ['user_id', 'created_at'], {
name: 'idx_user_posts',
});
},

async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
await queryInterface.removeIndex('posts', 'idx_user_id');
await queryInterface.removeIndex('posts', 'idx_created_at');
await queryInterface.removeIndex('posts', 'idx_user_posts');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*
*/
await queryInterface.addConstraint('posts', {
fields: ['original_post_id'],
type: 'foreign key',
name: 'fk_original_post_id_post',
references: {
table: 'posts',
field: 'id',
},
});
},

async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
await queryInterface.removeConstraint('posts', 'fk_original_post_id_post');
},
};
2 changes: 2 additions & 0 deletions src/config/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from '#core/posts/infra/db/repository/sequelize/post-model';
export * from '#core/users/infra/db/repository/sequelize/user-model';
46 changes: 46 additions & 0 deletions src/config/seeders/20220725010659-created_base_users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const users = [
{
id: '4cfe67a9-defc-42b9-8410-cb5086bec2f5',
username: 'alucard',
created_at: new Date(),
},
{
id: 'b8903f77-5d16-4176-890f-f597594ff952',
username: 'anderson',
created_at: new Date(),
},
{
id: '75135a97-46be-405f-8948-0821290ca83e',
username: 'seras_victoria',
created_at: new Date(),
},
];
module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add seed commands here.
*
* Example:
* await queryInterface.bulkInsert('People', [{
* name: 'John Doe',
* isBetaMember: false
* }], {});
*/
await queryInterface.bulkInsert('users', users, {});
},

async down(queryInterface, Sequelize) {
/**
* Add commands to revert seed here.
*
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
await queryInterface.bulkDelete(
'users',
users.map((u) => u.id),
{},
);
},
};
8 changes: 8 additions & 0 deletions src/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Module } from '@nestjs/common';
import { databaseProviders } from './database.providers';
import { ConfigModule } from '@nestjs/config';
import appConfig from './config/app.config';

@Module({
imports: [
ConfigModule.forRoot({
load: [appConfig],
isGlobal: true,
}),
],
providers: [...databaseProviders],
exports: [...databaseProviders],
})
Expand Down
15 changes: 6 additions & 9 deletions src/database.providers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { Sequelize } from 'sequelize-typescript';
import { PostModel } from '#core/posts';
import { UserModel } from '#core/users';
import { ConfigService } from '@nestjs/config';

export const databaseProviders = [
{
provide: 'SEQUELIZE',
useFactory: async () => {
const sequelize = new Sequelize({
dialect: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: '123456',
database: 'posterr',
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const sequelize = new Sequelize(configService.get('pg.dsn'), {
dialect: configService.get('pg.dialect'),
});
sequelize.addModels([PostModel, UserModel]);
await sequelize.sync();
// await sequelize.sync();
return sequelize;
},
},
Expand Down

0 comments on commit 1b19fc9

Please sign in to comment.