UNPKG

2.28 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 this.wrongRequest = null
15}
16
17API.prototype.addMiddleware = function(name, func) {
18 this.middlewares[name] = func
19}
20
21API.prototype.mongoConnect = function() {
22 let opts = {}
23 if(this.mongooseCredentials == null) throw 'Please set mongooseCredentials with setMongooseCredentials(object) function ...'
24 else this.mongodb = mongoose.createConnection('mongodb://' + this.mongooseCredentials.user + ':' + this.mongooseCredentials.passwd + this.mongooseCredentials.url, opts)
25}
26
27API.prototype.mongoClose = function() {
28 this.mongodb.close()
29 mongoose.disconnect()
30 this.openSchema = null
31}
32
33API.prototype.setMongooseCredentials = function(object) {
34 if(object.user === 'undefined' || object.user == null) throw 'You must define the \'USER\' of the connection!'
35 else if(object.url === 'undefined' || object.url == null) throw 'You must define the \'URL\' of the connection!'
36 else if(object.passwd === 'undefined' || object.passwd == null) throw 'You must define the \'PASSWORD\' of the connection!'
37 else { this.mongooseCredentials = object }
38}
39
40API.prototype.addSchema = function(database, object) {
41 if(this.mongodb == null) throw 'Please set a connection with mongoConnect function!'
42 else if(this.schemas.hasOwnProperty(database)) throw 'Duplicated schema... u idiot'
43 else this.schemas[database] = this.mongodb.model(database, new mongoose.Schema(object))
44}
45
46API.prototype.addFunction = function(defs, rFunction) {
47 if(rFunction instanceof Function) {
48 this[defs.name] = function() {
49 this.queue = new Queue(arguments)
50 this.queue.setMasterFunction(defs.schema, rFunction)
51 this.queue.next()
52 }
53 }
54 else new Error('The second parameter must be a function!')
55}
56
57API.prototype.checkResponses = function(requests) {
58 for(key in requests) {
59 if(requests[key].type != 'success') {
60 this.wrongRequest = requests[key]
61 return false
62 }
63 }
64 return true
65}
66
67module.exports = API
\No newline at end of file