doix-db 0.0.24 → 0.0.25

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.
Files changed (2) hide show
  1. package/lib/DbLang.js +49 -0
  2. package/package.json +1 -1
package/lib/DbLang.js CHANGED
@@ -108,6 +108,55 @@ class DbLang {
108
108
 
109
109
  }
110
110
 
111
+ genInsertParamsSql (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
+ let 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
+ if (sql.length !== 0) sql += ','
130
+
131
+ sql += qName
132
+
133
+ params.push (v)
134
+
135
+ }
136
+
137
+ const {length} = params;
138
+
139
+ if (length === 0) {
140
+
141
+ sql = 'DEFAULT VALUES'
142
+
143
+ }
144
+ else {
145
+
146
+ sql = `(${sql}) VALUES (?`
147
+
148
+ if (length !== 1) sql += ',?'.repeat (length - 1)
149
+
150
+ sql += ')'
151
+
152
+ }
153
+
154
+ params.push (`INSERT INTO ${qName} ${sql}`)
155
+
156
+ return params
157
+
158
+ }
159
+
111
160
  genUpdateParamsSql (name, data) {
112
161
 
113
162
  const {model} = this; if (!model) throw Error ('Model not set')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "description": "Shared database related code for doix",
5
5
  "main": "index.js",
6
6
  "files": [