1 | var developerConsole = require('letsnet-developerconsole')()
|
2 | console.developer = developerConsole.developerConsole
|
3 |
|
4 | var mongoose = require('mongoose')
|
5 | const fs = require('fs')
|
6 | var Queue = require('./queue')
|
7 | var lRequester = require('./requester')
|
8 |
|
9 |
|
10 | var 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 |
|
19 | API.prototype.addMiddleware = function(name, func) {
|
20 | this.middlewares[name] = func
|
21 | }
|
22 |
|
23 | API.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 |
|
31 | API.prototype.mongoClose = function() {
|
32 | this.mongodb.close()
|
33 | mongoose.disconnect()
|
34 | this.openSchema = null
|
35 | }
|
36 |
|
37 | API.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 |
|
44 | API.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 |
|
50 | API.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 |
|
61 | module.exports = API |
\ | No newline at end of file |