@snowtop/ent 0.0.21 → 0.0.22
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/core/convert.d.ts +2 -0
- package/core/convert.js +9 -1
- package/package.json +1 -1
- package/schema/field.d.ts +1 -0
- package/schema/field.js +32 -9
- package/schema/json_field.d.ts +3 -1
- package/schema/json_field.js +9 -1
package/core/convert.d.ts
CHANGED
|
@@ -10,3 +10,5 @@ export declare function convertBoolList(val: any): boolean[];
|
|
|
10
10
|
export declare function convertNullableBoolList(val: any): boolean[] | null;
|
|
11
11
|
export declare function convertJSON(val: any): any;
|
|
12
12
|
export declare function convertNullableJSON(val: any): any | null;
|
|
13
|
+
export declare function convertJSONList(val: any): boolean[];
|
|
14
|
+
export declare function convertNullableJSONList(val: any): any[] | null;
|
package/core/convert.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertNullableJSON = exports.convertJSON = exports.convertNullableBoolList = exports.convertBoolList = exports.convertNullableDateList = exports.convertDateList = exports.convertNullableList = exports.convertList = exports.convertNullableBool = exports.convertBool = exports.convertNullableDate = exports.convertDate = void 0;
|
|
3
|
+
exports.convertNullableJSONList = exports.convertJSONList = exports.convertNullableJSON = exports.convertJSON = exports.convertNullableBoolList = exports.convertBoolList = exports.convertNullableDateList = exports.convertDateList = exports.convertNullableList = exports.convertList = exports.convertNullableBool = exports.convertBool = exports.convertNullableDate = exports.convertDate = void 0;
|
|
4
4
|
const luxon_1 = require("luxon");
|
|
5
5
|
// these are needed to deal with SQLite having different types stored in the db vs the representation
|
|
6
6
|
// gotten back from the db/needed in ent land
|
|
@@ -95,3 +95,11 @@ function convertNullableJSON(val) {
|
|
|
95
95
|
return convertJSON(val);
|
|
96
96
|
}
|
|
97
97
|
exports.convertNullableJSON = convertNullableJSON;
|
|
98
|
+
function convertJSONList(val) {
|
|
99
|
+
return convertList(val, convertJSON);
|
|
100
|
+
}
|
|
101
|
+
exports.convertJSONList = convertJSONList;
|
|
102
|
+
function convertNullableJSONList(val) {
|
|
103
|
+
return convertNullableList(val, convertJSON);
|
|
104
|
+
}
|
|
105
|
+
exports.convertNullableJSONList = convertNullableJSONList;
|
package/package.json
CHANGED
package/schema/field.d.ts
CHANGED
|
@@ -128,6 +128,7 @@ export declare class ListField extends BaseField {
|
|
|
128
128
|
constructor(field: Field, options: FieldOptions);
|
|
129
129
|
validate(validator: (val: any[]) => boolean): this;
|
|
130
130
|
valid(val: any): boolean;
|
|
131
|
+
private postgresVal;
|
|
131
132
|
format(val: any): any;
|
|
132
133
|
minLen(l: number): this;
|
|
133
134
|
maxLen(l: number): this;
|
package/schema/field.js
CHANGED
|
@@ -521,22 +521,45 @@ class ListField extends BaseField {
|
|
|
521
521
|
}
|
|
522
522
|
return true;
|
|
523
523
|
}
|
|
524
|
+
postgresVal(val, jsonType) {
|
|
525
|
+
if (!jsonType) {
|
|
526
|
+
return val;
|
|
527
|
+
}
|
|
528
|
+
return JSON.stringify(val);
|
|
529
|
+
}
|
|
524
530
|
format(val) {
|
|
525
531
|
if (!Array.isArray(val)) {
|
|
526
532
|
throw new Error(`need an array to format`);
|
|
527
533
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
534
|
+
const elemDBType = this.type.listElemType.dbType;
|
|
535
|
+
const jsonType = elemDBType === "JSON" || elemDBType === "JSONB";
|
|
536
|
+
const postgres = db_1.default.getDialect() === db_1.Dialect.Postgres;
|
|
537
|
+
if (!postgres && !this.field.format) {
|
|
538
|
+
return JSON.stringify(val);
|
|
539
|
+
}
|
|
540
|
+
let ret = [];
|
|
541
|
+
let postgresRet = "{";
|
|
542
|
+
for (let i = 0; i < val.length; i++) {
|
|
543
|
+
let formatted = val[i];
|
|
544
|
+
if (this.field.format) {
|
|
545
|
+
formatted = this.field.format(val[i]);
|
|
546
|
+
}
|
|
547
|
+
// postgres supports arrays natively so we
|
|
548
|
+
// structure it in the expected format
|
|
549
|
+
if (postgres) {
|
|
550
|
+
postgresRet += this.postgresVal(formatted, jsonType);
|
|
551
|
+
if (i !== val.length - 1) {
|
|
552
|
+
postgresRet += ",";
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
ret[i] = formatted;
|
|
531
557
|
}
|
|
532
558
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
if (db_1.default.getDialect() === db_1.Dialect.Postgres) {
|
|
536
|
-
return `{${val.join(",")}}`;
|
|
559
|
+
if (postgres) {
|
|
560
|
+
return postgresRet + "}";
|
|
537
561
|
}
|
|
538
|
-
|
|
539
|
-
return JSON.stringify(val);
|
|
562
|
+
return JSON.stringify(ret);
|
|
540
563
|
}
|
|
541
564
|
minLen(l) {
|
|
542
565
|
return this.validate((val) => val.length >= l);
|
package/schema/json_field.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FieldOptions, Type, Field, ImportType } from "./schema";
|
|
2
|
-
import { BaseField } from "./field";
|
|
2
|
+
import { BaseField, ListField } from "./field";
|
|
3
3
|
export interface JSONOptions extends FieldOptions {
|
|
4
4
|
validator?: (val: any) => boolean;
|
|
5
5
|
importType?: ImportType;
|
|
@@ -13,3 +13,5 @@ export declare class JSONField extends BaseField implements Field {
|
|
|
13
13
|
}
|
|
14
14
|
export declare function JSONType(options: JSONOptions): JSONField;
|
|
15
15
|
export declare function JSONBType(options: JSONOptions): JSONField;
|
|
16
|
+
export declare function JSONBListType(options: JSONOptions): ListField;
|
|
17
|
+
export declare function JSONListType(options: JSONOptions): ListField;
|
package/schema/json_field.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JSONBType = exports.JSONType = exports.JSONField = void 0;
|
|
3
|
+
exports.JSONListType = exports.JSONBListType = exports.JSONBType = exports.JSONType = exports.JSONField = void 0;
|
|
4
4
|
const schema_1 = require("./schema");
|
|
5
5
|
const field_1 = require("./field");
|
|
6
6
|
class JSONField extends field_1.BaseField {
|
|
@@ -38,3 +38,11 @@ function JSONBType(options) {
|
|
|
38
38
|
return Object.assign(result, options);
|
|
39
39
|
}
|
|
40
40
|
exports.JSONBType = JSONBType;
|
|
41
|
+
function JSONBListType(options) {
|
|
42
|
+
return new field_1.ListField(JSONBType(options), options);
|
|
43
|
+
}
|
|
44
|
+
exports.JSONBListType = JSONBListType;
|
|
45
|
+
function JSONListType(options) {
|
|
46
|
+
return new field_1.ListField(JSONType(options), options);
|
|
47
|
+
}
|
|
48
|
+
exports.JSONListType = JSONListType;
|