This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
177 lines (151 loc) · 4.14 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var async = require('./lib/async')
var query = require('./lib/query')
var Table = require('./lib/table')
var hide = require('./lib/hideProperty')
var promiseResolver = require('./lib/promiseResolver')
var extend = require('./lib/extend')
var supportedDrivers = [
'pg',
'mysql'
]
var oreo = module.exports = function oreo(opts, cb) {
if (!(this instanceof oreo)) {
return new oreo(opts, cb)
}
opts = opts || {}
cb = cb || function () {}
var self = this
// use _ prefix to avoid conflict with table names
hide(self, '_driver')
hide(self, '_platform')
hide(self, '_query')
hide(self, '_tables')
hide(self, '_opts')
hide(self, '_Promise')
hide(self, '_promiseResolver')
hide(self, '_onReady')
hide(self, '_isReady')
hide(self, '_memo')
self._isReady = false
self._tables = []
self._opts = extend({}, opts)
self._Promise = opts.Promise
self._promiseResolver = promiseResolver
self._memo = {} // memoized query results
if (supportedDrivers.indexOf(opts.driver) === -1) {
return cb(new Error('"' + opts.driver + '" is not a supported driver.'))
}
self._driver = require(opts.driver)
// bind the platform-specific methods to this
self._platform = require('./lib/platforms/' + opts.driver)
self._platform.db = self
if (opts.debug && typeof opts.debug !== 'function') {
self._opts.debug = console.log
}
if (typeof opts.memoize !== 'number') {
self._opts.memoize = false
}
self._query = query(self, self._opts, function(err) {
if (err) {
return cb(err)
}
self._opts.pass = '***************' // obfuscate the password
self.execute = self._query.execute.bind(self._query)
self.executeWrite = self._query.executeWrite.bind(self._query)
if (opts.schema) {
return self.discover({
fromCache: true
}, cb)
}
self.discover(cb)
})
// purge memoized values periodically
var memoMs = self._opts.memoize
if (memoMs) {
var intervalMs = self._opts.memoizePurgeInterval || 10000
setInterval(function purgeMemo () {
Object.keys(self._memo).forEach(function (key) {
if (Date.now() - self._memo[key].timestamp > memoMs) {
delete self._memo[key]
}
})
}, intervalMs)
}
return this
}
/**
* [discover description]
*/
oreo.prototype.discover = function(opts, cb) {
var sql
var self = this
self._tables = []
opts = opts || {}
if (typeof opts === 'function') {
cb = opts
opts = {}
}
cb = cb || self._promiseResolver()
// get the tables from db or cache
var getTables = self._platform.getTables.bind(self._platform)
if (opts.fromCache) {
getTables = function (cb) {
return cb(null, Object.keys(self._opts.schema))
}
}
getTables(function(err, tables) {
if (err) return cb(err)
tables.sort()
// for each table
async.each(tables, function(tableName, done) {
self._tables.push(tableName)
// create a table object
self[tableName] = new Table({
tableName: tableName,
db: self,
fromCache: opts.fromCache
}, done)
}, function(err) {
if (err) return cb(err)
// determine the 1-to-m relationships for each table
self._tables.forEach(function(table) {
if (self[table].fk) {
Object.keys(self[table].fk).forEach(function(fkName) {
var fk = self[table].fk[fkName]
if (self[fk.foreignTable] && self[fk.foreignTable].many) {
var link = fk.constraintName + '_' + fk.table
self[fk.foreignTable].many[link] = fk
}
})
}
})
if (!self._isReady) {
self._onReady = self._onReady || []
self._onReady.forEach(function (fn) {
fn()
})
self._isReady = true
}
cb(null, self)
})
})
return cb.promise ? cb.promise : this
}
/**
* Adds a function to the stack to be executed when the database is ready
*/
oreo.prototype.onReady = function(fn) {
if (this._isReady) {
return fn()
}
this._onReady = this._onReady || []
this._onReady.push(fn)
return this
}
/**
*
*/
oreo.prototype.end = function(cb) {
cb = cb || function () {}
this._platform.end(cb)
}