@itrocks/mysql 0.0.9 → 0.0.11

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> {
@@ -38,6 +42,7 @@ export declare class Mysql extends DataSource {
38
42
  insertRelatedId<T extends Entity>(object: T, property: KeyOf<T>, id: Identifier): Promise<void>;
39
43
  propertiesToSearchSql(search: AnyObject): string;
40
44
  propertiesToSql(object: object): string;
45
+ propertiesToSqlSelect<T extends object>(type: Type<T>): string;
41
46
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
42
47
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Awaited<PT & {
43
48
  id: Identifier;
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,7 +226,7 @@ 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
  }
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> {
@@ -38,6 +42,7 @@ export declare class Mysql extends DataSource {
38
42
  insertRelatedId<T extends Entity>(object: T, property: KeyOf<T>, id: Identifier): Promise<void>;
39
43
  propertiesToSearchSql(search: AnyObject): string;
40
44
  propertiesToSql(object: object): string;
45
+ propertiesToSqlSelect<T extends object>(type: Type<T>): string;
41
46
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
42
47
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Awaited<PT & {
43
48
  id: Identifier;
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';
@@ -99,24 +101,40 @@ export class Mysql extends DataSource {
99
101
  propertiesToSql(object) {
100
102
  return Object.keys(object).map(name => '`' + depends.columnOf(name) + '` = ?').join(', ');
101
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
+ }
102
118
  async read(type, id) {
103
119
  const connection = this.connection ?? await this.connect();
120
+ const propertiesSql = this.propertiesToSqlSelect(type);
104
121
  if (DEBUG)
105
- console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
106
- 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]);
107
124
  return this.valuesFromDb(rows[0], type);
108
125
  }
109
126
  async readCollection(object, property, type = new ReflectProperty(object, property).collectionType.elementType) {
110
127
  const connection = this.connection ?? await this.connect();
128
+ const propertiesSql = this.propertiesToSqlSelect(type);
111
129
  const objectTable = depends.storeOf(object);
112
130
  const table = depends.storeOf(type);
113
131
  let query;
114
132
  if (depends.componentOf(object, property)) {
115
- query = 'SELECT * FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
133
+ query = 'SELECT ' + propertiesSql + ' FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
116
134
  }
117
135
  else {
118
136
  const joinTable = [objectTable, table].sort().join('_');
119
- query = 'SELECT `' + table + '`.* FROM `' + table + '`'
137
+ query = 'SELECT `' + table + '`.' + propertiesSql + ' FROM `' + table + '`'
120
138
  + ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + table + '_id = `' + table + '`.id'
121
139
  + ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
122
140
  }
@@ -143,10 +161,11 @@ export class Mysql extends DataSource {
143
161
  if (!ids.length)
144
162
  return [];
145
163
  const connection = this.connection ?? await this.connect();
164
+ const propertiesSql = this.propertiesToSqlSelect(type);
146
165
  const questionMarks = Array(ids.length).fill('?').join(', ');
147
166
  if (DEBUG)
148
- console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
149
- 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);
150
169
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
151
170
  }
152
171
  async save(object) {
@@ -156,12 +175,13 @@ export class Mysql extends DataSource {
156
175
  }
157
176
  async search(type, search = {}) {
158
177
  const connection = this.connection ?? await this.connect();
178
+ const propertiesSql = this.propertiesToSqlSelect(type);
159
179
  Object.setPrototypeOf(search, type.prototype);
160
180
  const sql = this.propertiesToSearchSql(search);
161
181
  const [values] = await this.valuesToDb(search);
162
182
  if (DEBUG)
163
- console.log('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
164
- 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));
165
185
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
166
186
  }
167
187
  async update(object) {
@@ -185,8 +205,8 @@ export class Mysql extends DataSource {
185
205
  if (value === depends.ignoreTransformedValue)
186
206
  continue;
187
207
  object[property] = value;
188
- if (property.endsWith('_id')) {
189
- delete object[property.slice(0, -3)];
208
+ if (property.endsWith('Id')) {
209
+ delete object[property.slice(0, -2)];
190
210
  }
191
211
  }
192
212
  return object;
@@ -202,7 +222,7 @@ export class Mysql extends DataSource {
202
222
  deferred.push(value);
203
223
  continue;
204
224
  }
205
- record[property] = value;
225
+ record[depends.columnOf(property)] = value;
206
226
  }
207
227
  return [record, deferred];
208
228
  }
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "@itrocks/reflect": "latest",
9
9
  "@itrocks/storage": "latest",
10
10
  "mariadb": "^3.4",
11
- "typescript": "~5.6"
11
+ "typescript": "~5.8"
12
12
  },
13
13
  "description": "Transforms model objects to and from MySQL database records",
14
14
  "devDependencies": {
@@ -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.9"
61
+ "version": "0.0.11"
62
62
  }