@snowtop/ent 0.1.0-alpha68 → 0.1.0-alpha74

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha68",
3
+ "version": "0.1.0-alpha74",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -4,14 +4,26 @@ export interface JSONOptions extends FieldOptions {
4
4
  validator?: (val: any) => boolean;
5
5
  importType?: ImportType;
6
6
  }
7
+ interface allJSONOptions extends JSONOptions {
8
+ jsonAsList?: boolean;
9
+ }
7
10
  export declare class JSONField extends BaseField implements Field {
8
11
  private options?;
9
12
  type: Type;
10
- constructor(jsonb: boolean, options?: JSONOptions | undefined);
13
+ constructor(jsonb: boolean, options?: allJSONOptions | undefined);
11
14
  format(val: any): string;
12
15
  valid(val: any): boolean;
13
16
  }
14
17
  export declare function JSONType(options?: JSONOptions): JSONField;
15
18
  export declare function JSONBType(options?: JSONOptions): JSONField;
19
+ /**
20
+ * @deprecated use JSONBTypeAsList
21
+ */
16
22
  export declare function JSONBListType(options?: JSONOptions): ListField;
23
+ /**
24
+ * @deprecated use JSONTypeAsList
25
+ */
17
26
  export declare function JSONListType(options?: JSONOptions): ListField;
27
+ export declare function JSONBTypeAsList(options?: JSONOptions): JSONField & JSONOptions;
28
+ export declare function JSONTypeAsList(options?: JSONOptions): JSONField & JSONOptions;
29
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JSONListType = exports.JSONBListType = exports.JSONBType = exports.JSONType = exports.JSONField = void 0;
3
+ exports.JSONTypeAsList = exports.JSONBTypeAsList = 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 {
@@ -16,6 +16,11 @@ class JSONField extends field_1.BaseField {
16
16
  if (options?.importType) {
17
17
  this.type.importType = options.importType;
18
18
  }
19
+ if (options?.jsonAsList) {
20
+ this.type.listElemType = {
21
+ dbType: schema_1.DBType.JSONB,
22
+ };
23
+ }
19
24
  }
20
25
  format(val) {
21
26
  return JSON.stringify(val);
@@ -38,11 +43,33 @@ function JSONBType(options) {
38
43
  return Object.assign(result, options);
39
44
  }
40
45
  exports.JSONBType = JSONBType;
46
+ /**
47
+ * @deprecated use JSONBTypeAsList
48
+ */
41
49
  function JSONBListType(options) {
42
50
  return new field_1.ListField(JSONBType(options), options);
43
51
  }
44
52
  exports.JSONBListType = JSONBListType;
53
+ /**
54
+ * @deprecated use JSONTypeAsList
55
+ */
45
56
  function JSONListType(options) {
46
57
  return new field_1.ListField(JSONType(options), options);
47
58
  }
48
59
  exports.JSONListType = JSONListType;
60
+ function JSONBTypeAsList(options) {
61
+ let result = new JSONField(true, {
62
+ ...options,
63
+ jsonAsList: true,
64
+ });
65
+ return Object.assign(result, options);
66
+ }
67
+ exports.JSONBTypeAsList = JSONBTypeAsList;
68
+ function JSONTypeAsList(options) {
69
+ let result = new JSONField(false, {
70
+ ...options,
71
+ jsonAsList: true,
72
+ });
73
+ return Object.assign(result, options);
74
+ }
75
+ exports.JSONTypeAsList = JSONTypeAsList;
@@ -6,12 +6,22 @@ export interface StructOptions extends FieldOptions {
6
6
  graphQLType?: string;
7
7
  jsonNotJSONB?: boolean;
8
8
  }
9
+ interface allStructOptions extends StructOptions {
10
+ jsonAsList?: boolean;
11
+ }
9
12
  export declare class StructField extends BaseField implements Field {
10
13
  private options;
11
14
  type: Type;
12
- constructor(options: StructOptions);
15
+ constructor(options: allStructOptions);
16
+ formatImpl(obj: any, nested?: boolean): string | Object;
13
17
  format(obj: any, nested?: boolean): string | Object;
18
+ private validImpl;
14
19
  valid(obj: any): Promise<boolean>;
15
20
  }
16
21
  export declare function StructType(options: StructOptions): StructField & StructOptions;
22
+ /**
23
+ * @deprecated use StructTypeAsList
24
+ */
17
25
  export declare function StructListType(options: StructOptions): ListField;
26
+ export declare function StructTypeAsList(options: allStructOptions): StructField & allStructOptions;
27
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StructListType = exports.StructType = exports.StructField = void 0;
3
+ exports.StructTypeAsList = exports.StructListType = exports.StructType = exports.StructField = void 0;
4
4
  const field_1 = require("./field");
5
5
  const schema_1 = require("./schema");
6
6
  const camel_case_1 = require("camel-case");
@@ -17,11 +17,16 @@ class StructField extends field_1.BaseField {
17
17
  if (options.jsonNotJSONB) {
18
18
  this.type.dbType = schema_1.DBType.JSON;
19
19
  }
20
+ if (options?.jsonAsList) {
21
+ this.type.listElemType = {
22
+ dbType: schema_1.DBType.JSONB,
23
+ };
24
+ }
20
25
  }
21
26
  // right now, we store things in the db in lowerCase format
22
27
  // this will lead to issues if field changes.
23
28
  // TODO: use storageKey and convert back...
24
- format(obj, nested) {
29
+ formatImpl(obj, nested) {
25
30
  if (!(obj instanceof Object)) {
26
31
  throw new Error("valid was not called");
27
32
  }
@@ -47,13 +52,23 @@ class StructField extends field_1.BaseField {
47
52
  ret[dbKey] = val;
48
53
  }
49
54
  }
50
- // don't json.stringify if nested
55
+ // don't json.stringify if nested or list
51
56
  if (nested) {
52
57
  return ret;
53
58
  }
54
59
  return JSON.stringify(ret);
55
60
  }
56
- async valid(obj) {
61
+ format(obj, nested) {
62
+ if (Array.isArray(obj) && this.options.jsonAsList) {
63
+ const ret = obj.map((v) => this.formatImpl(v, true));
64
+ if (nested) {
65
+ return ret;
66
+ }
67
+ return JSON.stringify(ret);
68
+ }
69
+ return this.formatImpl(obj, nested);
70
+ }
71
+ async validImpl(obj) {
57
72
  if (!(obj instanceof Object)) {
58
73
  return false;
59
74
  }
@@ -89,6 +104,19 @@ class StructField extends field_1.BaseField {
89
104
  const ret = await Promise.all(promises);
90
105
  return ret.every((v) => v);
91
106
  }
107
+ async valid(obj) {
108
+ if (this.options.jsonAsList) {
109
+ if (!Array.isArray(obj)) {
110
+ return false;
111
+ }
112
+ const valid = await Promise.all(obj.map((v) => this.validImpl(v)));
113
+ return valid.every((b) => b);
114
+ }
115
+ if (!(obj instanceof Object)) {
116
+ return false;
117
+ }
118
+ return this.validImpl(obj);
119
+ }
92
120
  }
93
121
  exports.StructField = StructField;
94
122
  function StructType(options) {
@@ -96,7 +124,18 @@ function StructType(options) {
96
124
  return Object.assign(result, options);
97
125
  }
98
126
  exports.StructType = StructType;
127
+ /**
128
+ * @deprecated use StructTypeAsList
129
+ */
99
130
  function StructListType(options) {
100
131
  return new field_1.ListField(StructType(options), options);
101
132
  }
102
133
  exports.StructListType = StructListType;
134
+ function StructTypeAsList(options) {
135
+ let result = new StructField({
136
+ ...options,
137
+ jsonAsList: true,
138
+ });
139
+ return Object.assign(result, options);
140
+ }
141
+ exports.StructTypeAsList = StructTypeAsList;