forked from balderdashy/sails-adapter-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (165 loc) · 7.32 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
/*---------------------------------------------------------------
:: sails-boilerplate
-> adapter
---------------------------------------------------------------*/
var async = require('async');
var adapter = module.exports = {
// 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 not using a non-SQL / non-schema-ed data store
syncable: false,
// Including a commitLog config enables transactions in this adapter
// Please note that these are not ACID-compliant transactions:
// They guarantee *ISOLATION*, and use a configurable persistent store, so they are *DURABLE* in the face of server crashes.
// However there is no scheduled task that rebuild state from a mid-step commit log at server start, so they're not CONSISTENT yet.
// and there is still lots of work to do as far as making them ATOMIC (they're not undoable right now)
//
// However, for the immediate future, they do a great job of preventing race conditions, and are
// better than a naive solution. They add the most value in findOrCreate() and createEach().
//
// commitLog: {
// identity: '__default_mongo_transaction',
// adapter: 'sails-mongo'
// },
// 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'
// 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.
//
// drop => Drop schema and data, then recreate it
// alter => Drop/add columns as necessary, but try
// safe => Don't change anything (good for production DBs)
migrate: 'alter'
},
// This method runs when a model is initially registered at server start time
registerCollection: function(collection, cb) {
cb();
},
// The following methods are optional
////////////////////////////////////////////////////////////
// Optional hook fired when a model is unregistered, typically at server halt
// useful for tearing down remaining open connections, etc.
teardown: function(cb) {
cb();
},
// REQUIRED method if integrating with a schemaful database
define: function(collectionName, definition, cb) {
// Define a new "table" or "collection" schema in the data store
cb();
},
// REQUIRED method if integrating with a schemaful database
describe: function(collectionName, cb) {
// 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 database
drop: function(collectionName, cb) {
// Drop a "table" or "collection" schema from the data store
cb();
},
// Optional override of built-in alter logic
// Can be simulated with describe(), define(), and drop(),
// but will probably be made much more efficient by an override here
// alter: function (collectionName, attributes, cb) {
// Modify the schema of a table or collection in the data store
// cb();
// },
// REQUIRED method if users expect to call Model.create() or any methods
create: function(collectionName, values, cb) {
// Create a single new model specified by values
// Respond with error or newly created model instance
cb(null, values);
},
// REQUIRED method if users expect to call Model.find(), Model.findAll() or related methods
// You're actually supporting find(), findAll(), and other methods here
// but the core will take care of supporting all the different usages.
// (e.g. if this is a find(), not a findAll(), it will only send back a single model)
find: function(collectionName, options, cb) {
// ** Filter by criteria in options to generate result set
// Respond with an error or a *list* of models in result set
cb(null, []);
},
// REQUIRED method if users expect to call Model.update()
update: function(collectionName, options, values, cb) {
// ** Filter by criteria in options to generate result set
// Then update all model(s) in the result set
// Respond with error or a *list* of models that were updated
cb();
},
// REQUIRED method if users expect to call Model.destroy()
destroy: function(collectionName, options, cb) {
// ** Filter by criteria in options to generate result set
// Destroy all model(s) in the result set
// Return an error or nothing at all
cb();
},
// REQUIRED method if users expect to call Model.stream()
stream: function(collectionName, options, stream) {
// options is a standard criteria/options object (like in find)
// stream.write() and stream.end() should be called.
// for an example, check out:
// https://github.com/balderdashy/sails-dirty/blob/master/DirtyAdapter.js#L247
}
/*
**********************************************
* Optional overrides
**********************************************
// Optional override of built-in batch create logic for increased efficiency
// otherwise, uses create()
createEach: function (collectionName, cb) { cb(); },
// Optional override of built-in findOrCreate logic for increased efficiency
// otherwise, uses find() and create()
findOrCreate: function (collectionName, cb) { cb(); },
// Optional override of built-in batch findOrCreate logic for increased efficiency
// otherwise, uses findOrCreate()
findOrCreateEach: function (collectionName, 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 some core methods, Sails.js will add support for .done()/promise usage.
//
// +
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Any other methods you include will be available on your models
foo: function (collectionName, cb) {
cb(null,"ok");
},
bar: function (collectionName, baz, watson, cb) {
cb("Failure!");
}
// Example success usage:
Model.foo(function (err, result) {
if (err) console.error(err);
else console.log(result);
// outputs: ok
})
// Example error usage:
Model.bar(235, {test: 'yes'}, function (err, result){
if (err) console.error(err);
else console.log(result);
// outputs: Failure!
})
*/
};
////////////// //////////////////////////////////////////
////////////// Private Methods //////////////////////////////////////////
////////////// //////////////////////////////////////////