@itrocks/mysql 0.0.24 → 0.1.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.
package/cjs/mysql.d.ts CHANGED
@@ -47,6 +47,9 @@ export declare class Mysql extends DataSource {
47
47
  propertiesToSearchSql(search: AnyObject): string;
48
48
  propertiesToSql(object: object): string;
49
49
  propertiesToSqlSelect<T extends object>(type: Type<T>): string;
50
+ query<T extends object>(type: Type<T>, query: string, values?: any): Promise<Awaited<T & {
51
+ id: Identifier;
52
+ }>[]>;
50
53
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
51
54
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Awaited<PT & {
52
55
  id: Identifier;
package/cjs/mysql.js CHANGED
@@ -60,7 +60,7 @@ class Mysql extends storage_1.DataSource {
60
60
  const sql = this.propertiesToSearchSql(search);
61
61
  const [values] = await this.valuesToDb(search);
62
62
  if (exports.DEBUG)
63
- console.log('SELECT COUNT(*) FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
63
+ console.log('SELECT COUNT(*) FROM `' + depends.storeOf(type) + '`' + sql, JSON.stringify(values));
64
64
  const row = (await connection.query('SELECT COUNT(*) `count` FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values)))[0];
65
65
  return row?.count;
66
66
  }
@@ -122,11 +122,15 @@ class Mysql extends storage_1.DataSource {
122
122
  .map(([name, value]) => {
123
123
  let sql;
124
124
  if (value instanceof depends.QueryFunction) {
125
- [search[name], sql] = depends.queryFunctionCall(value);
125
+ [value, sql] = depends.queryFunctionCall(value);
126
+ search[name] = value;
126
127
  }
127
128
  else {
128
129
  sql = ' = ?';
129
130
  }
131
+ if ((typeof value)[0] === 'o') {
132
+ name = Array.isArray(value) ? 'id' : (name + '_id');
133
+ }
130
134
  return '`' + depends.columnOf(name) + '`' + sql;
131
135
  })
132
136
  .join(' AND ');
@@ -153,6 +157,11 @@ class Mysql extends storage_1.DataSource {
153
157
  }
154
158
  return sql.join(', ');
155
159
  }
160
+ async query(type, query, values) {
161
+ const connection = this.connection ?? await this.connect();
162
+ const rows = await connection.query(query, values);
163
+ return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
164
+ }
156
165
  async read(type, id) {
157
166
  const connection = this.connection ?? await this.connect();
158
167
  const propertiesSql = this.propertiesToSqlSelect(type);
@@ -325,7 +334,7 @@ class Mysql extends storage_1.DataSource {
325
334
  const sql = this.propertiesToSearchSql(search);
326
335
  const [values] = await this.valuesToDb(search);
327
336
  if (exports.DEBUG)
328
- console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
337
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, JSON.stringify(values));
329
338
  const limit = limitOption?.limit ? ' LIMIT ' + limitOption.limit : '';
330
339
  const offset = limitOption?.offset ? ' OFFSET ' + limitOption.offset : '';
331
340
  const sort = sortOption?.properties.length
package/esm/mysql.d.ts CHANGED
@@ -47,6 +47,9 @@ export declare class Mysql extends DataSource {
47
47
  propertiesToSearchSql(search: AnyObject): string;
48
48
  propertiesToSql(object: object): string;
49
49
  propertiesToSqlSelect<T extends object>(type: Type<T>): string;
50
+ query<T extends object>(type: Type<T>, query: string, values?: any): Promise<Awaited<T & {
51
+ id: Identifier;
52
+ }>[]>;
50
53
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
51
54
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Awaited<PT & {
52
55
  id: Identifier;
package/esm/mysql.js CHANGED
@@ -55,7 +55,7 @@ export class Mysql extends DataSource {
55
55
  const sql = this.propertiesToSearchSql(search);
56
56
  const [values] = await this.valuesToDb(search);
57
57
  if (DEBUG)
58
- console.log('SELECT COUNT(*) FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
58
+ console.log('SELECT COUNT(*) FROM `' + depends.storeOf(type) + '`' + sql, JSON.stringify(values));
59
59
  const row = (await connection.query('SELECT COUNT(*) `count` FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values)))[0];
60
60
  return row?.count;
61
61
  }
@@ -117,11 +117,15 @@ export class Mysql extends DataSource {
117
117
  .map(([name, value]) => {
118
118
  let sql;
119
119
  if (value instanceof depends.QueryFunction) {
120
- [search[name], sql] = depends.queryFunctionCall(value);
120
+ [value, sql] = depends.queryFunctionCall(value);
121
+ search[name] = value;
121
122
  }
122
123
  else {
123
124
  sql = ' = ?';
124
125
  }
126
+ if ((typeof value)[0] === 'o') {
127
+ name = Array.isArray(value) ? 'id' : (name + '_id');
128
+ }
125
129
  return '`' + depends.columnOf(name) + '`' + sql;
126
130
  })
127
131
  .join(' AND ');
@@ -148,6 +152,11 @@ export class Mysql extends DataSource {
148
152
  }
149
153
  return sql.join(', ');
150
154
  }
155
+ async query(type, query, values) {
156
+ const connection = this.connection ?? await this.connect();
157
+ const rows = await connection.query(query, values);
158
+ return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
159
+ }
151
160
  async read(type, id) {
152
161
  const connection = this.connection ?? await this.connect();
153
162
  const propertiesSql = this.propertiesToSqlSelect(type);
@@ -320,7 +329,7 @@ export class Mysql extends DataSource {
320
329
  const sql = this.propertiesToSearchSql(search);
321
330
  const [values] = await this.valuesToDb(search);
322
331
  if (DEBUG)
323
- console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
332
+ console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, JSON.stringify(values));
324
333
  const limit = limitOption?.limit ? ' LIMIT ' + limitOption.limit : '';
325
334
  const offset = limitOption?.offset ? ' OFFSET ' + limitOption.offset : '';
326
335
  const sort = sortOption?.properties.length
package/package.json CHANGED
@@ -59,5 +59,5 @@
59
59
  "build:esm": "tsc -p tsconfig.esm.json"
60
60
  },
61
61
  "types": "./esm/mysql.d.ts",
62
- "version": "0.0.24"
62
+ "version": "0.1.0"
63
63
  }