@itrocks/mysql 0.0.16 → 0.0.18

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 = {
@@ -214,39 +217,48 @@ class Mysql extends storage_1.DataSource {
214
217
  const objectTable = depends.storeOf(object);
215
218
  const propertyType = new reflect_2.ReflectProperty(object, property).collectionType.elementType.type;
216
219
  const propertyTable = depends.storeOf(propertyType);
217
- const linkColumn = depends.columnOf(propertyTable + 'Id');
220
+ const linkColumn = depends.columnOf(propertyTable) + '_id';
218
221
  const linkTable = joinTableName(objectTable, propertyTable);
219
- const objectColumn = depends.columnOf(objectTable + 'Id');
222
+ const objectColumn = depends.columnOf(objectTable) + '_id';
220
223
  const objectId = object.id;
221
224
  const stored = await this.readCollectionIds(object, property, propertyType);
222
225
  const saved = [];
223
- const isLinkBigInt = typeof links[0] === 'bigint';
224
- const isStoredBigInt = typeof stored[0] === 'bigint';
225
- const isSameType = isLinkBigInt === isStoredBigInt;
226
- for (let link of links) {
226
+ for (const link of links) {
227
227
  const linkId = (typeof link === 'object')
228
- ? (link = this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
228
+ ? (this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
229
229
  : link;
230
- if (!stored.includes(isSameType ? linkId : (isStoredBigInt ? BigInt(linkId) : Number(linkId)))) {
231
- await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
232
- }
233
230
  saved.push(linkId);
231
+ if (stored.includes(linkId))
232
+ continue;
233
+ await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
234
+ stored.push(linkId);
234
235
  }
235
- for (let storedId of stored) {
236
- if (!saved.includes(isSameType ? storedId : (isLinkBigInt ? BigInt(storedId) : Number(storedId)))) {
237
- await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
238
- }
236
+ for (const storedId of stored) {
237
+ if (saved.includes(storedId))
238
+ continue;
239
+ await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
239
240
  }
240
241
  }
241
- async search(type, search = {}) {
242
+ async search(type, search = {}, options) {
242
243
  const connection = this.connection ?? await this.connect();
243
244
  const propertiesSql = this.propertiesToSqlSelect(type);
245
+ let sortOption = undefined;
246
+ for (const option of this.options(options)) {
247
+ if ((option instanceof storage_2.Sort) && (option.properties.length)) {
248
+ sortOption = option;
249
+ }
250
+ }
251
+ if (!sortOption) {
252
+ sortOption = new storage_2.Sort((0, sort_2.sortOf)(type));
253
+ }
244
254
  Object.setPrototypeOf(search, type.prototype);
245
255
  const sql = this.propertiesToSearchSql(search);
246
256
  const [values] = await this.valuesToDb(search);
247
257
  if (exports.DEBUG)
248
258
  console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
249
- const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
259
+ const sort = ' ORDER BY '
260
+ + sortOption.properties.map(property => '`' + property + '`' + (property instanceof sort_1.Reverse ? ' DESC' : '')).join(', ');
261
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
250
262
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
251
263
  }
252
264
  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 = {
@@ -209,39 +212,48 @@ export class Mysql extends DataSource {
209
212
  const objectTable = depends.storeOf(object);
210
213
  const propertyType = new ReflectProperty(object, property).collectionType.elementType.type;
211
214
  const propertyTable = depends.storeOf(propertyType);
212
- const linkColumn = depends.columnOf(propertyTable + 'Id');
215
+ const linkColumn = depends.columnOf(propertyTable) + '_id';
213
216
  const linkTable = joinTableName(objectTable, propertyTable);
214
- const objectColumn = depends.columnOf(objectTable + 'Id');
217
+ const objectColumn = depends.columnOf(objectTable) + '_id';
215
218
  const objectId = object.id;
216
219
  const stored = await this.readCollectionIds(object, property, propertyType);
217
220
  const saved = [];
218
- const isLinkBigInt = typeof links[0] === 'bigint';
219
- const isStoredBigInt = typeof stored[0] === 'bigint';
220
- const isSameType = isLinkBigInt === isStoredBigInt;
221
- for (let link of links) {
221
+ for (const link of links) {
222
222
  const linkId = (typeof link === 'object')
223
- ? (link = this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
223
+ ? (this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
224
224
  : link;
225
- if (!stored.includes(isSameType ? linkId : (isStoredBigInt ? BigInt(linkId) : Number(linkId)))) {
226
- await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
227
- }
228
225
  saved.push(linkId);
226
+ if (stored.includes(linkId))
227
+ continue;
228
+ await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
229
+ stored.push(linkId);
229
230
  }
230
- for (let storedId of stored) {
231
- if (!saved.includes(isSameType ? storedId : (isLinkBigInt ? BigInt(storedId) : Number(storedId)))) {
232
- await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
233
- }
231
+ for (const storedId of stored) {
232
+ if (saved.includes(storedId))
233
+ continue;
234
+ await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
234
235
  }
235
236
  }
236
- async search(type, search = {}) {
237
+ async search(type, search = {}, options) {
237
238
  const connection = this.connection ?? await this.connect();
238
239
  const propertiesSql = this.propertiesToSqlSelect(type);
240
+ let sortOption = undefined;
241
+ for (const option of this.options(options)) {
242
+ if ((option instanceof Sort) && (option.properties.length)) {
243
+ sortOption = option;
244
+ }
245
+ }
246
+ if (!sortOption) {
247
+ sortOption = new Sort(sortOf(type));
248
+ }
239
249
  Object.setPrototypeOf(search, type.prototype);
240
250
  const sql = this.propertiesToSearchSql(search);
241
251
  const [values] = await this.valuesToDb(search);
242
252
  if (DEBUG)
243
253
  console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
244
- const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
254
+ const sort = ' ORDER BY '
255
+ + sortOption.properties.map(property => '`' + property + '`' + (property instanceof Reverse ? ' DESC' : '')).join(', ');
256
+ const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
245
257
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
246
258
  }
247
259
  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.16"
62
+ "version": "0.0.18"
62
63
  }