@itrocks/mysql 0.0.22 → 0.0.23
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.js +12 -5
- package/esm/mysql.js +8 -1
- package/package.json +1 -1
package/cjs/mysql.js
CHANGED
|
@@ -15,6 +15,7 @@ const sort_1 = require("@itrocks/sort");
|
|
|
15
15
|
const sort_2 = require("@itrocks/sort");
|
|
16
16
|
const storage_1 = require("@itrocks/storage");
|
|
17
17
|
const storage_2 = require("@itrocks/storage");
|
|
18
|
+
const storage_3 = require("@itrocks/storage");
|
|
18
19
|
const mariadb_1 = require("mariadb");
|
|
19
20
|
exports.DEBUG = false;
|
|
20
21
|
const depends = {
|
|
@@ -297,13 +298,17 @@ class Mysql extends storage_1.DataSource {
|
|
|
297
298
|
async search(type, search = {}, options) {
|
|
298
299
|
const connection = this.connection ?? await this.connect();
|
|
299
300
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
301
|
+
let limitOption = undefined;
|
|
300
302
|
let sortOption = undefined;
|
|
301
303
|
for (const option of this.options(options)) {
|
|
302
|
-
if (option ===
|
|
303
|
-
sortOption = new
|
|
304
|
+
if (option === storage_3.Sort) {
|
|
305
|
+
sortOption = new storage_3.Sort((0, sort_2.sortOf)(type));
|
|
304
306
|
}
|
|
305
|
-
if (option instanceof storage_2.
|
|
306
|
-
|
|
307
|
+
if (option instanceof storage_2.Limit) {
|
|
308
|
+
limitOption = option;
|
|
309
|
+
}
|
|
310
|
+
if (option instanceof storage_3.Sort) {
|
|
311
|
+
sortOption = option.properties.length ? option : new storage_3.Sort((0, sort_2.sortOf)(type));
|
|
307
312
|
}
|
|
308
313
|
}
|
|
309
314
|
Object.setPrototypeOf(search, type.prototype);
|
|
@@ -311,13 +316,15 @@ class Mysql extends storage_1.DataSource {
|
|
|
311
316
|
const [values] = await this.valuesToDb(search);
|
|
312
317
|
if (exports.DEBUG)
|
|
313
318
|
console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
319
|
+
const limit = limitOption?.limit ? ' LIMIT ' + limitOption.limit : '';
|
|
320
|
+
const offset = limitOption?.offset ? ' OFFSET ' + limitOption.offset : '';
|
|
314
321
|
const sort = sortOption?.properties.length
|
|
315
322
|
? ' ORDER BY '
|
|
316
323
|
+ sortOption.properties
|
|
317
324
|
.map(property => '`' + property + '`' + (property instanceof sort_1.Reverse ? ' DESC' : ''))
|
|
318
325
|
.join(', ')
|
|
319
326
|
: '';
|
|
320
|
-
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
|
|
327
|
+
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort + limit + offset, Object.values(values));
|
|
321
328
|
return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
|
|
322
329
|
}
|
|
323
330
|
async update(object) {
|
package/esm/mysql.js
CHANGED
|
@@ -9,6 +9,7 @@ import { ReflectProperty } from '@itrocks/reflect';
|
|
|
9
9
|
import { Reverse } from '@itrocks/sort';
|
|
10
10
|
import { sortOf } from '@itrocks/sort';
|
|
11
11
|
import { DataSource } from '@itrocks/storage';
|
|
12
|
+
import { Limit } from '@itrocks/storage';
|
|
12
13
|
import { Sort } from '@itrocks/storage';
|
|
13
14
|
import { createConnection } from 'mariadb';
|
|
14
15
|
export const DEBUG = false;
|
|
@@ -292,11 +293,15 @@ export class Mysql extends DataSource {
|
|
|
292
293
|
async search(type, search = {}, options) {
|
|
293
294
|
const connection = this.connection ?? await this.connect();
|
|
294
295
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
296
|
+
let limitOption = undefined;
|
|
295
297
|
let sortOption = undefined;
|
|
296
298
|
for (const option of this.options(options)) {
|
|
297
299
|
if (option === Sort) {
|
|
298
300
|
sortOption = new Sort(sortOf(type));
|
|
299
301
|
}
|
|
302
|
+
if (option instanceof Limit) {
|
|
303
|
+
limitOption = option;
|
|
304
|
+
}
|
|
300
305
|
if (option instanceof Sort) {
|
|
301
306
|
sortOption = option.properties.length ? option : new Sort(sortOf(type));
|
|
302
307
|
}
|
|
@@ -306,13 +311,15 @@ export class Mysql extends DataSource {
|
|
|
306
311
|
const [values] = await this.valuesToDb(search);
|
|
307
312
|
if (DEBUG)
|
|
308
313
|
console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
314
|
+
const limit = limitOption?.limit ? ' LIMIT ' + limitOption.limit : '';
|
|
315
|
+
const offset = limitOption?.offset ? ' OFFSET ' + limitOption.offset : '';
|
|
309
316
|
const sort = sortOption?.properties.length
|
|
310
317
|
? ' ORDER BY '
|
|
311
318
|
+ sortOption.properties
|
|
312
319
|
.map(property => '`' + property + '`' + (property instanceof Reverse ? ' DESC' : ''))
|
|
313
320
|
.join(', ')
|
|
314
321
|
: '';
|
|
315
|
-
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
|
|
322
|
+
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort + limit + offset, Object.values(values));
|
|
316
323
|
return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
|
|
317
324
|
}
|
|
318
325
|
async update(object) {
|
package/package.json
CHANGED