Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pg support #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RedBeanNode is an easy to use **ORM** tool for Node.js, strongly inspired by Red

* MySQL / MariaDB
* SQLite
* PostgreSQL

## Installation

Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RedBeanNode is an easy to use **ORM** tool for Node.js, strongly inspired by Red
* Ported RedBeanPHP's main features and api design
* Supports **JavaScript** & **TypeScript**
* **async/await** or **promise** friendly
* Supports MySQL / MariaDB / SQLite
* Supports MySQL / MariaDB / SQLite / PostgreSQL

## Code Example

Expand Down
57 changes: 44 additions & 13 deletions lib/redbean-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import BeanConverterStream from "./bean-converter-stream";
import AwaitLock from "await-lock";
import StaticConnectionConfig = Knex.StaticConnectionConfig;
import PoolConfig = Knex.PoolConfig;
import Client = Knex.Client;
import RawBinding = Knex.RawBinding;
import QueryBuilder = Knex.QueryBuilder;
// import PromisePool from 'es6-promise-pool';
Expand Down Expand Up @@ -70,7 +69,7 @@ export class RedBeanNode {
}

if (dbType == "mariadb") {
dbType = "mysql";
dbType = "mysql2";
}

this.dbType = dbType;
Expand All @@ -84,6 +83,9 @@ export class RedBeanNode {
pool
});

if (this.dbType === "mysql2") {
this.dbType = "mysql";
}
} else {
this._knex = dbType;
this.dbType = this._knex.client.config.client;
Expand Down Expand Up @@ -193,11 +195,19 @@ export class RedBeanNode {
}

} else {
let queryPromise = this.knex(bean.getType()).insert(obj);

this.queryLog(queryPromise);
let result = await queryPromise;
bean.id = result[0];
if (this.dbType === "pg") {
// https://knexjs.org/guide/query-builder.html#insert
// postgresql requires setting returning to return data.
let queryPromise = this.knex(bean.getType()).insert(obj, ['id']);
this.queryLog(queryPromise);
let result = await queryPromise;
bean.id = result[0].id;
} else {
let queryPromise = this.knex(bean.getType()).insert(obj);
this.queryLog(queryPromise);
let result = await queryPromise;
bean.id = result[0];
}
}


Expand Down Expand Up @@ -512,13 +522,12 @@ export class RedBeanNode {

if (this.dbType == "sqlite") {
e = error.message;

} else if (this.dbType == "mysql") {
e = error.code;

} else if (this.dbType == "mssql") {
e = error.message;

} else if (this.dbType == "pg") {
e = error.message;
}

if (!e) {
Expand Down Expand Up @@ -575,6 +584,9 @@ export class RedBeanNode {

// MSSQL
"Invalid object name",

// Postgres
"does not exist",
]);
}

Expand All @@ -592,7 +604,10 @@ export class RedBeanNode {

// MYSQL
"ER_TABLE_EXISTS_ERROR",
"ER_DUP_FIELDNAME"
"ER_DUP_FIELDNAME",

// Postgres
"already exists",
]);
}

Expand Down Expand Up @@ -740,7 +755,10 @@ export class RedBeanNode {
}

} else {
let limitTemplate = this.knex.limit(1).toSQL().toNative();
let limitTemplate = this.knex.limit(1).toSQL()
if (this.dbType !== "pg") {
limitTemplate = limitTemplate.toNative()
}

// LIMIT 1
sql = sql + limitTemplate.sql.replace("select *", "");
Expand Down Expand Up @@ -772,6 +790,8 @@ export class RedBeanNode {

if (this.dbType == "mysql") {
result = result[0];
} else if (this.dbType == "pg") {
result = result.rows
}

return result;
Expand Down Expand Up @@ -832,7 +852,10 @@ export class RedBeanNode {
}

try {
return await this.getCell(`SELECT COUNT(*) FROM ?? ${where}`, [
// https://github.com/brianc/node-pg-types/blob/master/README.md#:~:text=Let%27s%20do%20something,would%20do%20this%3A
// https://www.postgresql.org/docs/8.2/functions-aggregate.html
// Because the postgres count(*) function returns a bigint type, but javascript does not support int8, node-postgres processes int8 as a string to prevent numeric overflow.
return await this.getCell(`SELECT COUNT(*)${this.dbType === "pg"? "::int" : ""} FROM ?? ${where}`, [
type,
...data,
], autoLimit)
Expand Down Expand Up @@ -1123,6 +1146,14 @@ export class RedBeanNode {
return this._modelList;
}

/**
* Transforming identifier names automatically to quoted versions for each dialect
* @param value
*/
wrapIdentifier(value: string) {
return this.knex.client.wrapIdentifierImpl(value)
}

}

export let R = new RedBeanNode();
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
"mocha": "~10.2.0",
"mocha-lcov-reporter": "~1.3.0",
"mysql": "~2.18.1",
"mysql2": "~3.11.3",
"np": "~7.7.0",
"nyc": "~15.1.0",
"pg": "^8.13.1",
"sqlite3": "~5.1.6",
"tcp-ping": "~0.1.1",
"ts-node": "~10.9.1",
Expand Down
Loading