@andymic/pigeon 2.0.1 → 2.1.0

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 +20 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andymic/pigeon",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
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": "565fcc64621e0c076f77933662e6f82969ef62e6"
38
+ "gitHead": "029bee420b59d84dfb269ec7eb3190aee486de10"
39
39
  }
package/src/index.js CHANGED
@@ -341,7 +341,7 @@ function createIndex(db) {
341
341
  text += "import pg from \"pg\";\n";
342
342
  text += "const { Client, Pool } = pg;\n";
343
343
  text += "\n";
344
- text += "const pool = new Pool({\n";
344
+ text += "export const pool = new Pool({\n";
345
345
  text += "\thost: \"" + db.host + "\",\n";
346
346
  text += "\tport: " + db.port + ",\n";
347
347
  text += "\tdatabase: \"" + db.db + "\",\n";
@@ -352,7 +352,7 @@ function createIndex(db) {
352
352
  text += "\tconnectionTimeoutMillis: 2000\n";
353
353
  text += "});\n";
354
354
  text += "\n";
355
- text += "const client = new Client({\n";
355
+ text += "export const client = new Client({\n";
356
356
  text += "\thost: \"" + db.host + "\",\n";
357
357
  text += "\tport: " + db.port + ",\n";
358
358
  text += "\tdatabase: \"" + db.db + "\",\n";
@@ -360,12 +360,9 @@ function createIndex(db) {
360
360
  text += "\tpassword: \"" + db.pass + "\"\n";
361
361
  text += "});\n";
362
362
  text += "\n";
363
- text += "/*\n";
364
- text += "export default (sql: string, params: any[]) => pool.query(sql, params);\n";
365
- text += "*/\n";
363
+ text += "const poolQuery = (sql: string, params: any[]) => pool.query(sql, params);\n";
366
364
  text += "\n";
367
- text += "/*\n";
368
- text += "export default async (sql: string, params: any[]) => {\n";
365
+ text += "const clientQuery = async (sql: string, params: any[]) => {\n";
369
366
  text += "\ttry {\n";
370
367
  text += "\t\tawait client.connect();\n";
371
368
  text += "\t\treturn await client.query(sql, params);\n";
@@ -375,7 +372,10 @@ function createIndex(db) {
375
372
  text += "\t\tawait client.end();\n";
376
373
  text += "\t}\n";
377
374
  text += "}\n";
378
- text += "*/";
375
+ text += "\n";
376
+ text += "//export default poolQuery;\n";
377
+ text += "\n";
378
+ text += "//export default clientQuery;\n";
379
379
  return text;
380
380
  }
381
381
  function createClass(tableName, columns) {
@@ -475,9 +475,10 @@ function createGet(tableSchema, tableName, columns) {
475
475
  text += " * @param {Partial<" + className + ">} criteria - An object containing properties of the " + className + " to filter by.\n";
476
476
  text += " * Only " + nameBeautifier(tableName).toLowerCase() + " matching all provided criteria will be returned.\n";
477
477
  text += " * An empty object or undefined criteria will fetch all " + className + " objects.\n";
478
+ text += " * @param {Function} [runQuery=query] - The function that executes the database query. This is optional and defaults to the production `query` function. It can be used for transactions when using pools.\n";
478
479
  text += " * @returns {Promise<" + className + "[]>} - A Promise object returning an array of " + nameBeautifier(tableName) + " matching the criteria.\n";
479
480
  text += " */\n";
480
- text += "export async function get" + nameBeautifier(tableName).replaceAll(" ", "") + "(criteria: Partial<" + className + ">): Promise<" + className + "[]> {\n";
481
+ text += "export async function get" + nameBeautifier(tableName).replaceAll(" ", "") + "(criteria: Partial<" + className + ">, runQuery: Function = query): Promise<" + className + "[]> {\n";
481
482
  text += "\tconst whereClauses: string[] = [];\n";
482
483
  text += "\tconst values: any[] = [];\n";
483
484
  text += "\tlet paramIndex = 1;\n";
@@ -495,7 +496,7 @@ function createGet(tableSchema, tableName, columns) {
495
496
  text += "\t\tsql += \" WHERE \" + whereClauses.join(\" AND \");\n";
496
497
  text += "\tsql += \";\";\n";
497
498
  text += "\n";
498
- text += "\tconst " + varName + "Query = await query(sql, values);\n";
499
+ text += "\tconst " + varName + "Query = await runQuery(sql, values);\n";
499
500
  text += arrayMaker(1, varName, className, columns) + "\n";
500
501
  text += "\treturn " + varName + ";\n";
501
502
  text += "}";
@@ -539,6 +540,7 @@ function createAdd(tableSchema, tableName, required, optional, autoGenerated, ha
539
540
  text += " * - The field" + (optional.length > 1 ? "s" : "") + " `" + optional.map((column) => {
540
541
  return column.name;
541
542
  }).join("`, `") + "` " + (optional.length > 1 ? "are" : "is") + " optional.\n";
543
+ text += " * @param {Function} [runQuery=query] - The function that executes the database query. This is optional and defaults to the production `query` function. It can be used for transactions when using pools.\n";
542
544
  text += " * @returns {Promise<" + className + ">} A Promise returning the newly created " + className + " object.\n";
543
545
  if (hasForeign) {
544
546
  text += " * @throws {Error} An exception in the case of the ";
@@ -549,7 +551,7 @@ function createAdd(tableSchema, tableName, required, optional, autoGenerated, ha
549
551
  text += " not existing in their table, or if other pre-insertion validation fails.\n";
550
552
  }
551
553
  text += " */\n";
552
- text += "export async function " + functionName + "(" + paramName + ": " + dataType.replaceAll("'", "\"") + "): Promise<" + className + "> {\n";
554
+ text += "export async function " + functionName + "(" + paramName + ": " + dataType.replaceAll("'", "\"") + ", runQuery: Function = query): Promise<" + className + "> {\n";
553
555
  if (hasForeign) {
554
556
  for (const column of editableColumns) {
555
557
  if (column.isForeign && column.foreignColumn && column.foreignTable && column.foreignSchema) {
@@ -590,7 +592,7 @@ function createAdd(tableSchema, tableName, required, optional, autoGenerated, ha
590
592
  text += "\t\tthrow new Error(\"No data provided for " + paramName + " creation.\");\n";
591
593
  text += "\n";
592
594
  text += "\tconst sql = \"INSERT INTO " + tableSchema + "." + tableName + " (\" + intoClauses.join(\", \") + \") VALUES (\" + valuesClauses.join(\", \") + \") RETURNING *;\";\n";
593
- text += "\tconst insertQuery = await query(sql, values);\n";
595
+ text += "\tconst insertQuery = await runQuery(sql, values);\n";
594
596
  text += "\treturn new " + className + "(\n";
595
597
  let columns = editableColumns.concat(autoGenerated);
596
598
  columns = [...new Set(columns)];
@@ -620,11 +622,12 @@ function createUpdate(tableSchema, tableName, columns, autoGenerated, hasForeign
620
622
  text += " * An empty object would typically mean updating ALL " + nameBeautifier(tableName) + ", which should be handled with caution or disallowed.\n";
621
623
  text += " * @param {" + updatesType + "} updates - An object containing the new values for the fields to be updated.\n";
622
624
  text += " * Only fields present in this object will be updated.\n";
625
+ text += " * @param {Function} [runQuery=query] - The function that executes the database query. This is optional and defaults to the production `query` function. It can be used for transactions when using pools.\n";
623
626
  text += " * @returns {Promise<" + className + "[]>} A promise that resolves to an array of the updated " + className + " objects.\n";
624
627
  text += " * Returns an empty array if no records matched the criteria or if no rows were updated.\n";
625
628
  text += " * @throws {Error} If updates object is empty" + (hasForeign ? ", or if foreign key checks fail for provided update values" : "") + ".\n";
626
629
  text += " */\n";
627
- text += "export async function update" + nameBeautifier(tableName).replaceAll(" ", "") + "(criteria: " + criteriaType.replaceAll("'", "\"") + ", updates: " + updatesType.replaceAll("'", "\"") + "): Promise<" + className + "[]> {\n";
630
+ text += "export async function update" + nameBeautifier(tableName).replaceAll(" ", "") + "(criteria: " + criteriaType.replaceAll("'", "\"") + ", updates: " + updatesType.replaceAll("'", "\"") + ", runQuery: Function = query): Promise<" + className + "[]> {\n";
628
631
  text += "\tif (Object.keys(updates).length === 0)\n";
629
632
  text += "\t\tthrow new Error(\"No update data provided.\");\n";
630
633
  text += "\n";
@@ -674,7 +677,7 @@ function createUpdate(tableSchema, tableName, columns, autoGenerated, hasForeign
674
677
  text += "\n";
675
678
  text += "\tlet sql = \"UPDATE " + tableSchema + "." + tableName + " SET \" + setClauses.join(\", \") + (whereClauses.length !== 0 ? \" WHERE \" + whereClauses.join(\" AND \") : \"\") + \" RETURNING *;\";\n";
676
679
  text += "\n";
677
- text += "\tconst updateQuery = await query(sql, values);\n";
680
+ text += "\tconst updateQuery = await runQuery(sql, values);\n";
678
681
  text += arrayMaker(1, "update", className, columns) + "\n";
679
682
  text += "\treturn update;\n";
680
683
  text += "}";
@@ -692,9 +695,10 @@ function createDelete(tableSchema, tableName, columns) {
692
695
  text += " * @param {Partial<" + className + ">} criteria - An object containing properties of the " + className + " to filter by for deletion.\n";
693
696
  text += " * An empty object would typically mean deleting ALL " + nameBeautifier(tableName) + ", which should be handled with caution or disallowed.\n";
694
697
  text += " * Only accounts matching all provided criteria will be deleted.\n";
698
+ text += " * @param {Function} [runQuery=query] - The function that executes the database query. This is optional and defaults to the production `query` function. It can be used for transactions when using pools.\n";
695
699
  text += " * @returns {Promise<" + className + "[]>} - A Promise object returning the deleted " + nameBeautifier(tableName) + ".\n";
696
700
  text += " */\n";
697
- text += "export async function " + functionName + "(criteria : Partial<" + className + ">): Promise<" + className + "[]> {\n";
701
+ text += "export async function " + functionName + "(criteria : Partial<" + className + ">, runQuery: Function = query): Promise<" + className + "[]> {\n";
698
702
  text += "\tconst whereClauses: string[] = [];\n";
699
703
  text += "\tconst values: any[] = [];\n";
700
704
  text += "\tlet paramIndex = 1;\n";
@@ -711,7 +715,7 @@ function createDelete(tableSchema, tableName, columns) {
711
715
  text += "\n";
712
716
  text += "\tlet sql = \"DELETE FROM " + tableSchema + "." + tableName + "\" + (whereClauses.length !== 0 ? \" WHERE \" + whereClauses.join(\" AND \") : \"\") + \" RETURNING *;\";\n";
713
717
  text += "\n";
714
- text += "\tconst removeQuery = await query(sql, values);\n";
718
+ text += "\tconst removeQuery = await runQuery(sql, values);\n";
715
719
  text += arrayMaker(1, "remove", className, columns) + "\n";
716
720
  text += "\treturn remove;\n";
717
721
  text += "}";