@snowtop/ent 0.1.0-alpha91 → 0.1.0-alpha92-7ebc5680-4591-11ed-b331-8b0a5597e9d8

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-alpha91",
3
+ "version": "0.1.0-alpha92-7ebc5680-4591-11ed-b331-8b0a5597e9d8",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/schema/field.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Builder } from "../action/action";
1
2
  import { Field, FieldMap, FieldOptions, ForeignKey, PolymorphicOptions, Type } from "./schema";
2
3
  export declare abstract class BaseField {
3
4
  name: string;
@@ -88,6 +89,18 @@ export declare class StringField extends BaseField implements Field {
88
89
  trimLeft(): this;
89
90
  trimRight(): this;
90
91
  }
92
+ interface PolymorphicStringOptions extends StringOptions {
93
+ parentFieldToValidate: string;
94
+ types?: string[];
95
+ }
96
+ export declare class PolymorphicStringField extends StringField {
97
+ private opts;
98
+ private camelCaseVals;
99
+ constructor(opts: PolymorphicStringOptions);
100
+ validateWithFullData(val: any, b: Builder<any>): boolean;
101
+ valid(val: any): boolean;
102
+ format(val: any): any;
103
+ }
91
104
  export declare function StringType(options?: StringOptions): StringField;
92
105
  export interface TimestampOptions extends FieldOptions {
93
106
  withTimezone?: boolean;
package/schema/field.js CHANGED
@@ -23,9 +23,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.UUIDListType = exports.IntegerEnumListType = exports.EnumListType = exports.DateListType = exports.TimetzListType = exports.TimeListType = exports.TimestamptzListType = exports.TimestampListType = exports.BooleanListType = exports.BigIntegerListType = exports.FloatListType = exports.IntegerListType = exports.IntListType = exports.StringListType = exports.ListField = exports.IntegerEnumType = exports.IntegerEnumField = exports.EnumType = exports.StringEnumField = exports.EnumField = exports.DateType = exports.DateField = exports.TimetzType = exports.TimeType = exports.TimeField = exports.leftPad = exports.TimestamptzType = exports.TimestampType = exports.TimestampField = exports.StringType = exports.StringField = exports.BooleanType = exports.BooleanField = exports.FloatType = exports.FloatField = exports.BigIntegerType = exports.BigIntegerField = exports.IntegerType = exports.IntegerField = exports.UUIDType = exports.UUIDField = exports.BaseField = void 0;
26
+ exports.UUIDListType = exports.IntegerEnumListType = exports.EnumListType = exports.DateListType = exports.TimetzListType = exports.TimeListType = exports.TimestamptzListType = exports.TimestampListType = exports.BooleanListType = exports.BigIntegerListType = exports.FloatListType = exports.IntegerListType = exports.IntListType = exports.StringListType = exports.ListField = exports.IntegerEnumType = exports.IntegerEnumField = exports.EnumType = exports.StringEnumField = exports.EnumField = exports.DateType = exports.DateField = exports.TimetzType = exports.TimeType = exports.TimeField = exports.leftPad = exports.TimestamptzType = exports.TimestampType = exports.TimestampField = exports.StringType = exports.PolymorphicStringField = exports.StringField = exports.BooleanType = exports.BooleanField = exports.FloatType = exports.FloatField = exports.BigIntegerType = exports.BigIntegerField = exports.IntegerType = exports.IntegerField = exports.UUIDType = exports.UUIDField = exports.BaseField = void 0;
27
27
  const luxon_1 = require("luxon");
28
28
  const snake_case_1 = require("snake-case");
29
+ const camel_case_1 = require("camel-case");
29
30
  const util_1 = require("util");
30
31
  const uuid_1 = require("uuid");
31
32
  const base_1 = require("../core/base");
@@ -66,38 +67,26 @@ class UUIDField extends BaseField {
66
67
  else {
67
68
  throw new Error(`unsupported id polymorhpic type ${fieldName}`);
68
69
  }
70
+ let types;
71
+ let serverDefault = undefined;
72
+ if (typeof polymorphic === "object") {
73
+ serverDefault = polymorphic.serverDefault;
74
+ types = polymorphic.types;
75
+ }
69
76
  // polymorphic field automatically hidden from GraphQL
70
77
  // can be made visible with custom fields if user wants to change this behavior
71
78
  // can't be foreignKey so need to make other changes to the field
72
79
  // intentionally not made private as it doesn't seem like it needs to be hidden
73
- if (typeof polymorphic === "object" && polymorphic.types) {
74
- // an enum with types validated here
75
- return {
76
- [name]: PolymorphicStringEnumType({
77
- values: polymorphic.types,
78
- hideFromGraphQL: true,
79
- derivedWhenEmbedded: true,
80
- nullable: this.options?.nullable,
81
- parentFieldToValidate: fieldName,
82
- serverDefault: polymorphic.serverDefault,
83
- }),
84
- };
85
- }
86
- else {
87
- let serverDefault = undefined;
88
- if (typeof polymorphic === "object") {
89
- serverDefault = polymorphic.serverDefault;
90
- }
91
- return {
92
- [name]: PolymorphicStringType({
93
- hideFromGraphQL: true,
94
- derivedWhenEmbedded: true,
95
- nullable: this.options?.nullable,
96
- parentFieldToValidate: fieldName,
97
- serverDefault: serverDefault,
98
- }),
99
- };
100
- }
80
+ return {
81
+ [name]: PolymorphicStringType({
82
+ types: types,
83
+ hideFromGraphQL: true,
84
+ derivedWhenEmbedded: true,
85
+ nullable: this.options?.nullable,
86
+ parentFieldToValidate: fieldName,
87
+ serverDefault: serverDefault,
88
+ }),
89
+ };
101
90
  }
102
91
  return {};
103
92
  }
@@ -323,29 +312,54 @@ class StringField extends BaseField {
323
312
  }
324
313
  }
325
314
  exports.StringField = StringField;
326
- function validatePolymorphicTypeWithFullData(val, b, field) {
327
- const input = b.getInput();
328
- const inputKey = b.orchestrator.__getOptions().fieldInfo[field].inputKey;
329
- const v = input[inputKey];
330
- if (val === null) {
331
- // if this is being set to null, ok if v is also null
332
- return v === null;
333
- }
334
- // if this is not being set, ok if v is not being set
335
- if (val === undefined && b.operation === base_1.WriteOperation.Insert) {
336
- return v === undefined;
337
- }
338
- return true;
339
- }
340
315
  class PolymorphicStringField extends StringField {
341
316
  constructor(opts) {
342
317
  super(opts);
343
318
  this.opts = opts;
319
+ if (opts.types) {
320
+ this.camelCaseVals = opts.types.map((v) => (0, camel_case_1.camelCase)(v));
321
+ }
344
322
  }
345
323
  validateWithFullData(val, b) {
346
- return validatePolymorphicTypeWithFullData(val, b, this.opts.parentFieldToValidate);
324
+ const input = b.getInput();
325
+ const inputKey = b.orchestrator.__getOptions().fieldInfo[this.opts.parentFieldToValidate]
326
+ .inputKey;
327
+ const v = input[inputKey];
328
+ if (val === null) {
329
+ // if this is being set to null, ok if v is also null
330
+ return v === null;
331
+ }
332
+ // if this is not being set, ok if v is not being set
333
+ if (val === undefined && b.operation === base_1.WriteOperation.Insert) {
334
+ return v === undefined;
335
+ }
336
+ return true;
337
+ }
338
+ valid(val) {
339
+ if (!this.camelCaseVals) {
340
+ return true;
341
+ }
342
+ let str = (0, camel_case_1.camelCase)(String(val));
343
+ // allow different cases because it could be coming from different clients who don't have strong typing
344
+ return this.camelCaseVals.some((value) => value === str);
345
+ }
346
+ format(val) {
347
+ if (!this.camelCaseVals) {
348
+ return val;
349
+ }
350
+ const converted = (0, camel_case_1.camelCase)(String(val));
351
+ for (const v of this.camelCaseVals) {
352
+ if (v === val) {
353
+ return val;
354
+ }
355
+ if (converted === v) {
356
+ return converted;
357
+ }
358
+ }
359
+ return val;
347
360
  }
348
361
  }
362
+ exports.PolymorphicStringField = PolymorphicStringField;
349
363
  function PolymorphicStringType(opts) {
350
364
  let result = new PolymorphicStringField(opts);
351
365
  return Object.assign(result, opts);
@@ -596,19 +610,6 @@ exports.EnumField = EnumField;
596
610
  class StringEnumField extends EnumField {
597
611
  }
598
612
  exports.StringEnumField = StringEnumField;
599
- class PolymorphicStringEnumField extends StringEnumField {
600
- constructor(opts) {
601
- super(opts);
602
- this.opts = opts;
603
- }
604
- validateWithFullData(val, b) {
605
- return validatePolymorphicTypeWithFullData(val, b, this.opts.parentFieldToValidate);
606
- }
607
- }
608
- function PolymorphicStringEnumType(options) {
609
- let result = new PolymorphicStringEnumField(options);
610
- return Object.assign(result, options);
611
- }
612
613
  function EnumType(options) {
613
614
  let result = new StringEnumField(options);
614
615
  return Object.assign(result, options);
@@ -23,27 +23,28 @@ class StructField extends field_1.BaseField {
23
23
  };
24
24
  }
25
25
  }
26
- // right now, we store things in the db in lowerCase format
27
- // this will lead to issues if field changes.
28
- // TODO: use storageKey and convert back...
29
26
  formatImpl(obj, nested) {
30
27
  if (!(obj instanceof Object)) {
31
28
  throw new Error("valid was not called");
32
29
  }
33
30
  let ret = {};
34
31
  for (const k in this.options.fields) {
32
+ const field = this.options.fields[k];
33
+ // check two values
34
+ // wait, two different things
35
+ // getStoragetKey and getCamelCase()
36
+ // for weird graphql case and for typescript format...
37
+ // store in dbKey format
35
38
  // TODO more #510
36
- let dbKey = (0, camel_case_1.camelCase)(k);
37
- let val = obj[dbKey];
38
- // for tests with snake_case
39
- if (val === undefined && obj[k] !== undefined) {
40
- val = obj[k];
41
- dbKey = k;
39
+ let dbKey = (0, schema_1.getStorageKey)(field, k);
40
+ let camelKey = (0, camel_case_1.camelCase)(k);
41
+ let val = obj[camelKey];
42
+ if (val === undefined && obj[dbKey] !== undefined) {
43
+ val = obj[dbKey];
42
44
  }
43
45
  if (val === undefined) {
44
46
  continue;
45
47
  }
46
- const field = this.options.fields[k];
47
48
  if (field.format) {
48
49
  // indicate nested so this isn't JSON stringified
49
50
  ret[dbKey] = field.format(val, true);
@@ -78,12 +79,11 @@ class StructField extends field_1.BaseField {
78
79
  for (const k in this.options.fields) {
79
80
  const field = this.options.fields[k];
80
81
  // TODO more #510
81
- let dbKey = (0, camel_case_1.camelCase)(k);
82
- let val = obj[dbKey];
83
- // for tests with snake_case
84
- if (val === undefined && obj[k] !== undefined) {
85
- val = obj[k];
86
- dbKey = k;
82
+ let dbKey = (0, schema_1.getStorageKey)(field, k);
83
+ let camelKey = (0, camel_case_1.camelCase)(k);
84
+ let val = obj[camelKey];
85
+ if (val === undefined && obj[dbKey] !== undefined) {
86
+ val = obj[dbKey];
87
87
  }
88
88
  if (val === undefined || val === null) {
89
89
  // nullable, nothing to do here