Skip to content

Commit 37de0ab

Browse files
committed
Finish cleanup of notes
1 parent 109c883 commit 37de0ab

File tree

1 file changed

+153
-35
lines changed

1 file changed

+153
-35
lines changed

SYNTAX_NOTES.js

Lines changed: 153 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,31 @@ Waterline.connect({
168168
// and to `releaseConnection()`.
169169
meta: {},
170170

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,
173193
getConnection: MySQL.getConnection,
174194
releaseConnection: MySQL.releaseConnection,
195+
destroyManager: MySQL.destroyManager
175196

176197
}).exec(/*...*/);
177198

@@ -197,17 +218,59 @@ Waterline.transaction({
197218
},
198219

199220
// `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.)
202223
meta: {},
203224

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,
206268
getConnection: MySQL.getConnection,
207269
beginTransaction: MySQL.beginTransaction,
208270
rollbackTransaction: MySQL.rollbackTransaction,
209271
commitTransaction: MySQL.commitTransaction,
210272
releaseConnection: MySQL.releaseConnection,
273+
destroyManager: MySQL.destroyManager
211274

212275
}).exec(/*...*/);
213276

@@ -218,8 +281,11 @@ Waterline.transaction({
218281
// Waterline.query()
219282
Waterline.query({
220283

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+
},
223289

224290
// `meta` is passed through as the `meta` argin to each of
225291
// the six custom driver functions:
@@ -232,19 +298,50 @@ Waterline.query({
232298
//
233299
// If `connection` is omitted, a new connection will be acquired
234300
// from the manager using `getConnection()`.
235-
// connection: '===',
301+
connection: someConnection,
236302
//
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.)
239305

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,
242338
getConnection: MySQL.getConnection,
243339
compileStatement: MySQL.compileStatement,
244340
sendNativeQuery: MySQL.sendNativeQuery,
245341
parseNativeQueryError: MySQL.parseNativeQueryError,
246342
parseNativeQueryResult: MySQL.parseNativeQueryResult,
247343
releaseConnection: MySQL.releaseConnection,
344+
destroyManager: MySQL.destroyManager,
248345

249346
}).exec(/*...*/);
250347

@@ -257,7 +354,8 @@ Waterline.query({
257354

258355

259356
////////////////////////////////////////////////////////////////////////////////////////
260-
// SIMPLISTIC USAGE OF TRANSACTIONS IN USERLAND CODE
357+
// EXAMPLE:
358+
// LOW-LEVEL USAGE OF TRANSACTIONS IN USERLAND CODE VIA MACHINEPACK
261359

262360

263361
var MySQL = require('machinepack-mysql');
@@ -271,12 +369,12 @@ MySQL.createManager({
271369
}).exec(function (err, report){
272370
if (err) { /* ... */ }
273371

274-
var larrysDbCluster = report.manager;
372+
var larrysDbClusterMgr = report.manager;
275373

276374

277375
Waterline.transaction({
278376

279-
manager: larrysDbCluster,
377+
manager: larrysDbClusterMgr,
280378
getConnection: MySQL.getConnection,
281379
beginTransaction: MySQL.beginTransaction,
282380
rollbackTransaction: MySQL.rollbackTransaction,
@@ -315,7 +413,7 @@ MySQL.createManager({
315413

316414

317415
////////////////////////////////////////////////////////////////////////////////////////
318-
// SAME THING, BUT IN A SAILS APP
416+
// NOW MORE OR LESS THE SAME THING, BUT IN A SAILS APP:
319417

320418

321419
// Fetches a preconfigured deferred object hooked up to the sails-mysql adapter
@@ -349,6 +447,12 @@ sails.datastore('larrysDbCluster')
349447
});
350448
});
351449
})
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()`
352456
.exec(function afterwards(err, mush) {
353457
if (err) {
354458
// Transaction failed to start, or failed and had to be rolled back.
@@ -374,12 +478,11 @@ sails.datastore('larrysDbCluster')
374478

375479
////////////////////////////////////////////////////////////////////////////////////////
376480
// NOW ONCE MORE, BUT BACK TO PURE MACHINE USAGE
377-
// (taking advantage of some hypotheticals)
481+
// (taking advantage of the cleanest, simplest usage)
378482

379483
var Waterline = require('machinepack-waterline');
380-
381484
Waterline.transaction({
382-
485+
datastore: 'larrysDbCluster',
383486
during: function (T, done) {
384487

385488
// First check that the location exists.
@@ -399,26 +502,14 @@ Waterline.transaction({
399502
return done(undefined, mush);
400503
});
401504
});
402-
},
403-
404-
meta: {},
505+
}
405506

406-
// The identity of the datastore to use.
407-
datastore: 'larrysDbCluster',
507+
}).exec(/*...*/);
408508

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,
418509

419-
});
420510

421511

512+
////////////////////////////////////////////////////////////////////////////////////////////////
422513
// Note that for the above to work, process-global memory would need to be used.
423514
// This is ok as long as we are very careful, and we namespace not just under pkg name,
424515
// but also under version string. For example, the global might be a dictionary of
@@ -430,6 +521,33 @@ Waterline.transaction({
430521
// (effectively what happens in Sails/Waterline core as well).
431522
//
432523
// 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+
433551

434552

435553

0 commit comments

Comments
 (0)