-
Notifications
You must be signed in to change notification settings - Fork 17
/
MongooseProvider.js
111 lines (95 loc) · 2.42 KB
/
MongooseProvider.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'
/*
* adonis-mongoose-model
*
* (c) Juan Pablo Barreto <juampi92@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const { ioc, ServiceProvider } = require('@adonisjs/fold')
const Mongoose = require('mongoose')
class MongooseProvider extends ServiceProvider {
/**
* Install mongoose serializer
*/
_registerSerializer () {
ioc.extend('Adonis/Src/Auth',
'mongoose',
(app) => require('../src/Serializers/MongooseSerializer'),
'serializer')
}
async _registerMongoose () {
this.app.singleton('Adonis/Addons/Mongoose', function (app) {
const Config = app.use('Adonis/Src/Config')
let connectionString = Config.get('database.mongodb.connectionString', null)
const {
host = 'localhost',
port = 27017,
database = 'test',
user = null,
pass = null,
options = {},
debug = false
} = Config.get('database.mongodb.connection')
const auth = user ? `${user}:${pass}@` : ''
if (!connectionString) {
connectionString = `mongodb://${auth}${host}:${port}/${database}`
}
Mongoose.Promise = global.Promise
Mongoose.connect(connectionString, {
useMongoClient: true,
...options
})
if (debug) {
Mongoose.set('debug', true)
}
return Mongoose
})
}
_registerModel () {
this.app.bind('Adonis/Src/Model', (app) => require('../src/Model/Base'))
this.app.bind('AdonisMongoose/Src/Token', (app) => require('../src/Model/TokenMongoose'))
this.app.alias('Adonis/Src/Model', 'Model')
}
/**
* Register the `make:Mongoose` command to the IoC container
*
* @method _registerCommands
*
* @return {void}
*
* @private
*/
_registerCommands () {
this.app.bind('Adonis/Commands/Make:Mongoose', () => require('../commands/MakeModel'))
}
/**
* Register bindings
*
* @method register
*
* @return {void}
*/
async register () {
this._registerSerializer()
this._registerModel()
this._registerCommands()
await this._registerMongoose()
}
/**
* On boot
*
* @method boot
*
* @return {void}
*/
boot () {
/**
* Register command with ace.
*/
const ace = require('@adonisjs/ace')
ace.addCommand('Adonis/Commands/Make:Mongoose')
}
}
module.exports = MongooseProvider