-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (123 loc) · 3.79 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict'
const fs = require('fs')
const fp = require('fastify-plugin')
const Waterline = require('waterline')
const DiskAdapter = require('sails-disk')
const MySQLAdapter = require('sails-mysql')
const MongoAdapter = require('sails-mongo')
const PostgresAdapter = require('sails-postgresql')
const { promisify } = require('util')
const path = require('path')
const readdir = promisify(fs.readdir)
const lstat = promisify(fs.lstat)
const _ = require('lodash')
const FastifyWaterline = require('./lib/fastifyWaterline')
const ADAPTERS = {
mysql: MySQLAdapter,
mongo: MongoAdapter,
postgresql: PostgresAdapter,
disk: DiskAdapter
}
async function decorateFastifyInstance (fastify, connections, next) {
if (connections.length === 0) {
next(Error('fastify-waterline: no connection info provided'))
return
}
const adapters = {}
const datastores = {}
const models = {}
for (const connection of connections) {
if (!connection.adapter) {
next(Error('fastify-waterline: specify a valid adapter: mysql, mongo, postgresql, disk'))
return
}
if (Object.keys(ADAPTERS).indexOf(connection.adapter) === -1) {
next(Error('fastify-waterline: adapter name not valid: mysql, mongo, postgresql, disk'))
return
}
Object.assign(adapters, {
[`sails-${connection.adapter}`]: ADAPTERS[connection.adapter]
})
const connectionName = connection.name || 'default'
delete connection.name
connection.adapter = `sails-${connection.adapter}`
const entities = connection.entities || null
const entitiesFolder = connection.entitiesFolder || ''
delete connection.entities
delete connection.entitiesFolder
Object.assign(datastores, {
[connectionName]: connection
})
if (entities) {
const keys = Object.keys(entities)
for (const key of keys) {
if (!entities[key].datastore) {
entities[key].datastore = connectionName
}
Object.assign(models, {
[key]: entities[key]
})
}
}
if (entitiesFolder) {
const folder = path.resolve(__dirname, entitiesFolder)
try {
await lstat(folder)
} catch (e) {
next(Error(`fastify-waterline: Entities folder ${entitiesFolder} does not exist.`))
return
}
const files = await readdir(folder)
for (let index = 0; index < files.length; index++) {
const file = files[index]
if (!file.match(/.js$/)) {
continue
}
const entityName = _.camelCase(file.replace(/.js$/g, ''))
const entity = require(path.join(folder, file))
if (!entity.datastore) {
entity.datastore = connectionName
}
Object.assign(models, {
[entityName]: entity
})
}
}
}
Waterline.start({
adapters: adapters,
datastores: datastores,
models: models
}, function (err, orm) {
if (err) {
next(Error('fastify-waterline: Could not start up the Waterline ORM\n' + err))
return
}
fastify.decorate('fw', new FastifyWaterline(orm))
fastify.addHook('onClose', (_, done) => Waterline.stop(orm, done))
next()
})
}
function fastifyWaterline (fastify, options, next) {
if (fastify.waterline) {
return next(Error('fastify-waterline has already been registered'))
}
const connections = []
if (Array.isArray(options)) {
for (let i = 0; i < options.length; i++) {
if (!options[i].name) {
return next(Error('fastify-waterline: a connection name must be provided'))
}
connections.push(options[i])
}
} else {
if (Object.keys(options).length > 0) {
connections.push(options)
}
}
decorateFastifyInstance(fastify, Object.values(connections), next)
}
module.exports = fp(fastifyWaterline, {
fastify: '>=2.0.0',
name: 'fastify-waterline'
})