@itrocks/mysql 0.0.22 → 0.0.24
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 +1 -0
- package/cjs/mysql.js +22 -5
- package/esm/mysql.d.ts +1 -0
- package/esm/mysql.js +18 -1
- package/package.json +1 -1
package/cjs/mysql.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export declare class Mysql extends DataSource {
|
|
|
38
38
|
database: string;
|
|
39
39
|
});
|
|
40
40
|
connect(): Promise<Connection>;
|
|
41
|
+
count<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<number>;
|
|
41
42
|
delete<T extends object>(object: Entity<T>, property?: KeyOf<Entity<T>>): Promise<T>;
|
|
42
43
|
deleteId<T extends object>(type: ObjectOrType<T>, id: any, property?: KeyOf<Entity<T>>): Promise<void>;
|
|
43
44
|
deleteRelatedId<T extends Entity>(object: T, property: KeyOf<T>, id: Identifier): Promise<void>;
|
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 = {
|
|
@@ -53,6 +54,16 @@ class Mysql extends storage_1.DataSource {
|
|
|
53
54
|
});
|
|
54
55
|
return this.connection = await (0, mariadb_1.createConnection)(mariaDbConfig);
|
|
55
56
|
}
|
|
57
|
+
async count(type, search = {}) {
|
|
58
|
+
const connection = this.connection ?? await this.connect();
|
|
59
|
+
Object.setPrototypeOf(search, type.prototype);
|
|
60
|
+
const sql = this.propertiesToSearchSql(search);
|
|
61
|
+
const [values] = await this.valuesToDb(search);
|
|
62
|
+
if (exports.DEBUG)
|
|
63
|
+
console.log('SELECT COUNT(*) FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
64
|
+
const row = (await connection.query('SELECT COUNT(*) `count` FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values)))[0];
|
|
65
|
+
return row?.count;
|
|
66
|
+
}
|
|
56
67
|
async delete(object, property = 'id') {
|
|
57
68
|
await this.deleteId(object, object[property], property);
|
|
58
69
|
return this.disconnectObject(object);
|
|
@@ -297,13 +308,17 @@ class Mysql extends storage_1.DataSource {
|
|
|
297
308
|
async search(type, search = {}, options) {
|
|
298
309
|
const connection = this.connection ?? await this.connect();
|
|
299
310
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
311
|
+
let limitOption = undefined;
|
|
300
312
|
let sortOption = undefined;
|
|
301
313
|
for (const option of this.options(options)) {
|
|
302
|
-
if (option ===
|
|
303
|
-
sortOption = new
|
|
314
|
+
if (option === storage_3.Sort) {
|
|
315
|
+
sortOption = new storage_3.Sort((0, sort_2.sortOf)(type));
|
|
316
|
+
}
|
|
317
|
+
if (option instanceof storage_2.Limit) {
|
|
318
|
+
limitOption = option;
|
|
304
319
|
}
|
|
305
|
-
if (option instanceof
|
|
306
|
-
sortOption = option.properties.length ? option : new
|
|
320
|
+
if (option instanceof storage_3.Sort) {
|
|
321
|
+
sortOption = option.properties.length ? option : new storage_3.Sort((0, sort_2.sortOf)(type));
|
|
307
322
|
}
|
|
308
323
|
}
|
|
309
324
|
Object.setPrototypeOf(search, type.prototype);
|
|
@@ -311,13 +326,15 @@ class Mysql extends storage_1.DataSource {
|
|
|
311
326
|
const [values] = await this.valuesToDb(search);
|
|
312
327
|
if (exports.DEBUG)
|
|
313
328
|
console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
329
|
+
const limit = limitOption?.limit ? ' LIMIT ' + limitOption.limit : '';
|
|
330
|
+
const offset = limitOption?.offset ? ' OFFSET ' + limitOption.offset : '';
|
|
314
331
|
const sort = sortOption?.properties.length
|
|
315
332
|
? ' ORDER BY '
|
|
316
333
|
+ sortOption.properties
|
|
317
334
|
.map(property => '`' + property + '`' + (property instanceof sort_1.Reverse ? ' DESC' : ''))
|
|
318
335
|
.join(', ')
|
|
319
336
|
: '';
|
|
320
|
-
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
|
|
337
|
+
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort + limit + offset, Object.values(values));
|
|
321
338
|
return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
|
|
322
339
|
}
|
|
323
340
|
async update(object) {
|
package/esm/mysql.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export declare class Mysql extends DataSource {
|
|
|
38
38
|
database: string;
|
|
39
39
|
});
|
|
40
40
|
connect(): Promise<Connection>;
|
|
41
|
+
count<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<number>;
|
|
41
42
|
delete<T extends object>(object: Entity<T>, property?: KeyOf<Entity<T>>): Promise<T>;
|
|
42
43
|
deleteId<T extends object>(type: ObjectOrType<T>, id: any, property?: KeyOf<Entity<T>>): Promise<void>;
|
|
43
44
|
deleteRelatedId<T extends Entity>(object: T, property: KeyOf<T>, id: Identifier): Promise<void>;
|
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;
|
|
@@ -48,6 +49,16 @@ export class Mysql extends DataSource {
|
|
|
48
49
|
});
|
|
49
50
|
return this.connection = await createConnection(mariaDbConfig);
|
|
50
51
|
}
|
|
52
|
+
async count(type, search = {}) {
|
|
53
|
+
const connection = this.connection ?? await this.connect();
|
|
54
|
+
Object.setPrototypeOf(search, type.prototype);
|
|
55
|
+
const sql = this.propertiesToSearchSql(search);
|
|
56
|
+
const [values] = await this.valuesToDb(search);
|
|
57
|
+
if (DEBUG)
|
|
58
|
+
console.log('SELECT COUNT(*) FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
59
|
+
const row = (await connection.query('SELECT COUNT(*) `count` FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values)))[0];
|
|
60
|
+
return row?.count;
|
|
61
|
+
}
|
|
51
62
|
async delete(object, property = 'id') {
|
|
52
63
|
await this.deleteId(object, object[property], property);
|
|
53
64
|
return this.disconnectObject(object);
|
|
@@ -292,11 +303,15 @@ export class Mysql extends DataSource {
|
|
|
292
303
|
async search(type, search = {}, options) {
|
|
293
304
|
const connection = this.connection ?? await this.connect();
|
|
294
305
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
306
|
+
let limitOption = undefined;
|
|
295
307
|
let sortOption = undefined;
|
|
296
308
|
for (const option of this.options(options)) {
|
|
297
309
|
if (option === Sort) {
|
|
298
310
|
sortOption = new Sort(sortOf(type));
|
|
299
311
|
}
|
|
312
|
+
if (option instanceof Limit) {
|
|
313
|
+
limitOption = option;
|
|
314
|
+
}
|
|
300
315
|
if (option instanceof Sort) {
|
|
301
316
|
sortOption = option.properties.length ? option : new Sort(sortOf(type));
|
|
302
317
|
}
|
|
@@ -306,13 +321,15 @@ export class Mysql extends DataSource {
|
|
|
306
321
|
const [values] = await this.valuesToDb(search);
|
|
307
322
|
if (DEBUG)
|
|
308
323
|
console.log('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
324
|
+
const limit = limitOption?.limit ? ' LIMIT ' + limitOption.limit : '';
|
|
325
|
+
const offset = limitOption?.offset ? ' OFFSET ' + limitOption.offset : '';
|
|
309
326
|
const sort = sortOption?.properties.length
|
|
310
327
|
? ' ORDER BY '
|
|
311
328
|
+ sortOption.properties
|
|
312
329
|
.map(property => '`' + property + '`' + (property instanceof Reverse ? ' DESC' : ''))
|
|
313
330
|
.join(', ')
|
|
314
331
|
: '';
|
|
315
|
-
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort, Object.values(values));
|
|
332
|
+
const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '`' + sql + sort + limit + offset, Object.values(values));
|
|
316
333
|
return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
|
|
317
334
|
}
|
|
318
335
|
async update(object) {
|
package/package.json
CHANGED