UNPKG

2.38 kBtext/coffeescriptView Raw
1mohair = require 'mohair'
2
3associations = require './associations'
4
5module.exports =
6
7 # default
8 # --------
9
10 _mohair: mohair
11 _primaryKey: 'id'
12
13 # setter
14 # ------
15
16 set: (key, value) ->
17 object = Object.create this
18 object[key] = value
19 object
20
21 connection: (arg) ->
22 this.set '_connection', arg
23 attributes: (arg) ->
24 this.set '_attributes', arg
25 primaryKey: (arg) ->
26 this.set '_primaryKey', arg
27 includes: (arg) ->
28 this.set '_includes', arg
29
30 table: (arg) ->
31 this.set('_table', arg).set '_mohair', this._mohair.table arg
32
33 # mohair passthroughs
34 # -------------------
35
36 sql: ->
37 this._mohair.sql()
38 params: ->
39 this._mohair.params()
40
41 raw: (args...) ->
42 this._mohair.raw args...
43
44 where: (args...) ->
45 this.set '_mohair', this._mohair.where args...
46 join: (args...) ->
47 this.set '_mohair', this._mohair.join args...
48
49 select: (args...) ->
50 this.set '_mohair', this._mohair.select args...
51 limit: (arg) ->
52 this.set '_mohair', this._mohair.limit arg
53 offset: (arg) ->
54 this.set '_mohair', this._mohair.offset arg
55 order: (arg) ->
56 this.set '_mohair', this._mohair.order arg
57 group: (arg) ->
58 this.set '_mohair', this._mohair.group arg
59 with: (arg) ->
60 this.set '_mohair', this._mohair.with arg
61
62 # misc
63 # ----
64
65 assertTable: ->
66 unless this._table?
67 throw new Error 'mesa requires `table()` to be called before an insert, update or delete query'
68
69 assertConnection: ->
70 unless this._connection?
71 throw new Error 'mesa requires `connection()` to be called before any query'
72
73 assertAttributes: ->
74 unless this._attributes?
75 throw new Error 'mesa requires `attributes()` to be called before an insert or update query'
76
77 # associations
78 # ------------
79
80
81 enableConnectionReuseForIncludes: false
82 enableParallelIncludes: false
83
84 hasAssociated: associations.hasAssociated
85 hasOne: associations.hasOne
86 hasMany: associations.hasMany
87 belongsTo: associations.belongsTo
88 hasManyThrough: associations.hasManyThrough
89 hasOneThrough: associations.hasOneThrough
90
91 _getIncludes: associations._getIncludes
92 _prepareAssociatedTable: associations._prepareAssociatedTable