-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
87 lines (75 loc) · 1.52 KB
/
db.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
const test = require('assert');
const Promise = require('promise');
const Mongoose = require('mongoose');
const config = require('./config/config')
// Database Name
const dbName = config.mongo.db;
// Connection url
const url = 'mongodb://localhost:27017/' + dbName;
// Connect using MongoClient
Mongoose.Promise = Promise;
const Schema = Mongoose.Schema;
let Modules = {};
let data= {
noteList:{
Lid: null,
name: 'test list'
},
noteName: 'test not ',
noteLink: 'link',
publish: true
}
let databook = {
name: 'testbook'
}
initialDataBase = function () {
return new Promise(function(resolve, refuse){
new Mongoose.connect(url).then(() => {
loadCollection();
resolve();
}, (err) => {
refuse(err);
})
})
}
loadCollection = function () {
for (let s in config.schema){
let name = config.schema[s].schemaName;
let rule = new Schema(config.schema[s].schemaRule);
Modules[name] = Mongoose.model(name, rule);
}
}
// Add
Add = function (collectionName, singleRecord) {
getDb().then(()=>{
Modules[collectionName].create(singleRecord).then((entity)=>{
entity.save()
});
},(err)=>{
console.log(err);
})
}
// Remove
Remove = function (callback) {
}
// Fetch
Fetch = function (callback) {
}
// Change
Change = function (callback) {
}
// get db
getDb = function () {
if(Modules.length == 0){
return initialDataBase();
}
}
shutDownDb = function (){
DbClient.close();
DbClient = null;
}
module.exports = {
getDb: getDb,
Add: Add,
shutDownDb:shutDownDb
}