UNPKG

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