@asla/yoursql 0.0.3 → 0.0.4
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 +23 -7
- package/dist/sql_value/db_type.d.ts +2 -2
- package/dist/sql_value/sql_value.d.ts +14 -2
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -65,6 +65,16 @@ class SqlValuesCreator {
|
|
|
65
65
|
*/
|
|
66
66
|
constructor(map = new Map()) {
|
|
67
67
|
this.map = map;
|
|
68
|
+
const fn = (value) => this.toSqlStr(value);
|
|
69
|
+
Reflect.setPrototypeOf(fn, this);
|
|
70
|
+
return fn;
|
|
71
|
+
}
|
|
72
|
+
/** 设置转换器 */
|
|
73
|
+
setTransformer(type, transformer) {
|
|
74
|
+
if (!transformer)
|
|
75
|
+
this.map.delete(type);
|
|
76
|
+
else
|
|
77
|
+
this.map.set(type, transformer);
|
|
68
78
|
}
|
|
69
79
|
string(value) {
|
|
70
80
|
return SqlValuesCreator.string(value);
|
|
@@ -72,7 +82,9 @@ class SqlValuesCreator {
|
|
|
72
82
|
number(value) {
|
|
73
83
|
return value.toString();
|
|
74
84
|
}
|
|
75
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* 将 JS 对象转为 SQL 的字符值的形式
|
|
87
|
+
*/
|
|
76
88
|
toSqlStr(value) {
|
|
77
89
|
switch (typeof value) {
|
|
78
90
|
case "bigint":
|
|
@@ -93,14 +105,18 @@ class SqlValuesCreator {
|
|
|
93
105
|
if (value instanceof Class)
|
|
94
106
|
return this.map.get(Class).call(this, value);
|
|
95
107
|
}
|
|
96
|
-
return this.
|
|
108
|
+
return this.defaultObject(value);
|
|
109
|
+
case "undefined":
|
|
110
|
+
return "NULL";
|
|
97
111
|
default:
|
|
112
|
+
//function、symbol
|
|
98
113
|
let type = typeof value;
|
|
99
|
-
if (type === "object")
|
|
100
|
-
type = value.constructor?.name ?? "object";
|
|
101
114
|
throw new Error("不支持转换 " + type + " 类型");
|
|
102
115
|
}
|
|
103
116
|
}
|
|
117
|
+
defaultObject(value) {
|
|
118
|
+
return this.string(JSON.stringify(value));
|
|
119
|
+
}
|
|
104
120
|
objectListToValuesList(objectList, keys_types) {
|
|
105
121
|
if (objectList.length <= 0)
|
|
106
122
|
throw new Error("objectList 不能是空数组");
|
|
@@ -203,8 +219,8 @@ function getKeys(objectList) {
|
|
|
203
219
|
* PgSql的值转换
|
|
204
220
|
*/
|
|
205
221
|
class PgSqlValue extends SqlValuesCreator {
|
|
206
|
-
constructor() {
|
|
207
|
-
const map = new Map();
|
|
222
|
+
constructor(custom) {
|
|
223
|
+
const map = new Map(custom);
|
|
208
224
|
map.set(Array, PgSqlValue.prototype.array);
|
|
209
225
|
map.set(Date, PgSqlValue.prototype.timestamp);
|
|
210
226
|
super(map);
|
|
@@ -587,7 +603,7 @@ class DbTableQuery extends DbTable {
|
|
|
587
603
|
}
|
|
588
604
|
else if (values instanceof SqlQueryStatement) {
|
|
589
605
|
// todo 验证 values.columns 和 this.columns 是否匹配
|
|
590
|
-
valuesStr = values.
|
|
606
|
+
valuesStr = values.toString();
|
|
591
607
|
insertCol = values.columns;
|
|
592
608
|
}
|
|
593
609
|
else
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { SqlValuesCreator } from "./sql_value.ts";
|
|
1
|
+
import { SqlValuesCreator, JsObjectMapSql } from "./sql_value.ts";
|
|
2
2
|
/**
|
|
3
3
|
* @public
|
|
4
4
|
* PgSql的值转换
|
|
5
5
|
*/
|
|
6
6
|
export declare class PgSqlValue extends SqlValuesCreator {
|
|
7
|
-
constructor();
|
|
7
|
+
constructor(custom?: JsObjectMapSql);
|
|
8
8
|
timestamp(value: Date): string;
|
|
9
9
|
array(value: any[]): string;
|
|
10
10
|
}
|
|
@@ -11,7 +11,14 @@ export declare class SqlRaw<T = any> {
|
|
|
11
11
|
[SQL_RAW]: T;
|
|
12
12
|
}
|
|
13
13
|
/** @public */
|
|
14
|
-
export type JsObjectMapSql = Map<new (...args: any[]) => any, (value:
|
|
14
|
+
export type JsObjectMapSql = Map<new (...args: any[]) => any, (value: any) => string>;
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export interface SqlValuesCreator {
|
|
19
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 */
|
|
20
|
+
(value: any): string;
|
|
21
|
+
}
|
|
15
22
|
/**
|
|
16
23
|
* SQL value 生成器
|
|
17
24
|
* @public
|
|
@@ -26,11 +33,16 @@ export declare class SqlValuesCreator {
|
|
|
26
33
|
* @param map - 自定义对象转换
|
|
27
34
|
*/
|
|
28
35
|
constructor(map?: JsObjectMapSql);
|
|
36
|
+
/** 设置转换器 */
|
|
37
|
+
setTransformer<T>(type: new (...args: any[]) => T, transformer?: (value: T) => string): void;
|
|
29
38
|
private readonly map;
|
|
30
39
|
string(value: string): string;
|
|
31
40
|
number(value: number | bigint): string;
|
|
32
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* 将 JS 对象转为 SQL 的字符值的形式
|
|
43
|
+
*/
|
|
33
44
|
toSqlStr(value: any): string;
|
|
45
|
+
protected defaultObject(value: object): string;
|
|
34
46
|
/**
|
|
35
47
|
* 将对象列表转为 SQL 的 VALUES
|
|
36
48
|
* @example 返回示例: " (...),(...) "
|