-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_Sequelize.js
More file actions
74 lines (63 loc) · 1.98 KB
/
sql_Sequelize.js
File metadata and controls
74 lines (63 loc) · 1.98 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Dialect, Sequelize } from "sequelize";
import logger from "../app-logger";
import dbConfig from "../config/db.config";
const sequelize = new Sequelize((dbConfig.CLEARDB_DATABASE_URL || dbConfig.DATABASE_URL), {
dialect: dbConfig.dialect as Dialect,
// sync: { force: true },
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
});
const db: any = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
import { StoreFactory } from "./Store";
db.stores = StoreFactory(sequelize);
import { MessageFactory } from "./Message";
db.messages = MessageFactory(sequelize);
logger.debug("Messages=>", { messages: db.messages });
export { db };
//Message.ts
import { Model, Sequelize, DataTypes, TIME, BuildOptions } from "sequelize";
// We need to declare an interface for our model that is basically what our class would be
export interface MessageI {
id: number;
store_url: string;
store_name: string;
store_id: number;
messages: string;
readonly createdAt: Date;
readonly updatedAt: Date;
}
// export interface StoreModel extends Model<StoreI>, StoreI {}
export class Message extends Model {
public id!: number;
public store_url!: string;
public store_name!: string;
public store_id!: number;
public messages!: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
export const MessageFactory = (sequelize: Sequelize) => {
return Message.init(
{
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
store_url: { type: DataTypes.STRING, unique: true, allowNull: false },
store_name: { type: DataTypes.STRING, defaultValue: "" },
store_id: { type: DataTypes.BIGINT },
messages: { type: DataTypes.TEXT },
timestamps: { type: DataTypes.DATE },
},
{
// Other model options go here
sequelize, // We need to pass the connection instance
modelName: "Message", // We need to choose the model name,
tableName: "messages",
timestamps: true,
}
);
};