@itrocks/mysql 0.0.13 → 0.0.15

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/cjs/mysql.d.ts CHANGED
@@ -19,6 +19,7 @@ interface Dependencies<QF extends object = object> {
19
19
  queryFunctionCall: (value: QF) => [any, string];
20
20
  storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
21
21
  }
22
+ export declare function joinTableName(object1: string | ObjectOrType, object2: string | ObjectOrType): string;
22
23
  export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
23
24
  export declare class Mysql extends DataSource {
24
25
  config: {
package/cjs/mysql.js CHANGED
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Mysql = exports.DEBUG = void 0;
4
+ exports.joinTableName = joinTableName;
4
5
  exports.mysqlDependsOn = mysqlDependsOn;
5
6
  const class_type_1 = require("@itrocks/class-type");
6
7
  const class_type_2 = require("@itrocks/class-type");
7
8
  const class_type_3 = require("@itrocks/class-type");
9
+ const property_type_1 = require("@itrocks/property-type");
8
10
  const reflect_1 = require("@itrocks/reflect");
9
11
  const reflect_2 = require("@itrocks/reflect");
10
12
  const storage_1 = require("@itrocks/storage");
@@ -21,6 +23,13 @@ const depends = {
21
23
  queryFunctionCall: () => [undefined, ' = ?'],
22
24
  storeOf: target => (0, class_type_3.typeOf)(target).name.toLowerCase()
23
25
  };
26
+ function joinTableName(object1, object2) {
27
+ if (typeof object1 !== 'string')
28
+ object1 = depends.storeOf(object1);
29
+ if (typeof object2 !== 'string')
30
+ object2 = depends.storeOf(object2);
31
+ return [object1, object2].sort().join('_');
32
+ }
24
33
  function mysqlDependsOn(dependencies) {
25
34
  Object.assign(depends, dependencies);
26
35
  }
@@ -52,7 +61,10 @@ class Mysql extends storage_1.DataSource {
52
61
  const connection = this.connection ?? await this.connect();
53
62
  const objectTable = depends.storeOf(object);
54
63
  const propertyTable = depends.storeOf(new reflect_2.ReflectProperty(object, property).collectionType.elementType.type);
55
- const joinTable = [objectTable, propertyTable].sort().join('_');
64
+ if (!objectTable || !propertyTable) {
65
+ throw 'Collection objects are not stored';
66
+ }
67
+ const joinTable = joinTableName(objectTable, propertyTable);
56
68
  const query = 'DELETE FROM `' + joinTable + '` WHERE ' + objectTable + '_id = ? AND ' + propertyTable + '_id = ?';
57
69
  const values = [object.id, id];
58
70
  if (exports.DEBUG)
@@ -78,7 +90,10 @@ class Mysql extends storage_1.DataSource {
78
90
  const connection = this.connection ?? await this.connect();
79
91
  const objectTable = depends.storeOf(object);
80
92
  const propertyTable = depends.storeOf(new reflect_2.ReflectProperty(object, property).collectionType.elementType.type);
81
- const joinTable = [objectTable, propertyTable].sort().join('_');
93
+ if (!objectTable || !propertyTable) {
94
+ throw 'Collection objects are not stored';
95
+ }
96
+ const joinTable = joinTableName(objectTable, propertyTable);
82
97
  const query = 'INSERT INTO `' + joinTable + '` SET ' + objectTable + '_id = ?, ' + propertyTable + '_id = ?';
83
98
  const values = [object.id, id];
84
99
  if (exports.DEBUG)
@@ -109,7 +124,9 @@ class Mysql extends storage_1.DataSource {
109
124
  const sql = ['id'];
110
125
  for (const property of new reflect_1.ReflectClass(type).properties) {
111
126
  const propertyType = property.type;
112
- const propertyName = ((0, class_type_2.isAnyType)(propertyType) && depends.storeOf(propertyType))
127
+ if (propertyType instanceof property_type_1.CollectionType)
128
+ continue;
129
+ const propertyName = ((0, class_type_2.isAnyType)(propertyType.type) && depends.storeOf(propertyType.type))
113
130
  ? property.name + 'Id'
114
131
  : property.name;
115
132
  const columnName = depends.columnOf(propertyName);
@@ -131,15 +148,18 @@ class Mysql extends storage_1.DataSource {
131
148
  const connection = this.connection ?? await this.connect();
132
149
  const propertiesSql = this.propertiesToSqlSelect(type);
133
150
  const objectTable = depends.storeOf(object);
134
- const table = depends.storeOf(type);
151
+ const propertyTable = depends.storeOf(type);
152
+ if (!objectTable || !propertyTable) {
153
+ throw 'Collection objects are not stored';
154
+ }
135
155
  let query;
136
156
  if (depends.componentOf(object, property)) {
137
- query = 'SELECT ' + propertiesSql + ' FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
157
+ query = 'SELECT ' + propertiesSql + ' FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
138
158
  }
139
159
  else {
140
- const joinTable = [objectTable, table].sort().join('_');
141
- query = 'SELECT `' + table + '`.' + propertiesSql + ' FROM `' + table + '`'
142
- + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + table + '_id = `' + table + '`.id'
160
+ const joinTable = joinTableName(objectTable, propertyTable);
161
+ query = 'SELECT `' + propertyTable + '`.' + propertiesSql + ' FROM `' + propertyTable + '`'
162
+ + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + propertyTable + '_id = `' + propertyTable + '`.id'
143
163
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
144
164
  }
145
165
  const rows = await connection.query(query, [object.id]);
@@ -149,12 +169,15 @@ class Mysql extends storage_1.DataSource {
149
169
  const connection = this.connection ?? await this.connect();
150
170
  const objectTable = depends.storeOf(object);
151
171
  const propertyTable = depends.storeOf(type);
172
+ if (!objectTable || !propertyTable) {
173
+ throw 'Collection objects are not stored';
174
+ }
152
175
  let query;
153
176
  if (depends.componentOf(object, property)) {
154
177
  query = 'SELECT id FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
155
178
  }
156
179
  else {
157
- const joinTable = [objectTable, propertyTable].sort().join('_');
180
+ const joinTable = joinTableName(objectTable, propertyTable);
158
181
  query = 'SELECT ' + propertyTable + '_id id FROM `' + joinTable + '`'
159
182
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
160
183
  }
package/esm/mysql.d.ts CHANGED
@@ -19,6 +19,7 @@ interface Dependencies<QF extends object = object> {
19
19
  queryFunctionCall: (value: QF) => [any, string];
20
20
  storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
21
21
  }
22
+ export declare function joinTableName(object1: string | ObjectOrType, object2: string | ObjectOrType): string;
22
23
  export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
23
24
  export declare class Mysql extends DataSource {
24
25
  config: {
package/esm/mysql.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { isAnyFunction } from '@itrocks/class-type';
2
2
  import { isAnyType } from '@itrocks/class-type';
3
3
  import { typeOf } from '@itrocks/class-type';
4
+ import { CollectionType } from '@itrocks/property-type';
4
5
  import { ReflectClass } from '@itrocks/reflect';
5
6
  import { ReflectProperty } from '@itrocks/reflect';
6
7
  import { DataSource } from '@itrocks/storage';
@@ -17,6 +18,13 @@ const depends = {
17
18
  queryFunctionCall: () => [undefined, ' = ?'],
18
19
  storeOf: target => typeOf(target).name.toLowerCase()
19
20
  };
21
+ export function joinTableName(object1, object2) {
22
+ if (typeof object1 !== 'string')
23
+ object1 = depends.storeOf(object1);
24
+ if (typeof object2 !== 'string')
25
+ object2 = depends.storeOf(object2);
26
+ return [object1, object2].sort().join('_');
27
+ }
20
28
  export function mysqlDependsOn(dependencies) {
21
29
  Object.assign(depends, dependencies);
22
30
  }
@@ -48,7 +56,10 @@ export class Mysql extends DataSource {
48
56
  const connection = this.connection ?? await this.connect();
49
57
  const objectTable = depends.storeOf(object);
50
58
  const propertyTable = depends.storeOf(new ReflectProperty(object, property).collectionType.elementType.type);
51
- const joinTable = [objectTable, propertyTable].sort().join('_');
59
+ if (!objectTable || !propertyTable) {
60
+ throw 'Collection objects are not stored';
61
+ }
62
+ const joinTable = joinTableName(objectTable, propertyTable);
52
63
  const query = 'DELETE FROM `' + joinTable + '` WHERE ' + objectTable + '_id = ? AND ' + propertyTable + '_id = ?';
53
64
  const values = [object.id, id];
54
65
  if (DEBUG)
@@ -74,7 +85,10 @@ export class Mysql extends DataSource {
74
85
  const connection = this.connection ?? await this.connect();
75
86
  const objectTable = depends.storeOf(object);
76
87
  const propertyTable = depends.storeOf(new ReflectProperty(object, property).collectionType.elementType.type);
77
- const joinTable = [objectTable, propertyTable].sort().join('_');
88
+ if (!objectTable || !propertyTable) {
89
+ throw 'Collection objects are not stored';
90
+ }
91
+ const joinTable = joinTableName(objectTable, propertyTable);
78
92
  const query = 'INSERT INTO `' + joinTable + '` SET ' + objectTable + '_id = ?, ' + propertyTable + '_id = ?';
79
93
  const values = [object.id, id];
80
94
  if (DEBUG)
@@ -105,7 +119,9 @@ export class Mysql extends DataSource {
105
119
  const sql = ['id'];
106
120
  for (const property of new ReflectClass(type).properties) {
107
121
  const propertyType = property.type;
108
- const propertyName = (isAnyType(propertyType) && depends.storeOf(propertyType))
122
+ if (propertyType instanceof CollectionType)
123
+ continue;
124
+ const propertyName = (isAnyType(propertyType.type) && depends.storeOf(propertyType.type))
109
125
  ? property.name + 'Id'
110
126
  : property.name;
111
127
  const columnName = depends.columnOf(propertyName);
@@ -127,15 +143,18 @@ export class Mysql extends DataSource {
127
143
  const connection = this.connection ?? await this.connect();
128
144
  const propertiesSql = this.propertiesToSqlSelect(type);
129
145
  const objectTable = depends.storeOf(object);
130
- const table = depends.storeOf(type);
146
+ const propertyTable = depends.storeOf(type);
147
+ if (!objectTable || !propertyTable) {
148
+ throw 'Collection objects are not stored';
149
+ }
131
150
  let query;
132
151
  if (depends.componentOf(object, property)) {
133
- query = 'SELECT ' + propertiesSql + ' FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
152
+ query = 'SELECT ' + propertiesSql + ' FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
134
153
  }
135
154
  else {
136
- const joinTable = [objectTable, table].sort().join('_');
137
- query = 'SELECT `' + table + '`.' + propertiesSql + ' FROM `' + table + '`'
138
- + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + table + '_id = `' + table + '`.id'
155
+ const joinTable = joinTableName(objectTable, propertyTable);
156
+ query = 'SELECT `' + propertyTable + '`.' + propertiesSql + ' FROM `' + propertyTable + '`'
157
+ + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + propertyTable + '_id = `' + propertyTable + '`.id'
139
158
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
140
159
  }
141
160
  const rows = await connection.query(query, [object.id]);
@@ -145,12 +164,15 @@ export class Mysql extends DataSource {
145
164
  const connection = this.connection ?? await this.connect();
146
165
  const objectTable = depends.storeOf(object);
147
166
  const propertyTable = depends.storeOf(type);
167
+ if (!objectTable || !propertyTable) {
168
+ throw 'Collection objects are not stored';
169
+ }
148
170
  let query;
149
171
  if (depends.componentOf(object, property)) {
150
172
  query = 'SELECT id FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
151
173
  }
152
174
  else {
153
- const joinTable = [objectTable, propertyTable].sort().join('_');
175
+ const joinTable = joinTableName(objectTable, propertyTable);
154
176
  query = 'SELECT ' + propertyTable + '_id id FROM `' + joinTable + '`'
155
177
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
156
178
  }
package/package.json CHANGED
@@ -58,5 +58,5 @@
58
58
  "build:esm": "tsc -p tsconfig.esm.json"
59
59
  },
60
60
  "types": "./esm/mysql.d.ts",
61
- "version": "0.0.13"
61
+ "version": "0.0.15"
62
62
  }