forked from RFP2310-Team-Moon/Q-A-Service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.js
124 lines (115 loc) · 2.54 KB
/
postgres.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// const csv = require('csvtojson');
const { DataTypes } = require('sequelize');
const { sequelize } = require('./server/db');
const Questions = sequelize.define(
'Questions',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
product_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
body: {
type: DataTypes.STRING(1000),
allowNull: false,
},
date_written: {
type: DataTypes.BIGINT,
allowNull: false,
},
asker_name: {
type: DataTypes.STRING,
allowNull: false,
},
asker_email: {
type: DataTypes.STRING,
allowNull: false,
},
reported: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
helpful: {
type: DataTypes.INTEGER,
allowNull: true,
},
},
{ logging: false, timestamps: false }
);
const Answers = sequelize.define(
'Answers',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
body: {
type: DataTypes.STRING(1000),
allowNull: false,
},
date_written: {
type: DataTypes.BIGINT,
allowNull: false,
},
answerer_name: {
type: DataTypes.STRING,
allowNull: false,
},
answerer_email: {
type: DataTypes.STRING,
allowNull: false,
},
reported: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
helpful: {
type: DataTypes.INTEGER,
allowNull: true,
},
},
{ logging: false, timestamps: false }
);
const Photos = sequelize.define(
'Photos',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: true,
},
url: {
type: DataTypes.STRING,
allowNull: true,
},
},
{ logging: false, timestamps: false }
);
Questions.hasMany(Answers, { foreignKey: 'question_id' });
Answers.belongsTo(Questions, { foreignKey: 'question_id' });
Answers.hasMany(Photos, { foreignKey: 'answer_id' });
Photos.belongsTo(Answers, { foreignKey: 'answer_id' });
// redundant??
Questions.sync();
Answers.sync();
Photos.sync();
// OLD INSERT METHOD - NOW USING COPY THROUGH POSTGRES
// const questionsFilePath = './csv_files/test.questions.csv';
// async function bulkInsert() {
// try {
// const questions = await csv().fromFile(questionsFilePath);
// await Questions.bulkCreate(questions);
// } catch (error) {
// console.error(error);
// }
// }
// bulkInsert();
module.exports = { Questions, Answers, Photos };