@@ -97,7 +97,6 @@ _Supported Deno verisons:_ **^1.43.0**
97
97
- [ jsonParse()] ( #jsonparse )
98
98
- [ Extensions] ( #extensions )
99
99
- [ Zod] ( #zod )
100
- - [ zodModel()] ( #zodmodel )
101
100
- [ Schemas] ( #schemas )
102
101
- [ Migrate] ( #migrate )
103
102
- [ Script] ( #script )
@@ -109,14 +108,13 @@ _Supported Deno verisons:_ **^1.43.0**
109
108
## Models
110
109
111
110
Collections are typed using models. Standard models can be defined using the
112
- ` model() ` function. Alternatively, any object that implements the Model type can
113
- be used as a model. Zod is therefore compatible, without being a dependency (see
114
- [ zodModel()] ( #zodmodel ) for additional support). The standard model uses type
115
- casting only, and does not validate any data when parsing. Asymmetric models can
116
- be created by passing a transform function which maps from an input type to an
117
- output type. Asymmetric models are useful for storing derived values or filling
118
- default values. It is up to the developer to choose the strategy that fits their
119
- use case the best.
111
+ ` model() ` function. Alternatively, any object that is compatible with the Model
112
+ type can be used as a model. Zod is therefore supported, without being a
113
+ dependency. The standard model uses type casting only, and does not validate any
114
+ data when parsing. Asymmetric models can be created by passing a transform
115
+ function which maps from an input type to an output type. Asymmetric models are
116
+ useful for storing derived values or filling default values. It is up to the
117
+ developer to choose the strategy that fits their use case the best.
120
118
121
119
** _ NOTE_ :** When using interfaces instead of types, they must extend the KvValue
122
120
type.
@@ -957,6 +955,17 @@ db.numbers.watch("id", (doc) => {
957
955
})
958
956
```
959
957
958
+ Watchers can also be stopped.
959
+
960
+ ``` ts
961
+ const { promise, cancel } = db .numbers .watch (" id" , (doc ) => {
962
+ // ...
963
+ })
964
+
965
+ await cancel ()
966
+ await promise
967
+ ```
968
+
960
969
### watchMany()
961
970
962
971
Listen for live changes to an array of specified documents by id.
@@ -976,6 +985,20 @@ db.numbers.watchMany(["id1", "id2", "id3"], (docs) => {
976
985
})
977
986
```
978
987
988
+ Watchers can also be stopped.
989
+
990
+ ``` ts
991
+ const { promise, cancel } = db .numbers .watchMany (
992
+ [" id1" , " id2" , " id3" ],
993
+ (docs ) => {
994
+ // ...
995
+ },
996
+ )
997
+
998
+ await cancel ()
999
+ await promise
1000
+ ```
1001
+
979
1002
## Database Methods
980
1003
981
1004
These are methods which can be found at the top level of your database object,
@@ -1317,57 +1340,22 @@ const value = jsonParse(str)
1317
1340
## Extensions
1318
1341
1319
1342
Additional features outside of the basic functionality provided by ` kvdex ` .
1320
- While the basic functions are dependency-free, extended features may rely on
1321
- some dependenices to enhance integration. All extensions are found in the
1322
- ` kvdex/ext/ ` sub-path.
1343
+ While the core functionalities are dependency-free, extended features may rely
1344
+ on some dependenices to enhance integration.
1323
1345
1324
1346
### Zod
1325
1347
1326
- Extended support for Zod. Includes a model parser and schemas for some KV-types.
1327
-
1328
- #### zodModel()
1329
-
1330
- Provides additional compatibility when using zod schemas as models. While zod
1331
- schemas can be used as models directly, ` zodModel() ` properly parses a model
1332
- from a zod schema, recognizing default fields as optional.
1333
-
1334
- ``` ts
1335
- import { z } from " npm:zod"
1336
- import { zodModel } from " jsr:@olli/kvdex/ext/zod"
1337
- import { collection , kvdex } from " jsr:@olli/kvdex"
1338
-
1339
- const UserSchema = z .object ({
1340
- username: z .string (),
1341
- gender: z .string ().default (" not given" ),
1342
- })
1343
-
1344
- const kv = await Deno .openKv ()
1345
-
1346
- const db = kvdex (kv , {
1347
- users_basic: collection (UserSchema ),
1348
- users_zod: collection (zodModel (UserSchema )),
1349
- })
1350
-
1351
- // Produces a type error for missing "gender" field.
1352
- const result = await db .users_basic .add ({
1353
- username: " oliver" ,
1354
- })
1355
-
1356
- // No type error for missing "gender" field.
1357
- const result = await db .users_zod .add ({
1358
- username: " oliver" ,
1359
- })
1360
- ```
1348
+ Extended support for Zod. Includes schemas for some of the KV-types.
1361
1349
1362
1350
#### Schemas
1363
1351
1364
- The zod extension provides schemas for some of the Kv -types, such as KvId,
1352
+ The zod extension provides schemas for some of the KV -types, such as KvId,
1365
1353
KvValue, KvObject and KvArray. This makes it easier to properly build your
1366
1354
schemas.
1367
1355
1368
1356
``` ts
1369
1357
import { z } from " npm:zod"
1370
- import { KvIdSchema } from " jsr:@olli/kvdex/ext/ zod"
1358
+ import { KvIdSchema } from " jsr:@olli/kvdex/zod"
1371
1359
1372
1360
const UserSchema = z .object ({
1373
1361
username: z .string (),
@@ -1392,7 +1380,7 @@ Run the migrate script and provide --source and --target arguments. Optionally
1392
1380
pass --all to migrate all entries.
1393
1381
1394
1382
``` console
1395
- deno run -A --unstable-kv jsr:@olli/kvdex/ext/ migrate --source=./source.sqlite3 --target=./target.sqlite3
1383
+ deno run -A --unstable-kv jsr:@olli/kvdex/migrate --source=./source.sqlite3 --target=./target.sqlite3
1396
1384
```
1397
1385
1398
1386
#### Function
@@ -1401,7 +1389,7 @@ Use the migrate function and pass a source KV instance and a target KV instance.
1401
1389
Optionally pass ` all: true ` to migrate all entries.
1402
1390
1403
1391
``` ts
1404
- import { migrate } from " jsr:@olli/kvdex/ext/ migrate"
1392
+ import { migrate } from " jsr:@olli/kvdex/migrate"
1405
1393
1406
1394
const source = await Deno .openKv (" ./source.sqlite3" )
1407
1395
const target = await Deno .openKv (" ./target.sqlite3" )
0 commit comments