This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
/
index.js
414 lines (328 loc) · 13.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* Module Dependencies
*/
// ...
// e.g.
// var _ = require('lodash');
// var mysql = require('node-mysql');
// ...
/**
* Sails Boilerplate Adapter
*
* Most of the methods below are optional.
*
* If you don't need / can't get to every method, just implement
* what you have time for. The other methods will only fail if
* you try to call them!
*
* For many adapters, this file is all you need. For very complex adapters, you may need more flexiblity.
* In any case, it's probably a good idea to start with one file and refactor only if necessary.
* If you do go that route, it's conventional in Node to create a `./lib` directory for your private submodules
* and load them at the top of the file with other dependencies. e.g. var update = `require('./lib/update')`;
*/
module.exports = (function () {
// You'll want to maintain a reference to each collection
// (aka model) that gets registered with this adapter.
var _modelReferences = {};
// You may also want to store additional, private data
// per-collection (esp. if your data store uses persistent
// connections).
//
// Keep in mind that models can be configured to use different databases
// within the same app, at the same time.
//
// i.e. if you're writing a MariaDB adapter, you should be aware that one
// model might be configured as `host="localhost"` and another might be using
// `host="foo.com"` at the same time. Same thing goes for user, database,
// password, or any other config.
//
// You don't have to support this feature right off the bat in your
// adapter, but it ought to get done eventually.
//
// Sounds annoying to deal with...
// ...but it's not bad. In each method, acquire a connection using the config
// for the current model (looking it up from `_modelReferences`), establish
// a connection, then tear it down before calling your method's callback.
// Finally, as an optimization, you might use a db pool for each distinct
// connection configuration, partioning pools for each separate configuration
// for your adapter (i.e. worst case scenario is a pool for each model, best case
// scenario is one single single pool.) For many databases, any change to
// host OR database OR user OR password = separate pool.
var _dbPools = {};
var adapter = {
// Set to true if this adapter supports (or requires) things like data types, validations, keys, etc.
// If true, the schema for models using this adapter will be automatically synced when the server starts.
// Not terribly relevant if your data store is not SQL/schemaful.
syncable: false,
// Default configuration for collections
// (same effect as if these properties were included at the top level of the model definitions)
defaults: {
// For example:
// port: 3306,
// host: 'localhost',
// schema: true,
// ssl: false,
// customThings: ['eh']
// If setting syncable, you should consider the migrate option,
// which allows you to set how the sync will be performed.
// It can be overridden globally in an app (config/adapters.js)
// and on a per-model basis.
//
// IMPORTANT:
// `migrate` is not a production data migration solution!
// In production, always use `migrate: safe`
//
// drop => Drop schema and data, then recreate it
// alter => Drop/add columns as necessary.
// safe => Don't change anything (good for production DBs)
migrate: 'alter'
},
/**
*
* This method runs when a model is initially registered
* at server-start-time. This is the only required method.
*
* @param {[type]} collection [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
registerCollection: function(collection, cb) {
// Keep a reference to this collection
_modelReferences[collection.identity] = collection;
cb();
},
/**
* Fired when a model is unregistered, typically when the server
* is killed. Useful for tearing-down remaining open connections,
* etc.
*
* @param {Function} cb [description]
* @return {[type]} [description]
*/
teardown: function(cb) {
cb();
},
/**
*
* REQUIRED method if integrating with a schemaful
* (SQL-ish) database.
*
* @param {[type]} collectionName [description]
* @param {[type]} definition [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
define: function(collectionName, definition, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// Define a new "table" or "collection" schema in the data store
cb();
},
/**
*
* REQUIRED method if integrating with a schemaful
* (SQL-ish) database.
*
* @param {[type]} collectionName [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
describe: function(collectionName, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// Respond with the schema (attributes) for a collection or table in the data store
var attributes = {};
cb(null, attributes);
},
/**
*
*
* REQUIRED method if integrating with a schemaful
* (SQL-ish) database.
*
* @param {[type]} collectionName [description]
* @param {[type]} relations [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
drop: function(collectionName, relations, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// Drop a "table" or "collection" schema from the data store
cb();
},
// OVERRIDES NOT CURRENTLY FULLY SUPPORTED FOR:
//
// alter: function (collectionName, changes, cb) {},
// addAttribute: function(collectionName, attrName, attrDef, cb) {},
// removeAttribute: function(collectionName, attrName, attrDef, cb) {},
// alterAttribute: function(collectionName, attrName, attrDef, cb) {},
// addIndex: function(indexName, options, cb) {},
// removeIndex: function(indexName, options, cb) {},
/**
*
* REQUIRED method if users expect to call Model.find(), Model.findOne(),
* or related.
*
* You should implement this method to respond with an array of instances.
* Waterline core will take care of supporting all the other different
* find methods/usages.
*
* @param {[type]} collectionName [description]
* @param {[type]} options [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
find: function(collectionName, options, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// Options object is normalized for you:
//
// options.where
// options.limit
// options.skip
// options.sort
// Filter, paginate, and sort records from the datastore.
// You should end up w/ an array of objects as a result.
// If no matches were found, this will be an empty array.
// Respond with an error, or the results.
cb(null, []);
},
/**
*
* REQUIRED method if users expect to call Model.create() or any methods
*
* @param {[type]} collectionName [description]
* @param {[type]} values [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
create: function(collectionName, values, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// Create a single new model (specified by `values`)
// Respond with error or the newly-created record.
cb(null, values);
},
//
/**
*
*
* REQUIRED method if users expect to call Model.update()
*
* @param {[type]} collectionName [description]
* @param {[type]} options [description]
* @param {[type]} values [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
update: function(collectionName, options, values, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// 1. Filter, paginate, and sort records from the datastore.
// You should end up w/ an array of objects as a result.
// If no matches were found, this will be an empty array.
//
// 2. Update all result records with `values`.
//
// (do both in a single query if you can-- it's faster)
// Respond with error or an array of updated records.
cb(null, []);
},
/**
*
* REQUIRED method if users expect to call Model.destroy()
*
* @param {[type]} collectionName [description]
* @param {[type]} options [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
destroy: function(collectionName, options, cb) {
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// 1. Filter, paginate, and sort records from the datastore.
// You should end up w/ an array of objects as a result.
// If no matches were found, this will be an empty array.
//
// 2. Destroy all result records.
//
// (do both in a single query if you can-- it's faster)
// Return an error, otherwise it's declared a success.
cb();
},
/*
**********************************************
* Optional overrides
**********************************************
// Optional override of built-in batch create logic for increased efficiency
// (since most databases include optimizations for pooled queries, at least intra-connection)
// otherwise, Waterline core uses create()
createEach: function (collectionName, arrayOfObjects, cb) { cb(); },
// Optional override of built-in findOrCreate logic for increased efficiency
// (since most databases include optimizations for pooled queries, at least intra-connection)
// otherwise, uses find() and create()
findOrCreate: function (collectionName, arrayOfAttributeNamesWeCareAbout, newAttributesObj, cb) { cb(); },
*/
/*
**********************************************
* Custom methods
**********************************************
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// > NOTE: There are a few gotchas here you should be aware of.
//
// + The collectionName argument is always prepended as the first argument.
// This is so you can know which model is requesting the adapter.
//
// + All adapter functions are asynchronous, even the completely custom ones,
// and they must always include a callback as the final argument.
// The first argument of callbacks is always an error object.
// For core CRUD methods, Waterline will add support for .done()/promise usage.
//
// + The function signature for all CUSTOM adapter methods below must be:
// `function (collectionName, options, cb) { ... }`
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Custom methods defined here will be available on all models
// which are hooked up to this adapter:
//
// e.g.:
//
foo: function (collectionName, options, cb) {
return cb(null,"ok");
},
bar: function (collectionName, options, cb) {
if (!options.jello) return cb("Failure!");
else return cb();
}
// So if you have three models:
// Tiger, Sparrow, and User
// 2 of which (Tiger and Sparrow) implement this custom adapter,
// then you'll be able to access:
//
// Tiger.foo(...)
// Tiger.bar(...)
// Sparrow.foo(...)
// Sparrow.bar(...)
// Example success usage:
//
// (notice how the first argument goes away:)
Tiger.foo({}, function (err, result) {
if (err) return console.error(err);
else console.log(result);
// outputs: ok
});
// Example error usage:
//
// (notice how the first argument goes away:)
Sparrow.bar({test: 'yes'}, function (err, result){
if (err) console.error(err);
else console.log(result);
// outputs: Failure!
})
*/
};
// Expose adapter definition
return adapter;
})();