@andymic/pigeon 2.0.0 → 2.0.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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +12 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andymic/pigeon",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "author": "Andreas Michael <ateasm03@gmail.com>",
5
5
  "description": "Pigeon is a TypeScript-based tool for generating TypeScript classes and methods from PostgreSQL database schemas.",
6
6
  "keywords": [
@@ -35,5 +35,5 @@
35
35
  "pg": "^8.16.0",
36
36
  "prompt-sync": "^4.2.0"
37
37
  },
38
- "gitHead": "85641ef9cc7fe7b42c78f7470608793f192b880a"
38
+ "gitHead": "565fcc64621e0c076f77933662e6f82969ef62e6"
39
39
  }
package/src/index.js CHANGED
@@ -272,19 +272,21 @@ export function runGeneration(dir, db, tables, enums) {
272
272
  }
273
273
  for (const table of tables) {
274
274
  let ts = "";
275
- let hasForeign = false;
275
+ const imports = [];
276
276
  for (const column of table.columns) {
277
277
  if (column.isForeign && column.foreignColumn && column.foreignTable && column.foreignSchema) {
278
- hasForeign = true;
279
278
  if ((table.schema === column.foreignSchema) && (table.name === column.foreignTable))
280
279
  continue;
280
+ if (imports.includes(column.foreignSchema + "/" + column.foreignTable))
281
+ continue;
281
282
  ts += "import {get" + nameBeautifier(column.foreignTable).replaceAll(" ", "") + "} from \".";
282
283
  if (table.schema !== column.foreignSchema)
283
284
  ts += "./" + column.foreignSchema;
284
285
  ts += "/" + column.foreignTable + ".js\";\n";
286
+ imports.push(column.foreignSchema + "/" + column.foreignTable);
285
287
  }
286
288
  }
287
- if (hasForeign)
289
+ if (imports.length !== 0)
288
290
  ts += "\n";
289
291
  ts += "import query from \"./index.js\";\n\n";
290
292
  if (enums) {
@@ -326,9 +328,9 @@ export function runGeneration(dir, db, tables, enums) {
326
328
  else if ((column.defaultValue !== null && column.defaultValue.includes("nextval")) || (column.isIdentity && column.identityGeneration === "ALWAYS"))
327
329
  autoGenerated.push(column);
328
330
  }
329
- ts += createAdd(table.schema, table.name, required, optional, autoGenerated, hasForeign);
331
+ ts += createAdd(table.schema, table.name, required, optional, autoGenerated, imports.length !== 0);
330
332
  ts += "\n\n";
331
- ts += createUpdate(table.schema, table.name, table.columns, autoGenerated, hasForeign);
333
+ ts += createUpdate(table.schema, table.name, table.columns, autoGenerated, imports.length !== 0);
332
334
  ts += "\n\n";
333
335
  ts += createDelete(table.schema, table.name, table.columns);
334
336
  fs.writeFileSync(path.join(dir, table.schema, table.name + ".ts"), ts);
@@ -506,9 +508,9 @@ function createAdd(tableSchema, tableName, required, optional, autoGenerated, ha
506
508
  const functionName = "add" + singularize(nameBeautifier(tableName).replaceAll(" ", ""));
507
509
  const dataType = "Omit<" + className + ", '" + autoGenerated.concat(optional).map((column) => {
508
510
  return column.name;
509
- }).join("\' | \'") + "'> & Partial<Pick<" + className + ", '" + optional.map((column) => {
511
+ }).join("\' | \'") + "'>" + (optional.length > 0 ? " & Partial<Pick<" + className + ", '" + optional.map((column) => {
510
512
  return column.name;
511
- }).join("\' | \'") + "'>>";
513
+ }).join("\' | \'") + "'>>" : "");
512
514
  const editableColumns = required.concat(optional);
513
515
  text += "/**\n";
514
516
  text += " * Adds a new " + singularize(nameBeautifier(tableName)) + " object to the database.\n";
@@ -547,7 +549,7 @@ function createAdd(tableSchema, tableName, required, optional, autoGenerated, ha
547
549
  text += " not existing in their table, or if other pre-insertion validation fails.\n";
548
550
  }
549
551
  text += " */\n";
550
- text += "export async function " + functionName + "( " + paramName + ": " + dataType.replaceAll("'", "\"") + "): Promise<" + className + "> {\n";
552
+ text += "export async function " + functionName + "(" + paramName + ": " + dataType.replaceAll("'", "\"") + "): Promise<" + className + "> {\n";
551
553
  if (hasForeign) {
552
554
  for (const column of editableColumns) {
553
555
  if (column.isForeign && column.foreignColumn && column.foreignTable && column.foreignSchema) {
@@ -606,9 +608,9 @@ function createUpdate(tableSchema, tableName, columns, autoGenerated, hasForeign
606
608
  const className = singularize(nameBeautifier(tableName)).replaceAll(" ", "");
607
609
  const functionName = "update" + nameBeautifier(tableName).replaceAll(" ", "");
608
610
  const criteriaType = "Partial<" + className + ">";
609
- const updatesType = "Partial<Omit<" + className + ", '" + autoGenerated.map((column) => {
611
+ const updatesType = "Partial<" + (autoGenerated.length !== 0 ? "Omit<" + className + ", '" + autoGenerated.map((column) => {
610
612
  return column.name;
611
- }).join("' | '") + "'>>";
613
+ }).join("' | '") + "'>" : className) + ">";
612
614
  text += "/**\n";
613
615
  text += " * Updates existing " + className + " objects in the database based on the specified criteria.\n";
614
616
  text += " *\n";