You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit includes the following changes:
- Updated VitePress configuration to render outlines for h2 and h3 headings.
- Removed references to subqueries within other queries from `docs/advanced-queries.md` as this feature is not currently supported.
- Reviewed and made minor improvements to all documentation files, including:
- Clarifying explanations and code examples.
- Fixing typos and ensuring consistency.
- Improving the structure and readability of examples, particularly in `docs/databases/do.md`.
fields: ['users.name AS userName', 'orderCounts.count AS orderCount'],
107
-
join: {
108
-
type: 'LEFT',
109
-
table: qb.select('orders') // Subquery using select builder
110
-
.fields('user_id, COUNT(*) AS count')
111
-
.groupBy('user_id')
112
-
.getQueryAll(), // Get the Query object from SelectBuilder
113
-
alias: 'orderCounts',
114
-
on: 'users.id = orderCounts.user_id',
115
-
},
116
-
}).execute();
117
-
118
-
console.log('Users with order counts:', usersWithOrderCounts.results);
119
-
```
120
-
121
-
In this example, a subquery is built using `qb.select('orders')...getQueryAll()` to calculate the order count for each user. This subquery is then joined with the `users` table.
122
-
123
90
## Modular Select Queries
124
91
125
92
`workers-qb` provides a modular `select()` builder for constructing SELECT queries in a chainable and readable manner.
The `SelectBuilder` provides methods to execute the built query and retrieve results:
181
148
182
-
*`.execute()`: Executes the query and returns `ArrayResult` or `OneResult` based on the query configuration (implicitly calls `fetchAll` or `fetchOne` based on limit, etc. - in this case `fetchAll`).
149
+
*`.execute()`: Executes the query and returns `ArrayResult` or `OneResult` based on the nature of the constructed query (e.g., if `.limit(1)` is used, it might behave like `fetchOne`).
183
150
*`.all()`: Explicitly executes as `fetchAll` and returns `ArrayResult`.
184
151
*`.one()`: Explicitly executes as `fetchOne` and returns `OneResult`.
185
152
*`.count()`: Executes a `COUNT(*)` query based on the current builder configuration (ignoring fields, limit, offset, orderBy) and returns `CountResult`.
@@ -258,11 +225,6 @@ import { D1QB } from 'workers-qb';
258
225
259
226
// ... (D1QB initialization) ...
260
227
261
-
const usersInRoles =awaitqb.fetchAll({
262
-
tableName: 'users',
263
-
where: qb.select('roles').fields('id').where('is_admin = ?', true).getQueryAll(), // Subquery to get admin role IDs
264
-
}).execute() // Error! 'where' option should be 'whereIn' for IN clause
console.log('Users in specific role and department combinations:', usersInSpecificRolesMultipleColumns.results);
277
239
```
278
240
279
-
**Important:** Note the corrected example using `whereIn` method on the `SelectBuilder` for IN clauses, instead of trying to put a subquery directly into the `where` option, which is not intended for `IN` clause subqueries in this builder's API design.
Copy file name to clipboardExpand all lines: docs/basic-queries.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,7 +74,7 @@ export default {
74
74
Use the `createTable` method to define and create a new table. You need to specify the `tableName` and the `schema` as a string defining the table columns and their types.
For more complex conflict resolution, you can perform an UPSERT operation, updating specific columns if a conflict occurs. Use `onConflict` with an object to define the columns causing conflict, the data to update, and optional `where` conditions for the update.
255
255
256
256
```typescript
257
-
import { D1QB} from'workers-qb';
257
+
import { D1QB, Raw} from'workers-qb';// Ensure Raw is imported here
258
258
259
259
// ... (D1QB initialization) ...
260
260
@@ -413,13 +413,13 @@ import { D1QB } from 'workers-qb';
413
413
414
414
// ... (D1QB initialization) ...
415
415
416
-
typeUpdatedUser= {
416
+
typeUser= {// Assuming User type includes id, name, email
417
417
id:number;
418
418
name:string;
419
419
email:string;
420
420
};
421
421
422
-
const updatedUser =awaitqb.update<UpdatedUser>({
422
+
const updatedUser =awaitqb.update<User>({
423
423
tableName: 'users',
424
424
data: {
425
425
name: 'Corrected John Doe',
@@ -488,13 +488,13 @@ import { D1QB } from 'workers-qb';
488
488
489
489
// ... (D1QB initialization) ...
490
490
491
-
typeDeletedUser= {
491
+
typeUser= {// Assuming User type includes id, name, email
**To create a production-ready adapter for your database:**
101
101
102
-
1.**Choose a suitable database client library** for your target database in JavaScript/TypeScript (if one exists for Cloudflare Workers environment).
103
-
2.**Study the documentation of your chosen database and its client library.**
104
-
3.**Implement `execute`, `batchExecute`, and `lazyExecute` methods** in your custom QueryBuilder class, handling connection, query execution, parameter binding, error handling, and result formatting according to your database's specifics.
105
-
4.**Consider adding migration support** by extending or adapting `syncMigrationsBuilder` or `asyncMigrationsBuilder` if needed.
106
-
5.**Thoroughly test your custom adapter** with various query types and scenarios.
102
+
1.**Choose a suitable database client library** for your target database in JavaScript/TypeScript (if one exists for the Cloudflare Workers environment).
103
+
2.**Study the documentation of your chosen database and its client library** thoroughly.
104
+
3.**Implement `execute`, `batchExecute` (if applicable), and `lazyExecute`(if applicable) methods** in your custom `QueryBuilder` class. This involves handling connections, query execution, parameter binding, error management, and transforming results into the `workers-qb` expected formats.
105
+
4.**Consider adding migration support** by extending or adapting `syncMigrationsBuilder` or `asyncMigrationsBuilder` if your database requires schema management.
106
+
5.**Thoroughly test your custom adapter** with a comprehensive suite of query types and edge cases.
107
107
108
108
By creating custom database adapters, you can extend the reach of `workers-qb` to support a wide variety of SQL and SQL-like databases in your Cloudflare Worker projects.
Copy file name to clipboardExpand all lines: docs/migrations.md
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,15 +13,17 @@ Database migrations are scripts that define changes to your database schema. Eac
13
13
14
14
## Creating Migration Files
15
15
16
-
Each migration is defined as an object with a `name` and `sql` property. The `name` should be a unique identifier for the migration. The `sql` property contains the SQL statements to be executed.
16
+
Each migration is defined as an object with a `name` and `sql` property. The `name` should be a unique identifier for the migration (e.g., `YYYYMMDDHHMMSS_descriptive_name` or a sequential number like `0001_descriptive_name`). The `sql` property contains the SQL statements to be executed.
17
+
18
+
You can organize your migration files as you see fit. For example, you might have one `.ts` file per migration object, or a single file that exports an array of all migration objects.
17
19
18
20
**Example Migration Structure (in code):**
19
21
20
22
```typescript
21
-
//Optinally you can type the migrations with Migration
23
+
//Optionally you can type the migrations with Migration
22
24
import { typeMigration } from'workers-qb';
23
25
24
-
// migrations/0001_create_users_table.ts (if using separate files)
26
+
//Example: migrations/0001_create_users_table.ts (if using separate files)
25
27
exportconst createUsersTableMigration = {
26
28
name: '0001_create_users_table',
27
29
sql: `
@@ -88,12 +90,14 @@ export class MyDurableObject extends DurableObject {
88
90
89
91
this.#qb=newDOQB(this.ctx.storage.sql);
90
92
voidthis.ctx.blockConcurrencyWhile(async () => {
93
+
// Assuming 'migrations' is an array of Migration objects defined elsewhere
0 commit comments