mysql2-core 0.3.9 → 0.5.1

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/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Connection, createPool as createPool2, FieldPacket, Pool, PoolConnection, ResultSetHeader } from "mysql2"
2
2
  import { Pool as PromisePool } from "mysql2/promise"
3
- import { buildToSave, buildToSaveBatch, param } from "./build"
3
+ import { buildToSave, buildToSaveBatch, metadata, param } from "./build"
4
4
  import { Attribute, Attributes, DB, Statement, StringMap, Transaction } from "./metadata"
5
5
 
6
6
  export * from "./build"
@@ -40,32 +40,24 @@ export function getConnection(pool: Pool): Promise<PoolConnection> {
40
40
  })
41
41
  })
42
42
  }
43
- export function beginTransaction(connection: PoolConnection, rollbackIfError?: boolean): Promise<void> {
43
+ export function beginTransaction(connection: PoolConnection): Promise<void> {
44
44
  return new Promise<void>((resolve, reject) => {
45
- connection.beginTransaction((err) => {
46
- if (err) {
47
- if (!rollbackIfError) {
48
- return reject(err)
49
- }
50
- return connection.rollback(() => {
51
- return reject(err)
52
- })
45
+ connection.beginTransaction((beginError) => {
46
+ if (beginError) {
47
+ connection.release()
48
+ return reject(beginError)
53
49
  }
54
50
  return resolve()
55
51
  })
56
52
  })
57
53
  }
58
- export function commit(connection: PoolConnection, rollbackIfError?: boolean): Promise<void> {
54
+ export function commit(connection: PoolConnection): Promise<void> {
59
55
  return new Promise<void>((resolve, reject) => {
60
56
  connection.commit((err) => {
61
57
  if (err) {
62
- if (!rollbackIfError) {
63
- return reject(err)
64
- }
65
- return connection.rollback(() => {
66
- return reject(err)
67
- })
58
+ return reject(err)
68
59
  }
60
+ connection.release()
69
61
  return resolve()
70
62
  })
71
63
  })
@@ -73,6 +65,7 @@ export function commit(connection: PoolConnection, rollbackIfError?: boolean): P
73
65
  export function rollback(connection: PoolConnection): Promise<void> {
74
66
  return new Promise<void>((resolve, reject) => {
75
67
  connection.rollback((err) => {
68
+ connection.release()
76
69
  if (err) {
77
70
  return reject(err)
78
71
  }
@@ -103,39 +96,31 @@ export class PoolManager implements DB {
103
96
  }
104
97
  connection.beginTransaction((beginError) => {
105
98
  if (beginError) {
106
- return connection.rollback(() => {
107
- connection.release()
108
- return reject(beginError)
109
- })
99
+ connection.release()
100
+ return reject(beginError)
110
101
  }
111
102
  return resolve(new PoolConnectionManager(connection))
112
103
  })
113
104
  })
114
105
  })
115
106
  }
116
- execute(sql: string, args?: any[], ctx?: any): Promise<number> {
117
- const p = ctx ? ctx : this.pool
118
- return execute(p, sql, args)
107
+ execute(sql: string, args?: any[]): Promise<number> {
108
+ return execute(this.pool, sql, args)
119
109
  }
120
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
121
- const p = ctx ? ctx : this.pool
122
- return executeBatch(p, statements, firstSuccess)
110
+ executeBatch(statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
111
+ return executeBatch(this.pool, statements, requireFirstAffected)
123
112
  }
124
- query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
125
- const p = ctx ? ctx : this.pool
126
- return query(p, sql, args, m, bools)
113
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
114
+ return query(this.pool, sql, args, m, bools)
127
115
  }
128
- queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
129
- const p = ctx ? ctx : this.pool
130
- return queryOne(p, sql, args, m, bools)
116
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null> {
117
+ return queryOne(this.pool, sql, args, m, bools)
131
118
  }
132
- executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null> {
133
- const p = ctx ? ctx : this.pool
134
- return executeScalar<T>(p, sql, args)
119
+ executeScalar<T>(sql: string, args?: any[]): Promise<T | null> {
120
+ return executeScalar<T>(this.pool, sql, args)
135
121
  }
136
- count(sql: string, args?: any[], ctx?: any): Promise<number> {
137
- const p = ctx ? ctx : this.pool
138
- return count(p, sql, args)
122
+ count(sql: string, args?: any[]): Promise<number> {
123
+ return count(this.pool, sql, args)
139
124
  }
140
125
  }
141
126
  // tslint:disable-next-line:max-classes-per-file
@@ -149,6 +134,7 @@ export class PoolConnectionManager implements Transaction {
149
134
  this.queryOne = this.queryOne.bind(this)
150
135
  this.executeScalar = this.executeScalar.bind(this)
151
136
  this.count = this.count.bind(this)
137
+ this.release = this.release.bind(this)
152
138
  }
153
139
  driver = "mysql"
154
140
  param(i: number): string {
@@ -158,10 +144,7 @@ export class PoolConnectionManager implements Transaction {
158
144
  return new Promise<void>((resolve, reject) => {
159
145
  this.connection.commit((err) => {
160
146
  if (err) {
161
- return this.connection.rollback(() => {
162
- this.release()
163
- return reject(err)
164
- })
147
+ return reject(err)
165
148
  }
166
149
  this.release()
167
150
  return resolve()
@@ -186,32 +169,26 @@ export class PoolConnectionManager implements Transaction {
186
169
  this.released = true
187
170
  this.connection.release()
188
171
  }
189
- execute(sql: string, args?: any[], ctx?: any): Promise<number> {
190
- const p = ctx ? ctx : this.connection
191
- return execute(p, sql, args)
172
+ execute(sql: string, args?: any[]): Promise<number> {
173
+ return execute(this.connection, sql, args)
192
174
  }
193
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
194
- const p = ctx ? ctx : this.connection
195
- return executeBatchConnection(p, statements, firstSuccess)
175
+ executeBatch(statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
176
+ return executeBatchConnection(this.connection, statements, requireFirstAffected)
196
177
  }
197
- query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
198
- const p = ctx ? ctx : this.connection
199
- return query(p, sql, args, m, bools)
178
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
179
+ return query(this.connection, sql, args, m, bools)
200
180
  }
201
- queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
202
- const p = ctx ? ctx : this.connection
203
- return queryOne(p, sql, args, m, bools)
181
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null> {
182
+ return queryOne(this.connection, sql, args, m, bools)
204
183
  }
205
- executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null> {
206
- const p = ctx ? ctx : this.connection
207
- return executeScalar<T>(p, sql, args)
184
+ executeScalar<T>(sql: string, args?: any[]): Promise<T | null> {
185
+ return executeScalar<T>(this.connection, sql, args)
208
186
  }
209
- count(sql: string, args?: any[], ctx?: any): Promise<number> {
210
- const p = ctx ? ctx : this.connection
211
- return count(p, sql, args)
187
+ count(sql: string, args?: any[]): Promise<number> {
188
+ return count(this.connection, sql, args)
212
189
  }
213
190
  }
214
- export async function executeBatch(pool: Pool, statements: Statement[], firstSuccess?: boolean): Promise<number> {
191
+ export async function executeBatch(pool: Pool, statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
215
192
  if (!statements || statements.length === 0) {
216
193
  return 0
217
194
  }
@@ -219,31 +196,29 @@ export async function executeBatch(pool: Pool, statements: Statement[], firstSuc
219
196
  return execute(pool, statements[0].query, statements[0].params)
220
197
  }
221
198
  const connection = await getConnection(pool)
222
- try {
223
- return await executeBatchConnectionTx(connection, statements, firstSuccess)
224
- } finally {
225
- connection.release()
226
- }
199
+ return await executeBatchConnectionTx(connection, statements, requireFirstAffected)
227
200
  }
228
- export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[], firstSuccess?: boolean): Promise<number> {
201
+ export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
229
202
  if (!statements || statements.length === 0) {
230
203
  return 0
231
204
  }
232
205
  if (statements.length === 1) {
233
- return execute(connection, statements[0].query, statements[0].params)
206
+ const count = await execute(connection, statements[0].query, statements[0].params)
207
+ connection.release()
208
+ return count
234
209
  }
235
- await beginTransaction(connection, true)
210
+ await beginTransaction(connection)
236
211
  try {
237
212
  let count = 0
238
213
 
239
214
  if (resource.multipleStatements) {
240
- if (firstSuccess) {
215
+ if (requireFirstAffected) {
241
216
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
242
217
 
243
218
  count = firstCount
244
219
 
245
220
  if (firstCount === 0) {
246
- await commit(connection, true)
221
+ await commit(connection)
247
222
  return 0
248
223
  }
249
224
  const remaining = statements.slice(1)
@@ -252,12 +227,12 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
252
227
  count = await executeMultipleStatements(connection, statements)
253
228
  }
254
229
  } else {
255
- const start = firstSuccess ? 1 : 0
256
- if (firstSuccess) {
230
+ const start = requireFirstAffected ? 1 : 0
231
+ if (requireFirstAffected) {
257
232
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
258
233
  count = firstCount
259
234
  if (firstCount === 0) {
260
- await commit(connection, true)
235
+ await commit(connection)
261
236
  return 0
262
237
  }
263
238
  }
@@ -267,7 +242,7 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
267
242
  }
268
243
  }
269
244
 
270
- await commit(connection, true)
245
+ await commit(connection)
271
246
 
272
247
  return count
273
248
  } catch (err) {
@@ -276,12 +251,12 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
276
251
  } catch {
277
252
  // Preserve the original error.
278
253
  }
279
-
280
- throw buildError(err)
254
+ buildError(err)
255
+ throw err
281
256
  }
282
257
  }
283
258
 
284
- export async function executeBatchConnection(connection: PoolConnection, statements: Statement[], firstSuccess?: boolean): Promise<number> {
259
+ export async function executeBatchConnection(connection: PoolConnection, statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
285
260
  if (!statements || statements.length === 0) {
286
261
  return 0
287
262
  }
@@ -291,7 +266,7 @@ export async function executeBatchConnection(connection: PoolConnection, stateme
291
266
  }
292
267
 
293
268
  if (resource.multipleStatements) {
294
- if (firstSuccess) {
269
+ if (requireFirstAffected) {
295
270
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
296
271
 
297
272
  if (firstCount === 0) {
@@ -305,9 +280,9 @@ export async function executeBatchConnection(connection: PoolConnection, stateme
305
280
  }
306
281
 
307
282
  let count = 0
308
- const start = firstSuccess ? 1 : 0
283
+ const start = requireFirstAffected ? 1 : 0
309
284
 
310
- if (firstSuccess) {
285
+ if (requireFirstAffected) {
311
286
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
312
287
 
313
288
  count = firstCount
@@ -437,25 +412,6 @@ export function count(pool: Pool | PoolConnection, sql: string, args?: any[]): P
437
412
  return executeScalar<number>(pool, sql, args).then((res) => (res !== null ? res : 0))
438
413
  }
439
414
 
440
- export function save<T>(pool: Pool | PoolConnection | ((sql: string, args?: any[]) => Promise<number>), obj: T, table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Promise<number> {
441
- const s = buildToSave(obj, table, attrs, ver, buildParam)
442
- if (!s.query) {
443
- return Promise.resolve(-1)
444
- }
445
- if (typeof pool === "function") {
446
- return pool(s.query, s.params)
447
- } else {
448
- return execute(pool, s.query, s.params)
449
- }
450
- }
451
- export function saveBatch<T>(pool: Pool | ((statements: Statement[]) => Promise<number>), objs: T[], table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Promise<number> {
452
- const s = buildToSaveBatch(objs, table, attrs, ver, buildParam)
453
- if (typeof pool === "function") {
454
- return pool(s)
455
- } else {
456
- return executeBatch(pool, s)
457
- }
458
- }
459
415
  export function toArray(arr?: any[]): any[] {
460
416
  if (!arr || arr.length === 0) {
461
417
  return []
@@ -646,20 +602,12 @@ export class StringAdapter {
646
602
  return execute(this.pool, s, values)
647
603
  }
648
604
  }
605
+ export const StringRepository = StringAdapter
606
+ export const StringService = StringAdapter
649
607
 
650
- export function version(attrs: Attributes): Attribute | undefined {
651
- const ks = Object.keys(attrs)
652
- for (const k of ks) {
653
- const attr = attrs[k]
654
- if (attr.version) {
655
- attr.name = k
656
- return attr
657
- }
658
- }
659
- return undefined
660
- }
661
608
  // tslint:disable-next-line:max-classes-per-file
662
609
  export class MySQLWriter<T> {
610
+ protected keys: Attribute[]
663
611
  protected version?: string
664
612
  protected param?: (i: number) => string
665
613
  constructor(
@@ -672,10 +620,9 @@ export class MySQLWriter<T> {
672
620
  ) {
673
621
  this.write = this.write.bind(this)
674
622
  this.param = buildParam ? buildParam : param
675
- const x = version(attributes)
676
- if (x) {
677
- this.version = x.name
678
- }
623
+ const m = metadata(attributes)
624
+ this.keys = m.keys
625
+ this.version = m.version
679
626
  }
680
627
  write(obj: T): Promise<number> {
681
628
  if (!obj) {
@@ -685,7 +632,7 @@ export class MySQLWriter<T> {
685
632
  if (this.map) {
686
633
  obj2 = this.map(obj)
687
634
  }
688
- const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param)
635
+ const stmt = buildToSave(obj2, this.table, this.attributes, this.keys, this.version, this.param)
689
636
  if (stmt.query) {
690
637
  if (this.oneIfSuccess) {
691
638
  return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
@@ -698,8 +645,9 @@ export class MySQLWriter<T> {
698
645
  }
699
646
  }
700
647
  // tslint:disable-next-line:max-classes-per-file
701
- export class MySQLStreamWriter<T> {
648
+ export class BufferedBatchWriter<T> {
702
649
  protected list: T[] = []
650
+ protected keys: Attribute[]
703
651
  protected version?: string
704
652
  protected param?: (i: number) => string
705
653
  constructor(
@@ -713,10 +661,9 @@ export class MySQLStreamWriter<T> {
713
661
  this.write = this.write.bind(this)
714
662
  this.flush = this.flush.bind(this)
715
663
  this.param = buildParam ? buildParam : param
716
- const x = version(attributes)
717
- if (x) {
718
- this.version = x.name
719
- }
664
+ const m = metadata(attributes)
665
+ this.keys = m.keys
666
+ this.version = m.version
720
667
  }
721
668
  write(obj: T): Promise<number> {
722
669
  if (!obj) {
@@ -739,37 +686,36 @@ export class MySQLStreamWriter<T> {
739
686
  if (!this.list || this.list.length === 0) {
740
687
  return Promise.resolve(0)
741
688
  } else {
742
- const total = this.list.length
743
- const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param)
689
+ const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.keys, this.version, this.param)
744
690
  if (stmt.length > 0) {
745
691
  return executeBatch(this.pool as any, stmt).then((r) => {
746
692
  this.list = []
747
- return total
693
+ return r
748
694
  })
749
695
  } else {
696
+ this.list = []
750
697
  return Promise.resolve(0)
751
698
  }
752
699
  }
753
700
  }
754
701
  }
755
702
  // tslint:disable-next-line:max-classes-per-file
756
- export class MySQLBatchWriter<T> {
703
+ export class BatchWriter<T> {
704
+ protected keys: Attribute[]
757
705
  protected version?: string
758
706
  protected param?: (i: number) => string
759
707
  constructor(
760
708
  protected pool: Pool,
761
709
  protected table: string,
762
710
  protected attributes: Attributes,
763
- protected oneIfSuccess?: boolean,
764
711
  protected map?: (v: T) => T,
765
712
  buildParam?: (i: number) => string,
766
713
  ) {
767
714
  this.write = this.write.bind(this)
768
715
  this.param = buildParam ? buildParam : param
769
- const x = version(attributes)
770
- if (x) {
771
- this.version = x.name
772
- }
716
+ const m = metadata(attributes)
717
+ this.keys = m.keys
718
+ this.version = m.version
773
719
  }
774
720
  write(objs: T[]): Promise<number> {
775
721
  if (!objs || objs.length === 0) {
@@ -783,13 +729,9 @@ export class MySQLBatchWriter<T> {
783
729
  list.push(obj2)
784
730
  }
785
731
  }
786
- const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param)
732
+ const stmts = buildToSaveBatch(list, this.table, this.attributes, this.keys, this.version, this.param)
787
733
  if (stmts.length > 0) {
788
- if (this.oneIfSuccess) {
789
- return executeBatch(this.pool, stmts).then((ct) => stmts.length)
790
- } else {
791
- return executeBatch(this.pool, stmts)
792
- }
734
+ return executeBatch(this.pool, stmts)
793
735
  } else {
794
736
  return Promise.resolve(0)
795
737
  }
@@ -903,38 +845,38 @@ export class Exporter<T> {
903
845
  const reader = this.connection.query(stmt.query, stmt.params)
904
846
  let er: any
905
847
  let i = 0
906
- let k = 0
848
+ let j = 0
907
849
  reader.on("error", (err) => (er = err))
908
850
  // (D2) WRITE ROW-BY-ROW
909
851
  if (this.map) {
910
852
  reader.on("result", async (row: any) => {
911
853
  ++i
912
- k++
854
+ j++
913
855
  this.connection.pause()
914
856
  const obj = mapOne<T>(row, this.map)
915
857
  const data = this.format(obj)
916
858
  this.write(data)
917
859
  this.connection.resume()
918
- if (k >= this.progressSize) {
860
+ if (j >= this.progressSize) {
919
861
  if (this.logInfo) {
920
862
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
921
863
  }
922
- k = 0
864
+ j = 0
923
865
  }
924
866
  })
925
867
  } else {
926
868
  reader.on("result", async (row: any) => {
927
869
  ++i
928
- k++
870
+ j++
929
871
  this.connection.pause()
930
872
  const data = this.format(row as T)
931
873
  this.write(data)
932
874
  this.connection.resume()
933
- if (k >= this.progressSize) {
875
+ if (j >= this.progressSize) {
934
876
  if (this.logInfo) {
935
877
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
936
878
  }
937
- k = 0
879
+ j = 0
938
880
  }
939
881
  })
940
882
  }
@@ -981,37 +923,37 @@ export class ExportService<T> {
981
923
  let er: any
982
924
  reader.on("error", (err) => (er = err))
983
925
  let i = 0
984
- let k = 0
926
+ let j = 0
985
927
  // (D2) WRITE ROW-BY-ROW
986
928
  if (this.map) {
987
929
  reader.on("result", async (row: any) => {
988
930
  ++i
989
- k++
931
+ j++
990
932
  this.connection.pause()
991
933
  const obj = mapOne<T>(row, this.map)
992
934
  const data = this.formatter.format(obj)
993
935
  this.writer.write(data)
994
936
  this.connection.resume()
995
- if (k >= this.progressSize) {
937
+ if (j >= this.progressSize) {
996
938
  if (this.logInfo) {
997
939
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
998
940
  }
999
- k = 0
941
+ j = 0
1000
942
  }
1001
943
  })
1002
944
  } else {
1003
945
  reader.on("result", async (row: any) => {
1004
946
  ++i
1005
- k++
947
+ j++
1006
948
  this.connection.pause()
1007
949
  const data = this.formatter.format(row as T)
1008
950
  this.writer.write(data)
1009
951
  this.connection.resume()
1010
- if (k >= this.progressSize) {
952
+ if (j >= this.progressSize) {
1011
953
  if (this.logInfo) {
1012
954
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
1013
955
  }
1014
- k = 0
956
+ j = 0
1015
957
  }
1016
958
  })
1017
959
  }
package/src/metadata.ts CHANGED
@@ -9,12 +9,12 @@ export interface Statement {
9
9
  export interface Executor {
10
10
  driver: string
11
11
  param(i: number): string
12
- execute(sql: string, args?: any[], ctx?: any): Promise<number>
13
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
14
- query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
15
- queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null>
16
- executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null>
17
- count(sql: string, args?: any[], ctx?: any): Promise<number>
12
+ execute(sql: string, args?: any[]): Promise<number>
13
+ executeBatch(statements: Statement[], requireFirstAffected?: boolean): Promise<number>
14
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]>
15
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null>
16
+ executeScalar<T>(sql: string, args?: any[]): Promise<T | null>
17
+ count(sql: string, args?: any[]): Promise<number>
18
18
  }
19
19
  export interface Transaction extends Executor {
20
20
  commit(): Promise<void>