@joystick.js/node-canary 0.0.0-canary.306 → 0.0.0-canary.307

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.
@@ -0,0 +1,33 @@
1
+ var generate_sql_from_object_default = {
2
+ insert: (options = {}) => {
3
+ const column_names = Object.keys(options?.data)?.join(",");
4
+ const value_placeholders = Object.keys(options?.data)?.map((_, index) => `$${index + 1}`)?.join(",");
5
+ return {
6
+ statement: `INSERT INTO ${options?.table} (${column_names}) VALUES (${value_placeholders})`,
7
+ column_names,
8
+ value_placeholders,
9
+ values: Object.values(options?.data)
10
+ };
11
+ },
12
+ update: (options = {}) => {
13
+ const whereEntries = Object.entries(options?.where);
14
+ const sets = Object.keys(options?.data).map((key, index) => {
15
+ return `${key} = $${whereEntries.length + index + 1}`;
16
+ })?.join(",");
17
+ const where = whereEntries?.map(([key], index) => {
18
+ return `${key} = $${index + 1}`;
19
+ })?.join(",");
20
+ return {
21
+ statement: `UPDATE ${options?.table} SET ${sets} WHERE ${where}`,
22
+ sets,
23
+ where,
24
+ values: [
25
+ ...Object.values(options?.where),
26
+ ...Object.values(options?.data)
27
+ ]
28
+ };
29
+ }
30
+ };
31
+ export {
32
+ generate_sql_from_object_default as default
33
+ };
@@ -2,6 +2,7 @@ import postgresql from "pg";
2
2
  import chalk from "chalk";
3
3
  import os from "os";
4
4
  import fs from "fs";
5
+ import generate_sql_from_object from "../generate_sql_from_object";
5
6
  const { Pool } = postgresql;
6
7
  var postgresql_default = async (settings = {}, databasePort = 2610) => {
7
8
  const connection = settings?.connection || {
@@ -30,8 +31,44 @@ var postgresql_default = async (settings = {}, databasePort = 2610) => {
30
31
  const pool = new Pool(connection_config);
31
32
  return {
32
33
  pool,
33
- query: (...args) => {
34
- return pool.query(...args).then((response) => {
34
+ query: (...args2) => {
35
+ return pool.query(...args2).then((response) => {
36
+ return response?.rows || [];
37
+ }).catch((error) => {
38
+ console.log(chalk.redBright(`
39
+ Failed SQL Statement:
40
+ `));
41
+ console.log(args2[0]);
42
+ console.log(`
43
+ `);
44
+ console.log(chalk.redBright(`
45
+ Failed Values:
46
+ `));
47
+ console.log(args2[1]);
48
+ throw error;
49
+ });
50
+ },
51
+ insert: (options = {}) => {
52
+ const insert = generate_sql_from_object.insert(options);
53
+ return pool.query(insert.statement, insert.values).then((response) => {
54
+ return response?.rows || [];
55
+ }).catch((error) => {
56
+ console.log(chalk.redBright(`
57
+ Failed SQL Statement:
58
+ `));
59
+ console.log(args[0]);
60
+ console.log(`
61
+ `);
62
+ console.log(chalk.redBright(`
63
+ Failed Values:
64
+ `));
65
+ console.log(args[1]);
66
+ throw error;
67
+ });
68
+ },
69
+ update: (options = {}) => {
70
+ const update = generate_sql_from_object.update(options);
71
+ return pool.query(update.statement, update.values).then((response) => {
35
72
  return response?.rows || [];
36
73
  }).catch((error) => {
37
74
  console.log(chalk.redBright(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joystick.js/node-canary",
3
- "version": "0.0.0-canary.306",
3
+ "version": "0.0.0-canary.307",
4
4
  "type": "module",
5
5
  "description": "A Node.js framework for building web apps.",
6
6
  "main": "./dist/index.js",