@truto/ginger 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -8
- package/dist/adapters/bun-sqlite.d.ts +2 -1
- package/dist/adapters/bun-sqlite.d.ts.map +1 -1
- package/dist/adapters/bun-sqlite.js +39 -75
- package/dist/adapters/bun-sqlite.js.map +1 -1
- package/dist/adapters/durable-object.d.ts +1 -1
- package/dist/adapters/durable-object.d.ts.map +1 -1
- package/dist/adapters/durable-object.js +39 -79
- package/dist/adapters/durable-object.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -1
- package/dist/errors.js.map +1 -1
- package/dist/example.d.ts +5 -1
- package/dist/example.d.ts.map +1 -1
- package/dist/example.js +6 -31
- package/dist/example.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +9 -19
- package/dist/pagination.js.map +1 -1
- package/dist/service.d.ts +7 -1
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +87 -76
- package/dist/service.js.map +1 -1
- package/dist/sql-builder.d.ts.map +1 -1
- package/dist/sql-builder.js +13 -10
- package/dist/sql-builder.js.map +1 -1
- package/dist/types.d.ts +10 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Built with TypeScript and [Zod v4](https://zod.dev/) for complete type safety. A
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
bun add ginger
|
|
10
|
+
bun add @truto/ginger
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
Peer dependencies:
|
|
@@ -40,7 +40,7 @@ import {
|
|
|
40
40
|
type Database,
|
|
41
41
|
type JoinDef,
|
|
42
42
|
type SecretFieldDef,
|
|
43
|
-
} from 'ginger'
|
|
43
|
+
} from '@truto/ginger'
|
|
44
44
|
|
|
45
45
|
// ── Schemas ──────────────────────────────────────────────────────────
|
|
46
46
|
|
|
@@ -134,6 +134,7 @@ function createUsersService(
|
|
|
134
134
|
joins: userJoins,
|
|
135
135
|
secrets: userSecrets,
|
|
136
136
|
encryptionKeys,
|
|
137
|
+
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
|
|
137
138
|
hooks: {
|
|
138
139
|
list: {
|
|
139
140
|
before: async (ctx: any) => {
|
|
@@ -209,7 +210,7 @@ Ginger works with any SQLite database that satisfies the `Database` interface. T
|
|
|
209
210
|
**Cloudflare D1** — pass the binding directly, no adapter needed:
|
|
210
211
|
|
|
211
212
|
```typescript
|
|
212
|
-
import { createService } from 'ginger'
|
|
213
|
+
import { createService } from '@truto/ginger'
|
|
213
214
|
|
|
214
215
|
const service = createService({
|
|
215
216
|
table: 'users',
|
|
@@ -222,7 +223,7 @@ const service = createService({
|
|
|
222
223
|
|
|
223
224
|
```typescript
|
|
224
225
|
import { Database } from 'bun:sqlite'
|
|
225
|
-
import { createService, fromBunSqlite } from 'ginger'
|
|
226
|
+
import { createService, fromBunSqlite } from '@truto/ginger'
|
|
226
227
|
|
|
227
228
|
const bunDb = new Database('myapp.sqlite')
|
|
228
229
|
const service = createService({
|
|
@@ -236,7 +237,7 @@ const service = createService({
|
|
|
236
237
|
|
|
237
238
|
```typescript
|
|
238
239
|
import { DurableObject } from 'cloudflare:workers'
|
|
239
|
-
import { createService, fromDurableObjectStorage } from 'ginger'
|
|
240
|
+
import { createService, fromDurableObjectStorage } from '@truto/ginger'
|
|
240
241
|
|
|
241
242
|
export class MyDO extends DurableObject {
|
|
242
243
|
service = createService({
|
|
@@ -250,7 +251,7 @@ export class MyDO extends DurableObject {
|
|
|
250
251
|
### Service configuration
|
|
251
252
|
|
|
252
253
|
```typescript
|
|
253
|
-
import { createService, z } from 'ginger'
|
|
254
|
+
import { createService, z } from '@truto/ginger'
|
|
254
255
|
|
|
255
256
|
const service = createService({
|
|
256
257
|
table: 'users',
|
|
@@ -266,6 +267,7 @@ const service = createService({
|
|
|
266
267
|
deps: { teams: teamsService }, // other services
|
|
267
268
|
primaryKey: 'id', // default "id"
|
|
268
269
|
defaultOrderBy: { column: 'created_at', direction: 'desc' },
|
|
270
|
+
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
|
|
269
271
|
keyProvider: customProvider, // or pass encryptionKeys: { ... }
|
|
270
272
|
})
|
|
271
273
|
```
|
|
@@ -398,7 +400,7 @@ const service = createService({
|
|
|
398
400
|
Generate a key:
|
|
399
401
|
|
|
400
402
|
```typescript
|
|
401
|
-
import { generateSecretKey } from 'ginger'
|
|
403
|
+
import { generateSecretKey } from '@truto/ginger'
|
|
402
404
|
|
|
403
405
|
const key = await generateSecretKey()
|
|
404
406
|
// → base64-encoded 256-bit key
|
|
@@ -512,7 +514,7 @@ import {
|
|
|
512
514
|
EncryptionError,
|
|
513
515
|
HookError,
|
|
514
516
|
CursorError,
|
|
515
|
-
} from 'ginger'
|
|
517
|
+
} from '@truto/ginger'
|
|
516
518
|
|
|
517
519
|
try {
|
|
518
520
|
await service.get(id, { auth })
|
|
@@ -18,6 +18,7 @@ export interface BunSqliteStatement {
|
|
|
18
18
|
export interface BunSqliteDatabase {
|
|
19
19
|
prepare(query: string): BunSqliteStatement;
|
|
20
20
|
exec(query: string): void;
|
|
21
|
+
serialize?(): Uint8Array;
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
24
|
* Wrap a Bun `Database` (from `bun:sqlite`) so it satisfies the
|
|
@@ -26,7 +27,7 @@ export interface BunSqliteDatabase {
|
|
|
26
27
|
* @example
|
|
27
28
|
* ```typescript
|
|
28
29
|
* import { Database } from 'bun:sqlite'
|
|
29
|
-
* import { createService, fromBunSqlite } from 'ginger'
|
|
30
|
+
* import { createService, fromBunSqlite } from '@truto/ginger'
|
|
30
31
|
*
|
|
31
32
|
* const bunDb = new Database(':memory:')
|
|
32
33
|
* const db = fromBunSqlite(bunDb)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bun-sqlite.d.ts","sourceRoot":"","sources":["../../src/adapters/bun-sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EAIT,MAAM,aAAa,CAAA;AAEpB;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAA;IAClC,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,CAAA;IACpC,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG;QACzB,OAAO,EAAE,MAAM,CAAA;QACf,eAAe,EAAE,MAAM,GAAG,MAAM,CAAA;KACjC,CAAA;CACF;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB,CAAA;IAC1C,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"bun-sqlite.d.ts","sourceRoot":"","sources":["../../src/adapters/bun-sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EAIT,MAAM,aAAa,CAAA;AAEpB;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAA;IAClC,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,CAAA;IACpC,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG;QACzB,OAAO,EAAE,MAAM,CAAA;QACf,eAAe,EAAE,MAAM,GAAG,MAAM,CAAA;KACjC,CAAA;CACF;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB,CAAA;IAC1C,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACzB,SAAS,CAAC,IAAI,UAAU,CAAA;CACzB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,iBAAiB,GAAG,QAAQ,CA2FhE"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @example
|
|
6
6
|
* ```typescript
|
|
7
7
|
* import { Database } from 'bun:sqlite'
|
|
8
|
-
* import { createService, fromBunSqlite } from 'ginger'
|
|
8
|
+
* import { createService, fromBunSqlite } from '@truto/ginger'
|
|
9
9
|
*
|
|
10
10
|
* const bunDb = new Database(':memory:')
|
|
11
11
|
* const db = fromBunSqlite(bunDb)
|
|
@@ -22,78 +22,40 @@ export function fromBunSqlite(bunDb) {
|
|
|
22
22
|
return makebound([...values, ...more]);
|
|
23
23
|
},
|
|
24
24
|
first() {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return Promise.resolve(row ?? null);
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
return Promise.resolve(null);
|
|
31
|
-
}
|
|
25
|
+
const row = stmt.get(...values);
|
|
26
|
+
return Promise.resolve(row ?? null);
|
|
32
27
|
},
|
|
33
28
|
all() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
return Promise.resolve({
|
|
52
|
-
success: false,
|
|
53
|
-
results: [],
|
|
54
|
-
meta: {
|
|
55
|
-
duration: 0,
|
|
56
|
-
size_after: 0,
|
|
57
|
-
rows_read: 0,
|
|
58
|
-
rows_written: 0,
|
|
59
|
-
last_row_id: 0,
|
|
60
|
-
changed_db: false,
|
|
61
|
-
changes: 0,
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
}
|
|
29
|
+
const rows = stmt.all(...values);
|
|
30
|
+
return Promise.resolve({
|
|
31
|
+
success: true,
|
|
32
|
+
results: rows,
|
|
33
|
+
meta: {
|
|
34
|
+
duration: 0,
|
|
35
|
+
size_after: 0,
|
|
36
|
+
rows_read: rows.length,
|
|
37
|
+
rows_written: 0,
|
|
38
|
+
last_row_id: 0,
|
|
39
|
+
changed_db: false,
|
|
40
|
+
changes: 0,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
65
43
|
},
|
|
66
44
|
run() {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
return Promise.resolve({
|
|
85
|
-
success: false,
|
|
86
|
-
meta: {
|
|
87
|
-
duration: 0,
|
|
88
|
-
size_after: 0,
|
|
89
|
-
rows_read: 0,
|
|
90
|
-
rows_written: 0,
|
|
91
|
-
last_row_id: 0,
|
|
92
|
-
changed_db: false,
|
|
93
|
-
changes: 0,
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
}
|
|
45
|
+
const result = stmt.run(...values);
|
|
46
|
+
const changes = result.changes ?? 0;
|
|
47
|
+
return Promise.resolve({
|
|
48
|
+
success: true,
|
|
49
|
+
meta: {
|
|
50
|
+
duration: 0,
|
|
51
|
+
size_after: 0,
|
|
52
|
+
rows_read: 0,
|
|
53
|
+
rows_written: changes,
|
|
54
|
+
last_row_id: Number(result.lastInsertRowid ?? 0),
|
|
55
|
+
changed_db: changes > 0,
|
|
56
|
+
changes,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
97
59
|
},
|
|
98
60
|
raw() {
|
|
99
61
|
return Promise.resolve(stmt.all(...values));
|
|
@@ -118,18 +80,20 @@ export function fromBunSqlite(bunDb) {
|
|
|
118
80
|
},
|
|
119
81
|
};
|
|
120
82
|
},
|
|
121
|
-
batch(statements) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return r;
|
|
125
|
-
}));
|
|
83
|
+
async batch(statements) {
|
|
84
|
+
const results = await Promise.all(statements.map((s) => s.run()));
|
|
85
|
+
return results;
|
|
126
86
|
},
|
|
127
87
|
exec(query) {
|
|
128
88
|
bunDb.exec(query);
|
|
129
89
|
return Promise.resolve({ count: 1, duration: 0 });
|
|
130
90
|
},
|
|
131
91
|
dump() {
|
|
132
|
-
|
|
92
|
+
if (!bunDb.serialize) {
|
|
93
|
+
throw new Error('dump() is not supported: the underlying database does not implement serialize()');
|
|
94
|
+
}
|
|
95
|
+
const bytes = bunDb.serialize();
|
|
96
|
+
return Promise.resolve(bytes.buffer);
|
|
133
97
|
},
|
|
134
98
|
};
|
|
135
99
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bun-sqlite.js","sourceRoot":"","sources":["../../src/adapters/bun-sqlite.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bun-sqlite.js","sourceRoot":"","sources":["../../src/adapters/bun-sqlite.ts"],"names":[],"mappings":"AA8BA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAAC,KAAwB;IACpD,OAAO;QACL,OAAO,CAAC,KAAa;YACnB,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YAEjC,SAAS,SAAS,CAAC,MAAiB;gBAClC,OAAO;oBACL,IAAI,CAAC,GAAG,IAAe;wBACrB,OAAO,SAAS,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAA;oBACxC,CAAC;oBACD,KAAK;wBACH,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAA;wBAC/B,OAAO,OAAO,CAAC,OAAO,CAA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C,KAAK,EAAK,CAAA;gBACjC,CAAC;gBACD,GAAG;oBACD,OAAO,SAAS,CAAC,EAAE,CAAC,CAAC,GAAG,EAAK,CAAA;gBAC/B,CAAC;gBACD,GAAG;oBACD,OAAO,SAAS,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,CAAA;gBAC5B,CAAC;gBACD,GAAG;oBACD,OAAO,SAAS,CAAC,EAAE,CAAC,CAAC,GAAG,EAAK,CAAA;gBAC/B,CAAC;aACF,CAAA;QACH,CAAC;QAED,KAAK,CAAC,KAAK,CAAI,UAA+B;YAC5C,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;YACjE,OAAO,OAAsC,CAAA;QAC/C,CAAC;QAED,IAAI,CAAC,KAAa;YAChB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACjB,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAA;QACnD,CAAC;QAED,IAAI;YACF,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;gBACrB,MAAM,IAAI,KAAK,CACb,iFAAiF,CAClF,CAAA;YACH,CAAC;YACD,MAAM,KAAK,GAAG,KAAK,CAAC,SAAS,EAAE,CAAA;YAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,MAAqB,CAAC,CAAA;QACrD,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -26,7 +26,7 @@ export interface DurableObjectSqlStorage {
|
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
28
|
* ```typescript
|
|
29
|
-
* import { createService, fromDurableObjectStorage } from 'ginger'
|
|
29
|
+
* import { createService, fromDurableObjectStorage } from '@truto/ginger'
|
|
30
30
|
*
|
|
31
31
|
* export class MyDO extends DurableObject {
|
|
32
32
|
* service = createService({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable-object.d.ts","sourceRoot":"","sources":["../../src/adapters/durable-object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EAIT,MAAM,aAAa,CAAA;AAEpB;;;GAGG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC3D,OAAO,IAAI,CAAC,EAAE,CAAA;IACd,GAAG,IAAI,CAAC,CAAA;IACR,QAAQ,CAAC,WAAW,EAAE,MAAM,EAAE,CAAA;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;CAC7B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,uBAAuB;IACtC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,KAAK,EAAE,MAAM,EACb,GAAG,QAAQ,EAAE,OAAO,EAAE,GACrB,gBAAgB,CAAC,CAAC,CAAC,CAAA;CACvB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,uBAAuB,GAC3B,QAAQ,
|
|
1
|
+
{"version":3,"file":"durable-object.d.ts","sourceRoot":"","sources":["../../src/adapters/durable-object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EAIT,MAAM,aAAa,CAAA;AAEpB;;;GAGG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC3D,OAAO,IAAI,CAAC,EAAE,CAAA;IACd,GAAG,IAAI,CAAC,CAAA;IACR,QAAQ,CAAC,WAAW,EAAE,MAAM,EAAE,CAAA;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;CAC7B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,uBAAuB;IACtC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,KAAK,EAAE,MAAM,EACb,GAAG,QAAQ,EAAE,OAAO,EAAE,GACrB,gBAAgB,CAAC,CAAC,CAAC,CAAA;CACvB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,uBAAuB,GAC3B,QAAQ,CA0FV"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @example
|
|
6
6
|
* ```typescript
|
|
7
|
-
* import { createService, fromDurableObjectStorage } from 'ginger'
|
|
7
|
+
* import { createService, fromDurableObjectStorage } from '@truto/ginger'
|
|
8
8
|
*
|
|
9
9
|
* export class MyDO extends DurableObject {
|
|
10
10
|
* service = createService({
|
|
@@ -23,82 +23,44 @@ export function fromDurableObjectStorage(sql) {
|
|
|
23
23
|
return makeBound([...values, ...more]);
|
|
24
24
|
},
|
|
25
25
|
first() {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return Promise.resolve(rows[0] ?? null);
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return Promise.resolve(null);
|
|
33
|
-
}
|
|
26
|
+
const cursor = sql.exec(query, ...values);
|
|
27
|
+
const rows = cursor.toArray();
|
|
28
|
+
return Promise.resolve(rows[0] ?? null);
|
|
34
29
|
},
|
|
35
30
|
all() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
return Promise.resolve({
|
|
55
|
-
success: false,
|
|
56
|
-
results: [],
|
|
57
|
-
meta: {
|
|
58
|
-
duration: 0,
|
|
59
|
-
size_after: 0,
|
|
60
|
-
rows_read: 0,
|
|
61
|
-
rows_written: 0,
|
|
62
|
-
last_row_id: 0,
|
|
63
|
-
changed_db: false,
|
|
64
|
-
changes: 0,
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
}
|
|
31
|
+
const cursor = sql.exec(query, ...values);
|
|
32
|
+
const rows = cursor.toArray();
|
|
33
|
+
return Promise.resolve({
|
|
34
|
+
success: true,
|
|
35
|
+
results: rows,
|
|
36
|
+
meta: {
|
|
37
|
+
duration: 0,
|
|
38
|
+
size_after: 0,
|
|
39
|
+
rows_read: cursor.rowsRead,
|
|
40
|
+
rows_written: cursor.rowsWritten,
|
|
41
|
+
last_row_id: 0,
|
|
42
|
+
changed_db: cursor.rowsWritten > 0,
|
|
43
|
+
changes: cursor.rowsWritten,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
68
46
|
},
|
|
69
47
|
run() {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
catch {
|
|
89
|
-
return Promise.resolve({
|
|
90
|
-
success: false,
|
|
91
|
-
meta: {
|
|
92
|
-
duration: 0,
|
|
93
|
-
size_after: 0,
|
|
94
|
-
rows_read: 0,
|
|
95
|
-
rows_written: 0,
|
|
96
|
-
last_row_id: 0,
|
|
97
|
-
changed_db: false,
|
|
98
|
-
changes: 0,
|
|
99
|
-
},
|
|
100
|
-
});
|
|
101
|
-
}
|
|
48
|
+
const cursor = sql.exec(query, ...values);
|
|
49
|
+
const lastRowId = sql
|
|
50
|
+
.exec('SELECT last_insert_rowid() AS lid')
|
|
51
|
+
.one().lid;
|
|
52
|
+
return Promise.resolve({
|
|
53
|
+
success: true,
|
|
54
|
+
meta: {
|
|
55
|
+
duration: 0,
|
|
56
|
+
size_after: 0,
|
|
57
|
+
rows_read: cursor.rowsRead,
|
|
58
|
+
rows_written: cursor.rowsWritten,
|
|
59
|
+
last_row_id: lastRowId,
|
|
60
|
+
changed_db: cursor.rowsWritten > 0,
|
|
61
|
+
changes: cursor.rowsWritten,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
102
64
|
},
|
|
103
65
|
raw() {
|
|
104
66
|
const cursor = sql.exec(query, ...values);
|
|
@@ -124,18 +86,16 @@ export function fromDurableObjectStorage(sql) {
|
|
|
124
86
|
},
|
|
125
87
|
};
|
|
126
88
|
},
|
|
127
|
-
batch(statements) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return r;
|
|
131
|
-
}));
|
|
89
|
+
async batch(statements) {
|
|
90
|
+
const results = await Promise.all(statements.map((s) => s.run()));
|
|
91
|
+
return results;
|
|
132
92
|
},
|
|
133
93
|
exec(query) {
|
|
134
94
|
sql.exec(query);
|
|
135
95
|
return Promise.resolve({ count: 1, duration: 0 });
|
|
136
96
|
},
|
|
137
97
|
dump() {
|
|
138
|
-
|
|
98
|
+
throw new Error('dump() is not supported by the Durable Object SqlStorage adapter');
|
|
139
99
|
},
|
|
140
100
|
};
|
|
141
101
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable-object.js","sourceRoot":"","sources":["../../src/adapters/durable-object.ts"],"names":[],"mappings":"AAiCA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,wBAAwB,CACtC,GAA4B;IAE5B,OAAO;QACL,OAAO,CAAC,KAAa;YACnB,SAAS,SAAS,CAAC,MAAiB;gBAClC,OAAO;oBACL,IAAI,CAAC,GAAG,IAAe;wBACrB,OAAO,SAAS,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAA;oBACxC,CAAC;oBACD,KAAK;wBACH,
|
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AACH,qBAAa,SAAU,SAAQ,YAAY;gBAEvC,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,aAAa,EAAE,KAAK,EACpB,OAAO,CAAC,EAAE,OAAO;CAWpB;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,YAAY;gBAC/B,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;CAG/C;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,YAAY;gBACnC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;CAG/C;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,YAAY;gBACnC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;CAG/C"}
|
package/dist/errors.js
CHANGED
|
@@ -60,7 +60,9 @@ export class EncryptionError extends ServiceError {
|
|
|
60
60
|
*/
|
|
61
61
|
export class HookError extends ServiceError {
|
|
62
62
|
constructor(hookPhase, methodName, originalError, details) {
|
|
63
|
-
super(`Hook error in ${hookPhase} phase of ${methodName}: ${originalError.message}`, 'HOOK_ERROR', 500, details
|
|
63
|
+
super(`Hook error in ${hookPhase} phase of ${methodName}: ${originalError.message}`, 'HOOK_ERROR', 500, details
|
|
64
|
+
? { ...details, originalError }
|
|
65
|
+
: { originalError });
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
68
|
/**
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrB,IAAI,CAAQ;IACZ,UAAU,CAAQ;IAClB,OAAO,CAAU;IAEjC,YACE,OAAe,EACf,IAAY,EACZ,aAAqB,GAAG,EACxB,OAAiB;QAEjB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IA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AE,GAAG,EAAE,OAAO,CAAC,CAAA;IAClD,CAAC;CACF"}
|
package/dist/example.d.ts
CHANGED
|
@@ -41,7 +41,11 @@ export declare function createUsersService(db: any, encryptionKeys: Record<strin
|
|
|
41
41
|
select: string[];
|
|
42
42
|
alias: string;
|
|
43
43
|
};
|
|
44
|
-
where:
|
|
44
|
+
where: {
|
|
45
|
+
$teams: {
|
|
46
|
+
active: number;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
45
49
|
schema: z.ZodObject<{
|
|
46
50
|
id: z.ZodNumber;
|
|
47
51
|
name: z.ZodString;
|
package/dist/example.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAiB,CAAC,EAAqC,MAAM,YAAY,CAAA;AAoEhF;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,EAAE,EAAE,GAAG,EACP,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC
|
|
1
|
+
{"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAiB,CAAC,EAAqC,MAAM,YAAY,CAAA;AAoEhF;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,EAAE,EAAE,GAAG,EACP,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA2CvC;AAED;;GAEG;;oBAEqB,OAAO,OAAO,GAAG,QAAQ,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC;;AADxE,wBA+GC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG"}
|
package/dist/example.js
CHANGED
|
@@ -54,7 +54,7 @@ const userJoins = {
|
|
|
54
54
|
select: ['id', 'name', 'description'],
|
|
55
55
|
alias: 'team',
|
|
56
56
|
},
|
|
57
|
-
where:
|
|
57
|
+
where: { $teams: { active: 1 } },
|
|
58
58
|
schema: TeamRowSchema,
|
|
59
59
|
},
|
|
60
60
|
};
|
|
@@ -80,24 +80,18 @@ export function createUsersService(db, encryptionKeys) {
|
|
|
80
80
|
secrets: userSecrets,
|
|
81
81
|
primaryKey: 'id',
|
|
82
82
|
defaultOrderBy: { column: 'created_at', direction: 'desc' },
|
|
83
|
-
encryptionKeys,
|
|
83
|
+
encryptionKeys,
|
|
84
|
+
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
|
|
84
85
|
hooks: {
|
|
85
|
-
// Enforce tenant filtering on all operations
|
|
86
86
|
list: {
|
|
87
87
|
before: async (ctx) => {
|
|
88
88
|
if (!ctx.auth.user?.tenantId) {
|
|
89
89
|
throw new Error('User must have a tenant ID');
|
|
90
90
|
}
|
|
91
|
-
// Add tenant filter to where clause
|
|
92
91
|
if (!ctx.params.where) {
|
|
93
92
|
ctx.params.where = {};
|
|
94
93
|
}
|
|
95
94
|
ctx.params.where.tenantId = ctx.auth.user.tenantId;
|
|
96
|
-
// For joins that need tenant filtering, you can:
|
|
97
|
-
// 1. Ensure proper foreign key relationships in your database schema
|
|
98
|
-
// 2. Use additional where clauses on the main query
|
|
99
|
-
// 3. Implement custom filtering logic in hooks
|
|
100
|
-
// The join definitions remain clean and auth-agnostic
|
|
101
95
|
},
|
|
102
96
|
},
|
|
103
97
|
get: {
|
|
@@ -105,8 +99,6 @@ export function createUsersService(db, encryptionKeys) {
|
|
|
105
99
|
if (!ctx.auth.user?.tenantId) {
|
|
106
100
|
throw new Error('User must have a tenant ID');
|
|
107
101
|
}
|
|
108
|
-
// This would be handled by ensuring the ID belongs to the tenant
|
|
109
|
-
// In practice, you'd modify the query to include tenant check
|
|
110
102
|
},
|
|
111
103
|
},
|
|
112
104
|
create: {
|
|
@@ -114,15 +106,7 @@ export function createUsersService(db, encryptionKeys) {
|
|
|
114
106
|
if (!ctx.auth.user?.tenantId) {
|
|
115
107
|
throw new Error('User must have a tenant ID');
|
|
116
108
|
}
|
|
117
|
-
// Ensure created records belong to the user's tenant
|
|
118
109
|
ctx.data.tenantId = ctx.auth.user.tenantId;
|
|
119
|
-
ctx.data.createdAt = new Date().toISOString();
|
|
120
|
-
ctx.data.updatedAt = new Date().toISOString();
|
|
121
|
-
},
|
|
122
|
-
},
|
|
123
|
-
update: {
|
|
124
|
-
before: async (ctx) => {
|
|
125
|
-
ctx.data.updatedAt = new Date().toISOString();
|
|
126
110
|
},
|
|
127
111
|
},
|
|
128
112
|
},
|
|
@@ -141,30 +125,24 @@ export default {
|
|
|
141
125
|
// Initialize the service with the database binding and encryption keys
|
|
142
126
|
const usersService = createService({
|
|
143
127
|
table: 'users',
|
|
144
|
-
db: env.DB,
|
|
128
|
+
db: env.DB,
|
|
145
129
|
rowSchema: UserRowSchema,
|
|
146
130
|
createSchema: UserCreateSchema,
|
|
147
131
|
updateSchema: UserUpdateSchema,
|
|
148
132
|
joins: userJoins,
|
|
149
133
|
secrets: userSecrets,
|
|
150
|
-
encryptionKeys,
|
|
134
|
+
encryptionKeys,
|
|
135
|
+
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
|
|
151
136
|
hooks: {
|
|
152
|
-
// Enforce tenant filtering on all operations
|
|
153
137
|
list: {
|
|
154
138
|
before: async (ctx) => {
|
|
155
139
|
if (!ctx.auth.user?.tenantId) {
|
|
156
140
|
throw new Error('User must have a tenant ID');
|
|
157
141
|
}
|
|
158
|
-
// Add tenant filter to where clause
|
|
159
142
|
if (!ctx.params.where) {
|
|
160
143
|
ctx.params.where = {};
|
|
161
144
|
}
|
|
162
145
|
ctx.params.where.tenantId = ctx.auth.user.tenantId;
|
|
163
|
-
// For joins that need tenant filtering, you can:
|
|
164
|
-
// 1. Ensure proper foreign key relationships in your database schema
|
|
165
|
-
// 2. Use additional where clauses on the main query
|
|
166
|
-
// 3. Implement custom filtering logic in hooks
|
|
167
|
-
// The join definitions remain clean and auth-agnostic
|
|
168
146
|
},
|
|
169
147
|
},
|
|
170
148
|
create: {
|
|
@@ -172,10 +150,7 @@ export default {
|
|
|
172
150
|
if (!ctx.auth.user?.tenantId) {
|
|
173
151
|
throw new Error('User must have a tenant ID');
|
|
174
152
|
}
|
|
175
|
-
// Ensure created records belong to the user's tenant
|
|
176
153
|
ctx.data.tenantId = ctx.auth.user.tenantId;
|
|
177
|
-
ctx.data.createdAt = new Date().toISOString();
|
|
178
|
-
ctx.data.updatedAt = new Date().toISOString();
|
|
179
154
|
},
|
|
180
155
|
},
|
|
181
156
|
},
|