NestJS database module
Make you database simple and easy with sequelize. How to use sequelize, please look at it's own documentation.
How to use it
-
Import this module in your app module:
imports: [ DatabaseModule.forRoot([ UserEntity, OtherEntity, // all entities ], { dialect: 'postgres', port: 5432, name: 'database_name', user: 'database_user', password: 'mysecretpassword', host: '127.0.0.1', }), ]
-
Import with
forChild
to the rest of the modules (Optional if you have other modules)imports: [ DatabaseModule.forChild() ]
Gotchas
Synchronization
You can provide additional options to the module.
To enable synchronization and allow operator aliases you can use following example:
imports: [
DatabaseModule.forRoot([...], {...}, {
syncOptions: {
alter: true,
},
operatorsAliases: true,
}),
]
Enum values in postgres
Postgres does not support an easy sync with alter: true
for enum values. As a temporary solution we recommend to use a string, and prepared helpers to generate validation and column size.
Example:
enum UserImportStatus {
New = 'new',
InProgress = 'in-progress',
Completed = 'completed',
Error = 'error',
}
@Table()
export class UserEntity extends Model<UserEntity> {
...
@Column({
allowNull: false,
defaultValue: UserImportStatus.New,
type: DataType.STRING(getMaxLengthOfEnumValues(UserImportStatus)),
validate: {
isIn: [getEnumValues(UserImportStatus)],
},
})
public importStatus!: string;
...
}
Note: If you want to use Typescript 2.x you will need to use this module in version 1.x.
Stay in touch
License
This module is MIT licensed.