firebird-query
Advanced tools
Comparing version
@@ -47,3 +47,2 @@ import Firebird from "node-firebird"; | ||
export type QueryParam = PrimetiveValue | WhereObject; | ||
export declare const paginatedQuery: (query: string, take: number, page: number) => string; | ||
export type InsertOneParams<T extends { | ||
@@ -56,5 +55,2 @@ [key: string]: any; | ||
}; | ||
export declare const insertOneQuery: <T extends { | ||
[key: string]: any; | ||
}>(params: InsertOneParams<T>) => string; | ||
export type InsertParams<T> = { | ||
@@ -67,3 +63,2 @@ readonly tableName: string; | ||
}; | ||
export declare const insertManyQuery: <T>({ tableName, columnNames, rowValues, }: InsertParams<T>) => string; | ||
export type UpdateOneParams<T> = { | ||
@@ -77,4 +72,3 @@ readonly tableName: string; | ||
}; | ||
export declare const updateOneQuery: <T = void>({ tableName, rowValues, returning, where, }: UpdateOneParams<T>) => string; | ||
export type UpdateOrInsertParams<T> = { | ||
type UpdateOrInsertParams<T> = { | ||
readonly tableName: string; | ||
@@ -86,3 +80,2 @@ readonly rowValues: { | ||
}; | ||
export declare const updateOrInsertQuery: <T>({ tableName, rowValues, returning, }: UpdateOrInsertParams<T>) => string; | ||
export type DeleteOneParams<T> = { | ||
@@ -93,4 +86,4 @@ readonly tableName: string; | ||
}; | ||
export declare const deleteOneQuery: <T>(params: DeleteOneParams<T>) => string; | ||
export declare class FirebirdQuery { | ||
private db?; | ||
private conn; | ||
@@ -97,0 +90,0 @@ constructor(options?: Firebird.Options, max?: number); |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "node-firebird plugin for easy and safe query building.", | ||
@@ -15,9 +15,8 @@ "author": { | ||
}, | ||
"main": "./lib/cjs/index.js", | ||
"types": "./lib/cjs/types/index.d.ts", | ||
"main": "./lib/esm/index.js", | ||
"types": "./lib/esm/types/index.d.ts", | ||
"scripts": { | ||
"clean": "rm -rf ./lib", | ||
"build": "npm run clean && npm run build:esm && npm run build:cjs", | ||
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs", | ||
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json", | ||
"build": "npm run clean && npm run build:esm", | ||
"build:esm": "tsc && mv lib/esm/index.js lib/esm/index.mjs", | ||
"prepack": "npm run build" | ||
@@ -58,6 +57,2 @@ }, | ||
"default": "./lib/esm/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./lib/cjs/types/index.d.ts", | ||
"default": "./lib/cjs/index.js" | ||
} | ||
@@ -64,0 +59,0 @@ } |
291
readme.md
@@ -0,20 +1,33 @@ | ||
# firebird-query | ||
A node-firebird wrapper for easy and safe query building. | ||
# firebird-query | ||
A node-firebird wrapper for easy and safe query building. | ||
> **Support with a start ⭐️** | ||
## Installation | ||
```bash | ||
npm install firebird-query | ||
npm install firebird-query | ||
``` | ||
## Setting up | ||
### Quick setup | ||
```typescript | ||
const { FirebirdQuery } = require('firebird-query'); | ||
const max = 10; /* opened sockets */ | ||
import { FirebirdQuery } from 'firebird-query'; | ||
const maxConn = 10; /* optional */ | ||
const options = { | ||
@@ -27,7 +40,12 @@ host: '000.000.000.000', | ||
}; | ||
export const db = new FirebirdQuery(options, max); | ||
export const db = new FirebirdQuery(options, maxConn); | ||
``` | ||
### Or | ||
Configure a `.env` | ||
Configure a `.env` | ||
```batch | ||
@@ -42,16 +60,26 @@ DB_HOST="000.000.000.000" | ||
```typescript | ||
export const db = new FirebirdQuery(); | ||
export const db = new FirebirdQuery(); // use it wherever you want | ||
``` | ||
## Usage | ||
### queryRaw | ||
- Input: template string literal. Parameters are automatically escaped avoiding query injection. | ||
- Execution return: array of objects. | ||
- Supports pagination. | ||
- Input: template string literal. Parameters are automatically escaped avoiding query injection. | ||
- Execution return: array of objects. | ||
- Supports pagination. | ||
```typescript | ||
import { db } from './db.service.js'; | ||
const result = db.queryRaw` | ||
const result = await db.queryRaw` | ||
SELECT COD, NAME | ||
@@ -64,94 +92,165 @@ FROM USERS | ||
const result = db.queryRaw` | ||
SELECT COD, NAME | ||
FROM USERS | ||
WHERE SIGN_UP_DATE < ${date}`.paginated(1,2); // take: 1, page: 2 | ||
WHERE SIGN_UP_DATE < ${date}`.paginated(1,2); | ||
// take: 1, page: 2 (default: 1) | ||
console.log(result); | ||
console.log(result); | ||
// --> [ { COD: 2, NAME: 'JANE' } ] | ||
``` | ||
#### Where clauses | ||
An object can be provided instead of a raw value. | ||
- Object keys correspond to column names. Object values to column | ||
values. | ||
- Multiple keys are combined as `AND` clauses | ||
- Object keys correspond to column names. Object values to column values. | ||
- Multiple keys are combined as `AND` clauses | ||
```typescript | ||
const result = t.queryRaw`SELECT COD, NAME FROM USERS WHERE ${{ | ||
COD: 1, | ||
NAME: "John", | ||
const result = t.queryRaw`SELECT COD, NAME FROM USERS WHERE ${{ | ||
COD: 1, | ||
NAME: "John", | ||
}}`.getQuery(); | ||
console.log(result); | ||
// SELECT COD, NAME FROM USERS WHERE COD = '1' AND NAME = 'John' | ||
``` | ||
#### Conditional statements | ||
If a where clause resolved to `undefined`, it will be replaced with a tautology, making it irrelevant to the query result . | ||
Take advantage of this behavior to conditionally add statements. | ||
```typescript | ||
const name = "Tom"; | ||
const result = t.queryRaw`SELECT COD, NAME FROM USERS WHERE ${{ | ||
COD: name.startsWith("J") ? 1 : undefined, | ||
NAME: name, | ||
}}`.getQuery(); | ||
const name = "Tom"; | ||
const result = await t.queryRaw` | ||
SELECT COD, NAME FROM USERS WHERE ${{ | ||
COD: name.startsWith("J") ? 1 : undefined, | ||
NAME: name | ||
}}`.getQuery(); | ||
console.log(result); | ||
// SELECT COD, NAME FROM USERS WHERE 1=1 AND NAME = 'Tom' | ||
``` | ||
#### Advance statements | ||
Set anything as object key. | ||
Set anything as object key. | ||
This example handles **case insensitive** queries. | ||
```typescript | ||
const name = "Tom"; | ||
const result = t.queryRaw`SELECT COD, NAME FROM USERS WHERE ${{ | ||
["LOWER(NAME)"]: name.toLowerCase(), | ||
}}`.getQuery(); | ||
const name = "Tom"; | ||
const result = await t.queryRaw` | ||
SELECT COD, NAME FROM USERS WHERE ${{ | ||
["LOWER(NAME)"]: name.toLowerCase(), | ||
}}`.getQuery(); | ||
console.log(result); | ||
// SELECT COD, NAME FROM USERS WHERE LOWER(NAME) = 'tom' | ||
``` | ||
#### Operators | ||
- Number operators | ||
- ne: not equal != | ||
- gt: greater than > | ||
- gte: greater than or equal >= | ||
- lt: lower than < | ||
- lte: lower than or equal <= | ||
- between: { from: number; to: number } | ||
- IN: number array. [1,2,3...] | ||
- notIN: NOT IN. Number array. | ||
- Date operators | ||
- ne: not equal != | ||
- gt: greater than > | ||
- gte: greater than or equal >= | ||
- lt: lower than < | ||
- lte: lower than or equal <= | ||
- between: { from: Date; to: Date } | ||
- IN: array | ||
- notIN. array. | ||
- String operators | ||
- ne: not equal | ||
- IN | ||
- notIN | ||
- startsWith | ||
- endsWith | ||
- contains | ||
- Number operators | ||
- ne: not equal != | ||
- gt: greater than > | ||
- gte: greater than or equal >= | ||
- lt: lower than < | ||
- lte: lower than or equal <= | ||
- between: { from: number; to: number } | ||
- IN: number array. [1,2,3...] | ||
- notIN: NOT IN. Number array. | ||
- Date operators | ||
- ne: not equal != | ||
- gt: greater than > | ||
- gte: greater than or equal >= | ||
- lt: lower than < | ||
- lte: lower than or equal <= | ||
- between: { from: Date; to: Date } | ||
- IN: array | ||
- notIN. array. | ||
- String operators | ||
- ne: not equal | ||
- IN | ||
- notIN | ||
- startsWith | ||
- endsWith | ||
- contains | ||
```typescript | ||
const name = "Tom"; | ||
const result = t.queryRaw`SELECT COD, NAME FROM USERS WHERE ${{ | ||
COD: { gte: 1 }, | ||
NAME: { startsWith: name }, | ||
}}`.getQuery(); | ||
const name = "Tom"; | ||
const result = await t.queryRaw` | ||
SELECT COD, NAME FROM USERS WHERE ${{ | ||
COD: { gte: 1 }, | ||
NAME: { startsWith: name }, | ||
}}`.getQuery(); | ||
console.log(result); | ||
// SELECT COD, NAME FROM USERS WHERE COD >= '1' AND NAME LIKE 'Tom%' | ||
``` | ||
### insertOne | ||
- rowValues: the object keys correspond to database column names | ||
- returning: optional array of string with column names to be returned | ||
- returning: optional array of string with column names to be returned | ||
```typescript | ||
@@ -165,12 +264,19 @@ const result = await db.insertOne({ | ||
}).execute() | ||
console.log(result); // --> { COD: 3 } | ||
``` | ||
### 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({ | ||
@@ -186,12 +292,15 @@ tableName: 'USERS', | ||
console.log(result); // --> 2 rows inserted | ||
``` | ||
**updateOne** | ||
Update a single row. Supports returning. | ||
Update a single row. Optionally, supports returning. | ||
```typescript | ||
const result = await db.updateOne({ | ||
tableName: 'USERS', | ||
tableName: 'USERS', | ||
rowValues: { | ||
@@ -201,3 +310,3 @@ NAME: 'John', | ||
}, | ||
conditions: { | ||
where: { | ||
COD: 1 | ||
@@ -211,3 +320,6 @@ }, | ||
**updateOrInsert** | ||
Update or insert a single row. Supports returning clause | ||
@@ -217,2 +329,4 @@ | ||
```typescript | ||
@@ -229,12 +343,20 @@ const result = await db.updateOrInsert({ | ||
console.log(result); // --> { COD: 1 } | ||
``` | ||
## Typescript usage | ||
Each method counts on typescript inference as long as a return parameter is provided. | ||
Each method counts on typescript inference as long as a return parameter is provided. | ||
### queryRaw | ||
The ouput must be manually inferred. | ||
> The result is always an array of the type provided | ||
@@ -244,26 +366,37 @@ | ||
```typescript | ||
const result = db.queryRaw<{ COD: number }>` | ||
const result = await db.queryRaw<{ COD: number }>` | ||
SELECT COD | ||
FROM USERS | ||
WHERE COD = ${1}`.execute(); | ||
console.log(result); // --> [ { COD: 1 } ] | ||
``` | ||
## 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 | ||
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 | ||
// recommended usage | ||
db.initTransaction().then(async (t) => { | ||
// t(ransaction) is scoped in this async function. | ||
//Every query and mutation correspond to this specific transaction. | ||
// t(ransaction) is scoped into this async function. | ||
//Every query or mutation correspond to this specific transaction. | ||
}) | ||
``` | ||
## Support with a start ⭐️ | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
392
51.35%5
-50%31236
-44.21%6
-33.33%604
-49.07%