This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmesa.coffee
613 lines (506 loc) · 19.2 KB
/
mesa.coffee
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
mohair = require 'mohair'
Promise = require 'bluebird'
_ = require 'lodash'
################################################################################
# helpers
helpers = {}
# escape that handles table schemas correctly:
# schemaAwareEscape('someschema.users') -> '"someschema"."users"
helpers.schemaAwareEscape = (string) ->
string.split('.').map((str) -> "\"#{str}\"").join '.'
helpers.defaultMohair = mohair
.escape(helpers.schemaAwareEscape)
# return everything by default
.returning('*')
# TODO better name
helpers.replacePlaceholders = (sql) ->
# replace ?, ?, ... with $1, $2, ...
index = 1
sql.replace /\?/g, -> '$' + index++
helpers.ignoredArgumentWarning = (receiver) ->
"you called #{receiver} with an argument but #{receiver} ignores all arguments. #{receiver} returns a promise and maybe you wanted to call that promise instead: #{receiver}.then(function(result) { ... })"
helpers.normalizeLink = (leftTable, rightTable, immutableLink) ->
leftTableName = leftTable.getTable()
rightTableName = rightTable.getTable()
link = if immutableLink? then _.clone immutableLink else {}
link.forward ?= true
link.first ?= false
unless link.left?
if link.forward
# primary key
link.left = leftTable.getPrimaryKey()
else
# foreign key
unless rightTableName?
# TODO fix error message
throw new Error 'default for embed option `thisKey` requires call to .table(name) on this table'
link.left = rightTableName + '_' + rightTable.getPrimaryKey()
unless link.right?
if link.forward
# foreign key
unless leftTableName?
# TODO fix error message
throw new Error 'default for embed option `otherKey` requires call to .table(name) on other table'
link.right = leftTableName + '_' + leftTable.getPrimaryKey()
else
# primary key
link.right = rightTable.getPrimaryKey()
if link.as is true
unless rightTableName?
# TODO fix error message
throw new Error 'default for embed option `as` requires call to .table(name) on other table'
link.as = rightTableName
unless link.first
link.as += 's'
return link
# returns a list of tables with complete links between them
# TODO error handling for malformed argument lists
helpers.normalizeIncludeArguments = (args...) ->
# some state that will be modified by the loop below
normalized = []
leftTable = null
link = null
lastIndex = args.length - 1
args.forEach (arg, index) ->
if helpers.isInstance arg
if leftTable?
# last results are always included
link = if link? then _.clone(link) else {}
if index is lastIndex
link.as ?= true
rightTable = arg
link = helpers.normalizeLink leftTable, rightTable, link
link.table = rightTable
normalized.push link
# dont use the link again
link = null
# in any case set the next leftTable
leftTable = arg
else
link = arg
return normalized
################################################################################
# core
Mesa = (source) ->
if source
# only copy OWN properties.
# don't copy properties on the prototype.
# OWN properties are just non-default values and user defined methods.
# OWN properties tend to be very few for most queries.
for own k, v of source
this[k] = v
return this
Mesa.prototype =
# the magic behind mohair's fluent interface:
# after benchmarking several possible solutions
# and even prototypically inheriting from the calling objects
# i found that
# splitting the properties
# and
# is the most time and space efficient solution.
# in reality only very few properties
#
# with prototypical inheritance produces a long prototype chain.
# long prorotypical inheritance chains are not performant.
# it also prevents the entire chain from being garbage collected
# which requires a lot of space.
#
# all intermediaries can be safely garbage collected
fluent: (key, value) ->
next = new Mesa @
next[key] = value
return next
# call a one-off function as if it were part of mesa
call: (f, args...) ->
result = f.apply @, args
unless helpers.isInstance result
throw new Error 'the function passed to .call() must return a mesa-object'
return result
when: (condition, fn, args...) ->
if condition then @call fn, args... else @
each: (arrayOrObject, fn, args...) ->
callback = (that, value, indexOrKey) ->
result = fn.call that, value, indexOrKey, args...
unless helpers.isInstance result
throw new Error 'the function passed to .each() must return a mesa-object'
result
_.reduce arrayOrObject, callback, this
_returnFirst: false
returnFirst: (arg = true) ->
@fluent '_returnFirst', arg
debug: (arg) ->
@fluent '_debug', arg
################################################################################
# mass assignment protection
_allowed: []
allow: (columns...) ->
@fluent '_allowed', _.flatten(columns)
_isUnsafe: false
unsafe: (isUnsafe = true) ->
@fluent '_isUnsafe', isUnsafe
pickAllowed: (record) ->
if @_isUnsafe
return record
if @_allowed.length is 0
throw new Error [
'no columns are allowed.'
'this will make .update() or .insert() fail.'
'call .allow(columns...) with at least one column before .insert() or .update().'
'alternatively call .unsafe() before to disable mass assignment protection altogether.'
].join(' ')
_.pick record, @_allowed
################################################################################
# pass throughs to the underlying mohair query builder instance
_mohair: helpers.defaultMohair
# implementation of sql-fragment interface
sql: (escape) ->
@_mohair.sql(escape)
params: ->
@_mohair.params()
raw: (args...) ->
@_mohair.raw args...
table: (arg) ->
@fluent '_mohair', @_mohair.table arg
getTable: ->
@_mohair.getTable()
from: (args...) ->
@fluent '_mohair', @_mohair.from args...
where: (args...) ->
@fluent '_mohair', @_mohair.where args...
having: (args...) ->
@fluent '_mohair', @_mohair.having args...
join: (args...) ->
@fluent '_mohair', @_mohair.join args...
select: (args...) ->
@fluent '_mohair', @_mohair.select args...
limit: (arg) ->
@fluent '_mohair', @_mohair.limit arg
offset: (arg) ->
@fluent '_mohair', @_mohair.offset arg
order: (arg) ->
@fluent '_mohair', @_mohair.order arg
group: (arg) ->
@fluent '_mohair', @_mohair.group arg
with: (arg) ->
@fluent '_mohair', @_mohair.with arg
returning: (args...) ->
@fluent '_mohair', @_mohair.returning args...
distinct: (args...) ->
@fluent '_mohair', @_mohair.distinct args...
for: (args...) ->
@fluent '_mohair', @_mohair.for args...
window: (args...) ->
@fluent '_mohair', @_mohair.window args...
combine: (args...) ->
@fluent '_mohair', @_mohair.combine args...
union: (args...) ->
@fluent '_mohair', @_mohair.union args...
unionAll: (args...) ->
@fluent '_mohair', @_mohair.unionAll args...
intersect: (args...) ->
@fluent '_mohair', @_mohair.intersect args...
intersectAll: (args...) ->
@fluent '_mohair', @_mohair.intersectAll args...
except: (args...) ->
@fluent '_mohair', @_mohair.except args...
exceptAll: (args...) ->
@fluent '_mohair', @_mohair.exceptAll args...
################################################################################
# set and get connection
setConnection: (arg) ->
typeofArg = typeof arg
unless ('function' is typeof arg) or (('object' is typeof arg) and arg.query?)
throw new Error '.setConnection() must be called with either a connection object or a function that takes a callback and calls it with a connection'
@fluent '_connection', arg
getConnection: (arg) ->
that = @
if arg?
throw new Error helpers.ignoredArgumentWarning '.getConnection()'
connection = @_connection
debug = @_debug
unless connection?
return Promise.reject new Error "the method you are calling requires a call to .setConnection() before it"
new Promise (resolve, reject) ->
if 'function' is typeof connection
return connection (err, result, realDone) ->
done = ->
debug? 'connection', 'done', {}, {connection: connection}, that
realDone()
if err?
done?()
return reject err
debug? 'connection', 'fresh', {}, {
connection: result
done: done
}, that
resolve
connection: result
done: done
debug? 'connection', 'reuse', {}, {connection: connection}, that
resolve
connection: connection
################################################################################
# promise wrapper around node-postgres
wrapInConnection: (block) ->
@getConnection().then ({connection, done}) ->
block(connection).finally ->
done?()
query: (sqlOrFragment, params) ->
if mohair.implementsSqlFragmentInterface sqlOrFragment
sql = sqlOrFragment.sql helpers.schemaAwareEscape
if params?
throw new Error 'query with sql fragment as first arg is not allowed to have a second arg'
params = sqlOrFragment.params()
else
sql = sqlOrFragment
sql = helpers.replacePlaceholders sql
that = @
debug = @_debug
@wrapInConnection (connection) ->
new Promise (resolve, reject) ->
debug? 'query', 'before', {
sql: sql,
params: params
}, {
connection: connection
}, that
connection.query sql, params, (err, results) ->
debug? 'query', 'after', {
sql: sql
params: params
}, {
connection: connection
err: err
results: results
}, that
if err?
return reject err
resolve results
wrapInTransaction: (block) ->
that = this
debug = @_debug
@wrapInConnection (connection) ->
withConnection = that.setConnection(connection)
debug? 'transaction', 'begin', {}, {connection: connection, block: block}, that
withConnection.query('BEGIN;')
.then ->
block connection
.then (result) ->
debug? 'transaction', 'commit', {}, {connection: connection, block: block}, that
withConnection.query('COMMIT;').then ->
result
.catch (error) ->
debug? 'transaction', 'rollback', {error: error}, {
connection: connection
block: block
}, that
withConnection.query('ROLLBACK;').then ->
Promise.reject error
################################################################################
runQueue: (queue, value) ->
context = @
reducer = (soFar, step) ->
soFar.then step.bind(context)
queue.reduce reducer, Promise.resolve value
################################################################################
# what happens after the promise returned by .query(sql, params) is resolved
# but before the promise returned by the calling function
# (.select(), insert.(), ...) is resolved.
afterQuery: (results, options) ->
that = this
debug = @_debug
rows = results.rows
unless rows
return results
debug? 'after-query', 'before-queue', {rows: rows}, options, that
@runQueue(options.after, rows)
.map @runQueue.bind(@, options.afterEach)
.then (rows) ->
debug? 'after-query', 'after-queue', {rows: rows}, options, that
if options.returnFirst
rows[0]
else
rows
################################################################################
# command: these functions have side effects
insert: (args...) ->
that = this
debug = @_debug
records = _.flatten args
if records.length is 0
throw new Error 'no records to insert'
returnFirst = args.length is 1 and not Array.isArray(args[0])
debug? 'insert', 'before-queue', {records: records}, {}, that
@runQueue(@_queueBeforeInsert, records)
.map @runQueue.bind(@, @_queueBeforeEachInsert)
.then (records) ->
debug? 'insert', 'after-queue', {records: records}, {}, that
records.forEach (record, index) ->
if Object.keys(record).length is 0
throw new Error "insert would fail because record at index #{index} is empty after processing before queue"
that.query(that._mohair.insert(records)).then (results) ->
that.afterQuery(results,
returnFirst: returnFirst
after: that._queueAfterInsert
afterEach: that._queueAfterEachInsert
)
update: (update) ->
that = this
debug = @_debug
debug? 'update', 'before-queue', {update: update}, {}, that
@runQueue(@_queueBeforeEachUpdate, update).then (update) ->
debug? 'update', 'after-queue', {update: update}, {}, that
that.query(that._mohair.update(update)).then (results) ->
that.afterQuery results,
returnFirst: that._returnFirst
after: that._queueAfterUpdate
afterEach: that._queueAfterEachUpdate
delete: ->
if arg?
throw new Error helpers.ignoredArgumentWarning '.delete()'
that = this
that.query(that._mohair.delete()).then (results) ->
that.afterQuery results,
returnFirst: that._returnFirst
after: that._queueAfterDelete
afterEach: that._queueAfterEachDelete
################################################################################
# query: these functions have no side effects
find: (arg) ->
if arg?
throw new Error helpers.ignoredArgumentWarning '.find()'
that = this
that.query(this).then (results) ->
that.afterQuery results,
returnFirst: that._returnFirst
after: that._queueAfterSelect
afterEach: that._queueAfterEachSelect
first: (arg) ->
if arg?
throw new Error helpers.ignoredArgumentWarning '.first()'
@limit(1)
.returnFirst()
.find()
exists: (arg) ->
if arg?
throw new Error helpers.ignoredArgumentWarning '.exists()'
@query(@_mohair.limit(1)).then (results) ->
results.rows? and results.rows.length isnt 0
################################################################################
# primary key (used by embeds)
_primaryKey: 'id'
primaryKey: (arg) ->
@fluent '_primaryKey', arg
getPrimaryKey: ->
@_primaryKey
################################################################################
# embed
# TODO REFACTOR this function works and is well tested
# but a bit of a complicated mess of side effects and promises
baseEmbed: (originalRecords, includes) ->
# regardless of how we branch off we keep them in buckets
# buckets always contains the records of the last layer
# that are connected to the records in the starting table
groupedByFirst = null
prevRecords = originalRecords
reducer = (soFar, include) ->
# run in series
# wait for previous include steps to continue
soFar.then ->
condition = {}
condition[include.right] = _.map prevRecords, include.left
include.table
.where(condition)
.find()
.then (nextRecords) ->
groupedByCurrent = _.groupBy nextRecords, include.right
groupedByFirst =
unless groupedByFirst?
groupedByCurrent
else
_.mapValues groupedByFirst, (records) ->
# forward bucket to the next layer
# by replacing records by the records they are
# associated with
_.reduce records, ((acc, record) ->
records = groupedByCurrent[record[include.left]]
if records? then acc.concat records else acc
), []
if include.as?
# embed this layer (currently in buckets) into the original records
originalRecords.forEach (record) ->
group = groupedByFirst[record[includes[0].left]] or []
if include.first
if group[0]?
record[include.as] = group[0]
else
record[include.as] = group
prevRecords = nextRecords
# run include steps in series
includes.reduce(reducer, Promise.resolve()).then ->
# finally return the original records
originalRecords
embed: (records, args...) ->
if records.length is 0
return records
@baseEmbed records, helpers.normalizeIncludeArguments @, args...
include: (args...) ->
@queueAfter _.partialRight @embed, args...
################################################################################
# automatic construction of setters and properties for queue:
# (automating this prevents copy & paste errors)
# TODO better name
payload = (f, args...) ->
if args.length is 0 then f else _.partialRight f, args...
setQueueProperties = (object, suffix) ->
setterPropertyName = 'queue' + suffix
dataPropertyName = '_' + setterPropertyName
object[dataPropertyName] = []
object[setterPropertyName] = (args...) ->
@fluent dataPropertyName, @[dataPropertyName].concat [payload args...]
# queueBeforeInsert, queueBeforeEachInsert
# queueBeforeEachUpdate (just that because the update is a single object/record)
setQueueProperties(Mesa.prototype, 'BeforeInsert')
setQueueProperties(Mesa.prototype, 'BeforeEachInsert')
setQueueProperties(Mesa.prototype, 'BeforeEachUpdate')
# queueAfterSelect, queueAfterEachSelect
# queueAfterInsert, queueAfterEachInsert
# queueAfterUpdate, queueAfterEachUpdate
# queueAfterDelete, queueAfterEachDelete
for phase in ['Select', 'Insert', 'Update', 'Delete']
setQueueProperties(Mesa.prototype, 'After' + phase)
setQueueProperties(Mesa.prototype, 'AfterEach' + phase)
Mesa.prototype.queueBeforeEach = (args...) ->
object = new Mesa @
['Insert', 'Update'].forEach (phase) ->
propertyName = '_queueBeforeEach' + phase
object[propertyName] = object[propertyName].concat [payload args...]
return object
Mesa.prototype.queueAfter = (args...) ->
object = new Mesa @
['Select', 'Insert', 'Update', 'Delete'].forEach (phase) ->
propertyName = '_queueAfter' + phase
object[propertyName] = object[propertyName].concat [payload args...]
return object
Mesa.prototype.queueAfterEach = (args...) ->
object = new Mesa @
['Select', 'Insert', 'Update', 'Delete'].forEach (phase) ->
propertyName = '_queueAfterEach' + phase
object[propertyName] = object[propertyName].concat [payload args...]
return object
################################################################################
# exports
Mesa.prototype.isInstance = helpers.isInstance = (object) ->
object instanceof Mesa
Mesa.prototype.helpers = helpers
# put mesaBaseProperties one step away from the exported object in the prototype chain
# such that mesa.clone() does not copy the mesaBase properties.
# mesa.clone() just copies OWN properties.
# user-added methods are OWN properties and get copied.
# this keeps the copies small which is nice for performance (memory and cpu)
# and makes inspecting the `this` objects more pleasant as they
# only contain relevant state.
mesa = new Mesa
module.exports = mesa
# enable mass assignment protection
.queueBeforeEach(mesa.pickAllowed)