@itrocks/mysql 0.0.8 → 0.0.10

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
@@ -1,8 +1,12 @@
1
1
  import { AnyObject } from '@itrocks/class-type';
2
- import { KeyOf, ObjectOrType, Type } from '@itrocks/class-type';
2
+ import { KeyOf } from '@itrocks/class-type';
3
+ import { ObjectOrType } from '@itrocks/class-type';
4
+ import { Type } from '@itrocks/class-type';
3
5
  import { DataSource } from '@itrocks/storage';
4
- import { Entity, MayEntity } from '@itrocks/storage';
5
- import { Identifier, SearchType } from '@itrocks/storage';
6
+ import { Entity } from '@itrocks/storage';
7
+ import { MayEntity } from '@itrocks/storage';
8
+ import { Identifier } from '@itrocks/storage';
9
+ import { SearchType } from '@itrocks/storage';
6
10
  import { Connection } from 'mariadb';
7
11
  export declare const DEBUG = false;
8
12
  interface Dependencies<QF extends object = object> {
@@ -16,8 +20,7 @@ interface Dependencies<QF extends object = object> {
16
20
  storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
17
21
  }
18
22
  export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
19
- export { Mysql };
20
- export default class Mysql extends DataSource {
23
+ export declare class Mysql extends DataSource {
21
24
  config: {
22
25
  host: string;
23
26
  user: string;
@@ -39,6 +42,7 @@ export default class Mysql extends DataSource {
39
42
  insertRelatedId<T extends Entity>(object: T, property: KeyOf<T>, id: Identifier): Promise<void>;
40
43
  propertiesToSearchSql(search: AnyObject): string;
41
44
  propertiesToSql(object: object): string;
45
+ propertiesToSqlSelect<T extends object>(type: Type<T>): string;
42
46
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
43
47
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Awaited<PT & {
44
48
  id: Identifier;
@@ -53,3 +57,4 @@ export default class Mysql extends DataSource {
53
57
  valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
54
58
  valuesToDb<T extends object>(object: T): Promise<[AnyObject, Function[]]>;
55
59
  }
60
+ export {};
package/cjs/mysql.js CHANGED
@@ -4,7 +4,9 @@ exports.Mysql = exports.DEBUG = void 0;
4
4
  exports.mysqlDependsOn = mysqlDependsOn;
5
5
  const class_type_1 = require("@itrocks/class-type");
6
6
  const class_type_2 = require("@itrocks/class-type");
7
+ const class_type_3 = require("@itrocks/class-type");
7
8
  const reflect_1 = require("@itrocks/reflect");
9
+ const reflect_2 = require("@itrocks/reflect");
8
10
  const storage_1 = require("@itrocks/storage");
9
11
  const mariadb_1 = require("mariadb");
10
12
  exports.DEBUG = false;
@@ -17,7 +19,7 @@ const depends = {
17
19
  QueryFunction: class {
18
20
  },
19
21
  queryFunctionCall: () => [undefined, ' = ?'],
20
- storeOf: target => (0, class_type_2.typeOf)(target).name.toLowerCase()
22
+ storeOf: target => (0, class_type_3.typeOf)(target).name.toLowerCase()
21
23
  };
22
24
  function mysqlDependsOn(dependencies) {
23
25
  Object.assign(depends, dependencies);
@@ -49,7 +51,7 @@ class Mysql extends storage_1.DataSource {
49
51
  async deleteRelatedId(object, property, id) {
50
52
  const connection = this.connection ?? await this.connect();
51
53
  const objectTable = depends.storeOf(object);
52
- const propertyTable = depends.storeOf(new reflect_1.ReflectProperty(object, property).collectionType.elementType);
54
+ const propertyTable = depends.storeOf(new reflect_2.ReflectProperty(object, property).collectionType.elementType);
53
55
  const joinTable = [objectTable, propertyTable].sort().join('_');
54
56
  const query = 'DELETE FROM `' + joinTable + '` WHERE ' + objectTable + '_id = ? AND ' + propertyTable + '_id = ?';
55
57
  const values = [object.id, id];
@@ -75,7 +77,7 @@ class Mysql extends storage_1.DataSource {
75
77
  async insertRelatedId(object, property, id) {
76
78
  const connection = this.connection ?? await this.connect();
77
79
  const objectTable = depends.storeOf(object);
78
- const propertyTable = depends.storeOf(new reflect_1.ReflectProperty(object, property).collectionType.elementType);
80
+ const propertyTable = depends.storeOf(new reflect_2.ReflectProperty(object, property).collectionType.elementType);
79
81
  const joinTable = [objectTable, propertyTable].sort().join('_');
80
82
  const query = 'INSERT INTO `' + joinTable + '` SET ' + objectTable + '_id = ?, ' + propertyTable + '_id = ?';
81
83
  const values = [object.id, id];
@@ -103,31 +105,47 @@ class Mysql extends storage_1.DataSource {
103
105
  propertiesToSql(object) {
104
106
  return Object.keys(object).map(name => '`' + depends.columnOf(name) + '` = ?').join(', ');
105
107
  }
108
+ propertiesToSqlSelect(type) {
109
+ const sql = ['id'];
110
+ for (const property of new reflect_1.ReflectClass(type).properties) {
111
+ const propertyType = property.type;
112
+ const propertyName = ((0, class_type_2.isAnyType)(propertyType) && depends.storeOf(propertyType))
113
+ ? property.name + 'Id'
114
+ : property.name;
115
+ const columnName = depends.columnOf(propertyName);
116
+ sql.push((columnName.length !== propertyName.length)
117
+ ? columnName + ' ' + propertyName
118
+ : propertyName);
119
+ }
120
+ return sql.join(', ');
121
+ }
106
122
  async read(type, id) {
107
123
  const connection = this.connection ?? await this.connect();
124
+ const propertiesSql = this.propertiesToSqlSelect(type);
108
125
  if (exports.DEBUG)
109
- console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
110
- const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
126
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
127
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
111
128
  return this.valuesFromDb(rows[0], type);
112
129
  }
113
- async readCollection(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType) {
130
+ async readCollection(object, property, type = new reflect_2.ReflectProperty(object, property).collectionType.elementType) {
114
131
  const connection = this.connection ?? await this.connect();
132
+ const propertiesSql = this.propertiesToSqlSelect(type);
115
133
  const objectTable = depends.storeOf(object);
116
134
  const table = depends.storeOf(type);
117
135
  let query;
118
136
  if (depends.componentOf(object, property)) {
119
- query = 'SELECT * FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
137
+ query = 'SELECT ' + propertiesSql + ' FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
120
138
  }
121
139
  else {
122
140
  const joinTable = [objectTable, table].sort().join('_');
123
- query = 'SELECT `' + table + '`.* FROM `' + table + '`'
141
+ query = 'SELECT `' + table + '`.' + propertiesSql + ' FROM `' + table + '`'
124
142
  + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + table + '_id = `' + table + '`.id'
125
143
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
126
144
  }
127
145
  const rows = await connection.query(query, [object.id]);
128
146
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
129
147
  }
130
- async readCollectionIds(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType) {
148
+ async readCollectionIds(object, property, type = new reflect_2.ReflectProperty(object, property).collectionType.elementType) {
131
149
  const connection = this.connection ?? await this.connect();
132
150
  const objectTable = depends.storeOf(object);
133
151
  const propertyTable = depends.storeOf(type);
@@ -147,10 +165,11 @@ class Mysql extends storage_1.DataSource {
147
165
  if (!ids.length)
148
166
  return [];
149
167
  const connection = this.connection ?? await this.connect();
168
+ const propertiesSql = this.propertiesToSqlSelect(type);
150
169
  const questionMarks = Array(ids.length).fill('?').join(', ');
151
170
  if (exports.DEBUG)
152
- console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
153
- const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
171
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
172
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
154
173
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
155
174
  }
156
175
  async save(object) {
@@ -160,12 +179,13 @@ class Mysql extends storage_1.DataSource {
160
179
  }
161
180
  async search(type, search = {}) {
162
181
  const connection = this.connection ?? await this.connect();
182
+ const propertiesSql = this.propertiesToSqlSelect(type);
163
183
  Object.setPrototypeOf(search, type.prototype);
164
184
  const sql = this.propertiesToSearchSql(search);
165
185
  const [values] = await this.valuesToDb(search);
166
186
  if (exports.DEBUG)
167
- console.log('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
168
- const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
187
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
188
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
169
189
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
170
190
  }
171
191
  async update(object) {
@@ -189,8 +209,8 @@ class Mysql extends storage_1.DataSource {
189
209
  if (value === depends.ignoreTransformedValue)
190
210
  continue;
191
211
  object[property] = value;
192
- if (property.endsWith('_id')) {
193
- delete object[property.slice(0, -3)];
212
+ if (property.endsWith('Id')) {
213
+ delete object[property.slice(0, -2)];
194
214
  }
195
215
  }
196
216
  return object;
@@ -206,11 +226,10 @@ class Mysql extends storage_1.DataSource {
206
226
  deferred.push(value);
207
227
  continue;
208
228
  }
209
- record[property] = value;
229
+ record[depends.columnOf(property)] = value;
210
230
  }
211
231
  return [record, deferred];
212
232
  }
213
233
  }
214
- exports.default = Mysql;
215
234
  exports.Mysql = Mysql;
216
235
  //# sourceMappingURL=mysql.js.map
package/esm/mysql.d.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import { AnyObject } from '@itrocks/class-type';
2
- import { KeyOf, ObjectOrType, Type } from '@itrocks/class-type';
2
+ import { KeyOf } from '@itrocks/class-type';
3
+ import { ObjectOrType } from '@itrocks/class-type';
4
+ import { Type } from '@itrocks/class-type';
3
5
  import { DataSource } from '@itrocks/storage';
4
- import { Entity, MayEntity } from '@itrocks/storage';
5
- import { Identifier, SearchType } from '@itrocks/storage';
6
+ import { Entity } from '@itrocks/storage';
7
+ import { MayEntity } from '@itrocks/storage';
8
+ import { Identifier } from '@itrocks/storage';
9
+ import { SearchType } from '@itrocks/storage';
6
10
  import { Connection } from 'mariadb';
7
11
  export declare const DEBUG = false;
8
12
  interface Dependencies<QF extends object = object> {
@@ -16,8 +20,7 @@ interface Dependencies<QF extends object = object> {
16
20
  storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
17
21
  }
18
22
  export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
19
- export { Mysql };
20
- export default class Mysql extends DataSource {
23
+ export declare class Mysql extends DataSource {
21
24
  config: {
22
25
  host: string;
23
26
  user: string;
@@ -39,6 +42,7 @@ export default class Mysql extends DataSource {
39
42
  insertRelatedId<T extends Entity>(object: T, property: KeyOf<T>, id: Identifier): Promise<void>;
40
43
  propertiesToSearchSql(search: AnyObject): string;
41
44
  propertiesToSql(object: object): string;
45
+ propertiesToSqlSelect<T extends object>(type: Type<T>): string;
42
46
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
43
47
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Awaited<PT & {
44
48
  id: Identifier;
@@ -53,3 +57,4 @@ export default class Mysql extends DataSource {
53
57
  valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
54
58
  valuesToDb<T extends object>(object: T): Promise<[AnyObject, Function[]]>;
55
59
  }
60
+ export {};
package/esm/mysql.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { isAnyFunction } from '@itrocks/class-type';
2
+ import { isAnyType } from '@itrocks/class-type';
2
3
  import { typeOf } from '@itrocks/class-type';
4
+ import { ReflectClass } from '@itrocks/reflect';
3
5
  import { ReflectProperty } from '@itrocks/reflect';
4
6
  import { DataSource } from '@itrocks/storage';
5
7
  import { createConnection } from 'mariadb';
@@ -18,8 +20,7 @@ const depends = {
18
20
  export function mysqlDependsOn(dependencies) {
19
21
  Object.assign(depends, dependencies);
20
22
  }
21
- export { Mysql };
22
- export default class Mysql extends DataSource {
23
+ export class Mysql extends DataSource {
23
24
  config;
24
25
  connection;
25
26
  constructor(config) {
@@ -100,24 +101,40 @@ export default class Mysql extends DataSource {
100
101
  propertiesToSql(object) {
101
102
  return Object.keys(object).map(name => '`' + depends.columnOf(name) + '` = ?').join(', ');
102
103
  }
104
+ propertiesToSqlSelect(type) {
105
+ const sql = ['id'];
106
+ for (const property of new ReflectClass(type).properties) {
107
+ const propertyType = property.type;
108
+ const propertyName = (isAnyType(propertyType) && depends.storeOf(propertyType))
109
+ ? property.name + 'Id'
110
+ : property.name;
111
+ const columnName = depends.columnOf(propertyName);
112
+ sql.push((columnName.length !== propertyName.length)
113
+ ? columnName + ' ' + propertyName
114
+ : propertyName);
115
+ }
116
+ return sql.join(', ');
117
+ }
103
118
  async read(type, id) {
104
119
  const connection = this.connection ?? await this.connect();
120
+ const propertiesSql = this.propertiesToSqlSelect(type);
105
121
  if (DEBUG)
106
- console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
107
- const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
122
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
123
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
108
124
  return this.valuesFromDb(rows[0], type);
109
125
  }
110
126
  async readCollection(object, property, type = new ReflectProperty(object, property).collectionType.elementType) {
111
127
  const connection = this.connection ?? await this.connect();
128
+ const propertiesSql = this.propertiesToSqlSelect(type);
112
129
  const objectTable = depends.storeOf(object);
113
130
  const table = depends.storeOf(type);
114
131
  let query;
115
132
  if (depends.componentOf(object, property)) {
116
- query = 'SELECT * FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
133
+ query = 'SELECT ' + propertiesSql + ' FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
117
134
  }
118
135
  else {
119
136
  const joinTable = [objectTable, table].sort().join('_');
120
- query = 'SELECT `' + table + '`.* FROM `' + table + '`'
137
+ query = 'SELECT `' + table + '`.' + propertiesSql + ' FROM `' + table + '`'
121
138
  + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + table + '_id = `' + table + '`.id'
122
139
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
123
140
  }
@@ -144,10 +161,11 @@ export default class Mysql extends DataSource {
144
161
  if (!ids.length)
145
162
  return [];
146
163
  const connection = this.connection ?? await this.connect();
164
+ const propertiesSql = this.propertiesToSqlSelect(type);
147
165
  const questionMarks = Array(ids.length).fill('?').join(', ');
148
166
  if (DEBUG)
149
- console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
150
- const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
167
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
168
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
151
169
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
152
170
  }
153
171
  async save(object) {
@@ -157,12 +175,13 @@ export default class Mysql extends DataSource {
157
175
  }
158
176
  async search(type, search = {}) {
159
177
  const connection = this.connection ?? await this.connect();
178
+ const propertiesSql = this.propertiesToSqlSelect(type);
160
179
  Object.setPrototypeOf(search, type.prototype);
161
180
  const sql = this.propertiesToSearchSql(search);
162
181
  const [values] = await this.valuesToDb(search);
163
182
  if (DEBUG)
164
- console.log('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
165
- const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
183
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
184
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
166
185
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
167
186
  }
168
187
  async update(object) {
@@ -186,8 +205,8 @@ export default class Mysql extends DataSource {
186
205
  if (value === depends.ignoreTransformedValue)
187
206
  continue;
188
207
  object[property] = value;
189
- if (property.endsWith('_id')) {
190
- delete object[property.slice(0, -3)];
208
+ if (property.endsWith('Id')) {
209
+ delete object[property.slice(0, -2)];
191
210
  }
192
211
  }
193
212
  return object;
@@ -203,7 +222,7 @@ export default class Mysql extends DataSource {
203
222
  deferred.push(value);
204
223
  continue;
205
224
  }
206
- record[property] = value;
225
+ record[depends.columnOf(property)] = value;
207
226
  }
208
227
  return [record, deferred];
209
228
  }
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.8"
61
+ "version": "0.0.10"
62
62
  }