@postxl/generator 0.7.0 → 0.7.1

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.
@@ -12,6 +12,7 @@ function generateRepository({ model, meta }) {
12
12
  const { idField, fields } = model;
13
13
  const decoder = meta.data.repository.decoderFnName;
14
14
  const uniqueStringFields = fields.filter(fields_1.isUniqueStringField);
15
+ const maxLengthStringFields = fields.filter(fields_1.isMaxLengthStringField);
15
16
  const defaultValueInitFn = `
16
17
  if (item.${(_a = model.defaultField) === null || _a === void 0 ? void 0 : _a.name}) {
17
18
  if (this.defaultValue) {
@@ -36,7 +37,7 @@ function generateRepository({ model, meta }) {
36
37
  return `
37
38
  import { Injectable, Logger } from '@nestjs/common'
38
39
  import { DbService, ${model.sourceName} as DbType } from '@${model.schemaConfig.project}/db'
39
- import { format, pluralize } from '@${model.schemaConfig.project}/common'
40
+ import { format, pluralize } from '@pxl/common'
40
41
 
41
42
  import { Repository } from '../repository.type'
42
43
 
@@ -145,6 +146,8 @@ export class ${meta.data.repositoryClassName} implements Repository<
145
146
  }`
146
147
  : ''}
147
148
 
149
+ ${maxLengthStringFields.map((f) => `this.${getEnsureMaxLengthFnName(f)}(item)`).join('\n')}
150
+
148
151
  ${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
149
152
 
150
153
  return {
@@ -156,6 +159,7 @@ export class ${meta.data.repositoryClassName} implements Repository<
156
159
  }): Promise<${model.typeName}> {
157
160
  ${model.attributes.inMemoryOnly
158
161
  ? `
162
+ ${maxLengthStringFields.map((f) => `this.${getEnsureMaxLengthFnName(f)}(item)`).join('\n')}
159
163
  ${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
160
164
  const newItem = await Promise.resolve(item)`
161
165
  : `
@@ -180,6 +184,8 @@ export class ${meta.data.repositoryClassName} implements Repository<
180
184
 
181
185
  ${isGenerated ? `const id = ++this.currentMaxId\n` : ''}
182
186
 
187
+ ${maxLengthStringFields.map((f) => `this.${getEnsureMaxLengthFnName(f)}(item)`).join('\n')}
188
+
183
189
  ${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
184
190
 
185
191
  ${model.attributes.inMemoryOnly
@@ -206,9 +212,10 @@ export class ${meta.data.repositoryClassName} implements Repository<
206
212
  }
207
213
 
208
214
  public async createMany(items: ${model.typeName}[]) {
209
- ${uniqueStringFields.length > 0
215
+ ${uniqueStringFields.length > 0 || maxLengthStringFields.length > 0
210
216
  ? `
211
217
  for (const item of items) {
218
+ ${maxLengthStringFields.map((f) => `this.${getEnsureMaxLengthFnName(f)}(item)`).join('\n')}
212
219
  ${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
213
220
  }`
214
221
  : ''}
@@ -238,6 +245,16 @@ export class ${meta.data.repositoryClassName} implements Repository<
238
245
  throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
239
246
  }
240
247
 
248
+
249
+ ${maxLengthStringFields
250
+ .map((f) => {
251
+ return `
252
+ if (item.${f.name} !== undefined && existingItem.${f.name} !== item.${f.name}) {
253
+ this.${getEnsureMaxLengthFnName(f)}(item)
254
+ }
255
+ `;
256
+ })
257
+ .join('\n')}
241
258
  ${uniqueStringFields
242
259
  .map((f) => {
243
260
  return `
@@ -305,6 +322,18 @@ export class ${meta.data.repositoryClassName} implements Repository<
305
322
  this.data.delete(${meta.types.toBrandedIdTypeFnName}(id))
306
323
  }
307
324
 
325
+ ${maxLengthStringFields.map((f) => {
326
+ return `
327
+ /**
328
+ * Utility function that ensures that the ${f.name} field has a max length of ${f.attributes.maxLength}
329
+ */
330
+ private ${getEnsureMaxLengthFnName(f)}(item: { ${f.name}?: string }) {
331
+ if (!item.${f.name}) return
332
+ if (item.${f.name}.length <= ${f.attributes.maxLength}) return
333
+ item.${f.name} = item.${f.name}.substring(0, ${f.attributes.maxLength - 4}) + \`...\`
334
+ }`;
335
+ })}
336
+
308
337
  ${uniqueStringFields
309
338
  .map((f) => {
310
339
  return `
@@ -347,3 +376,6 @@ exports.generateRepository = generateRepository;
347
376
  function getEnsureUniqueFnName(field) {
348
377
  return `ensureUnique${(0, string_1.toPascalCase)(field.name)}`;
349
378
  }
379
+ function getEnsureMaxLengthFnName(field) {
380
+ return `ensureMaxLength${(0, string_1.toPascalCase)(field.name)}`;
381
+ }
@@ -33,3 +33,7 @@ export declare const getDefaultValueForType: (type: string) => string;
33
33
  * Returns true if the given field is unique and a string field.
34
34
  */
35
35
  export declare const isUniqueStringField: (f: Field) => boolean;
36
+ /**
37
+ * Returns true if the given field has a maxLength attribute.
38
+ */
39
+ export declare const isMaxLengthStringField: (f: Field) => boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isUniqueStringField = exports.getDefaultValueForType = exports.getEnumFields = exports.getRelationFields = exports.getScalarFields = exports.getDefaultField = void 0;
3
+ exports.isMaxLengthStringField = exports.isUniqueStringField = exports.getDefaultValueForType = exports.getEnumFields = exports.getRelationFields = exports.getScalarFields = exports.getDefaultField = void 0;
4
4
  /**
5
5
  * The default field of the model.
6
6
  * Note: A model can only have one or no default field!
@@ -47,3 +47,9 @@ exports.getDefaultValueForType = getDefaultValueForType;
47
47
  const isUniqueStringField = (f) => (f.kind === 'id' && f.isUnique && f.unbrandedTypeName === 'string') ||
48
48
  (f.kind === 'scalar' && f.isUnique && f.typeName === 'string');
49
49
  exports.isUniqueStringField = isUniqueStringField;
50
+ /**
51
+ * Returns true if the given field has a maxLength attribute.
52
+ */
53
+ const isMaxLengthStringField = (f) => (f.kind === 'id' && !!f.attributes.maxLength && f.unbrandedTypeName === 'string') ||
54
+ (f.kind === 'scalar' && !!f.attributes.maxLength && f.typeName === 'string');
55
+ exports.isMaxLengthStringField = isMaxLengthStringField;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {