@postxl/generator 0.44.5 → 0.45.0

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.
@@ -6,6 +6,7 @@ const meta_1 = require("../../lib/meta");
6
6
  const fields_1 = require("../../lib/schema/fields");
7
7
  const schema_1 = require("../../lib/schema/schema");
8
8
  const types_1 = require("../../lib/schema/types");
9
+ const types_2 = require("../../lib/types");
9
10
  const jsdoc_1 = require("../../lib/utils/jsdoc");
10
11
  const string_1 = require("../../lib/utils/string");
11
12
  /**
@@ -341,7 +342,7 @@ function _generateMainBuildingBlocks_InMemoryOnly({ model, meta, schemaMeta, imp
341
342
  throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
342
343
  }
343
344
  const mutationId = await execution.startUpdateMutation({
344
- model: 'post',
345
+ model: '${meta.actions.actionScopeConstType}',
345
346
  entityId: item.id,
346
347
  sourceObject: existingItem,
347
348
  updateObject: item,
@@ -639,7 +640,7 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, schemaMeta, import
639
640
  }
640
641
 
641
642
  const mutationId = await execution.startUpdateMutation({
642
- model: 'post',
643
+ model: '${meta.actions.actionScopeConstType}',
643
644
  entityId: item.id,
644
645
  sourceObject: existingItem,
645
646
  updateObject: item,
@@ -788,8 +789,9 @@ function generateUserRepositorySpecificBlocks_InDatabase({ model, meta, imports,
788
789
  from: meta.types.importPath,
789
790
  items: [(0, types_1.toVariableName)('UserRole')],
790
791
  });
792
+ const { rootUserId, rootUserValue } = generateSharedRootUserBlocks({ model, meta, imports });
791
793
  return {
792
- rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}('root')`,
794
+ rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(${rootUserId})`,
793
795
  getterBlock: `
794
796
  // We initialize the root user in the init() function
795
797
  private _rootUser!: ${meta.types.typeName}
@@ -806,16 +808,7 @@ function generateUserRepositorySpecificBlocks_InDatabase({ model, meta, imports,
806
808
  }
807
809
 
808
810
  const rawUser = await this.db.user.create({
809
- data: {
810
- id: ${meta.data.repository.className}.ROOT_USER_ID,
811
- name: 'System',
812
- email: 'system@postxl.com',
813
- role: UserRole.Admin,
814
- login: 'not-set',
815
- familyName: 'System user',
816
- age: 0,
817
- countryId: null,
818
- },
811
+ data: { ${rootUserValue} },
819
812
  })
820
813
  const newRootUser = this.toUser(rawUser)
821
814
  this.set(newRootUser)
@@ -832,12 +825,9 @@ function generateUserRepositorySpecificBlocks_InMemoryOnly({ model, meta, import
832
825
  rootUserInitializeBlock: '',
833
826
  };
834
827
  }
835
- imports.addImport({
836
- from: meta.types.importPath,
837
- items: [(0, types_1.toVariableName)('UserRole')],
838
- });
828
+ const { rootUserId, rootUserValue } = generateSharedRootUserBlocks({ model, meta, imports });
839
829
  return {
840
- rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}('root')`,
830
+ rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(${rootUserId})`,
841
831
  getterBlock: `
842
832
  // We initialize the root user in the init() function
843
833
  private _rootUser!: ${meta.types.typeName}
@@ -853,22 +843,126 @@ function generateUserRepositorySpecificBlocks_InMemoryOnly({ model, meta, import
853
843
  return
854
844
  }
855
845
 
856
- const rawUser = {
857
- id: ${meta.data.repository.className}.ROOT_USER_ID,
858
- name: 'System',
859
- email: 'system@postxl.com',
860
- role: UserRole.Admin,
861
- login: 'not-set',
862
- familyName: 'System user',
863
- age: 0,
864
- countryId: null,
865
- }
846
+ const rawUser = { ${rootUserValue} }
866
847
  const newRootUser = this.verifyItem (rawUser)
867
848
  this.set(newRootUser)
868
849
  this._rootUser = newRootUser
869
850
  }`,
870
851
  };
871
852
  }
853
+ function generateSharedRootUserBlocks({ model, meta, imports, }) {
854
+ var _a;
855
+ const providedDefault = model.attributes.systemUser;
856
+ const assignments = [];
857
+ let rootUserId = '';
858
+ for (const field of model.fields) {
859
+ let value = undefined;
860
+ if (providedDefault && field.name in providedDefault) {
861
+ value = providedDefault[field.name];
862
+ }
863
+ else if ((_a = field.attributes.examples) === null || _a === void 0 ? void 0 : _a.length) {
864
+ value = field.attributes.examples[0];
865
+ }
866
+ else if (!field.isRequired) {
867
+ value = null;
868
+ }
869
+ else if (field.kind === 'id') {
870
+ if (field.unbrandedTypeName === 'string') {
871
+ value = "rootId";
872
+ }
873
+ else if (field.unbrandedTypeName === 'number') {
874
+ value = -1;
875
+ }
876
+ else {
877
+ throw new Error(`Could not generate root user: Unsupported id type ${field.unbrandedTypeName}!`);
878
+ }
879
+ }
880
+ else {
881
+ if (field.isRequired && !field.attributes.isCreatedAt && !field.attributes.isUpdatedAt) {
882
+ throw new Error(`Could not generate root user: No value for field ${field.name} provided!`);
883
+ }
884
+ else {
885
+ continue;
886
+ }
887
+ }
888
+ switch (field.kind) {
889
+ case 'id': {
890
+ if (field.unbrandedTypeName === 'string') {
891
+ value = `'${value}'`;
892
+ }
893
+ else if (field.unbrandedTypeName === 'number') {
894
+ value = `${value}`;
895
+ }
896
+ else {
897
+ throw new Error(`Could not generate root user: Unsupported id type ${field.unbrandedTypeName}!`);
898
+ }
899
+ rootUserId = value;
900
+ break;
901
+ }
902
+ case 'scalar': {
903
+ if (value === null) {
904
+ break;
905
+ }
906
+ switch (field.tsTypeName) {
907
+ case 'string': {
908
+ value = `'${value}'`;
909
+ break;
910
+ }
911
+ case 'number': {
912
+ value = `${value}`;
913
+ break;
914
+ }
915
+ case 'boolean': {
916
+ value = value ? 'true' : 'false';
917
+ break;
918
+ }
919
+ case 'Date': {
920
+ value = `${value}`;
921
+ break;
922
+ }
923
+ default: {
924
+ throw new Error(`Could not generate root user: Unsupported scalar type ${field.tsTypeName}!`);
925
+ }
926
+ }
927
+ break;
928
+ }
929
+ case 'relation': {
930
+ if (value === null) {
931
+ break;
932
+ }
933
+ if (field.unbrandedTypeName === 'string') {
934
+ value = `'${value}'`;
935
+ }
936
+ else if (field.unbrandedTypeName === 'number') {
937
+ value = `${value}`;
938
+ }
939
+ else {
940
+ throw new Error(`Could not generate root user: Unsupported relation type ${field.unbrandedTypeName}!`);
941
+ }
942
+ break;
943
+ }
944
+ case 'enum': {
945
+ if (value === null) {
946
+ break;
947
+ }
948
+ imports.addImport({
949
+ from: meta.types.importPath,
950
+ items: [field.enumerator.name],
951
+ });
952
+ value = `${field.enumerator.name}.${value}`;
953
+ break;
954
+ }
955
+ default: {
956
+ throw new types_2.ExhaustiveSwitchCheck(field);
957
+ }
958
+ }
959
+ assignments.push(`${field.name}: ${value}`);
960
+ }
961
+ return {
962
+ rootUserId,
963
+ rootUserValue: assignments.join(', '),
964
+ };
965
+ }
872
966
  /**
873
967
  * Generates code chunks responsible for verifying the ID validity of a model instance and generating the id
874
968
  * value of a model with auto-generated id.
@@ -31,6 +31,11 @@ export type ModelAttributes = {
31
31
  * Seed to use for random generation.
32
32
  */
33
33
  randomSeed?: number;
34
+ /**
35
+ * Schema tag: ´@@SystemUser()`
36
+ * The user that is used for system actions.
37
+ */
38
+ systemUser?: object;
34
39
  };
35
40
  export type FieldAttributes = {
36
41
  /**
@@ -150,7 +150,7 @@ export declare const toPath: (t: string) => FilePath;
150
150
  /**
151
151
  * Branded string values that can be used as import statement values in the generators
152
152
  */
153
- export type ImportableTypes = FunctionName | ClassName | AnnotatedTypeName | VariableName;
153
+ export type ImportableTypes = FunctionName | ClassName | AnnotatedTypeName | VariableName | EnumName;
154
154
  /**
155
155
  * Branded string values that can be used as paths in import statements
156
156
  */
@@ -74,6 +74,10 @@ function getModelAttributes(model) {
74
74
  schema: zod_1.default.string().optional(),
75
75
  index: zod_1.default.array(zod_1.default.string()).optional(),
76
76
  seed: zod_1.default.string().optional(),
77
+ systemUser: zod_1.default
78
+ .string()
79
+ .transform((t) => JSON.parse(t))
80
+ .optional(),
77
81
  })
78
82
  .transform((obj) => ({
79
83
  ignore: obj.ignore,
@@ -81,6 +85,7 @@ function getModelAttributes(model) {
81
85
  description: obj.description,
82
86
  databaseSchema: obj.schema,
83
87
  index: obj.index,
88
+ systemUser: obj.systemUser,
84
89
  randomSeed: obj.seed !== undefined ? parseInt(obj.seed, 10) : undefined,
85
90
  }));
86
91
  const result = decoder.safeParse(attributes);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.44.5",
3
+ "version": "0.45.0",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {