firebird-query
Advanced tools
Comparing version
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "node-firebird plugin for easy and safe query building.", | ||
@@ -13,3 +13,3 @@ "author": { | ||
"engines": { | ||
"node": ">= 16.17.0" | ||
"node": ">= 16.14.2" | ||
}, | ||
@@ -16,0 +16,0 @@ "main": "dist/index.js", |
151
readme.md
@@ -0,53 +1,60 @@ | ||
# firebird-query | ||
A node-firebird wrapper for easy and safe query building. | ||
A node-firebird wrapper for easy and safe query building. | ||
> This package works with **node-firebird 1.1.5** under the hood. | ||
## Installation | ||
npm install firebird-query | ||
npm uninstall node-firebird | ||
npm install firebird-query | ||
## Setting up | ||
In a **db.service. js** file | ||
```typescript | ||
const { pool } = require('firebird-query'); | ||
const max = 10; /* count of opened sockets */ | ||
const options = { | ||
const { FirebirdQuery } = require('firebird-query'); | ||
const max = 10; /* opened sockets */ | ||
const options = { | ||
host: '000.000.000.000', | ||
port: 3050, | ||
database: '/path/Database/FILE.FDB', | ||
user: 'SYSDBA', | ||
password: 'my_secure_password' | ||
port: 3050, | ||
database: '/path/Database/FILE.FDB', | ||
user: 'SYSDBA', | ||
password: 'my_secure_password' | ||
}; | ||
export const db = pool(max, options); | ||
export const db = new FirebirdQuery(options, max); | ||
``` | ||
## Usage | ||
## Usage | ||
**queryRaw** | ||
Input a template string literal. Parameters will automatically be escaped to avoid query injection. | ||
Returns an array of objects | ||
**queryRaw** | ||
- Input: template string literal. Parameters will automatically be escaped to avoid query injection. | ||
- Return: array of objects | ||
- Supports pagination | ||
```typescript | ||
import { db } from './db.service.js'; | ||
import { db } from './db.service.js'; | ||
const result = await db.queryRaw` | ||
SELECT 1 AS TEST | ||
FROM RDB$DATABASE;`.execute() | ||
console.log(result); // --> [ { TEST: 1 } ] | ||
... | ||
const result = db.queryRaw` | ||
SELECT COD, NAME | ||
FROM USERS | ||
WHERE SIGN_UP_DATE < ${date}`.execute(); | ||
const result = db.queryRaw` | ||
SELECT COD, NAME | ||
FROM USERS | ||
WHERE SIGN_UP_DATE < ${date}`.execute(); | ||
console.log(result); | ||
// --> [ { COD: 1, NAME: 'JOHN' }, { COD: 2, NAME: 'JANE' } ] | ||
const result = db.queryRaw` | ||
SELECT COD, NAME | ||
FROM USERS | ||
WHERE SIGN_UP_DATE < ${date}`.paginated(1,2); // take: 1, page: 2 | ||
console.log(result); | ||
// --> [ { COD: 1, NAME: 'JOHN' }, { COD: 2, NAME: 'JANE' } ] | ||
// --> [ { COD: 2, NAME: 'JANE' } ] | ||
``` | ||
**insertOne** | ||
- rowValues: the object keys correspond to database column names | ||
- returning: optional array of string with column names | ||
**insertOne** | ||
- rowValues: the object keys correspond to database column names | ||
- returning: optional array of string with column names to be returned | ||
```typescript | ||
const result = await db.insertOne({ | ||
const result = await db.insertOne({ | ||
tableName: 'USERS', | ||
@@ -61,7 +68,11 @@ rowValues: { | ||
``` | ||
**insertMany** | ||
Performs an efficient INSERT statement and inserts multiple rows in a single query. | ||
**insertMany** | ||
Performs an efficient INSERT statement and inserts multiple rows in a single query. | ||
Does not support returning clause. | ||
```typescript | ||
const result = await db.insertMany({ | ||
const result = await db.insertMany({ | ||
tableName: 'USERS', | ||
@@ -71,12 +82,17 @@ columnNames: ['NAME', 'PHONE'], | ||
{ NAME: 'John', PHONE: '555-555-5555' }, | ||
{ NAME: 'Jane', PHONE: '555-555-5555' }, | ||
{ NAME: 'Jane', PHONE: '555-555-0000' }, | ||
] | ||
}).execute(); | ||
console.log(result); // --> 2 rows inserted | ||
``` | ||
**updateOne** | ||
Update a single row. Supports returning clause with **returning** optional array of strings parameter. | ||
**updateOne** | ||
Update a single row. Supports returning. | ||
```typescript | ||
const result = await db.updateOne({ | ||
tableName: 'USERS', | ||
const result = await db.updateOne({ | ||
tableName: 'USERS', | ||
rowValues: { | ||
@@ -91,11 +107,13 @@ NAME: 'John', | ||
}); | ||
console.log(result); // --> { COD: 1 } | ||
``` | ||
**updateOrInsert** | ||
Update or insert a single row. Supports returning clause with **returning** optional array of strings parameter. | ||
**updateOrInsert** | ||
Update or insert a single row. Supports returning clause | ||
> WARNING: Ensure there’s only one potential row affected. | ||
```typescript | ||
const result = await db.updateOrInsert({ | ||
const result = await db.updateOrInsert({ | ||
tableName: 'USERS', | ||
@@ -108,8 +126,11 @@ rowValues: { | ||
}); | ||
console.log(result); // --> { COD: 1 } | ||
``` | ||
## Typescript usage | ||
Each method counts on typescript inference if a returning parameter is provided. | ||
Each method counts on typescript inference as long as a return parameter is provided. | ||
### queryRaw | ||
@@ -120,24 +141,26 @@ The ouput must be manually inferred. | ||
```typescript | ||
const result = db.queryRaw<{ COD: number }>` | ||
SELECT COD | ||
FROM USERS | ||
WHERE COD = ${1}`.execute(); | ||
const result = db.queryRaw<{ COD: number }>` | ||
SELECT COD | ||
FROM USERS | ||
WHERE COD = ${1}`.execute(); | ||
console.log(result); // --> [ { COD: 1 } ] | ||
``` | ||
## Transactions | ||
1. Get a pool from the **db** instance. | ||
2. From the same instance, get the **$Firebird** object that contains thw whole Firebird module. | ||
3. Take advantage of **queryRaw** method to build a safe query. | ||
4. Every transaction logic is now available. | ||
## initTransaction | ||
An async method that returns a ISOLATION_READ_COMMITTED transaction instance to work with. It has the same methods to query and mutate the database in addition to | ||
1. commit | ||
2. close | ||
3. rollback | ||
```typescript | ||
db.$getPool().then(pool => { | ||
pool.transaction(db.$Firebird.ISOLATION_READ_COMMITTED, (err, transaction) => { | ||
const safeQuery = db.queryRaw`SELECT 1 AS test FROM RDB$DATABASE;`.getQuery(); | ||
transaction.query(safeQuery, [], (err, res) => { | ||
console.log(res); // --> [ { TEST: 1 } ] | ||
}); | ||
}); | ||
}); | ||
// recommended usage | ||
db.initTransaction().then(async (t) => { | ||
// t is scoped in this async function. | ||
//Every mutation and query correspond to that specific transaction. | ||
}) | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
161
16.67%0
-100%58961
-0.45%