@@ -168,10 +168,31 @@ Waterline.connect({
168
168
// and to `releaseConnection()`.
169
169
meta : { } ,
170
170
171
- // In WL core, these are passed in automatically:
172
- manager : manager ,
171
+
172
+ // `manager` is optional (sometimes). When this is called from WL core,
173
+ // `manager` is passed in if you provide `.usingManager(...)` when building
174
+ // your query as a deferred object.
175
+ //
176
+ // If `manager` is omitted, a new manager will be created
177
+ // using `createManager()` and destroyed with `destroyManager()`
178
+ // when this operation completes (whether it is successful or not).
179
+ manager : someManager ,
180
+ //
181
+ // (note that if `manager` is not provided, then both `createManager` AND
182
+ // `destroyManager()` below are required. And if either of those functions
183
+ // is not provided, then `manager` is required.)
184
+
185
+ // Either the identity of the datastore to use
186
+ datastore : 'larrysDbCluster' ,
187
+ //-AND/OR-
188
+ //
189
+ // Any of these things:
190
+ // (if `datastore` is provided, these are optional. If any of
191
+ // them are ALSO provided, then they are used as overrides)
192
+ createManager : MySQL . createManager ,
173
193
getConnection : MySQL . getConnection ,
174
194
releaseConnection : MySQL . releaseConnection ,
195
+ destroyManager : MySQL . destroyManager
175
196
176
197
} ) . exec ( /*...*/ ) ;
177
198
@@ -197,17 +218,59 @@ Waterline.transaction({
197
218
} ,
198
219
199
220
// `meta` is passed through as the `meta` argin to each of
200
- // the five custom functions:
201
- // (`getConnection()`, `beginTransaction ()`, etc.)
221
+ // the custom functions below.
222
+ // (e.g. `getConnection()`, `releaseConnection ()`, etc.)
202
223
meta : { } ,
203
224
204
- // In WL core, these are passed in automatically:
205
- manager : manager ,
225
+ // `connection` is optional-- e.g. in WL core, it is passed in if
226
+ // you provide `.usingConnection(...)` when building your query
227
+ // as a deferred object.
228
+ //
229
+ // If `connection` is omitted, a new connection will be acquired
230
+ // from the manager using `getConnection()`.
231
+ connection : someConnection ,
232
+ //
233
+ // (note that `getConnection` and `releaseConnection` below are always
234
+ // required, even if `connection` is provided. This is just for predictability.)
235
+
236
+ // (note that if `connection` is not provided, then both `getConnection` AND
237
+ // `releaseConnection()` below are required. And if either of those functions
238
+ // is not provided, then `connection` is required.)
239
+
240
+
241
+ // `manager` is optional (sometimes). When this is called from n WL core,
242
+ // `manager` is passed in if you provide `.usingManager(...)` when building
243
+ // your query as a deferred object. If `connection` was explicitly provided,
244
+ // then `manager` is completely ignored.
245
+ //
246
+ // If `manager` is omitted, a new manager will be created
247
+ // using `createManager()` and destroyed with `destroyManager()`
248
+ // when this operation completes (whether it is successful or not).
249
+ manager : someManager ,
250
+ //
251
+ // (note that if `connection` is provided, then `manager` and the two related
252
+ // functions are completely ignored. On the other hand, if connection is NOT
253
+ // provided, then `manager`, `createManager` and/or `destroyManager` are used.
254
+ //
255
+ // If `manager` is not provided, then both `createManager` AND
256
+ // `destroyManager()` below are required. And if either of those functions
257
+ // is not provided, then `manager` is required.)
258
+
259
+
260
+ // Either the identity of the datastore to use
261
+ datastore : 'larrysDbCluster' ,
262
+ //-AND/OR-
263
+ //
264
+ // Any of these things:
265
+ // (if `datastore` is provided, these are optional. If any of
266
+ // them are ALSO provided, then they are used as overrides)
267
+ createManager : MySQL . createManager ,
206
268
getConnection : MySQL . getConnection ,
207
269
beginTransaction : MySQL . beginTransaction ,
208
270
rollbackTransaction : MySQL . rollbackTransaction ,
209
271
commitTransaction : MySQL . commitTransaction ,
210
272
releaseConnection : MySQL . releaseConnection ,
273
+ destroyManager : MySQL . destroyManager
211
274
212
275
} ) . exec ( /*...*/ ) ;
213
276
@@ -218,8 +281,11 @@ Waterline.transaction({
218
281
// Waterline.query()
219
282
Waterline . query ( {
220
283
221
- // This is a radar query statemment to run
222
- statement : { } ,
284
+ // This is a radar query statement to run
285
+ statement : {
286
+ select : [ '*' ] ,
287
+ from : 'dogfood_brands'
288
+ } ,
223
289
224
290
// `meta` is passed through as the `meta` argin to each of
225
291
// the six custom driver functions:
@@ -232,19 +298,50 @@ Waterline.query({
232
298
//
233
299
// If `connection` is omitted, a new connection will be acquired
234
300
// from the manager using `getConnection()`.
235
- // connection: '===' ,
301
+ connection : someConnection ,
236
302
//
237
- // (note that `getConnection` below is always required, even if
238
- // `connection` is provided. This is just for predictability.)
303
+ // (note that `getConnection` and `releaseConnection` below are always
304
+ // required, even if `connection` is provided. This is just for predictability.)
239
305
240
- // In WL core, these are passed in automatically:
241
- manager : manager ,
306
+ // (note that if `connection` is not provided, then both `getConnection` AND
307
+ // `releaseConnection()` below are required. And if either of those functions
308
+ // is not provided, then `connection` is required.)
309
+
310
+
311
+ // `manager` is optional (sometimes). When this is called from n WL core,
312
+ // `manager` is passed in if you provide `.usingManager(...)` when building
313
+ // your query as a deferred object. If `connection` was explicitly provided,
314
+ // then `manager` is completely ignored.
315
+ //
316
+ // If `manager` is omitted, a new manager will be created
317
+ // using `createManager()` and destroyed with `destroyManager()`
318
+ // when this operation completes (whether it is successful or not).
319
+ manager : someManager ,
320
+ //
321
+ // (note that if `connection` is provided, then `manager` and the two related
322
+ // functions are completely ignored. On the other hand, if connection is NOT
323
+ // provided, then `manager`, `createManager` and/or `destroyManager` are used.
324
+ //
325
+ // If `manager` is not provided, then both `createManager` AND
326
+ // `destroyManager()` below are required. And if either of those functions
327
+ // is not provided, then `manager` is required.)
328
+
329
+
330
+ // Either the identity of the datastore to use
331
+ datastore : 'larrysDbCluster' ,
332
+ //-AND/OR-
333
+ //
334
+ // Any of these things:
335
+ // (if `datastore` is provided, these are optional. If any of
336
+ // them are ALSO provided, then they are used as overrides)
337
+ createManager : MySQL . createManager ,
242
338
getConnection : MySQL . getConnection ,
243
339
compileStatement : MySQL . compileStatement ,
244
340
sendNativeQuery : MySQL . sendNativeQuery ,
245
341
parseNativeQueryError : MySQL . parseNativeQueryError ,
246
342
parseNativeQueryResult : MySQL . parseNativeQueryResult ,
247
343
releaseConnection : MySQL . releaseConnection ,
344
+ destroyManager : MySQL . destroyManager ,
248
345
249
346
} ) . exec ( /*...*/ ) ;
250
347
@@ -257,7 +354,8 @@ Waterline.query({
257
354
258
355
259
356
////////////////////////////////////////////////////////////////////////////////////////
260
- // SIMPLISTIC USAGE OF TRANSACTIONS IN USERLAND CODE
357
+ // EXAMPLE:
358
+ // LOW-LEVEL USAGE OF TRANSACTIONS IN USERLAND CODE VIA MACHINEPACK
261
359
262
360
263
361
var MySQL = require ( 'machinepack-mysql' ) ;
@@ -271,12 +369,12 @@ MySQL.createManager({
271
369
} ) . exec ( function ( err , report ) {
272
370
if ( err ) { /* ... */ }
273
371
274
- var larrysDbCluster = report . manager ;
372
+ var larrysDbClusterMgr = report . manager ;
275
373
276
374
277
375
Waterline . transaction ( {
278
376
279
- manager : larrysDbCluster ,
377
+ manager : larrysDbClusterMgr ,
280
378
getConnection : MySQL . getConnection ,
281
379
beginTransaction : MySQL . beginTransaction ,
282
380
rollbackTransaction : MySQL . rollbackTransaction ,
@@ -315,7 +413,7 @@ MySQL.createManager({
315
413
316
414
317
415
////////////////////////////////////////////////////////////////////////////////////////
318
- // SAME THING, BUT IN A SAILS APP
416
+ // NOW MORE OR LESS THE SAME THING, BUT IN A SAILS APP:
319
417
320
418
321
419
// Fetches a preconfigured deferred object hooked up to the sails-mysql adapter
@@ -349,6 +447,12 @@ sails.datastore('larrysDbCluster')
349
447
} ) ;
350
448
} ) ;
351
449
} )
450
+ // Note that I could have also used `.usingConnection()` and `.usingManager()`
451
+ // here if I needed to:
452
+ // .getManager(manager)
453
+ // .getConnection(someExistingConnection)
454
+
455
+ // Finally, when finished setting up, we call `.exec()`
352
456
. exec ( function afterwards ( err , mush ) {
353
457
if ( err ) {
354
458
// Transaction failed to start, or failed and had to be rolled back.
@@ -374,12 +478,11 @@ sails.datastore('larrysDbCluster')
374
478
375
479
////////////////////////////////////////////////////////////////////////////////////////
376
480
// NOW ONCE MORE, BUT BACK TO PURE MACHINE USAGE
377
- // (taking advantage of some hypotheticals )
481
+ // (taking advantage of the cleanest, simplest usage )
378
482
379
483
var Waterline = require ( 'machinepack-waterline' ) ;
380
-
381
484
Waterline . transaction ( {
382
-
485
+ datastore : 'larrysDbCluster' ,
383
486
during : function ( T , done ) {
384
487
385
488
// First check that the location exists.
@@ -399,26 +502,14 @@ Waterline.transaction({
399
502
return done ( undefined , mush ) ;
400
503
} ) ;
401
504
} ) ;
402
- } ,
403
-
404
- meta : { } ,
505
+ }
405
506
406
- // The identity of the datastore to use.
407
- datastore : 'larrysDbCluster' ,
507
+ } ) . exec ( /*...*/ ) ;
408
508
409
- // That means all of these get to be OPTIONAL:
410
- // (if provided, they are used as overrides)
411
- //
412
- // manager: larrysDbCluster,
413
- // getConnection: MySQL.getConnection,
414
- // beginTransaction: MySQL.beginTransaction,
415
- // rollbackTransaction: MySQL.rollbackTransaction,
416
- // commitTransaction: MySQL.commitTransaction,
417
- // releaseConnection: MySQL.releaseConnection,
418
509
419
- } ) ;
420
510
421
511
512
+ ////////////////////////////////////////////////////////////////////////////////////////////////
422
513
// Note that for the above to work, process-global memory would need to be used.
423
514
// This is ok as long as we are very careful, and we namespace not just under pkg name,
424
515
// but also under version string. For example, the global might be a dictionary of
@@ -430,6 +521,33 @@ Waterline.transaction({
430
521
// (effectively what happens in Sails/Waterline core as well).
431
522
//
432
523
// We should go with one approach or the other; definitely not both.
524
+ ////////////////////////////////////////////////////////////////////////////////////////////////
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
+
548
+
549
+
550
+
433
551
434
552
435
553
0 commit comments