doix-db 0.0.22 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/DbLang.js CHANGED
@@ -7,6 +7,8 @@ const QQ_ESC = new stringEscape ([
7
7
  [ '"', '""'],
8
8
  ])
9
9
 
10
+ const DELIM_UPDATE = [',', ' AND ']
11
+
10
12
  class DbLang {
11
13
 
12
14
  getDbObjectClass (o) {
@@ -105,6 +107,61 @@ class DbLang {
105
107
  }
106
108
 
107
109
  }
110
+
111
+ genUpdateParamsSql (name, data) {
112
+
113
+ const {model} = this; if (!model) throw Error ('Model not set')
114
+
115
+ const {map} = model; if (!map.has (name)) throw Error ('Entity not defined: ' + name)
116
+
117
+ const {pk, columns, qName} = map.get (name)
118
+
119
+ const sql = ['', ''], params = []
120
+
121
+ for (const [k, v] of Object.entries (data)) {
122
+
123
+ if (v === undefined) continue
124
+
125
+ if (!(k in columns)) continue
126
+
127
+ const {qName, nullable} = columns [k]
128
+
129
+ const i = pk.includes (k) ? 1 : 0
130
+
131
+ if (sql [i].length !== 0) sql [i] += DELIM_UPDATE [i]
132
+
133
+ sql [i] += qName + '='
134
+
135
+ if (v === null && !nullable) {
136
+
137
+ sql [i] += 'DEFAULT'
138
+
139
+ }
140
+ else {
141
+
142
+ sql [i] += '?'
143
+
144
+ if (i === 0) params.push (v)
145
+
146
+ }
147
+
148
+ }
149
+
150
+ for (const k of pk) {
151
+
152
+ const v = data [k]; if (v == null) throw Error (`The value of ${k} must be defined and not null`)
153
+
154
+ params.push (v)
155
+
156
+ }
157
+
158
+ if (sql [0].length === 0) return null
159
+
160
+ params.push (`UPDATE ${qName} SET ${sql [0]} WHERE ${sql [1]}`)
161
+
162
+ return params
163
+
164
+ }
108
165
 
109
166
  toParamsSql (query) {
110
167
 
@@ -38,6 +38,8 @@ class DbModel extends EventEmitter {
38
38
  }
39
39
 
40
40
  this.lang = 'db' in this ? this.db.lang : new DbLang ()
41
+
42
+ this.lang.model = this
41
43
 
42
44
  this.map = new DbObjectMap (mapOptions)
43
45
 
@@ -80,7 +80,7 @@ class DbQuery {
80
80
 
81
81
  const t = q.addTable (table.relation ? table.relation.name : table.sql, o)
82
82
 
83
- for (const f of table.filters) f.clone (t)
83
+ for (const {sql, params} of table.filters) t.filters.push ({sql, params: [...params]})
84
84
 
85
85
  }
86
86
 
@@ -16,18 +16,6 @@ class DbQueryTableColumnComparison {
16
16
 
17
17
  }
18
18
 
19
- clone (table) {
20
-
21
- const result = new DbQueryTableColumnComparison (table, this.column.name, this.op, this.params)
22
-
23
- result.sql = this.sql
24
-
25
- result.params = this.params
26
-
27
- return result
28
-
29
- }
30
-
31
19
  setParams (value) {
32
20
 
33
21
  const {op, table} = this, {lang} = table
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "Shared database related code for doix",
5
5
  "main": "index.js",
6
6
  "files": [