@itrocks/mysql 0.0.3 → 0.0.5
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 +3 -3
- package/cjs/mysql.js +33 -33
- package/esm/mysql.d.ts +3 -3
- package/esm/mysql.js +2 -2
- package/package.json +1 -1
package/cjs/mysql.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Entity, MayEntity } from '@itrocks/storage';
|
|
|
5
5
|
import { Identifier, SearchType } from '@itrocks/storage';
|
|
6
6
|
import { Connection } from 'mariadb';
|
|
7
7
|
export declare const DEBUG = false;
|
|
8
|
-
|
|
8
|
+
interface Dependencies<QF extends object = object> {
|
|
9
9
|
applyReadTransformer: <T extends object>(object: T, property: KeyOf<T>, data: AnyObject) => any;
|
|
10
10
|
applySaveTransformer: <T extends object>(object: T, property: KeyOf<T>, data: AnyObject) => any;
|
|
11
11
|
columnOf: (property: string) => string;
|
|
@@ -15,8 +15,7 @@ export interface Dependencies<QF extends object = object> {
|
|
|
15
15
|
queryFunctionCall: (value: QF) => [any, string];
|
|
16
16
|
storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
|
|
17
17
|
}
|
|
18
|
-
export declare
|
|
19
|
-
export declare function dependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
|
|
18
|
+
export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
|
|
20
19
|
export default class Mysql extends DataSource {
|
|
21
20
|
config: {
|
|
22
21
|
host: string;
|
|
@@ -53,3 +52,4 @@ export default class Mysql extends DataSource {
|
|
|
53
52
|
valuesFromDb<T extends object>(row: Entity<T>, type: Type<T>): Promise<Entity<T>>;
|
|
54
53
|
valuesToDb<T extends object>(object: T, type?: Type<T>): Promise<[AnyObject, Function[]]>;
|
|
55
54
|
}
|
|
55
|
+
export {};
|
package/cjs/mysql.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.DEBUG = void 0;
|
|
4
|
+
exports.mysqlDependsOn = mysqlDependsOn;
|
|
5
5
|
const class_type_1 = require("@itrocks/class-type");
|
|
6
6
|
const reflect_1 = require("@itrocks/reflect");
|
|
7
7
|
const storage_1 = require("@itrocks/storage");
|
|
8
8
|
const mariadb_1 = require("mariadb");
|
|
9
9
|
exports.DEBUG = false;
|
|
10
|
-
|
|
10
|
+
const depends = {
|
|
11
11
|
applyReadTransformer: value => value,
|
|
12
12
|
applySaveTransformer: value => value,
|
|
13
13
|
columnOf: name => name,
|
|
@@ -18,8 +18,8 @@ exports.depends = {
|
|
|
18
18
|
queryFunctionCall: () => [undefined, ' = ?'],
|
|
19
19
|
storeOf: () => false
|
|
20
20
|
};
|
|
21
|
-
function
|
|
22
|
-
Object.assign(
|
|
21
|
+
function mysqlDependsOn(dependencies) {
|
|
22
|
+
Object.assign(depends, dependencies);
|
|
23
23
|
}
|
|
24
24
|
class Mysql extends storage_1.DataSource {
|
|
25
25
|
config;
|
|
@@ -42,13 +42,13 @@ class Mysql extends storage_1.DataSource {
|
|
|
42
42
|
async deleteId(type, id, property = 'id') {
|
|
43
43
|
const connection = this.connection ?? await this.connect();
|
|
44
44
|
if (exports.DEBUG)
|
|
45
|
-
console.log('DELETE FROM `' +
|
|
46
|
-
await connection.query('DELETE FROM `' +
|
|
45
|
+
console.log('DELETE FROM `' + depends.storeOf(type) + '` WHERE `' + depends.columnOf(property) + '` = ?', [id]);
|
|
46
|
+
await connection.query('DELETE FROM `' + depends.storeOf(type) + '` WHERE `' + depends.columnOf(property) + '` = ?', [id]);
|
|
47
47
|
}
|
|
48
48
|
async deleteRelatedId(object, property, id) {
|
|
49
49
|
const connection = this.connection ?? await this.connect();
|
|
50
|
-
const objectTable =
|
|
51
|
-
const propertyTable =
|
|
50
|
+
const objectTable = depends.storeOf(object);
|
|
51
|
+
const propertyTable = depends.storeOf(new reflect_1.ReflectProperty(object, property).collectionType.elementType);
|
|
52
52
|
const joinTable = [objectTable, propertyTable].sort().join('_');
|
|
53
53
|
const query = 'DELETE FROM `' + joinTable + '` WHERE ' + objectTable + '_id = ? AND ' + propertyTable + '_id = ?';
|
|
54
54
|
const values = [object.id, id];
|
|
@@ -60,7 +60,7 @@ class Mysql extends storage_1.DataSource {
|
|
|
60
60
|
const connection = this.connection ?? await this.connect();
|
|
61
61
|
const [values, deferred] = await this.valuesToDb(object);
|
|
62
62
|
const sql = this.propertiesToSql(values);
|
|
63
|
-
const query = 'INSERT INTO `' +
|
|
63
|
+
const query = 'INSERT INTO `' + depends.storeOf(object) + '` SET ' + sql;
|
|
64
64
|
if (exports.DEBUG)
|
|
65
65
|
console.log(query, JSON.stringify(Object.values(values)));
|
|
66
66
|
const result = await connection.query(query, Object.values(values));
|
|
@@ -73,8 +73,8 @@ class Mysql extends storage_1.DataSource {
|
|
|
73
73
|
}
|
|
74
74
|
async insertRelatedId(object, property, id) {
|
|
75
75
|
const connection = this.connection ?? await this.connect();
|
|
76
|
-
const objectTable =
|
|
77
|
-
const propertyTable =
|
|
76
|
+
const objectTable = depends.storeOf(object);
|
|
77
|
+
const propertyTable = depends.storeOf(new reflect_1.ReflectProperty(object, property).collectionType.elementType);
|
|
78
78
|
const joinTable = [objectTable, propertyTable].sort().join('_');
|
|
79
79
|
const query = 'INSERT INTO `' + joinTable + '` SET ' + objectTable + '_id = ?, ' + propertyTable + '_id = ?';
|
|
80
80
|
const values = [object.id, id];
|
|
@@ -86,13 +86,13 @@ class Mysql extends storage_1.DataSource {
|
|
|
86
86
|
const sql = Object.entries(search)
|
|
87
87
|
.map(([name, value]) => {
|
|
88
88
|
let sql;
|
|
89
|
-
if (value instanceof
|
|
90
|
-
[search[name], sql] =
|
|
89
|
+
if (value instanceof depends.QueryFunction) {
|
|
90
|
+
[search[name], sql] = depends.queryFunctionCall(value);
|
|
91
91
|
}
|
|
92
92
|
else {
|
|
93
93
|
sql = ' = ?';
|
|
94
94
|
}
|
|
95
|
-
return '`' +
|
|
95
|
+
return '`' + depends.columnOf(name) + '`' + sql;
|
|
96
96
|
})
|
|
97
97
|
.join(' AND ');
|
|
98
98
|
return sql.length
|
|
@@ -100,21 +100,21 @@ class Mysql extends storage_1.DataSource {
|
|
|
100
100
|
: '';
|
|
101
101
|
}
|
|
102
102
|
propertiesToSql(object) {
|
|
103
|
-
return Object.keys(object).map(name => '`' +
|
|
103
|
+
return Object.keys(object).map(name => '`' + depends.columnOf(name) + '` = ?').join(', ');
|
|
104
104
|
}
|
|
105
105
|
async read(type, id) {
|
|
106
106
|
const connection = this.connection ?? await this.connect();
|
|
107
107
|
if (exports.DEBUG)
|
|
108
|
-
console.log('SELECT * FROM `' +
|
|
109
|
-
const rows = await connection.query('SELECT * FROM `' +
|
|
108
|
+
console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
|
|
109
|
+
const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id = ?', [id]);
|
|
110
110
|
return this.valuesFromDb(rows[0], type);
|
|
111
111
|
}
|
|
112
112
|
async readCollection(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType) {
|
|
113
113
|
const connection = this.connection ?? await this.connect();
|
|
114
|
-
const objectTable =
|
|
115
|
-
const table =
|
|
114
|
+
const objectTable = depends.storeOf(object);
|
|
115
|
+
const table = depends.storeOf(type);
|
|
116
116
|
let query;
|
|
117
|
-
if (
|
|
117
|
+
if (depends.componentOf(object, property)) {
|
|
118
118
|
query = 'SELECT * FROM `' + table + '` WHERE ' + objectTable + '_id = ?';
|
|
119
119
|
}
|
|
120
120
|
else {
|
|
@@ -128,10 +128,10 @@ class Mysql extends storage_1.DataSource {
|
|
|
128
128
|
}
|
|
129
129
|
async readCollectionIds(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType) {
|
|
130
130
|
const connection = this.connection ?? await this.connect();
|
|
131
|
-
const objectTable =
|
|
132
|
-
const propertyTable =
|
|
131
|
+
const objectTable = depends.storeOf(object);
|
|
132
|
+
const propertyTable = depends.storeOf(type);
|
|
133
133
|
let query;
|
|
134
|
-
if (
|
|
134
|
+
if (depends.componentOf(object, property)) {
|
|
135
135
|
query = 'SELECT id FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
|
|
136
136
|
}
|
|
137
137
|
else {
|
|
@@ -148,8 +148,8 @@ class Mysql extends storage_1.DataSource {
|
|
|
148
148
|
const connection = this.connection ?? await this.connect();
|
|
149
149
|
const questionMarks = Array(ids.length).fill('?').join(', ');
|
|
150
150
|
if (exports.DEBUG)
|
|
151
|
-
console.log('SELECT * FROM `' +
|
|
152
|
-
const rows = await connection.query('SELECT * FROM `' +
|
|
151
|
+
console.log('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
|
|
152
|
+
const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
|
|
153
153
|
return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
|
|
154
154
|
}
|
|
155
155
|
async save(object) {
|
|
@@ -162,15 +162,15 @@ class Mysql extends storage_1.DataSource {
|
|
|
162
162
|
const sql = this.propertiesToSearchSql(search);
|
|
163
163
|
const [values] = await this.valuesToDb(search, type);
|
|
164
164
|
if (exports.DEBUG)
|
|
165
|
-
console.log('SELECT * FROM `' +
|
|
166
|
-
const rows = await connection.query('SELECT * FROM `' +
|
|
165
|
+
console.log('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, '[', values, ']');
|
|
166
|
+
const rows = await connection.query('SELECT * FROM `' + depends.storeOf(type) + '`' + sql, Object.values(values));
|
|
167
167
|
return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
|
|
168
168
|
}
|
|
169
169
|
async update(object) {
|
|
170
170
|
const connection = this.connection ?? await this.connect();
|
|
171
171
|
const [values, deferred] = await this.valuesToDb(object);
|
|
172
172
|
const sql = this.propertiesToSql(values);
|
|
173
|
-
const query = 'UPDATE `' +
|
|
173
|
+
const query = 'UPDATE `' + depends.storeOf(object) + '` SET ' + sql + ' WHERE id = ?';
|
|
174
174
|
if (exports.DEBUG)
|
|
175
175
|
console.log(query, JSON.stringify(Object.values(values).concat([object.id])));
|
|
176
176
|
await connection.query(query, Object.values(values).concat([object.id]));
|
|
@@ -183,8 +183,8 @@ class Mysql extends storage_1.DataSource {
|
|
|
183
183
|
const object = (new type);
|
|
184
184
|
let property;
|
|
185
185
|
for (property in row) {
|
|
186
|
-
const value = await
|
|
187
|
-
if (value ===
|
|
186
|
+
const value = await depends.applyReadTransformer(object, property, row);
|
|
187
|
+
if (value === depends.ignoreTransformedValue)
|
|
188
188
|
continue;
|
|
189
189
|
object[property] = value;
|
|
190
190
|
}
|
|
@@ -195,8 +195,8 @@ class Mysql extends storage_1.DataSource {
|
|
|
195
195
|
const deferred = [];
|
|
196
196
|
const values = {};
|
|
197
197
|
for (const property of type ? Object.keys(object) : new reflect_1.ReflectClass(object).propertyNames) {
|
|
198
|
-
const value = await
|
|
199
|
-
if (value ===
|
|
198
|
+
const value = await depends.applySaveTransformer(typeObject, property, values);
|
|
199
|
+
if (value === depends.ignoreTransformedValue)
|
|
200
200
|
continue;
|
|
201
201
|
if ((0, class_type_1.isAnyFunction)(value)) {
|
|
202
202
|
deferred.push(value);
|
package/esm/mysql.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Entity, MayEntity } from '@itrocks/storage';
|
|
|
5
5
|
import { Identifier, SearchType } from '@itrocks/storage';
|
|
6
6
|
import { Connection } from 'mariadb';
|
|
7
7
|
export declare const DEBUG = false;
|
|
8
|
-
|
|
8
|
+
interface Dependencies<QF extends object = object> {
|
|
9
9
|
applyReadTransformer: <T extends object>(object: T, property: KeyOf<T>, data: AnyObject) => any;
|
|
10
10
|
applySaveTransformer: <T extends object>(object: T, property: KeyOf<T>, data: AnyObject) => any;
|
|
11
11
|
columnOf: (property: string) => string;
|
|
@@ -15,8 +15,7 @@ export interface Dependencies<QF extends object = object> {
|
|
|
15
15
|
queryFunctionCall: (value: QF) => [any, string];
|
|
16
16
|
storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
|
|
17
17
|
}
|
|
18
|
-
export declare
|
|
19
|
-
export declare function dependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
|
|
18
|
+
export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
|
|
20
19
|
export default class Mysql extends DataSource {
|
|
21
20
|
config: {
|
|
22
21
|
host: string;
|
|
@@ -53,3 +52,4 @@ export default class Mysql extends DataSource {
|
|
|
53
52
|
valuesFromDb<T extends object>(row: Entity<T>, type: Type<T>): Promise<Entity<T>>;
|
|
54
53
|
valuesToDb<T extends object>(object: T, type?: Type<T>): Promise<[AnyObject, Function[]]>;
|
|
55
54
|
}
|
|
55
|
+
export {};
|
package/esm/mysql.js
CHANGED
|
@@ -3,7 +3,7 @@ import { ReflectClass, ReflectProperty } from '@itrocks/reflect';
|
|
|
3
3
|
import { DataSource } from '@itrocks/storage';
|
|
4
4
|
import { createConnection } from 'mariadb';
|
|
5
5
|
export const DEBUG = false;
|
|
6
|
-
|
|
6
|
+
const depends = {
|
|
7
7
|
applyReadTransformer: value => value,
|
|
8
8
|
applySaveTransformer: value => value,
|
|
9
9
|
columnOf: name => name,
|
|
@@ -14,7 +14,7 @@ export const depends = {
|
|
|
14
14
|
queryFunctionCall: () => [undefined, ' = ?'],
|
|
15
15
|
storeOf: () => false
|
|
16
16
|
};
|
|
17
|
-
export function
|
|
17
|
+
export function mysqlDependsOn(dependencies) {
|
|
18
18
|
Object.assign(depends, dependencies);
|
|
19
19
|
}
|
|
20
20
|
export default class Mysql extends DataSource {
|
package/package.json
CHANGED