-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.js
50 lines (45 loc) · 968 Bytes
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { Sequelize, Model, DataTypes } = require('sequelize');
const { DB } = require('./utils/constants');
module.exports.createContext = () => {
const sequelize = new Sequelize(
DB.DATABASE_DB,
DB.DATABASE_USER,
DB.DATABASE_PASS,
{
host: DB.DATABASE_HOST,
dialect: 'postgres',
dialectOptions: {
useUTC: false,
},
timezone: 'America/Santiago',
}
);
class Library extends Model {}
Library.init(
{
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [5, 20],
},
},
},
{ sequelize, modelName: 'library', tableName: 'Library' }
);
class Book extends Model {}
Book.init(
{
name: DataTypes.STRING,
book: DataTypes.INTEGER,
},
{ sequelize, modelName: 'book', tableName: 'Book' }
);
Book.belongsTo(Library, { as: 'library' });
return {
db: {
Library,
Book,
},
};
};