@postxl/generator 0.0.1 → 0.0.2

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/changelog.md ADDED
@@ -0,0 +1,15 @@
1
+ # 0.0.2
2
+
3
+ ## Added
4
+
5
+ - Field attribute: maxLength attribute - this is now enforced in the repository
6
+ - `createMany` generated by repository generator
7
+ - Internal: added sample schemas for testing
8
+
9
+ ## Removed
10
+
11
+ - `get` method of repositories does not accept unbranded ids anymore
12
+
13
+ ## Fixed
14
+
15
+ - `unique` field attribute is now generated correctly
@@ -72,7 +72,7 @@ export class ${meta.data.repositoryClassName} implements Repository<
72
72
  this.data.set(item.id, item)
73
73
 
74
74
  ${model.defaultField ? defaultValueInitFn : ''}
75
- ${uniqueStringFields.map((f) => `this.uniqueIds.${f}.set(item.${f}, item)`).join('\n')}
75
+ ${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(item.${f.name}, item)`).join('\n')}
76
76
  }
77
77
 
78
78
  ${isGenerated ? idIntInitFn : ''}
@@ -93,7 +93,7 @@ export class ${meta.data.repositoryClassName} implements Repository<
93
93
  return this.init()
94
94
  }
95
95
 
96
- public get(id: ${model.brandedIdType}| ${idField.unbrandedTypeName} | null): ${model.typeName} | null {
96
+ public get(id: ${model.brandedIdType} | null): ${model.typeName} | null {
97
97
  if (id === null) return null
98
98
  return this.data.get(${meta.types.toBrandedIdTypeFnName}(id)) ?? null
99
99
  }
@@ -177,6 +177,15 @@ export class ${meta.data.repositoryClassName} implements Repository<
177
177
  return newItem
178
178
  }
179
179
 
180
+ public async createMany(items: Omit<${model.typeName}, 'id' | 'createdAt' | 'updatedAt'>[]): Promise<${model.typeName}[]> {
181
+ const result: ${model.typeName}[] = []
182
+ for (const item of items) {
183
+ const i = await this.create(item)
184
+ result.push(i)
185
+ }
186
+ return result
187
+ }
188
+
180
189
  public async update(item: Partial<${model.typeName}> & {
181
190
  id: ${model.brandedIdType} | ${idField.unbrandedTypeName}
182
191
  }): Promise<${model.typeName}> {
@@ -190,7 +199,7 @@ export class ${meta.data.repositoryClassName} implements Repository<
190
199
  ${uniqueStringFields
191
200
  .map((f) => {
192
201
  return `
193
- if (item.${f.name} !== undefined && existingItem.${f} !== item.${f}) {
202
+ if (item.${f.name} !== undefined && existingItem.${f.name} !== item.${f.name}) {
194
203
  this.${getEnsureUniqueFnName(f)}(item)
195
204
  }
196
205
  `;
@@ -247,22 +256,25 @@ export class ${meta.data.repositoryClassName} implements Repository<
247
256
 
248
257
  ${uniqueStringFields
249
258
  .map((f) => {
250
- const fName = f.name;
251
259
  return `
252
- private ${getEnsureUniqueFnName(f)}(item: { ${fName}?: string }) {
253
- if (!item.${fName}) return
254
- if (!this.uniqueIds.${fName}.has(item.${fName})) return
255
- let counter = 1
256
- let ${fName}: string
257
-
258
- //TODO: Add attribute for max length of string
259
- do {
260
- ${fName} = \`\${item.${fName}} (\${++counter})\`
261
- } while (this.uniqueIds.${fName}.has(${fName}))
262
-
263
- this.logger.log(\`Asset ${fName} \${item.${fName}} already exists. Renaming to \${${fName}})\`)
264
- item.${fName} = ${fName}
265
- }
260
+ /**
261
+ * Utility function that ensures that the ${f.name} field is unique
262
+ */
263
+ private ${getEnsureUniqueFnName(f)}(item: { ${f.name}?: string }) {
264
+ if (!item.${f.name}) return
265
+ if (!this.uniqueIds.${f.name}.has(item.${f.name})) return
266
+ let counter = 1
267
+
268
+ let ${f.name}: string
269
+ const source${f.name} =${!f.attributes.maxLength ? `item.${f.name}` : `item.${f.name}.substring(0, ${f.attributes.maxLength - 5})`}
270
+
271
+ do {
272
+ ${f.name} = \`\${source${f.name}} (\${++counter})\`
273
+ } while (this.uniqueIds.${f.name}.has(${f.name}))
274
+
275
+ this.logger.log(\`Asset ${f.name} \${item.${f.name}} already exists. Renaming to \${${f.name}})\`)
276
+ item.${f.name} = ${f.name}
277
+ }
266
278
  `;
267
279
  })
268
280
  .join('\n\n')}
@@ -40,4 +40,9 @@ export type FieldAttributes = {
40
40
  * The property of the model that identifies the default row.
41
41
  */
42
42
  isDefaultField?: boolean;
43
+ /**
44
+ * Schema tag: `@@MaxLength(int)`
45
+ * The maximum length of a string.
46
+ */
47
+ maxLength?: number;
43
48
  };
@@ -61,6 +61,9 @@ exports.getModelAttributes = getModelAttributes;
61
61
  */
62
62
  function getFieldAttributes(field) {
63
63
  const attributes = parseAttributesFromDocumentation(field);
64
+ if (field.name === 'name') {
65
+ console.log('attributes', attributes);
66
+ }
64
67
  if (attributes.examples === undefined && attributes.example !== undefined) {
65
68
  attributes.examples = attributes.example;
66
69
  }
@@ -69,12 +72,19 @@ function getFieldAttributes(field) {
69
72
  // we handle this the same way as our custom "ignore" attribute
70
73
  ignore: field.isIgnored || Object.hasOwn(attributes, 'ignore'),
71
74
  description: attributes.description && (0, remeda_1.isString)(attributes.description) ? attributes.description : undefined,
72
- isDefaultField: Object.hasOwn(attributes, 'isDefaultField'),
75
+ isDefaultField: Object.hasOwn(attributes, 'isDefault'),
73
76
  examples: !attributes.examples
74
77
  ? undefined
75
78
  : Array.isArray(attributes.examples)
76
79
  ? attributes.examples
77
80
  : [attributes.examples],
81
+ maxLength: Object.hasOwn(attributes, 'maxLength')
82
+ ? (0, remeda_1.isString)(attributes.maxLength)
83
+ ? parseInt(attributes.maxLength, 10)
84
+ : (0, remeda_1.isNumber)(attributes.maxLength)
85
+ ? attributes.maxLength
86
+ : undefined
87
+ : undefined,
78
88
  };
79
89
  }
80
90
  exports.getFieldAttributes = getFieldAttributes;