UNPKG

2.04 kBJavaScriptView Raw
1var developerConsole = require('letsnet-developerconsole')()
2console.developer = developerConsole.developerConsole
3
4var mongoose = require('mongoose')
5const fs = require('fs')
6var Queue = require('./queue')
7
8var API = function(defaults) {
9 mongodb = null
10 mongooseCredentials = null
11 queue = null
12 this.middlewares = {}
13 this.schemas = {}
14}
15
16API.prototype.addMiddleware = function(name, func) {
17 this.middlewares[name] = func
18}
19
20API.prototype.mongoConnect = function() {
21 let opts = {}
22 if(this.mongooseCredentials == null) throw 'Please set mongooseCredentials with setMongooseCredentials(object) function ...'
23 else this.mongodb = mongoose.createConnection('mongodb://' + this.mongooseCredentials.user + ':' + this.mongooseCredentials.passwd + this.mongooseCredentials.url, opts)
24}
25
26API.prototype.mongoClose = function() {
27 this.mongodb.close()
28 mongoose.disconnect()
29 this.openSchema = null
30}
31
32API.prototype.setMongooseCredentials = function(object) {
33 if(object.user === 'undefined' || object.user == null) throw 'You must define the \'USER\' of the connection!'
34 else if(object.url === 'undefined' || object.url == null) throw 'You must define the \'URL\' of the connection!'
35 else if(object.passwd === 'undefined' || object.passwd == null) throw 'You must define the \'PASSWORD\' of the connection!'
36 else { this.mongooseCredentials = object }
37}
38
39API.prototype.addSchema = function(database, object) {
40 if(this.mongodb == null) throw 'Please set a connection with mongoConnect function!'
41 else if(this.schemas.hasOwnProperty(database)) throw 'Duplicated schema... u idiot'
42 else this.schemas[database] = this.mongodb.model(database, new mongoose.Schema(object))
43}
44
45API.prototype.addFunction = function(defs, rFunction) {
46 if(rFunction instanceof Function) {
47 this[defs.name] = function() {
48 this.queue = new Queue(arguments)
49 this.queue.setMasterFunction(defs.schema, rFunction)
50 this.queue.next()
51 }
52 }
53 else new Error('The second parameter must be a function!')
54}
55
56module.exports = API
\No newline at end of file