-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
53 lines (42 loc) · 1.26 KB
/
db.js
File metadata and controls
53 lines (42 loc) · 1.26 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
'use strict'
const mongoose = require('mongoose');
const querystring = require('querystring');
const schemas = {};
const normalizedPath = require("path").join(__dirname, "schemas");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
schemas[file.slice(0, -3)] = require("./schemas/" + file);
});
console.log('Availble schemas:', Object.keys(schemas));
const unhandledSchema = mongoose.Schema({
title: String,
data: [{
year: Number,
texts: [String]
}]
})
mongoose.connect('mongodb://taco:tacos123@ds159112.mlab.com:59112/when');
const db = mongoose.connection;
db.on('error', function(err) {
console.log(err);
});
db.once('open', function() {
console.log('DB UP!');
});
let getGeneric = (schema, query) => new Promise((res, rej) => {
console.log('getGeneric', schema);
const model = mongoose.model(schema, schemas[schema])
model.find(querystring.parse(query) || {}, (err, data) => {
err && rej(err) || res(data);
})
})
let postGeneric = (schema, query, payload) => new Promise((res, rej) => {
const model = mongoose.model(schema, schemas[schema])
new model(payload).save((err, doc) => {
err && rej(err);
res(doc);
})
})
module.exports = {
getGeneric,
postGeneric
}