@postxl/generator 0.73.5 → 0.73.6

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.
@@ -18,6 +18,10 @@ export declare const toPascalCase: (str: string) => string;
18
18
  * Returns a pluralized version of the given string based on the count.
19
19
  */
20
20
  export declare const pluralize: (s: string, count?: number) => string;
21
+ /**
22
+ * Returns true if the given string is already pluralized.
23
+ */
24
+ export declare const isPlural: (s: string) => boolean;
21
25
  /**
22
26
  * Converts each line of a string to a commented line
23
27
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.conjugateNames = exports.commentLines = exports.pluralize = exports.toPascalCase = exports.toCamelCase = exports.capitalize = exports.uncapitalize = void 0;
3
+ exports.conjugateNames = exports.commentLines = exports.isPlural = exports.pluralize = exports.toPascalCase = exports.toCamelCase = exports.capitalize = exports.uncapitalize = void 0;
4
4
  /**
5
5
  * Returns the same string with a lowercase first letter.
6
6
  */
@@ -88,6 +88,9 @@ const pluralize = (s, count = 2) => {
88
88
  return s.slice(0, -singular.length + 1) + plural.slice(1);
89
89
  }
90
90
  }
91
+ if (lower.endsWith('ss')) {
92
+ return s.slice(0, -2) + 'sses';
93
+ }
91
94
  if (s.endsWith('y') &&
92
95
  !s.endsWith('ay') &&
93
96
  !s.endsWith('ey') &&
@@ -102,6 +105,14 @@ const pluralize = (s, count = 2) => {
102
105
  return s + 's';
103
106
  };
104
107
  exports.pluralize = pluralize;
108
+ /**
109
+ * Returns true if the given string is already pluralized.
110
+ */
111
+ const isPlural = (s) => {
112
+ const plural = (0, exports.pluralize)(s);
113
+ return plural === s;
114
+ };
115
+ exports.isPlural = isPlural;
105
116
  /**
106
117
  * Converts each line of a string to a commented line
107
118
  */
@@ -34,6 +34,7 @@ const attributes_1 = require("./attributes");
34
34
  */
35
35
  function parsePrismaSchema({ datamodel: { enums: enumsRaw, models: modelsRaw }, config, }) {
36
36
  ensurePXLSystemModelsExist(modelsRaw);
37
+ ensureConsistency({ models: modelsRaw, enums: enumsRaw });
37
38
  // NOTE: We preprocess models and enums so that we can populate relationships.
38
39
  const models = modelsRaw.map((dmmfModel) => parseModelCore({ dmmfModel, config }));
39
40
  const enums = enumsRaw.map((dmmfEnum) => parseEnum({ dmmfEnum, config }));
@@ -72,6 +73,56 @@ function ensurePXLSystemModelsExist(models) {
72
73
  }
73
74
  }
74
75
  }
76
+ /**
77
+ * Validates:
78
+ * - That there are no duplicate model names
79
+ * - That model names are singular
80
+ * - That model attributes are valid
81
+ * - That field attributes are valid
82
+ * - That enum attributes are valid
83
+ */
84
+ function ensureConsistency({ models, enums }) {
85
+ const errors = [];
86
+ const modelNames = models.map((m) => m.name);
87
+ const duplicateModelName = modelNames.find((name, i) => modelNames.indexOf(name) !== i);
88
+ if (duplicateModelName) {
89
+ errors.push(`Model ${duplicateModelName} is defined more than once.`);
90
+ }
91
+ for (const model of models) {
92
+ if ((0, string_1.isPlural)(model.name)) {
93
+ errors.push(`Model ${(0, logger_1.highlight)(model.name)} is plural. Please use singular names for models.`);
94
+ }
95
+ try {
96
+ (0, attributes_1.getModelAttributes)(model);
97
+ }
98
+ catch (e) {
99
+ errors.push(`Model ${(0, logger_1.highlight)(model.name)} has invalid model attributes: ${(0, error_1.extractError)(e)}`);
100
+ }
101
+ }
102
+ for (const model of models) {
103
+ for (const field of model.fields) {
104
+ try {
105
+ (0, attributes_1.getFieldAttributes)(field);
106
+ }
107
+ catch (e) {
108
+ errors.push(`Model ${(0, logger_1.highlight)(model.name)} has invalid attributes for field ${(0, logger_1.highlight)(field.name)}:
109
+ ${(0, error_1.extractError)(e)}`);
110
+ }
111
+ }
112
+ }
113
+ for (const enumDef of enums) {
114
+ try {
115
+ (0, attributes_1.getEnumAttributes)(enumDef);
116
+ }
117
+ catch (e) {
118
+ errors.push(`Enum ${(0, logger_1.highlight)(enumDef.name)} has invalid attributes:
119
+ ${(0, error_1.extractError)(e)}`);
120
+ }
121
+ }
122
+ if (errors.length > 0) {
123
+ (0, error_1.throwError)(`${errors.length} ${(0, string_1.pluralize)('issue', errors.length)} detected in schema:\n * ${errors.join('\n * ')}`);
124
+ }
125
+ }
75
126
  function isModelNotIgnored(model) {
76
127
  return model !== undefined && !model.attributes.ignore;
77
128
  }
@@ -79,6 +130,9 @@ function isModelNotIgnored(model) {
79
130
  * Parses the core properties of a model without fields.
80
131
  */
81
132
  function parseModelCore({ dmmfModel, config, }) {
133
+ if ((0, string_1.isPlural)(dmmfModel.name)) {
134
+ (0, error_1.throwError)(`Model ${dmmfModel.name} is plural. Please use singular names for models.`);
135
+ }
82
136
  const attributes = (0, attributes_1.getModelAttributes)(dmmfModel);
83
137
  return {
84
138
  name: Types.toModelName((0, string_1.toPascalCase)(dmmfModel.name)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.73.5",
3
+ "version": "0.73.6",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {