Skip to content

Commit

Permalink
Add fields default value and simplified where interface when calling …
Browse files Browse the repository at this point in the history
…without parameters
  • Loading branch information
G4brym committed Mar 2, 2024
1 parent d504cf5 commit ccc71f7
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 74 deletions.
1 change: 0 additions & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ console.log(inserted)
console.log('Selecting rows...')
const selected = await qb.fetchAll({
tableName: 'testTable',
fields: '*',
})
console.log(selected)
```
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export default {
level: number
}

// Generated query: SELECT * FROM employees WHERE active = ?1 LIMIT 1
const employeeList = await qb
.fetchAll<Employee>({
tableName: 'employees',
fields: '*',
where: {
conditions: 'active = ?1',
params: [true],
Expand Down Expand Up @@ -101,6 +101,7 @@ export default {
const qb = new PGQB(new Client(env.DB_URL))
await qb.connect()

// Generated query: SELECT count(*) as count FROM employees WHERE active = ?1 LIMIT 1
const fetched = await qb
.fetchOne({
tableName: 'employees',
Expand Down
2 changes: 1 addition & 1 deletion docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ plugins:
nav:
- workers-qb: index.md
- getting-started.md
- type-check.md
- basic-queries.md
- upgrading-from-0.x.md
- Supported Databases:
- databases/cloudflare-d1.md
- databases/postgresql.md
Expand Down
17 changes: 16 additions & 1 deletion docs/pages/advanced-queries/fields.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
The field parameter can receive a string of a list of strings, you can use this to leverage your python code
to don't have to join the string together

!!! note

Starting from version `1.2.1` the `fields` now default to `*` when left blank.

## Selecting with a string

```ts
Expand All @@ -9,7 +13,18 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
// fields: '*', this is the default value
})
.execute()
```

```ts
const qb = new D1QB(env.DB)

const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: 'name',
})
.execute()
```
Expand Down
3 changes: 0 additions & 3 deletions docs/pages/advanced-queries/join.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
join: {
type: JoinTypes.LEFT,
table: 'payroll',
Expand All @@ -77,7 +76,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
join: [
{
table: 'payroll',
Expand All @@ -100,7 +98,6 @@ const qb = new D1QB(env.DB)
const result = new QuerybuilderTest()
.fetchAll({
tableName: 'testTable',
fields: '*',
where: {
conditions: 'field = ?1',
params: ['test'],
Expand Down
2 changes: 0 additions & 2 deletions docs/pages/advanced-queries/limit.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
limit: 5,
})
.execute()
Expand All @@ -22,7 +21,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
limit: 5,
offset: 10,
})
Expand Down
3 changes: 0 additions & 3 deletions docs/pages/advanced-queries/orderBy.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
orderBy: 'id',
})
.execute()
Expand All @@ -29,7 +28,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
orderBy: { id: OrderTypes.DESC },
})
.execute()
Expand All @@ -43,7 +41,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
orderBy: { id: 'DESC' },
})
.execute()
Expand Down
40 changes: 37 additions & 3 deletions docs/pages/advanced-queries/where.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
The where parameter can receive multiple inputs and all of them will result in the same query:
Currently due to a limitation in D1, there is only support for ordered parameters (but named parameters is in progress)

!!! note

Starting from version `1.2.1`, `where` now has a simplified interface, to use when you don't have parameters
```
{
...
where: 'active = true'
// or: where: ['active = true', 'department = "HR"'],
}
```

## Simple where

```ts
Expand All @@ -9,7 +20,19 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
where: 'active = true',
})
.execute()
```

The example above is the same as this

```ts
const qb = new D1QB(env.DB)

const fetched = await qb
.fetchAll({
tableName: 'employees',
where: {
conditions: 'active = true',
},
Expand All @@ -25,7 +48,19 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
where: ['active = true', 'department = "HR"'],
})
.execute()
```

The example above is the same as this

```ts
const qb = new D1QB(env.DB)

const fetched = await qb
.fetchAll({
tableName: 'employees',
where: {
conditions: ['active = true', 'department = "HR"'],
},
Expand All @@ -41,7 +76,6 @@ const qb = new D1QB(env.DB)
const fetched = await qb
.fetchAll({
tableName: 'employees',
fields: '*',
where: {
conditions: 'department = ?1',
params: ['HR'],
Expand Down
1 change: 0 additions & 1 deletion docs/pages/databases/bring-your-own-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const qb = new PGQB(new Client('postgresql://user:password@hostname:5432/db_name
const fetched = await qb
.fetchAll({
tableName: 'devices',
fields: '*',
})
.execute()
```
2 changes: 0 additions & 2 deletions docs/pages/databases/cloudflare-d1.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ export default {
const fetched = await qb.batchExecute([
qb.fetchAll({
tableName: 'tableA',
fields: '*',
}),
qb.fetchAll({
tableName: 'tableB',
fields: '*',
}),
])

Expand Down
22 changes: 16 additions & 6 deletions docs/pages/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
npm install workers-qb --save
```

## Example Cloudflare D1 Usage
## Example for Cloudflare Workers D1

```ts
import { D1QB } from 'workers-qb'
Expand All @@ -17,25 +17,34 @@ export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const qb = new D1QB(env.DB)

const fetched = await qb
.fetchOne({
type Employee = {
name: string
role: string
level: number
}

// Generated query: SELECT * FROM employees WHERE active = ?1 LIMIT 1
const employeeList = await qb
.fetchOne<Employee>({
tableName: 'employees',
fields: 'count(*) as count',
where: {
conditions: 'active = ?1',
params: [true],
},
})
.execute()

// You get IDE type hints on each employee data, like:
// employeeList.results[0].name

return Response.json({
activeEmployees: fetched.results?.count || 0,
activeEmployees: employeeList.results?.length || 0,
})
},
}
```

## Example Cloudflare Workers with PostgreSQL Usage
## Example for Cloudflare Workers with PostgreSQL

Remember to close the connection using `ctx.waitUntil(qb.close());` or `await qb.close();` at the end of your request.
You may also reuse this connection to execute multiple queries, or share it between multiple requests if you are using
Expand All @@ -62,6 +71,7 @@ export default {
const qb = new PGQB(new Client(env.DB_URL))
await qb.connect()

// Generated query: SELECT count(*) as count FROM employees WHERE active = ?1 LIMIT 1
const fetched = await qb
.fetchOne({
tableName: 'employees',
Expand Down
31 changes: 31 additions & 0 deletions docs/pages/type-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Starting from version `1.2.0` you can now get type checks when writing queries!

Just define your table schema as a typescript `type` and reference it when calling any function.

!!! note

This is available to all methods, even `Delete` or `Update` when calling with returning.

```ts
const qb = new D1QB(env.DB)

type Employee = {
name: string
role: string
level: number
}

// Generated query: SELECT * FROM employees WHERE active = ?1 LIMIT 1
const employeeList = await qb
.fetchOne<Employee>({
tableName: 'employees',
where: {
conditions: 'active = ?1',
params: [true],
},
})
.execute()

// You will get type checks on each row, like:
employeeList.results[0].name
```
25 changes: 0 additions & 25 deletions docs/pages/upgrading-from-0.x.md

This file was deleted.

Loading

0 comments on commit ccc71f7

Please sign in to comment.