@asla/yoursql 0.6.1 → 0.6.3
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 +103 -48
- package/dist/sql_value/sql_value.d.ts +25 -7
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -284,7 +284,7 @@ class SqlQueryStatement extends SqlSelectable {
|
|
|
284
284
|
}
|
|
285
285
|
_SqlQueryStatement_sql = new WeakMap();
|
|
286
286
|
|
|
287
|
-
var
|
|
287
|
+
var _YourValuesAs_asName, _YourValuesAs_valuesStr, _YourValuesAs_sql;
|
|
288
288
|
/**
|
|
289
289
|
* SQL 原始字符类。可以使用 String 类代替,这只是为了推断类型
|
|
290
290
|
* @public
|
|
@@ -313,20 +313,19 @@ class SqlValuesCreator {
|
|
|
313
313
|
* @param map - 自定义对象转换
|
|
314
314
|
*/
|
|
315
315
|
constructor(map) {
|
|
316
|
-
|
|
317
|
-
__classPrivateFieldSet(this, _SqlValuesCreator_map, new Map(map), "f");
|
|
316
|
+
this._map = new Map(map);
|
|
318
317
|
}
|
|
319
318
|
setTransformer(type_map, encoder) {
|
|
320
319
|
if (typeof type_map === "function") {
|
|
321
320
|
if (encoder)
|
|
322
|
-
|
|
321
|
+
this._map.set(type_map, encoder);
|
|
323
322
|
else
|
|
324
|
-
|
|
323
|
+
this._map.delete(type_map);
|
|
325
324
|
}
|
|
326
325
|
else {
|
|
327
326
|
for (const [type, encoder] of type_map) {
|
|
328
327
|
if (typeof type === "function" && typeof encoder === "function") {
|
|
329
|
-
|
|
328
|
+
this._map.set(type, encoder);
|
|
330
329
|
}
|
|
331
330
|
}
|
|
332
331
|
}
|
|
@@ -338,21 +337,28 @@ class SqlValuesCreator {
|
|
|
338
337
|
* v() 和 v.toSqlStr() 是等价的
|
|
339
338
|
* ```
|
|
340
339
|
*/
|
|
341
|
-
toSqlStr(value,
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
340
|
+
toSqlStr(value, assertJsType) {
|
|
341
|
+
if (value === null)
|
|
342
|
+
return "NULL";
|
|
343
|
+
else if (value === undefined)
|
|
344
|
+
return "DEFAULT";
|
|
345
|
+
let basicType = typeof value;
|
|
346
|
+
if (assertJsType) {
|
|
347
|
+
if (typeof assertJsType === "function") {
|
|
348
|
+
if (basicType !== "object")
|
|
349
|
+
throw new AssertError("object", basicType);
|
|
350
|
+
let type = this._map.get(assertJsType);
|
|
351
|
+
if (!type) {
|
|
352
|
+
if (assertJsType === Object)
|
|
353
|
+
return this.defaultObject(value);
|
|
347
354
|
throw new Error("类型不存在");
|
|
355
|
+
}
|
|
348
356
|
return type.call(this, value);
|
|
349
357
|
}
|
|
350
|
-
else {
|
|
351
|
-
|
|
358
|
+
else if (basicType !== assertJsType) {
|
|
359
|
+
throw new AssertError(assertJsType, basicType);
|
|
352
360
|
}
|
|
353
361
|
}
|
|
354
|
-
else
|
|
355
|
-
basicType = typeof value;
|
|
356
362
|
switch (basicType) {
|
|
357
363
|
case "bigint":
|
|
358
364
|
return value.toString();
|
|
@@ -363,8 +369,6 @@ class SqlValuesCreator {
|
|
|
363
369
|
case "boolean":
|
|
364
370
|
return value.toString();
|
|
365
371
|
case "object": {
|
|
366
|
-
if (value === null)
|
|
367
|
-
return "NULL";
|
|
368
372
|
if (value instanceof String)
|
|
369
373
|
return value.toString();
|
|
370
374
|
return this.getObjectType(value).call(this, value);
|
|
@@ -374,17 +378,27 @@ class SqlValuesCreator {
|
|
|
374
378
|
default:
|
|
375
379
|
//function、symbol
|
|
376
380
|
let type = typeof value;
|
|
377
|
-
throw new Error("
|
|
381
|
+
throw new Error("不支持 " + type + " 类型");
|
|
378
382
|
}
|
|
379
383
|
}
|
|
380
|
-
/**
|
|
384
|
+
/**
|
|
385
|
+
* @deprecated 已废弃
|
|
386
|
+
* 获取值对应的 SqlValueEncoder
|
|
387
|
+
*/
|
|
381
388
|
getObjectType(value) {
|
|
382
|
-
for (const Class of
|
|
389
|
+
for (const Class of this._map.keys()) {
|
|
383
390
|
if (value instanceof Class)
|
|
384
|
-
return
|
|
391
|
+
return this._map.get(Class);
|
|
385
392
|
}
|
|
386
393
|
return this.defaultObject;
|
|
387
394
|
}
|
|
395
|
+
/** 获取值对应已定义的类 */
|
|
396
|
+
getClassType(value) {
|
|
397
|
+
for (const Class of this._map.keys()) {
|
|
398
|
+
if (value instanceof Class)
|
|
399
|
+
return Class;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
388
402
|
defaultObject(value) {
|
|
389
403
|
return SqlValuesCreator.string(JSON.stringify(value));
|
|
390
404
|
}
|
|
@@ -392,6 +406,7 @@ class SqlValuesCreator {
|
|
|
392
406
|
if (objectList.length <= 0)
|
|
393
407
|
throw new Error("objectList 不能是空数组");
|
|
394
408
|
let keys;
|
|
409
|
+
let asserts = {};
|
|
395
410
|
if (!keys_types) {
|
|
396
411
|
keys = Array.from(getObjectListKeys(objectList, keepUndefinedKey));
|
|
397
412
|
}
|
|
@@ -400,8 +415,9 @@ class SqlValuesCreator {
|
|
|
400
415
|
}
|
|
401
416
|
else {
|
|
402
417
|
keys = Object.keys(keys_types);
|
|
418
|
+
asserts = initColumnAssert(keys, keys_types);
|
|
403
419
|
}
|
|
404
|
-
let str = "(" + this.
|
|
420
|
+
let str = "(" + this._internalObjectToValues(objectList[0], keys, asserts) + ")";
|
|
405
421
|
let i = 1;
|
|
406
422
|
let j;
|
|
407
423
|
let value;
|
|
@@ -425,17 +441,37 @@ class SqlValuesCreator {
|
|
|
425
441
|
return str;
|
|
426
442
|
}
|
|
427
443
|
objectToValues(object, keys_types) {
|
|
428
|
-
|
|
444
|
+
let type = {};
|
|
445
|
+
let keys;
|
|
446
|
+
if (keys_types instanceof Array) {
|
|
447
|
+
keys = keys_types;
|
|
448
|
+
type = {};
|
|
449
|
+
}
|
|
450
|
+
else if (keys_types) {
|
|
451
|
+
keys = Object.keys(keys_types);
|
|
452
|
+
type = initColumnAssert(keys, keys_types);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
keys = Object.keys(object);
|
|
456
|
+
}
|
|
457
|
+
return this._internalObjectToValues(object, keys, type);
|
|
458
|
+
}
|
|
459
|
+
_internalObjectToValues(object, keys, type) {
|
|
429
460
|
const values = [];
|
|
430
461
|
let i = 0;
|
|
431
462
|
let key;
|
|
432
463
|
let value;
|
|
464
|
+
let assertType;
|
|
433
465
|
try {
|
|
434
466
|
for (; i < keys.length; i++) {
|
|
435
467
|
key = keys[i];
|
|
436
468
|
value = object[key];
|
|
437
|
-
|
|
438
|
-
|
|
469
|
+
assertType = type[key];
|
|
470
|
+
if (assertType) {
|
|
471
|
+
values[i] = this.toSqlStr(value, assertType.assertJsType);
|
|
472
|
+
if (assertType.sqlType)
|
|
473
|
+
values[i] += "::" + assertType.sqlType;
|
|
474
|
+
}
|
|
439
475
|
else
|
|
440
476
|
values[i] = this.toSqlStr(value);
|
|
441
477
|
}
|
|
@@ -463,24 +499,26 @@ class SqlValuesCreator {
|
|
|
463
499
|
const column0 = new Array(insertKeys.length);
|
|
464
500
|
let columnName;
|
|
465
501
|
let item;
|
|
466
|
-
let
|
|
502
|
+
let sqlType;
|
|
503
|
+
let assertJsType;
|
|
467
504
|
let value;
|
|
468
505
|
for (let i = 0; i < insertKeys.length; i++) {
|
|
469
506
|
columnName = insertKeys[i];
|
|
470
507
|
item = valuesTypes[columnName];
|
|
471
508
|
if (typeof item === "string") {
|
|
472
|
-
|
|
509
|
+
sqlType = item;
|
|
473
510
|
defaultValues[i] = "NULL";
|
|
474
511
|
}
|
|
475
512
|
else {
|
|
476
|
-
|
|
513
|
+
sqlType = item.sqlType;
|
|
514
|
+
assertJsType = item.assertJsType;
|
|
477
515
|
defaultValues[i] = item.sqlDefault ?? "NULL";
|
|
478
516
|
}
|
|
479
517
|
value = values[0][columnName];
|
|
480
518
|
if (value === undefined)
|
|
481
|
-
column0[i] = defaultValues[i] + "::" +
|
|
519
|
+
column0[i] = defaultValues[i] + "::" + sqlType;
|
|
482
520
|
else
|
|
483
|
-
column0[i] = this.toSqlStr(value) + "::" +
|
|
521
|
+
column0[i] = this.toSqlStr(value, assertJsType) + "::" + sqlType;
|
|
484
522
|
}
|
|
485
523
|
valuesStr[0] = "(" + column0.join(",") + ")";
|
|
486
524
|
}
|
|
@@ -499,7 +537,6 @@ class SqlValuesCreator {
|
|
|
499
537
|
return new YourValuesAs(insertKeys, asName, valuesStr.join(",\n"));
|
|
500
538
|
}
|
|
501
539
|
}
|
|
502
|
-
_SqlValuesCreator_map = new WeakMap();
|
|
503
540
|
class YourValuesAs extends SqlSelectable {
|
|
504
541
|
constructor(columns, asName, valuesStr) {
|
|
505
542
|
super();
|
|
@@ -518,21 +555,26 @@ class YourValuesAs extends SqlSelectable {
|
|
|
518
555
|
}
|
|
519
556
|
}
|
|
520
557
|
_YourValuesAs_asName = new WeakMap(), _YourValuesAs_valuesStr = new WeakMap(), _YourValuesAs_sql = new WeakMap();
|
|
521
|
-
function
|
|
558
|
+
function initColumnAssert(keys, keys_types) {
|
|
559
|
+
let key;
|
|
560
|
+
let value;
|
|
522
561
|
let type = {};
|
|
523
|
-
let keys;
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
562
|
+
for (let i = 0; i < keys.length; i++) {
|
|
563
|
+
key = keys[i];
|
|
564
|
+
value = keys_types[key];
|
|
565
|
+
if (typeof value === "string") {
|
|
566
|
+
type[key] = { sqlType: value };
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
type[key] = value;
|
|
570
|
+
}
|
|
531
571
|
}
|
|
532
|
-
|
|
533
|
-
|
|
572
|
+
return type;
|
|
573
|
+
}
|
|
574
|
+
class AssertError extends TypeError {
|
|
575
|
+
constructor(assertType, actual) {
|
|
576
|
+
super(`Assert ${assertType} type, Actual ${actual} type`);
|
|
534
577
|
}
|
|
535
|
-
return { type, keys };
|
|
536
578
|
}
|
|
537
579
|
|
|
538
580
|
/** @public PgSql 转换器 */
|
|
@@ -546,17 +588,30 @@ const pgSqlTransformer = new Map([
|
|
|
546
588
|
let type;
|
|
547
589
|
let basicType;
|
|
548
590
|
for (let i = 0; i < value.length; i++) {
|
|
549
|
-
|
|
591
|
+
if (value[i] === null || value[i] === undefined)
|
|
592
|
+
valueStr[i] = this.toSqlStr(value[i]);
|
|
593
|
+
else if (type) {
|
|
594
|
+
valueStr[i] = this.toSqlStr(value[i], type);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
550
597
|
basicType = typeof value[i];
|
|
551
|
-
if (
|
|
552
|
-
|
|
598
|
+
if (basicType === "object") {
|
|
599
|
+
type = this.getClassType(value[i]);
|
|
600
|
+
}
|
|
601
|
+
else
|
|
602
|
+
type = basicType;
|
|
553
603
|
valueStr[i] = this.toSqlStr(value[i], type);
|
|
554
604
|
}
|
|
555
605
|
}
|
|
556
606
|
return "ARRAY[" + valueStr.join(",") + "]";
|
|
557
607
|
},
|
|
558
608
|
],
|
|
559
|
-
[
|
|
609
|
+
[
|
|
610
|
+
Date,
|
|
611
|
+
function (value) {
|
|
612
|
+
return SqlValuesCreator.string(value.toISOString());
|
|
613
|
+
},
|
|
614
|
+
],
|
|
560
615
|
]);
|
|
561
616
|
|
|
562
617
|
var _Selection_instances, _a, _Selection_sql, _Selection_join;
|
|
@@ -13,21 +13,26 @@ export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
|
|
|
13
13
|
/** @public 将 js 值转为 SQl 字符串的函数*/
|
|
14
14
|
export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T) => string;
|
|
15
15
|
/** @public 断言类型 */
|
|
16
|
-
export type
|
|
16
|
+
export type AssertJsType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated 改用 AssertJsType
|
|
19
|
+
* @public 断言类型
|
|
20
|
+
*/
|
|
21
|
+
export type ManualType = AssertJsType;
|
|
17
22
|
/** @public */
|
|
18
23
|
export type SqlValueFn = SqlValuesCreator & {
|
|
19
24
|
/**
|
|
20
25
|
* 安全将 JS 对象转为 SQL 的字符值的形式,可避免 SQL 注入。
|
|
21
26
|
* undefined 将被转换为 DEFAULT
|
|
27
|
+
* @param assertType - 如果断言了基本类型,并且值的基本类型与断言的类型不一致,则会抛出异常。 如果不是基本类型,则 value 会被传递给指定的转义器
|
|
22
28
|
*/
|
|
23
|
-
(value: any, assertType?:
|
|
29
|
+
(value: any, assertType?: AssertJsType): string;
|
|
24
30
|
};
|
|
25
31
|
/**
|
|
26
32
|
* SQL value 生成器
|
|
27
33
|
* @public
|
|
28
34
|
*/
|
|
29
35
|
export declare class SqlValuesCreator {
|
|
30
|
-
#private;
|
|
31
36
|
static create(map?: JsObjectMapSql): SqlValueFn;
|
|
32
37
|
/**
|
|
33
38
|
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
@@ -41,6 +46,7 @@ export declare class SqlValuesCreator {
|
|
|
41
46
|
/** 设置转换器 */
|
|
42
47
|
setTransformer(type: new (...args: any[]) => any, encoder?: SqlValueEncoder): void;
|
|
43
48
|
setTransformer(map: JsObjectMapSql): void;
|
|
49
|
+
private readonly _map;
|
|
44
50
|
/**
|
|
45
51
|
* 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT
|
|
46
52
|
* ```ts
|
|
@@ -48,9 +54,14 @@ export declare class SqlValuesCreator {
|
|
|
48
54
|
* v() 和 v.toSqlStr() 是等价的
|
|
49
55
|
* ```
|
|
50
56
|
*/
|
|
51
|
-
toSqlStr(value: any,
|
|
52
|
-
/**
|
|
57
|
+
toSqlStr(value: any, assertJsType?: AssertJsType): string;
|
|
58
|
+
/**
|
|
59
|
+
* @deprecated 已废弃
|
|
60
|
+
* 获取值对应的 SqlValueEncoder
|
|
61
|
+
*/
|
|
53
62
|
getObjectType(value: object): SqlValueEncoder;
|
|
63
|
+
/** 获取值对应已定义的类 */
|
|
64
|
+
getClassType(value: object): undefined | (new (...args: unknown[]) => unknown);
|
|
54
65
|
protected defaultObject(value: object): string;
|
|
55
66
|
/**
|
|
56
67
|
* 将对象列表转为 SQL 的 VALUES。
|
|
@@ -58,7 +69,7 @@ export declare class SqlValuesCreator {
|
|
|
58
69
|
* @param keys - 选择的键。如果指定了 keys, 值为 undefined 的属性将自动填充为 null; 如果未指定 keys,将选择 objectList 所有不是 undefined 项的键的并集
|
|
59
70
|
*/
|
|
60
71
|
objectListToValuesList<T extends object>(objectList: T[], keys?: readonly (keyof T)[] | {
|
|
61
|
-
[key in keyof T]?: string | undefined;
|
|
72
|
+
[key in keyof T]?: string | undefined | ColumnToValueConfig;
|
|
62
73
|
}, keepUndefinedKey?: boolean): string;
|
|
63
74
|
/**
|
|
64
75
|
* 将对象转为 SQL 的 value
|
|
@@ -66,8 +77,9 @@ export declare class SqlValuesCreator {
|
|
|
66
77
|
* @param keys - 如果指定了key, object undefined 的属性值将填充为 null,如果不指定,将自获取 object 所有非 undefined 的属性的key
|
|
67
78
|
*/
|
|
68
79
|
objectToValues<T extends object>(object: T, keys?: readonly (keyof T)[] | {
|
|
69
|
-
[key in keyof T]?: string | undefined;
|
|
80
|
+
[key in keyof T]?: string | undefined | ColumnToValueConfig;
|
|
70
81
|
}): string;
|
|
82
|
+
private _internalObjectToValues;
|
|
71
83
|
/**
|
|
72
84
|
* 将数组列表转为 SQL 的一个 value
|
|
73
85
|
* @example 返回示例: " 'abc', '6', 'now()' "
|
|
@@ -88,7 +100,13 @@ export declare class SqlValuesCreator {
|
|
|
88
100
|
createValues<T extends {}>(asName: string, values: T[], valuesTypes: Record<string, string | {
|
|
89
101
|
sqlType: string;
|
|
90
102
|
sqlDefault?: string;
|
|
103
|
+
assertJsType?: AssertJsType;
|
|
91
104
|
}>): SqlSelectable<T>;
|
|
92
105
|
}
|
|
106
|
+
/** @public */
|
|
107
|
+
export type ColumnToValueConfig = {
|
|
108
|
+
sqlType?: string;
|
|
109
|
+
assertJsType?: AssertJsType;
|
|
110
|
+
};
|
|
93
111
|
export {};
|
|
94
112
|
//# sourceMappingURL=sql_value.d.ts.map
|