-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathschema.js
60 lines (50 loc) · 1.4 KB
/
schema.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
const fs = require('fs');
const mongoose = require('mongoose');
var connectionURL;
if (process.env.NODE_ENV === 'production') {
connectionURL = process.env.MONGODB_URI;
console.log('the process env uri is', process.env.MONGODB_URI);
} else {
connectionURL = "mongodb://localhost/buyifyData";
}
mongoose.connect(connectionURL);
var userSchema = mongoose.Schema({
username: String,
password: String
});
var orderSchema = mongoose.Schema({
orderId: Number,
orderTotal: Number,
orderQTY: Number
});
var productSchema = mongoose.Schema({
productId: Number,
name: String,
ImageURL: String,
Price: Number
});
var userModel = mongoose.model('userSchema', userSchema);
var orderModel = mongoose.model('orderSchema', orderSchema);
var productModel = mongoose.model('productSchema', productSchema);
productModel.remove({}, function(err) {
console.log('productModel removed');
});
fs.readFile('product.json', function(err, products) {
if (err) throw err;
JSON.parse(products).forEach(function(product) {
productModel.create({
"productId": product.productId,
"name": product.name,
"ImageURL": product.ImageURL,
"Price": product.Price
}, function(err, newProductAdded) {
if (err) throw err;
//console.log('new Product Added', newProductAdded);
});
});
});
module.exports = {
userModel: userModel,
orderModel: orderModel,
productModel: productModel
};