Skip to content

Instantly share code, notes, and snippets.

@hzburki
Last active July 14, 2019 14:58
Show Gist options
  • Save hzburki/8c33f9e64fc52611251575aae40eb62b to your computer and use it in GitHub Desktop.
Save hzburki/8c33f9e64fc52611251575aae40eb62b to your computer and use it in GitHub Desktop.
For providing database connection details to Sequelize with NestJs
import { Sequelize } from 'sequelize-typescript';
/**
* SEQUELIZE variable is stored in a file named
* 'constants' so it can be easily reused anywhere
* without being subject to human error.
*/
import { SEQUELIZE } from '../utils/constants';
import { User } from '../user/user.entity';
export const databaseProviders = [
{
provide: SEQUELIZE,
useFactory: async () => {
const sequelize = new Sequelize({
dialect: 'mysql',
host: 'remotemysql.com',
port: 3306,
username: 'xxxxxxxxxx',
password: 'xxxxxxxxxx',
database: 'xxxxxxxxxx',
});
/**
* Add Models Here
* ===============
* You can add the models to
* Sequelize later on.
*/
sequelize.addModels([User]);
await sequelize.sync();
return sequelize;
},
},
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment