@asla/yoursql 0.0.6 → 0.1.0
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/dist/mod.js +13 -40
- package/dist/sql_value/db_type.d.ts +1 -11
- package/dist/sql_value/sql_value.d.ts +9 -12
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -65,7 +65,8 @@ class SqlValuesCreator {
|
|
|
65
65
|
*/
|
|
66
66
|
constructor(map = new Map()) {
|
|
67
67
|
this.map = map;
|
|
68
|
-
const fn =
|
|
68
|
+
const fn = this.toSqlStr.bind(this);
|
|
69
|
+
this.toSqlStr = fn;
|
|
69
70
|
Reflect.setPrototypeOf(fn, this);
|
|
70
71
|
return fn;
|
|
71
72
|
}
|
|
@@ -76,17 +77,7 @@ class SqlValuesCreator {
|
|
|
76
77
|
else
|
|
77
78
|
this.map.set(type, transformer);
|
|
78
79
|
}
|
|
79
|
-
/**
|
|
80
|
-
string(value) {
|
|
81
|
-
return SqlValuesCreator.string(value);
|
|
82
|
-
}
|
|
83
|
-
/** @deprecated 已废弃,改用 toSqlStr(value, "number") */
|
|
84
|
-
number(value) {
|
|
85
|
-
return value.toString();
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* 将 JS 对象转为 SQL 的字符值的形式
|
|
89
|
-
*/
|
|
80
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
90
81
|
toSqlStr(value, expectType) {
|
|
91
82
|
let basicType;
|
|
92
83
|
if (expectType) {
|
|
@@ -111,7 +102,7 @@ class SqlValuesCreator {
|
|
|
111
102
|
case "object":
|
|
112
103
|
return this.toObjectStr(value);
|
|
113
104
|
case "undefined":
|
|
114
|
-
return "
|
|
105
|
+
return "DEFAULT";
|
|
115
106
|
default:
|
|
116
107
|
//function、symbol
|
|
117
108
|
let type = typeof value;
|
|
@@ -132,12 +123,12 @@ class SqlValuesCreator {
|
|
|
132
123
|
defaultObject(value) {
|
|
133
124
|
return SqlValuesCreator.string(JSON.stringify(value));
|
|
134
125
|
}
|
|
135
|
-
objectListToValuesList(objectList, keys_types) {
|
|
126
|
+
objectListToValuesList(objectList, keys_types, keepUndefinedKey) {
|
|
136
127
|
if (objectList.length <= 0)
|
|
137
128
|
throw new Error("objectList 不能是空数组");
|
|
138
129
|
let keys;
|
|
139
130
|
if (!keys_types) {
|
|
140
|
-
keys = getObjectListKeys(objectList,
|
|
131
|
+
keys = getObjectListKeys(objectList, keepUndefinedKey);
|
|
141
132
|
}
|
|
142
133
|
else if (keys_types instanceof Array) {
|
|
143
134
|
keys = keys_types;
|
|
@@ -177,7 +168,7 @@ class SqlValuesCreator {
|
|
|
177
168
|
try {
|
|
178
169
|
for (; i < keys.length; i++) {
|
|
179
170
|
key = keys[i];
|
|
180
|
-
value = object[key]
|
|
171
|
+
value = object[key];
|
|
181
172
|
if (type[key])
|
|
182
173
|
values[i] = this.toSqlStr(value) + "::" + type[key];
|
|
183
174
|
else
|
|
@@ -210,14 +201,15 @@ function toKeyType(object, keys_types) {
|
|
|
210
201
|
type = keys_types;
|
|
211
202
|
}
|
|
212
203
|
else {
|
|
213
|
-
keys = Object.keys(object)
|
|
204
|
+
keys = Object.keys(object);
|
|
214
205
|
}
|
|
215
206
|
return { type, keys };
|
|
216
207
|
}
|
|
217
208
|
/**
|
|
218
209
|
* @public
|
|
210
|
+
* @param keepUndefinedKey - 是否保留值为 undefined 的 key
|
|
219
211
|
*/
|
|
220
|
-
function getObjectListKeys(objectList,
|
|
212
|
+
function getObjectListKeys(objectList, keepUndefinedKey) {
|
|
221
213
|
let keys = new Set();
|
|
222
214
|
for (let i = 0; i < objectList.length; i++) {
|
|
223
215
|
let obj = objectList[i];
|
|
@@ -225,9 +217,7 @@ function getObjectListKeys(objectList, keepNull) {
|
|
|
225
217
|
let k;
|
|
226
218
|
for (let j = 0; j < hasKeys.length; j++) {
|
|
227
219
|
k = hasKeys[j];
|
|
228
|
-
if (
|
|
229
|
-
continue;
|
|
230
|
-
if (obj[k] === undefined || typeof k !== "string")
|
|
220
|
+
if ((!keepUndefinedKey && obj[k] === undefined) || typeof k !== "string")
|
|
231
221
|
continue;
|
|
232
222
|
keys.add(k);
|
|
233
223
|
}
|
|
@@ -245,23 +235,6 @@ const pgSqlTransformer = new Map([
|
|
|
245
235
|
],
|
|
246
236
|
[Date, (value) => SqlValuesCreator.string(value.toISOString())],
|
|
247
237
|
]);
|
|
248
|
-
/**
|
|
249
|
-
* @public
|
|
250
|
-
* @deprecated 已废弃
|
|
251
|
-
* PgSql的值转换
|
|
252
|
-
*/
|
|
253
|
-
class PgSqlValue extends SqlValuesCreator {
|
|
254
|
-
constructor(custom) {
|
|
255
|
-
const map = new Map(custom ? [...pgSqlTransformer, ...custom] : pgSqlTransformer);
|
|
256
|
-
super(map);
|
|
257
|
-
}
|
|
258
|
-
timestamp(value) {
|
|
259
|
-
return SqlValuesCreator.string(value.toISOString());
|
|
260
|
-
}
|
|
261
|
-
array(value) {
|
|
262
|
-
return this.toSqlStr(value, Array);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
238
|
|
|
266
239
|
/**
|
|
267
240
|
* 可选择项。可以是 table、查询结果等,它能被 select 语句选择
|
|
@@ -670,7 +643,7 @@ class DbTableQuery extends DbTable {
|
|
|
670
643
|
for (const [k, v] of updateKey) {
|
|
671
644
|
if (v === undefined)
|
|
672
645
|
continue;
|
|
673
|
-
setList.push(k + " = " + this.statement
|
|
646
|
+
setList.push(k + " = " + this.statement(v));
|
|
674
647
|
}
|
|
675
648
|
let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
|
|
676
649
|
if (where)
|
|
@@ -711,4 +684,4 @@ function genWhere(where) {
|
|
|
711
684
|
return "\nWHERE " + where;
|
|
712
685
|
}
|
|
713
686
|
|
|
714
|
-
export { DbTable, DbTableQuery,
|
|
687
|
+
export { DbTable, DbTableQuery, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, createSelect, getObjectListKeys, pgSqlTransformer };
|
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JsObjectMapSql } from "./sql_value.ts";
|
|
2
2
|
/** @public PgSql 转换器 */
|
|
3
3
|
export declare const pgSqlTransformer: JsObjectMapSql;
|
|
4
|
-
/**
|
|
5
|
-
* @public
|
|
6
|
-
* @deprecated 已废弃
|
|
7
|
-
* PgSql的值转换
|
|
8
|
-
*/
|
|
9
|
-
export declare class PgSqlValue extends SqlValuesCreator {
|
|
10
|
-
constructor(custom?: JsObjectMapSql);
|
|
11
|
-
timestamp(value: Date): string;
|
|
12
|
-
array(value: any[]): string;
|
|
13
|
-
}
|
|
14
4
|
//# sourceMappingURL=db_type.d.ts.map
|
|
@@ -14,12 +14,14 @@ export declare class SqlRaw<T = any> {
|
|
|
14
14
|
export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
|
|
15
15
|
/** @public */
|
|
16
16
|
export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T, map: JsObjectMapSql) => string;
|
|
17
|
+
/** @public */
|
|
18
|
+
export type ManualType = "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any);
|
|
17
19
|
/**
|
|
18
20
|
* @public
|
|
19
21
|
*/
|
|
20
22
|
export interface SqlValuesCreator {
|
|
21
|
-
/** 将 JS 对象转为 SQL 的字符值的形式 */
|
|
22
|
-
(value: any): string;
|
|
23
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
24
|
+
(value: any, expectType?: ManualType): string;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* SQL value 生成器
|
|
@@ -38,14 +40,8 @@ export declare class SqlValuesCreator {
|
|
|
38
40
|
/** 设置转换器 */
|
|
39
41
|
setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
|
|
40
42
|
private readonly map;
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
/** @deprecated 已废弃,改用 toSqlStr(value, "number") */
|
|
44
|
-
number(value: number | bigint): string;
|
|
45
|
-
/**
|
|
46
|
-
* 将 JS 对象转为 SQL 的字符值的形式
|
|
47
|
-
*/
|
|
48
|
-
toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
|
|
43
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
44
|
+
protected toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
|
|
49
45
|
protected toObjectStr(value: object): string;
|
|
50
46
|
protected defaultObject(value: object): string;
|
|
51
47
|
/**
|
|
@@ -55,7 +51,7 @@ export declare class SqlValuesCreator {
|
|
|
55
51
|
*/
|
|
56
52
|
objectListToValuesList<T extends object>(objectList: T[], keys?: readonly (keyof T)[] | {
|
|
57
53
|
[key in keyof T]?: string | undefined;
|
|
58
|
-
}): string;
|
|
54
|
+
}, keepUndefinedKey?: boolean): string;
|
|
59
55
|
/**
|
|
60
56
|
* 将对象转为 SQL 的 value
|
|
61
57
|
* @example 返回示例: " 'abc', '6', 'now()' "
|
|
@@ -72,7 +68,8 @@ export declare class SqlValuesCreator {
|
|
|
72
68
|
}
|
|
73
69
|
/**
|
|
74
70
|
* @public
|
|
71
|
+
* @param keepUndefinedKey - 是否保留值为 undefined 的 key
|
|
75
72
|
*/
|
|
76
|
-
export declare function getObjectListKeys(objectList: any[],
|
|
73
|
+
export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): string[];
|
|
77
74
|
export {};
|
|
78
75
|
//# sourceMappingURL=sql_value.d.ts.map
|