@snowtop/ent 0.1.0-alpha44 → 0.1.0-alpha50

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.
@@ -5,7 +5,7 @@ export interface ActionOptions<T extends Ent, TData extends Data> {
5
5
  input?: TData;
6
6
  operation?: WriteOperation;
7
7
  }
8
- interface EntBuilder<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data> extends Builder<TEnt, TViewer> {
8
+ export interface EntBuilder<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data> extends Builder<TEnt, TViewer> {
9
9
  valid(): Promise<boolean>;
10
10
  validX(): Promise<void>;
11
11
  save(): Promise<void>;
@@ -33,9 +33,10 @@ export declare class BaseAction<TEnt extends Ent<TViewer>, TViewer extends Viewe
33
33
  saveX(): Promise<TEnt>;
34
34
  getInput(): TInput;
35
35
  }
36
- interface BuilderConstructor<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data> {
36
+ export interface BuilderConstructor<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data> {
37
37
  new (viewer: TViewer, operation: WriteOperation, action: Action<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>, existingEnt: TEnt | null): EntBuilder<TEnt, TViewer, TInput>;
38
38
  }
39
39
  export declare function updateRawObject<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, existingEnt: TEnt, input: TInput): Promise<TEnt>;
40
- export declare function getSimpleEditAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, existingEnt: TEnt, input: TInput): Action<TEnt, Builder<TEnt, TViewer>, TViewer, TInput>;
41
- export {};
40
+ export declare function getSimpleEditAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, existingEnt: TEnt, input: TInput): BaseAction<TEnt, TViewer, TInput>;
41
+ export declare function getSimpleDeleteAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, existingEnt: TEnt, input: TInput): BaseAction<TEnt, TViewer, TInput>;
42
+ export declare function getSimpleInsertAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, input: TInput): BaseAction<TEnt, TViewer, TInput>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSimpleEditAction = exports.updateRawObject = exports.BaseAction = void 0;
3
+ exports.getSimpleInsertAction = exports.getSimpleDeleteAction = exports.getSimpleEditAction = exports.updateRawObject = exports.BaseAction = void 0;
4
4
  const privacy_1 = require("../core/privacy");
5
5
  const action_1 = require("./action");
6
6
  class BaseAction {
@@ -85,6 +85,7 @@ async function updateRawObject(viewer, builderCtr, existingEnt, input) {
85
85
  return action.saveX();
86
86
  }
87
87
  exports.updateRawObject = updateRawObject;
88
+ // TODO need to fix types for all these
88
89
  // creates an action which has no privacy, triggers, observers etc
89
90
  // does do field validation
90
91
  // useful to batch a bunch of writes together with BaseAction.bulkAction
@@ -97,3 +98,18 @@ function getSimpleEditAction(viewer, builderCtr, existingEnt, input) {
97
98
  });
98
99
  }
99
100
  exports.getSimpleEditAction = getSimpleEditAction;
101
+ function getSimpleDeleteAction(viewer, builderCtr, existingEnt, input) {
102
+ return new BaseAction(viewer, builderCtr, {
103
+ existingEnt: existingEnt,
104
+ operation: action_1.WriteOperation.Delete,
105
+ input,
106
+ });
107
+ }
108
+ exports.getSimpleDeleteAction = getSimpleDeleteAction;
109
+ function getSimpleInsertAction(viewer, builderCtr, input) {
110
+ return new BaseAction(viewer, builderCtr, {
111
+ operation: action_1.WriteOperation.Insert,
112
+ input,
113
+ });
114
+ }
115
+ exports.getSimpleInsertAction = getSimpleInsertAction;
@@ -85,6 +85,12 @@ const addCustomType = (type) => {
85
85
  }
86
86
  }
87
87
  catch (e) {
88
+ if (type.secondaryImportPath) {
89
+ (0, exports.addCustomType)({
90
+ ...type,
91
+ importPath: type.secondaryImportPath,
92
+ });
93
+ }
88
94
  return;
89
95
  }
90
96
  GQLCapture.getCustomTypes().set(type.type, type);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha44",
3
+ "version": "0.1.0-alpha50",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/schema/field.d.ts CHANGED
@@ -118,26 +118,53 @@ export declare class DateField extends BaseField implements Field {
118
118
  format(val: any): any;
119
119
  }
120
120
  export declare function DateType(options?: FieldOptions): DateField;
121
- declare type EnumMap = {
121
+ declare type StringEnumMap = {
122
122
  [key: string]: string;
123
123
  };
124
+ /**
125
+ * @deprecated use StringEnumOptions
126
+ */
124
127
  export interface EnumOptions extends FieldOptions {
125
128
  values?: string[];
126
- map?: EnumMap;
129
+ map?: StringEnumMap;
127
130
  tsType?: string;
128
131
  graphQLType?: string;
129
132
  createEnumType?: boolean;
130
133
  }
134
+ export interface StringEnumOptions extends EnumOptions {
135
+ }
136
+ /**
137
+ * @deprecated Use StringEnumField
138
+ */
131
139
  export declare class EnumField extends BaseField implements Field {
132
140
  type: Type;
133
141
  private values?;
134
142
  private map?;
135
- constructor(options: EnumOptions);
143
+ constructor(options: StringEnumOptions);
136
144
  convertForGQL(value: string): string;
137
145
  valid(val: any): boolean;
138
146
  format(val: any): any;
139
147
  }
140
- export declare function EnumType(options: EnumOptions): EnumField;
148
+ export declare class StringEnumField extends EnumField {
149
+ }
150
+ export declare function EnumType(options: StringEnumOptions): EnumField;
151
+ declare type IntEnumMap = {
152
+ [key: string]: number;
153
+ };
154
+ export interface IntegerEnumOptions extends FieldOptions {
155
+ map: IntEnumMap;
156
+ deprecated?: IntEnumMap;
157
+ tsType?: string;
158
+ graphQLType?: string;
159
+ }
160
+ export declare class IntegerEnumField extends BaseField implements Field {
161
+ type: Type;
162
+ private map;
163
+ constructor(options: IntegerEnumOptions);
164
+ valid(val: any): boolean;
165
+ format(val: any): any;
166
+ }
167
+ export declare function IntegerEnumType(options: IntegerEnumOptions): IntegerEnumField;
141
168
  export declare class ListField extends BaseField {
142
169
  private field;
143
170
  type: Type;
@@ -163,6 +190,7 @@ export declare function TimestamptzListType(options?: TimestampOptions): ListFie
163
190
  export declare function TimeListType(options?: TimeOptions): ListField;
164
191
  export declare function TimetzListType(options: TimeOptions): ListField;
165
192
  export declare function DateListType(options?: FieldOptions): ListField;
166
- export declare function EnumListType(options: EnumOptions): ListField;
193
+ export declare function EnumListType(options: StringEnumOptions): ListField;
194
+ export declare function IntegerEnumListType(options: IntegerEnumOptions): ListField;
167
195
  export declare function UUIDListType(options?: FieldOptions): ListField;
168
196
  export {};
package/schema/field.js CHANGED
@@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.UUIDListType = 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.EnumType = 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;
22
+ 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;
23
23
  const luxon_1 = require("luxon");
24
24
  const snake_case_1 = require("snake-case");
25
25
  const db_1 = __importStar(require("../core/db"));
@@ -444,6 +444,9 @@ function DateType(options) {
444
444
  return Object.assign(result, options);
445
445
  }
446
446
  exports.DateType = DateType;
447
+ /**
448
+ * @deprecated Use StringEnumField
449
+ */
447
450
  class EnumField extends BaseField {
448
451
  constructor(options) {
449
452
  super();
@@ -546,11 +549,57 @@ class EnumField extends BaseField {
546
549
  }
547
550
  }
548
551
  exports.EnumField = EnumField;
552
+ class StringEnumField extends EnumField {
553
+ }
554
+ exports.StringEnumField = StringEnumField;
549
555
  function EnumType(options) {
550
- let result = new EnumField(options);
556
+ let result = new StringEnumField(options);
551
557
  return Object.assign(result, options);
552
558
  }
553
559
  exports.EnumType = EnumType;
560
+ class IntegerEnumField extends BaseField {
561
+ constructor(options) {
562
+ super();
563
+ this.type = {
564
+ dbType: schema_1.DBType.IntEnum,
565
+ intEnumMap: options.map,
566
+ type: options.tsType,
567
+ graphQLType: options.graphQLType,
568
+ deprecatedIntEnumMap: options.deprecated,
569
+ };
570
+ let count = 0;
571
+ for (const k in options.map) {
572
+ count++;
573
+ break;
574
+ }
575
+ if (!count) {
576
+ throw new Error("need at least one entry in enum map");
577
+ }
578
+ if (options.foreignKey) {
579
+ throw new Error(`foreignKey on intEnum not supported`);
580
+ }
581
+ this.map = options.map;
582
+ }
583
+ valid(val) {
584
+ // lookup table enum and indicated via presence of foreignKey
585
+ for (const k in this.map) {
586
+ const v = this.map[k];
587
+ if (v === val || v === parseInt(val)) {
588
+ return true;
589
+ }
590
+ }
591
+ return false;
592
+ }
593
+ format(val) {
594
+ return parseInt(val);
595
+ }
596
+ }
597
+ exports.IntegerEnumField = IntegerEnumField;
598
+ function IntegerEnumType(options) {
599
+ let result = new IntegerEnumField(options);
600
+ return Object.assign(result, options);
601
+ }
602
+ exports.IntegerEnumType = IntegerEnumType;
554
603
  class ListField extends BaseField {
555
604
  constructor(field, options) {
556
605
  super();
@@ -720,6 +769,14 @@ function EnumListType(options) {
720
769
  return new ListField(EnumType(options), options);
721
770
  }
722
771
  exports.EnumListType = EnumListType;
772
+ function IntegerEnumListType(options) {
773
+ // not all of these will make sense in a list...
774
+ // can make it work eventually but involves work we're not currently trying to do
775
+ // developer can try to work around it by calling below on their own.
776
+ // unclear what the behavior is
777
+ return new ListField(IntegerEnumType(options), options);
778
+ }
779
+ exports.IntegerEnumListType = IntegerEnumListType;
723
780
  function UUIDListType(options) {
724
781
  return new ListField(UUIDType(options), options);
725
782
  }
@@ -107,6 +107,7 @@ export declare enum DBType {
107
107
  JSONB = "JSONB",
108
108
  Enum = "Enum",
109
109
  StringEnum = "StringEnum",
110
+ IntEnum = "IntEnum",
110
111
  Date = "Date",
111
112
  Time = "Time",
112
113
  Timetz = "Timetz",
@@ -120,6 +121,9 @@ export interface ImportType {
120
121
  declare type EnumMap = {
121
122
  [key: string]: string;
122
123
  };
124
+ declare type IntEnumMap = {
125
+ [key: string]: number;
126
+ };
123
127
  export interface Type {
124
128
  dbType: DBType;
125
129
  listElemType?: Type;
@@ -127,6 +131,8 @@ export interface Type {
127
131
  graphQLType?: string;
128
132
  values?: string[];
129
133
  enumMap?: EnumMap;
134
+ intEnumMap?: IntEnumMap;
135
+ deprecatedIntEnumMap?: IntEnumMap;
130
136
  importType?: ImportType;
131
137
  subFields?: FieldMap;
132
138
  unionFields?: FieldMap;
package/schema/schema.js CHANGED
@@ -35,6 +35,7 @@ var DBType;
35
35
  DBType["JSONB"] = "JSONB";
36
36
  DBType["Enum"] = "Enum";
37
37
  DBType["StringEnum"] = "StringEnum";
38
+ DBType["IntEnum"] = "IntEnum";
38
39
  DBType["Date"] = "Date";
39
40
  DBType["Time"] = "Time";
40
41
  DBType["Timetz"] = "Timetz";
@@ -83,12 +83,6 @@ class StructField extends field_1.BaseField {
83
83
  }
84
84
  promises.push(field.valid(val));
85
85
  }
86
- for (const k in obj) {
87
- // extra undefined fields are invalid
88
- if (this.options.fields[k] === undefined) {
89
- return false;
90
- }
91
- }
92
86
  if (!valid) {
93
87
  return valid;
94
88
  }
@@ -235,7 +235,9 @@ async function main() {
235
235
  // if this list grows too long, need to build this into golang types and passed here
236
236
  // TODO foreign non-scalars eventually
237
237
  (0, graphql_1.addCustomType)({
238
- importPath: "../graphql/scalars/time",
238
+ importPath: MODULE_PATH,
239
+ // for go tests...
240
+ secondaryImportPath: "../graphql/scalars/time",
239
241
  type: "GraphQLTime",
240
242
  });
241
243
  (0, graphql_1.addCustomType)({