@itrocks/mysql 0.0.17 → 0.0.19

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
@@ -6,6 +6,7 @@ import { DataSource } from '@itrocks/storage';
6
6
  import { Entity } from '@itrocks/storage';
7
7
  import { MayEntity } from '@itrocks/storage';
8
8
  import { Identifier } from '@itrocks/storage';
9
+ import { Options } from '@itrocks/storage';
9
10
  import { SearchType } from '@itrocks/storage';
10
11
  import { Connection } from 'mariadb';
11
12
  export declare const DEBUG = false;
@@ -54,7 +55,7 @@ export declare class Mysql extends DataSource {
54
55
  saveCollection<T extends object>(object: Entity<T>, property: KeyOf<T>, value: (Identifier | MayEntity)[]): Promise<void>;
55
56
  saveComponents<T extends object>(object: Entity<T>, property: KeyOf<T>, components: (Identifier | MayEntity)[]): Promise<void>;
56
57
  saveLinks<T extends object>(object: Entity<T>, property: KeyOf<T>, links: (Identifier | MayEntity)[]): Promise<void>;
57
- search<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<Entity<T>[]>;
58
+ search<T extends object>(type: Type<T>, search?: SearchType<T>, options?: Options): Promise<Entity<T>[]>;
58
59
  update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
59
60
  valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
60
61
  valuesToDb<T extends object>(object: T): Promise<[AnyObject, Function[]]>;
package/cjs/mysql.js CHANGED
@@ -9,7 +9,10 @@ const class_type_3 = require("@itrocks/class-type");
9
9
  const property_type_1 = require("@itrocks/property-type");
10
10
  const reflect_1 = require("@itrocks/reflect");
11
11
  const reflect_2 = require("@itrocks/reflect");
12
+ const sort_1 = require("@itrocks/sort");
13
+ const sort_2 = require("@itrocks/sort");
12
14
  const storage_1 = require("@itrocks/storage");
15
+ const storage_2 = require("@itrocks/storage");
13
16
  const mariadb_1 = require("mariadb");
14
17
  exports.DEBUG = false;
15
18
  const depends = {
@@ -236,15 +239,30 @@ class Mysql extends storage_1.DataSource {
236
239
  await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
237
240
  }
238
241
  }
239
- async search(type, search = {}) {
242
+ async search(type, search = {}, options) {
240
243
  const connection = this.connection ?? await this.connect();
241
244
  const propertiesSql = this.propertiesToSqlSelect(type);
245
+ let sortOption = undefined;
246
+ for (const option of this.options(options)) {
247
+ if (option === storage_2.Sort) {
248
+ sortOption = new storage_2.Sort((0, sort_2.sortOf)(type));
249
+ }
250
+ if (option instanceof storage_2.Sort) {
251
+ sortOption = option.properties.length ? option : new storage_2.Sort((0, sort_2.sortOf)(type));
252
+ }
253
+ }
242
254
  Object.setPrototypeOf(search, type.prototype);
243
255
  const sql = this.propertiesToSearchSql(search);
244
256
  const [values] = await this.valuesToDb(search);
245
257
  if (exports.DEBUG)
246
258
  console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
247
- const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
259
+ const sort = sortOption?.properties.length
260
+ ? ' ORDER BY '
261
+ + sortOption.properties
262
+ .map(property => '`' + property + '`' + (property instanceof sort_1.Reverse ? ' DESC' : ''))
263
+ .join(', ')
264
+ : '';
265
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
248
266
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
249
267
  }
250
268
  async update(object) {
package/esm/mysql.d.ts CHANGED
@@ -6,6 +6,7 @@ import { DataSource } from '@itrocks/storage';
6
6
  import { Entity } from '@itrocks/storage';
7
7
  import { MayEntity } from '@itrocks/storage';
8
8
  import { Identifier } from '@itrocks/storage';
9
+ import { Options } from '@itrocks/storage';
9
10
  import { SearchType } from '@itrocks/storage';
10
11
  import { Connection } from 'mariadb';
11
12
  export declare const DEBUG = false;
@@ -54,7 +55,7 @@ export declare class Mysql extends DataSource {
54
55
  saveCollection<T extends object>(object: Entity<T>, property: KeyOf<T>, value: (Identifier | MayEntity)[]): Promise<void>;
55
56
  saveComponents<T extends object>(object: Entity<T>, property: KeyOf<T>, components: (Identifier | MayEntity)[]): Promise<void>;
56
57
  saveLinks<T extends object>(object: Entity<T>, property: KeyOf<T>, links: (Identifier | MayEntity)[]): Promise<void>;
57
- search<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<Entity<T>[]>;
58
+ search<T extends object>(type: Type<T>, search?: SearchType<T>, options?: Options): Promise<Entity<T>[]>;
58
59
  update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
59
60
  valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
60
61
  valuesToDb<T extends object>(object: T): Promise<[AnyObject, Function[]]>;
package/esm/mysql.js CHANGED
@@ -4,7 +4,10 @@ import { typeOf } from '@itrocks/class-type';
4
4
  import { CollectionType } from '@itrocks/property-type';
5
5
  import { ReflectClass } from '@itrocks/reflect';
6
6
  import { ReflectProperty } from '@itrocks/reflect';
7
+ import { Reverse } from '@itrocks/sort';
8
+ import { sortOf } from '@itrocks/sort';
7
9
  import { DataSource } from '@itrocks/storage';
10
+ import { Sort } from '@itrocks/storage';
8
11
  import { createConnection } from 'mariadb';
9
12
  export const DEBUG = false;
10
13
  const depends = {
@@ -231,15 +234,30 @@ export class Mysql extends DataSource {
231
234
  await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
232
235
  }
233
236
  }
234
- async search(type, search = {}) {
237
+ async search(type, search = {}, options) {
235
238
  const connection = this.connection ?? await this.connect();
236
239
  const propertiesSql = this.propertiesToSqlSelect(type);
240
+ let sortOption = undefined;
241
+ for (const option of this.options(options)) {
242
+ if (option === Sort) {
243
+ sortOption = new Sort(sortOf(type));
244
+ }
245
+ if (option instanceof Sort) {
246
+ sortOption = option.properties.length ? option : new Sort(sortOf(type));
247
+ }
248
+ }
237
249
  Object.setPrototypeOf(search, type.prototype);
238
250
  const sql = this.propertiesToSearchSql(search);
239
251
  const [values] = await this.valuesToDb(search);
240
252
  if (DEBUG)
241
253
  console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
242
- const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
254
+ const sort = sortOption?.properties.length
255
+ ? ' ORDER BY '
256
+ + sortOption.properties
257
+ .map(property => '`' + property + '`' + (property instanceof Reverse ? ' DESC' : ''))
258
+ .join(', ')
259
+ : '';
260
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
243
261
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
244
262
  }
245
263
  async update(object) {
package/package.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "dependencies": {
7
7
  "@itrocks/class-type": "latest",
8
8
  "@itrocks/reflect": "latest",
9
+ "@itrocks/sort": "latest",
9
10
  "@itrocks/storage": "latest",
10
11
  "mariadb": "^3.4",
11
12
  "typescript": "~5.8"
@@ -58,5 +59,5 @@
58
59
  "build:esm": "tsc -p tsconfig.esm.json"
59
60
  },
60
61
  "types": "./esm/mysql.d.ts",
61
- "version": "0.0.17"
62
+ "version": "0.0.19"
62
63
  }