mysql2-core 0.2.3 → 0.3.0
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/build.js +40 -40
- package/lib/index.js +806 -283
- package/package.json +2 -1
- package/src/build.ts +153 -153
- package/src/index.ts +925 -462
- package/src/metadata.ts +51 -70
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mysql2-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "mysql2-core",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"clean:lib": "rimraf lib"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
+
"mysql2": "^3.16.3",
|
|
12
13
|
"tslint": "5.10.0",
|
|
13
14
|
"typescript": "^3.3.3333"
|
|
14
15
|
},
|
package/src/build.ts
CHANGED
|
@@ -1,275 +1,275 @@
|
|
|
1
|
-
import {Attribute, Attributes, Statement, StringMap} from
|
|
1
|
+
import { Attribute, Attributes, Statement, StringMap } from "./metadata"
|
|
2
2
|
|
|
3
3
|
export function param(i: number): string {
|
|
4
|
-
return
|
|
4
|
+
return "?"
|
|
5
5
|
}
|
|
6
6
|
export function params(length: number, from?: number): string[] {
|
|
7
7
|
if (from === undefined || from == null) {
|
|
8
|
-
from = 0
|
|
8
|
+
from = 0
|
|
9
9
|
}
|
|
10
|
-
const ps: string[] = []
|
|
10
|
+
const ps: string[] = []
|
|
11
11
|
for (let i = 1; i <= length; i++) {
|
|
12
|
-
ps.push(param(i + from))
|
|
12
|
+
ps.push(param(i + from))
|
|
13
13
|
}
|
|
14
|
-
return ps
|
|
14
|
+
return ps
|
|
15
15
|
}
|
|
16
16
|
export interface Metadata {
|
|
17
|
-
keys: Attribute[]
|
|
18
|
-
bools?: Attribute[]
|
|
19
|
-
map?: StringMap
|
|
20
|
-
version?: string
|
|
21
|
-
fields?: string[]
|
|
17
|
+
keys: Attribute[]
|
|
18
|
+
bools?: Attribute[]
|
|
19
|
+
map?: StringMap
|
|
20
|
+
version?: string
|
|
21
|
+
fields?: string[]
|
|
22
22
|
}
|
|
23
23
|
export function metadata(attrs: Attributes): Metadata {
|
|
24
|
-
const mp: StringMap = {}
|
|
25
|
-
const ks = Object.keys(attrs)
|
|
26
|
-
const ats: Attribute[] = []
|
|
27
|
-
const bools: Attribute[] = []
|
|
28
|
-
const fields: string[] = []
|
|
29
|
-
const m: Metadata = {keys: ats, fields}
|
|
30
|
-
let isMap = false
|
|
24
|
+
const mp: StringMap = {}
|
|
25
|
+
const ks = Object.keys(attrs)
|
|
26
|
+
const ats: Attribute[] = []
|
|
27
|
+
const bools: Attribute[] = []
|
|
28
|
+
const fields: string[] = []
|
|
29
|
+
const m: Metadata = { keys: ats, fields }
|
|
30
|
+
let isMap = false
|
|
31
31
|
for (const k of ks) {
|
|
32
|
-
const attr = attrs[k]
|
|
33
|
-
attr.name = k
|
|
32
|
+
const attr = attrs[k]
|
|
33
|
+
attr.name = k
|
|
34
34
|
if (attr.key) {
|
|
35
|
-
ats.push(attr)
|
|
35
|
+
ats.push(attr)
|
|
36
36
|
}
|
|
37
37
|
if (!attr.ignored) {
|
|
38
|
-
fields.push(k)
|
|
38
|
+
fields.push(k)
|
|
39
39
|
}
|
|
40
|
-
if (attr.type ===
|
|
41
|
-
bools.push(attr)
|
|
40
|
+
if (attr.type === "boolean") {
|
|
41
|
+
bools.push(attr)
|
|
42
42
|
}
|
|
43
43
|
if (attr.version) {
|
|
44
|
-
m.version = k
|
|
44
|
+
m.version = k
|
|
45
45
|
}
|
|
46
|
-
const field =
|
|
47
|
-
const s = field.toLowerCase()
|
|
46
|
+
const field = attr.column ? attr.column : k
|
|
47
|
+
const s = field.toLowerCase()
|
|
48
48
|
if (s !== k) {
|
|
49
|
-
mp[s] = k
|
|
50
|
-
isMap = true
|
|
49
|
+
mp[s] = k
|
|
50
|
+
isMap = true
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
if (isMap) {
|
|
55
|
-
m.map = mp
|
|
55
|
+
m.map = mp
|
|
56
56
|
}
|
|
57
57
|
if (bools.length > 0) {
|
|
58
|
-
m.bools = bools
|
|
58
|
+
m.bools = bools
|
|
59
59
|
}
|
|
60
|
-
return m
|
|
60
|
+
return m
|
|
61
61
|
}
|
|
62
|
-
export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string, i?: number): Statement
|
|
62
|
+
export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string, i?: number): Statement {
|
|
63
63
|
if (!i) {
|
|
64
|
-
i = 1
|
|
64
|
+
i = 1
|
|
65
65
|
}
|
|
66
66
|
if (!buildParam) {
|
|
67
|
-
buildParam = param
|
|
67
|
+
buildParam = param
|
|
68
68
|
}
|
|
69
|
-
const ks = Object.keys(attrs)
|
|
70
|
-
const pks: Attribute[] = []
|
|
71
|
-
const cols: string[] = []
|
|
72
|
-
const values: string[] = []
|
|
73
|
-
const args: any[] = []
|
|
74
|
-
let isVersion = false
|
|
75
|
-
const o: any = obj
|
|
69
|
+
const ks = Object.keys(attrs)
|
|
70
|
+
const pks: Attribute[] = []
|
|
71
|
+
const cols: string[] = []
|
|
72
|
+
const values: string[] = []
|
|
73
|
+
const args: any[] = []
|
|
74
|
+
let isVersion = false
|
|
75
|
+
const o: any = obj
|
|
76
76
|
for (const k of ks) {
|
|
77
|
-
const attr = attrs[k]
|
|
78
|
-
attr.name = k
|
|
77
|
+
const attr = attrs[k]
|
|
78
|
+
attr.name = k
|
|
79
79
|
if (attr.key) {
|
|
80
|
-
pks.push(attr)
|
|
80
|
+
pks.push(attr)
|
|
81
81
|
}
|
|
82
|
-
let v = o[k]
|
|
82
|
+
let v = o[k]
|
|
83
83
|
if (v === undefined || v == null) {
|
|
84
|
-
v = attr.default
|
|
84
|
+
v = attr.default
|
|
85
85
|
}
|
|
86
86
|
if (v !== undefined && v != null && !attr.ignored && !attr.noinsert) {
|
|
87
|
-
const field =
|
|
88
|
-
cols.push(field)
|
|
87
|
+
const field = attr.column ? attr.column : k
|
|
88
|
+
cols.push(field)
|
|
89
89
|
if (k === ver) {
|
|
90
|
-
isVersion = true
|
|
91
|
-
values.push(`${1}`)
|
|
90
|
+
isVersion = true
|
|
91
|
+
values.push(`${1}`)
|
|
92
92
|
} else {
|
|
93
|
-
if (v ===
|
|
94
|
-
values.push(`''`)
|
|
95
|
-
} else if (typeof v ===
|
|
96
|
-
values.push(toString(v))
|
|
93
|
+
if (v === "") {
|
|
94
|
+
values.push(`''`)
|
|
95
|
+
} else if (typeof v === "number") {
|
|
96
|
+
values.push(toString(v))
|
|
97
97
|
} else {
|
|
98
|
-
const p = buildParam(i++)
|
|
99
|
-
values.push(p)
|
|
100
|
-
if (typeof v ===
|
|
98
|
+
const p = buildParam(i++)
|
|
99
|
+
values.push(p)
|
|
100
|
+
if (typeof v === "boolean") {
|
|
101
101
|
if (v === true) {
|
|
102
|
-
const v2 =
|
|
103
|
-
args.push(v2)
|
|
102
|
+
const v2 = attr.true ? attr.true : `'1'`
|
|
103
|
+
args.push(v2)
|
|
104
104
|
} else {
|
|
105
|
-
const v2 =
|
|
106
|
-
args.push(v2)
|
|
105
|
+
const v2 = attr.false ? attr.false : `'0'`
|
|
106
|
+
args.push(v2)
|
|
107
107
|
}
|
|
108
108
|
} else {
|
|
109
|
-
args.push(v)
|
|
109
|
+
args.push(v)
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
if (!isVersion && ver && ver.length > 0) {
|
|
116
|
-
const attr = attrs[ver]
|
|
117
|
-
const field =
|
|
118
|
-
cols.push(field)
|
|
119
|
-
values.push(`${1}`)
|
|
116
|
+
const attr = attrs[ver]
|
|
117
|
+
const field = attr.column ? attr.column : ver
|
|
118
|
+
cols.push(field)
|
|
119
|
+
values.push(`${1}`)
|
|
120
120
|
}
|
|
121
121
|
if (pks.length === 0) {
|
|
122
122
|
if (cols.length === 0) {
|
|
123
|
-
return
|
|
123
|
+
return { query: "", params: args }
|
|
124
124
|
} else {
|
|
125
|
-
const q = `insert into ${table}(${cols.join(
|
|
126
|
-
return { query: q, params: args }
|
|
125
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`
|
|
126
|
+
return { query: q, params: args }
|
|
127
127
|
}
|
|
128
128
|
} else {
|
|
129
|
-
const colSet: string[] = []
|
|
129
|
+
const colSet: string[] = []
|
|
130
130
|
for (const k of ks) {
|
|
131
|
-
const v = o[k]
|
|
131
|
+
const v = o[k]
|
|
132
132
|
if (v !== undefined) {
|
|
133
|
-
const attr = attrs[k]
|
|
133
|
+
const attr = attrs[k]
|
|
134
134
|
if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
|
|
135
|
-
const field =
|
|
136
|
-
let x: string
|
|
135
|
+
const field = attr.column ? attr.column : k
|
|
136
|
+
let x: string
|
|
137
137
|
if (v == null) {
|
|
138
|
-
x =
|
|
139
|
-
} else if (v ===
|
|
140
|
-
x = `''
|
|
141
|
-
} else if (typeof v ===
|
|
142
|
-
x = toString(v)
|
|
138
|
+
x = "null"
|
|
139
|
+
} else if (v === "") {
|
|
140
|
+
x = `''`
|
|
141
|
+
} else if (typeof v === "number") {
|
|
142
|
+
x = toString(v)
|
|
143
143
|
} else {
|
|
144
|
-
x = buildParam(i++)
|
|
145
|
-
if (typeof v ===
|
|
144
|
+
x = buildParam(i++)
|
|
145
|
+
if (typeof v === "boolean") {
|
|
146
146
|
if (v === true) {
|
|
147
|
-
const v2 =
|
|
148
|
-
args.push(v2)
|
|
147
|
+
const v2 = attr.true ? "" + attr.true : `'1'`
|
|
148
|
+
args.push(v2)
|
|
149
149
|
} else {
|
|
150
|
-
const v2 =
|
|
151
|
-
args.push(v2)
|
|
150
|
+
const v2 = attr.false ? "" + attr.false : `'0'`
|
|
151
|
+
args.push(v2)
|
|
152
152
|
}
|
|
153
153
|
} else {
|
|
154
|
-
args.push(v)
|
|
154
|
+
args.push(v)
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
|
-
colSet.push(`${field}=${x}`)
|
|
157
|
+
colSet.push(`${field}=${x}`)
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
if (colSet.length === 0) {
|
|
162
|
-
const q = `insert ignore into ${table}(${cols.join(
|
|
163
|
-
return { query: q, params: args }
|
|
162
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`
|
|
163
|
+
return { query: q, params: args }
|
|
164
164
|
} else {
|
|
165
|
-
const q = `insert into ${table}(${cols.join(
|
|
166
|
-
return { query: q, params: args }
|
|
165
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`
|
|
166
|
+
return { query: q, params: args }
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Statement[] {
|
|
171
171
|
if (!buildParam) {
|
|
172
|
-
buildParam = param
|
|
172
|
+
buildParam = param
|
|
173
173
|
}
|
|
174
|
-
const sts: Statement[] = []
|
|
175
|
-
const ks = Object.keys(attrs)
|
|
174
|
+
const sts: Statement[] = []
|
|
175
|
+
const ks = Object.keys(attrs)
|
|
176
176
|
for (const obj of objs) {
|
|
177
|
-
let i = 1
|
|
178
|
-
const cols: string[] = []
|
|
179
|
-
const values: string[] = []
|
|
180
|
-
const args: any[] = []
|
|
181
|
-
let isVersion = false
|
|
182
|
-
const o: any = obj
|
|
177
|
+
let i = 1
|
|
178
|
+
const cols: string[] = []
|
|
179
|
+
const values: string[] = []
|
|
180
|
+
const args: any[] = []
|
|
181
|
+
let isVersion = false
|
|
182
|
+
const o: any = obj
|
|
183
183
|
for (const k of ks) {
|
|
184
|
-
const attr = attrs[k]
|
|
185
|
-
let v = o[k]
|
|
184
|
+
const attr = attrs[k]
|
|
185
|
+
let v = o[k]
|
|
186
186
|
if (v === undefined || v == null) {
|
|
187
|
-
v = attr.default
|
|
187
|
+
v = attr.default
|
|
188
188
|
}
|
|
189
189
|
if (v != null && v !== undefined && !attr.ignored && !attr.noinsert) {
|
|
190
|
-
const field =
|
|
191
|
-
cols.push(field)
|
|
190
|
+
const field = attr.column ? attr.column : k
|
|
191
|
+
cols.push(field)
|
|
192
192
|
if (k === ver) {
|
|
193
|
-
isVersion = true
|
|
194
|
-
values.push(`${1}`)
|
|
193
|
+
isVersion = true
|
|
194
|
+
values.push(`${1}`)
|
|
195
195
|
} else {
|
|
196
|
-
if (v ===
|
|
197
|
-
values.push(`''`)
|
|
198
|
-
} else if (typeof v ===
|
|
199
|
-
values.push(toString(v))
|
|
196
|
+
if (v === "") {
|
|
197
|
+
values.push(`''`)
|
|
198
|
+
} else if (typeof v === "number") {
|
|
199
|
+
values.push(toString(v))
|
|
200
200
|
} else {
|
|
201
|
-
const p = buildParam(i++)
|
|
202
|
-
values.push(p)
|
|
203
|
-
if (typeof v ===
|
|
201
|
+
const p = buildParam(i++)
|
|
202
|
+
values.push(p)
|
|
203
|
+
if (typeof v === "boolean") {
|
|
204
204
|
if (v === true) {
|
|
205
|
-
const v2 =
|
|
206
|
-
args.push(v2)
|
|
205
|
+
const v2 = attr.true ? attr.true : `'1'`
|
|
206
|
+
args.push(v2)
|
|
207
207
|
} else {
|
|
208
|
-
const v2 =
|
|
209
|
-
args.push(v2)
|
|
208
|
+
const v2 = attr.false ? attr.false : `'0'`
|
|
209
|
+
args.push(v2)
|
|
210
210
|
}
|
|
211
211
|
} else {
|
|
212
|
-
args.push(v)
|
|
212
|
+
args.push(v)
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
217
|
}
|
|
218
218
|
if (!isVersion && ver && ver.length > 0) {
|
|
219
|
-
const attr = attrs[ver]
|
|
220
|
-
const field =
|
|
221
|
-
cols.push(field)
|
|
222
|
-
values.push(`${1}`)
|
|
219
|
+
const attr = attrs[ver]
|
|
220
|
+
const field = attr.column ? attr.column : ver
|
|
221
|
+
cols.push(field)
|
|
222
|
+
values.push(`${1}`)
|
|
223
223
|
}
|
|
224
|
-
const colSet: string[] = []
|
|
224
|
+
const colSet: string[] = []
|
|
225
225
|
for (const k of ks) {
|
|
226
|
-
const v = o[k]
|
|
226
|
+
const v = o[k]
|
|
227
227
|
if (v !== undefined) {
|
|
228
|
-
const attr = attrs[k]
|
|
228
|
+
const attr = attrs[k]
|
|
229
229
|
if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
|
|
230
|
-
const field =
|
|
231
|
-
let x: string
|
|
230
|
+
const field = attr.column ? attr.column : k
|
|
231
|
+
let x: string
|
|
232
232
|
if (v == null) {
|
|
233
|
-
x =
|
|
234
|
-
} else if (v ===
|
|
235
|
-
x = `''
|
|
236
|
-
} else if (typeof v ===
|
|
237
|
-
x = toString(v)
|
|
233
|
+
x = "null"
|
|
234
|
+
} else if (v === "") {
|
|
235
|
+
x = `''`
|
|
236
|
+
} else if (typeof v === "number") {
|
|
237
|
+
x = toString(v)
|
|
238
238
|
} else {
|
|
239
|
-
x = buildParam(i++)
|
|
240
|
-
if (typeof v ===
|
|
239
|
+
x = buildParam(i++)
|
|
240
|
+
if (typeof v === "boolean") {
|
|
241
241
|
if (v === true) {
|
|
242
|
-
const v2 =
|
|
243
|
-
args.push(v2)
|
|
242
|
+
const v2 = attr.true ? "" + attr.true : `'1'`
|
|
243
|
+
args.push(v2)
|
|
244
244
|
} else {
|
|
245
|
-
const v2 =
|
|
246
|
-
args.push(v2)
|
|
245
|
+
const v2 = attr.false ? "" + attr.false : `'0'`
|
|
246
|
+
args.push(v2)
|
|
247
247
|
}
|
|
248
248
|
} else {
|
|
249
|
-
args.push(v)
|
|
249
|
+
args.push(v)
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
|
-
colSet.push(`${field}=${x}`)
|
|
252
|
+
colSet.push(`${field}=${x}`)
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
256
|
if (colSet.length === 0) {
|
|
257
|
-
const q = `insert ignore into ${table}(${cols.join(
|
|
258
|
-
const smt: Statement = { query: q, params: args }
|
|
259
|
-
sts.push(smt)
|
|
257
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")});`
|
|
258
|
+
const smt: Statement = { query: q, params: args }
|
|
259
|
+
sts.push(smt)
|
|
260
260
|
} else {
|
|
261
|
-
const q = `insert into ${table}(${cols.join(
|
|
262
|
-
const smt: Statement = { query: q, params: args }
|
|
263
|
-
sts.push(smt)
|
|
261
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")};`
|
|
262
|
+
const smt: Statement = { query: q, params: args }
|
|
263
|
+
sts.push(smt)
|
|
264
264
|
}
|
|
265
265
|
}
|
|
266
|
-
return sts
|
|
266
|
+
return sts
|
|
267
267
|
}
|
|
268
|
-
const n =
|
|
268
|
+
const n = "NaN"
|
|
269
269
|
export function toString(v: number): string {
|
|
270
|
-
let x =
|
|
270
|
+
let x = "" + v
|
|
271
271
|
if (x === n) {
|
|
272
|
-
x =
|
|
272
|
+
x = "null"
|
|
273
273
|
}
|
|
274
|
-
return x
|
|
274
|
+
return x
|
|
275
275
|
}
|