tspace-mysql 1.3.8 → 1.4.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.
@@ -161,13 +161,34 @@ class DB extends AbstractDB_1.AbstractDB {
161
161
  * @return {Connection}
162
162
  */
163
163
  getConnection(options) {
164
- const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
165
- const pool = new connection_1.PoolConnection(Object.assign({ host,
166
- port,
167
- database,
168
- user,
169
- password }, others));
170
- return pool.connection();
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ if (options == null) {
166
+ const pool = yield this.$pool.get();
167
+ return yield pool.connection();
168
+ }
169
+ const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
170
+ const pool = new connection_1.PoolConnection(Object.assign({ host,
171
+ port,
172
+ database,
173
+ user,
174
+ password }, others));
175
+ return yield pool.connection();
176
+ });
177
+ }
178
+ /**
179
+ * Get a connection
180
+ * @return {ConnectionTransaction} object - Connection for the transaction
181
+ * @type {object} connection
182
+ * @property {function} connection.query - execute query sql then release connection to pool
183
+ * @property {function} connection.startTransaction - start transaction of query
184
+ * @property {function} connection.commit - commit transaction of query
185
+ * @property {function} connection.rollback - rollback transaction of query
186
+ */
187
+ beginTransaction() {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ const pool = yield this.$pool.get();
190
+ return yield pool.connection();
191
+ });
171
192
  }
172
193
  /**
173
194
  * Covert result to array
@@ -217,21 +238,6 @@ class DB extends AbstractDB_1.AbstractDB {
217
238
  };
218
239
  }
219
240
  }
220
- /**
221
- * Get a connection
222
- * @return {ConnectionTransaction} object - Connection for the transaction
223
- * @type {object} connection
224
- * @property {function} connection.query - execute query sql then release connection to pool
225
- * @property {function} connection.startTransaction - start transaction of query
226
- * @property {function} connection.commit - commit transaction of query
227
- * @property {function} connection.rollback - rollback transaction of query
228
- */
229
- beginTransaction() {
230
- return __awaiter(this, void 0, void 0, function* () {
231
- const pool = yield this.$pool.get();
232
- return yield pool.connection();
233
- });
234
- }
235
241
  /**
236
242
  * Assign table name
237
243
  * @static
@@ -329,13 +335,20 @@ class DB extends AbstractDB_1.AbstractDB {
329
335
  * @return {Connection}
330
336
  */
331
337
  static getConnection(options) {
332
- const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
333
- const pool = new connection_1.PoolConnection(Object.assign({ host,
334
- port,
335
- database,
336
- user,
337
- password }, others));
338
- return pool.connection();
338
+ return __awaiter(this, void 0, void 0, function* () {
339
+ if (options == null) {
340
+ const self = new this();
341
+ const pool = yield self.$pool.get();
342
+ return yield pool.connection();
343
+ }
344
+ const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
345
+ const pool = new connection_1.PoolConnection(Object.assign({ host,
346
+ port,
347
+ database,
348
+ user,
349
+ password }, others));
350
+ return pool.connection();
351
+ });
339
352
  }
340
353
  _typeOf(data) {
341
354
  return Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
@@ -55,6 +55,18 @@ export interface Backup {
55
55
  export interface BackupToFile {
56
56
  database: string;
57
57
  filePath: string;
58
+ table?: string;
59
+ connection?: {
60
+ host: string;
61
+ port: number;
62
+ database: string;
63
+ username: string;
64
+ password: string;
65
+ };
66
+ }
67
+ export interface BackupTableToFile {
68
+ filePath: string;
69
+ table: string;
58
70
  connection?: {
59
71
  host: string;
60
72
  port: number;
@@ -40,7 +40,7 @@ declare class Model extends AbstractModel {
40
40
  * import { Blueprint } from 'tspace-mysql'
41
41
  * class User extends Model {
42
42
  * constructor() {
43
- * this.useCreateTableIfNotExists ({
43
+ * this.useSchema ({
44
44
  * id : new Blueprint().int().notNull().primary().autoIncrement(),
45
45
  * uuid : new Blueprint().varchar(50).null(),
46
46
  * email : new Blueprint().varchar(50).null(),
@@ -52,7 +52,7 @@ declare class Model extends AbstractModel {
52
52
  * }
53
53
  * @return {this} this
54
54
  */
55
- protected useCreateTableIfNotExists(schema: Record<string, any>): this;
55
+ protected useSchema(schema: Record<string, any>): this;
56
56
  /**
57
57
  *
58
58
  * Assign function callback in model like constructor()
@@ -211,17 +211,25 @@ declare class Model extends AbstractModel {
211
211
  * @example
212
212
  * class User extends Model {
213
213
  * constructor() {
214
- * this.useValidationSchema({
215
- * id : Number,
216
- * email : String,
217
- * name : String,
218
- * date : Date
219
- * })
214
+ * this.useValidationSchema()
220
215
  * }
221
216
  * }
222
217
  * @return {this} this
223
218
  */
224
- protected useValidationSchema(schema: Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
219
+ protected useValidationSchema(schema?: null | Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
220
+ /**
221
+ *
222
+ * Assign schema column in model for validation data types
223
+ * @param {Object<NumberConstructor | StringConstructor | DateConstructor>} schema types (String Number and Date)
224
+ * @example
225
+ * class User extends Model {
226
+ * constructor() {
227
+ * this.useValidationSchema()
228
+ * }
229
+ * }
230
+ * @return {this} this
231
+ */
232
+ protected useValidateSchema(schema?: null | Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
225
233
  /**
226
234
  * Assign hook function when execute returned results to callback function
227
235
  * @param {Array<Function>} arrayFunctions functions for callback result
@@ -1016,7 +1024,7 @@ declare class Model extends AbstractModel {
1016
1024
  private _handleRelations;
1017
1025
  private _handleRelationsQuery;
1018
1026
  private _validateMethod;
1019
- private _tryToCreateTable;
1027
+ private _checkSchemaOrNextError;
1020
1028
  private _initialModel;
1021
1029
  }
1022
1030
  export { Model };
@@ -73,7 +73,7 @@ class Model extends AbstractModel_1.AbstractModel {
73
73
  * import { Blueprint } from 'tspace-mysql'
74
74
  * class User extends Model {
75
75
  * constructor() {
76
- * this.useCreateTableIfNotExists ({
76
+ * this.useSchema ({
77
77
  * id : new Blueprint().int().notNull().primary().autoIncrement(),
78
78
  * uuid : new Blueprint().varchar(50).null(),
79
79
  * email : new Blueprint().varchar(50).null(),
@@ -85,8 +85,8 @@ class Model extends AbstractModel_1.AbstractModel {
85
85
  * }
86
86
  * @return {this} this
87
87
  */
88
- useCreateTableIfNotExists(schema) {
89
- this.$state.set('CREATE_TABLE', schema);
88
+ useSchema(schema) {
89
+ this.$state.set('SCHEMA_TABLE', schema);
90
90
  return this;
91
91
  }
92
92
  /**
@@ -301,18 +301,31 @@ class Model extends AbstractModel_1.AbstractModel {
301
301
  * @example
302
302
  * class User extends Model {
303
303
  * constructor() {
304
- * this.useValidationSchema({
305
- * id : Number,
306
- * email : String,
307
- * name : String,
308
- * date : Date
309
- * })
304
+ * this.useValidationSchema()
310
305
  * }
311
306
  * }
312
307
  * @return {this} this
313
308
  */
314
309
  useValidationSchema(schema) {
315
- this.$state.set('SCHEMA', schema);
310
+ this.$state.set('VALIDATE_SCHEMA', true);
311
+ this.$state.set('VALIDATE_SCHEMA_DEFINED', schema);
312
+ return this;
313
+ }
314
+ /**
315
+ *
316
+ * Assign schema column in model for validation data types
317
+ * @param {Object<NumberConstructor | StringConstructor | DateConstructor>} schema types (String Number and Date)
318
+ * @example
319
+ * class User extends Model {
320
+ * constructor() {
321
+ * this.useValidationSchema()
322
+ * }
323
+ * }
324
+ * @return {this} this
325
+ */
326
+ useValidateSchema(schema) {
327
+ this.$state.set('VALIDATE_SCHEMA', true);
328
+ this.$state.set('VALIDATE_SCHEMA_DEFINED', schema);
316
329
  return this;
317
330
  }
318
331
  /**
@@ -340,8 +353,8 @@ class Model extends AbstractModel_1.AbstractModel {
340
353
  */
341
354
  exceptColumns() {
342
355
  return __awaiter(this, void 0, void 0, function* () {
343
- if (this.$state.get('SCHEMA')) {
344
- const columns = Object.keys(this.$state.get('SCHEMA'));
356
+ if (this.$state.get('SCHEMA_TABLE')) {
357
+ const columns = Object.keys(this.$state.get('SCHEMA_TABLE'));
345
358
  const removeExcept = columns.filter((column) => {
346
359
  const excepts = this.$state.get('EXCEPT');
347
360
  return excepts.every((except) => except !== column);
@@ -437,7 +450,7 @@ class Model extends AbstractModel_1.AbstractModel {
437
450
  return result;
438
451
  }
439
452
  catch (e) {
440
- yield this._tryToCreateTable(e);
453
+ yield this._checkSchemaOrNextError(e);
441
454
  return yield this.queryStatement(sql);
442
455
  }
443
456
  });
@@ -465,7 +478,7 @@ class Model extends AbstractModel_1.AbstractModel {
465
478
  return result;
466
479
  }
467
480
  catch (e) {
468
- yield this._tryToCreateTable(e);
481
+ yield this._checkSchemaOrNextError(e);
469
482
  return yield this.actionStatement({
470
483
  sql,
471
484
  returnId
@@ -1707,44 +1720,7 @@ class Model extends AbstractModel_1.AbstractModel {
1707
1720
  `${this.$constants('FROM')}`,
1708
1721
  `\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
1709
1722
  ].join(' ');
1710
- const raw = yield this.queryStatement(sql);
1711
- const schemas = raw.map((r) => {
1712
- let schema = { [r.Field]: String };
1713
- const numberLists = [
1714
- 'tinyint',
1715
- 'smallint',
1716
- 'mediumint',
1717
- 'int',
1718
- 'bigint',
1719
- 'float',
1720
- 'double',
1721
- 'decimal',
1722
- 'real',
1723
- 'bit',
1724
- 'boolean',
1725
- 'serial'
1726
- ];
1727
- const dateAndTimeLists = [
1728
- 'date',
1729
- 'datetime',
1730
- 'time',
1731
- 'timestamp',
1732
- 'year'
1733
- ];
1734
- if (numberLists.includes(r.Type)) {
1735
- schema = {
1736
- [r.Field]: Number
1737
- };
1738
- }
1739
- if (dateAndTimeLists.includes(r.Type)) {
1740
- schema = {
1741
- [r.Field]: Date
1742
- };
1743
- }
1744
- return schema;
1745
- });
1746
- const result = Object.assign({}, ...schemas);
1747
- return result;
1723
+ return yield this.queryStatement(sql);
1748
1724
  });
1749
1725
  }
1750
1726
  /**
@@ -1989,27 +1965,37 @@ class Model extends AbstractModel_1.AbstractModel {
1989
1965
  return result;
1990
1966
  }
1991
1967
  _validateSchema(results) {
1992
- const schema = this.$state.get('SCHEMA');
1993
- if (schema == null)
1994
- return;
1995
1968
  if (!results.length)
1996
1969
  return;
1970
+ const validateSchema = Boolean(this.$state.get('VALIDATE_SCHEMA'));
1971
+ if (!validateSchema)
1972
+ return;
1973
+ const schemaTable = this.$state.get('SCHEMA_TABLE');
1974
+ const schemaTableDefined = this.$state.get('VALIDATE_SCHEMA_DEFINED');
1975
+ this._assertError(schemaTableDefined == null && schemaTable == null, "Can't validate schema withouted schema");
1976
+ const schema = schemaTableDefined !== null && schemaTableDefined !== void 0 ? schemaTableDefined : Object.keys(schemaTable).reduce((acc, key) => {
1977
+ acc[key] = schemaTable[key].valueType;
1978
+ return acc;
1979
+ }, {});
1980
+ if (schema == null)
1981
+ return;
1997
1982
  const typeOf = (data) => Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
1998
1983
  const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
1999
1984
  const regexDateTime = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]/;
2000
- const selectedAll = this.$state.get('SELECT').replace('SELECT', '').trim() === '*';
1985
+ const select = this.$state.get('SELECT');
1986
+ const selectedAll = select.replace('SELECT', '').trim() === '*';
2001
1987
  for (const result of results) {
2002
1988
  const schemaKeys = Object.keys(schema);
2003
1989
  const resultKeys = Object.keys(result);
2004
1990
  if (schemaKeys.some(s => !resultKeys.includes(s)) && selectedAll) {
2005
1991
  const columns = schemaKeys.filter(x => !resultKeys.includes(x));
2006
- this._assertError(`Not found this column [ ${columns.join(', ')} ] in result`);
1992
+ this._assertError(`Not found this column "${columns.join(', ')}" in result`);
2007
1993
  }
2008
1994
  for (const column in result) {
2009
1995
  const s = schema[column];
2010
1996
  if (s == null && selectedAll) {
2011
1997
  if (!schemaKeys.every(s => resultKeys.includes(s))) {
2012
- this._assertError(`Not found this column [ ${column} ] in result`);
1998
+ this._assertError(`Not found this column "${column}" in result`);
2013
1999
  }
2014
2000
  continue;
2015
2001
  }
@@ -2018,13 +2004,13 @@ class Model extends AbstractModel_1.AbstractModel {
2018
2004
  if (regexDate.test(result[column]) || regexDateTime.test(result[column])) {
2019
2005
  if (typeOf(new Date(result[column])) === typeOf(new s()))
2020
2006
  continue;
2021
- this._assertError(`This column [ ${column} ] is invalid schema field type`);
2007
+ this._assertError(`This column "${column}" is invalid schema field type`);
2022
2008
  }
2023
2009
  if (result[column] == null)
2024
2010
  continue;
2025
2011
  if (typeOf(result[column]) === typeOf(new s()))
2026
2012
  continue;
2027
- this._assertError(`This column [ ${column} ] is invalid schema field type`);
2013
+ this._assertError(`This column "${column}" is invalid schema field type`);
2028
2014
  }
2029
2015
  }
2030
2016
  return;
@@ -2845,23 +2831,48 @@ class Model extends AbstractModel_1.AbstractModel {
2845
2831
  }
2846
2832
  }
2847
2833
  }
2848
- _tryToCreateTable(e) {
2849
- var _a;
2834
+ _checkSchemaOrNextError(e) {
2835
+ var _a, _b, _c, _d, _e;
2850
2836
  return __awaiter(this, void 0, void 0, function* () {
2851
- const createTable = this.$state.get('CREATE_TABLE');
2852
- if (createTable == null)
2853
- throw e;
2854
- const errorMessage = (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : '';
2855
- const errorWhenTableIsNotExists = "doesn't exist";
2856
- if (!errorMessage.toLocaleLowerCase().includes(errorWhenTableIsNotExists))
2857
- throw e;
2858
- if (this.$state.get('QUERIES') > 3)
2859
- throw e;
2860
2837
  try {
2838
+ if (this.$state.get('QUERIES') > 3)
2839
+ throw e;
2840
+ const schemaTable = this.$state.get('SCHEMA_TABLE');
2841
+ if (schemaTable == null)
2842
+ throw e;
2843
+ const errorMessage = (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : '';
2844
+ if (errorMessage.toLocaleLowerCase().includes('unknown column')) {
2845
+ const pattern = /'([^']+)'/;
2846
+ const column = errorMessage.match(pattern)
2847
+ ? String(errorMessage.match(pattern)[0]).replace(/'/g, '').split('.').pop()
2848
+ : null;
2849
+ if (column == null)
2850
+ throw e;
2851
+ const type = (_c = (_b = schemaTable[column]) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : null;
2852
+ const attributes = (_e = (_d = schemaTable[column]) === null || _d === void 0 ? void 0 : _d.attributes) !== null && _e !== void 0 ? _e : null;
2853
+ if (type == null || attributes == null)
2854
+ throw e;
2855
+ const entries = Object.entries(schemaTable);
2856
+ const indexWithColumn = entries.findIndex(([key]) => key === column);
2857
+ const findAfterIndex = indexWithColumn ? entries[indexWithColumn - 1][0] : null;
2858
+ if (findAfterIndex == null)
2859
+ throw e;
2860
+ const sql = [
2861
+ `${this.$constants('ALTER_TABLE')}`,
2862
+ `${this.$state.get('TABLE_NAME')}`,
2863
+ `${this.$constants('ADD')}`,
2864
+ `\`${column}\` ${type} ${attributes.join(' ')}`,
2865
+ `${this.$constants('AFTER')} \`${findAfterIndex !== null && findAfterIndex !== void 0 ? findAfterIndex : ''}\``
2866
+ ].join(' ');
2867
+ yield this.queryStatement(sql);
2868
+ return;
2869
+ }
2870
+ if (!errorMessage.toLocaleLowerCase().includes("doesn't exist"))
2871
+ throw e;
2861
2872
  const tableName = this.$state.get('TABLE_NAME');
2862
- yield new Schema_1.Schema()
2863
- .debug(this.$state.get('DEBUG'))
2864
- .createTable(tableName, createTable);
2873
+ const sql = new Schema_1.Schema().createTable(tableName, schemaTable);
2874
+ yield this.queryStatement(sql);
2875
+ return;
2865
2876
  }
2866
2877
  catch (e) {
2867
2878
  throw e;
@@ -1,7 +1,7 @@
1
1
  import { Builder } from "./Builder";
2
2
  declare class Schema extends Builder {
3
3
  table: (table: string, schemas: Record<string, any>) => Promise<void>;
4
- createTable: (table: string, schemas: Record<string, any>) => Promise<any>;
4
+ createTable: (table: string, schemas: Record<string, any>) => string;
5
5
  }
6
6
  export { Schema };
7
7
  export default Schema;
@@ -20,10 +20,10 @@ class Schema extends Builder_1.Builder {
20
20
  let columns = [];
21
21
  for (const key in schemas) {
22
22
  const data = schemas[key];
23
- const { type, attrbuites } = data;
23
+ const { type, attributes } = data;
24
24
  columns = [
25
25
  ...columns,
26
- `${key} ${type} ${attrbuites === null || attrbuites === void 0 ? void 0 : attrbuites.join(' ')}`
26
+ `${key} ${type} ${attributes === null || attributes === void 0 ? void 0 : attributes.join(' ')}`
27
27
  ];
28
28
  }
29
29
  const sql = [
@@ -39,14 +39,14 @@ class Schema extends Builder_1.Builder {
39
39
  console.log((_a = err.message) === null || _a === void 0 ? void 0 : _a.replace(/ER_TABLE_EXISTS_ERROR:/g, ""));
40
40
  }
41
41
  });
42
- this.createTable = (table, schemas) => __awaiter(this, void 0, void 0, function* () {
42
+ this.createTable = (table, schemas) => {
43
43
  let columns = [];
44
44
  for (const key in schemas) {
45
45
  const data = schemas[key];
46
- const { type, attrbuites } = data;
46
+ const { type, attributes } = data;
47
47
  columns = [
48
48
  ...columns,
49
- `${key} ${type} ${attrbuites === null || attrbuites === void 0 ? void 0 : attrbuites.join(' ')}`
49
+ `${key} ${type} ${attributes === null || attributes === void 0 ? void 0 : attributes.join(' ')}`
50
50
  ];
51
51
  }
52
52
  const sql = [
@@ -54,8 +54,8 @@ class Schema extends Builder_1.Builder {
54
54
  `${table} (${columns === null || columns === void 0 ? void 0 : columns.join(', ')})`,
55
55
  `ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8`
56
56
  ].join(' ');
57
- return yield this.rawQuery(sql);
58
- });
57
+ return sql;
58
+ };
59
59
  }
60
60
  }
61
61
  exports.Schema = Schema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.3.8",
3
+ "version": "1.4.0",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",