@postxl/generator 0.24.1 → 0.25.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.
@@ -23,6 +23,7 @@ CREATE OR REPLACE PROCEDURE "emptyDatabase"() AS $BODY$ BEGIN IF EXISTS (
23
23
  END IF;
24
24
  TRUNCATE TABLE
25
25
  ${tables};
26
+ UPDATE ${configTableName} SET "isSeeded"=false;
26
27
  END;
27
28
  $BODY$ LANGUAGE plpgsql;
28
29
  `;
@@ -52,7 +53,15 @@ function prismaMigrationExists(meta) {
52
53
  return firstMigration !== undefined;
53
54
  }
54
55
  exports.prismaMigrationExists = prismaMigrationExists;
56
+ /**
57
+ * Returns the first migration in the migrations folder if it exists.
58
+ */
55
59
  function getFirstPrismaMigration(meta) {
56
60
  const folder = meta.migrationsPath;
57
- return fs_1.default.readdirSync(folder).find((f) => fs_1.default.statSync(`${folder}/${f}`).isDirectory());
61
+ try {
62
+ return fs_1.default.readdirSync(folder).find((f) => fs_1.default.statSync(`${folder}/${f}`).isDirectory());
63
+ }
64
+ catch (err) {
65
+ return undefined;
66
+ }
58
67
  }
@@ -136,7 +136,7 @@ function generateSeedService({ models, meta }) {
136
136
  i++
137
137
  console.log(name, i, item.id)
138
138
  try {
139
- await repo.createWithId?.(item)
139
+ await repo.create?.(item)
140
140
  } catch (error) {
141
141
  console.log(item)
142
142
  throw error
@@ -45,7 +45,7 @@ function generateTableDecoder({ model, meta, imports, }) {
45
45
  imports.addImport({ items: [meta.types.toBrandedIdTypeFnName], from: meta.types.importPath });
46
46
  fieldDecoders.push(`${fieldMeta.excelColumnName}: ${toExcelDecoder({
47
47
  tsTypeName: field.unbrandedTypeName,
48
- dbTypeName: field.type,
48
+ dbTypeName: field.schemaType,
49
49
  nullable: false,
50
50
  imports,
51
51
  })}.transform((id: ${field.unbrandedTypeName}) => ${meta.types.toBrandedIdTypeFnName}(id))`);
@@ -54,7 +54,7 @@ function generateTableDecoder({ model, meta, imports, }) {
54
54
  case 'scalar': {
55
55
  fieldDecoders.push(`${fieldMeta.excelColumnName}: ${toExcelDecoder({
56
56
  tsTypeName: field.tsTypeName,
57
- dbTypeName: field.type,
57
+ dbTypeName: field.schemaType,
58
58
  nullable: !field.isRequired,
59
59
  imports,
60
60
  })}`);
@@ -66,7 +66,7 @@ function generateTableDecoder({ model, meta, imports, }) {
66
66
  imports.addImport({ items: [refMeta.types.toBrandedIdTypeFnName], from: refMeta.types.importPath });
67
67
  fieldDecoders.push(`${fieldMeta.excelColumnName}: ${toExcelDecoder({
68
68
  tsTypeName: field.unbrandedTypeName,
69
- dbTypeName: field.type,
69
+ dbTypeName: field.schemaType,
70
70
  nullable: !field.isRequired,
71
71
  imports,
72
72
  })}.transform((id: ${field.unbrandedTypeName}${field.isRequired ? '' : '| null'}) => ${field.isRequired ? '' : ' id === null ? null : '}${refMeta.types.toBrandedIdTypeFnName}(id))`);
@@ -114,6 +114,7 @@ function generateModelBusinessLogic({ model, meta }) {
114
114
  return item
115
115
  }
116
116
  `;
117
+ // prettier-ignore
117
118
  return `
118
119
  import { Inject, Injectable, forwardRef } from '@nestjs/common'
119
120
  import { FilterOperator } from '@pxl/common'
@@ -169,16 +170,14 @@ export class ${meta.businessLogic.serviceClassName} {
169
170
  /**
170
171
  * Creates a new ${meta.userFriendlyName}.
171
172
  */
172
- public async create(item: Omit<${model.typeName}, 'id'>): Promise<${model.typeName}> {
173
+ public async create(item: Omit<${model.typeName}, '${model.idField.name}'> & Partial<{${model.idField.name}: ${model.idField.unbrandedTypeName}}>): Promise<${model.typeName}> {
173
174
  return this.${modelRepositoryVariableName}.create(item)
174
175
  }
175
176
 
176
177
  /**
177
178
  * Updates the given ${meta.userFriendlyName}.
178
179
  */
179
- public async update(item: Partial<${model.typeName}> & {
180
- id: ${model.brandedIdType}
181
- }): Promise<${model.typeName}> {
180
+ public async update(item: Partial<${model.typeName}> & {id: ${model.brandedIdType}}): Promise<${model.typeName}> {
182
181
  return this.${modelRepositoryVariableName}.update(item)
183
182
  }
184
183
 
@@ -190,7 +189,7 @@ export class ${meta.businessLogic.serviceClassName} {
190
189
  */
191
190
  public async delete(id: ${model.brandedIdType}): Promise<void> {
192
191
  return this.${modelRepositoryVariableName}.delete(id)
193
- }
192
+ }
194
193
  }
195
194
 
196
195
  const compare = (a: ${model.typeName}, b: ${model.typeName}, field: keyof ${model.typeName}) => {
@@ -1,7 +1,17 @@
1
- import { Model } from '../../lib/schema/schema';
2
1
  import { ModelMetaData } from '../../lib/meta';
2
+ import { Model } from '../../lib/schema/schema';
3
3
  /**
4
4
  * Generates repository data structure for a given model.
5
+ * Based on the model, the repository is generated slightly differently, based on:
6
+ * - if the model has a default field, the repository will have a public variable "defaultValue"
7
+ * that is set and verified during init
8
+ * - if the model has a generated id, the repository will have a function "generateNextId" that
9
+ * is used to generate the next id. The `create` and `createMany` functions will use this function
10
+ * and allow being called with an id.
11
+ * - unique string fields are ensured to be unique
12
+ * - max length string fields are ensured to not exceed their max length
13
+ * - index for fields marked with index attribute
14
+ * - relations are indexed by the foreign key
5
15
  */
6
16
  export declare function generateRepository({ model, meta }: {
7
17
  model: Model;