Skip to content

Commit

Permalink
Exposed subscription API. Test coverage dropped below 90% as a result.
Browse files Browse the repository at this point in the history
  • Loading branch information
anywhichway committed Aug 9, 2023
1 parent 766f2a9 commit 5a851f7
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 102 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Support for `Date`, `RegExp` and `symbol` as part of keys. Support for `symbol`

A powerful `db.find` function that supports approximate matching and works on both indexes and regular keys with over 50 operators including regular expressions, soundex/echoes, credit card, SSNs and more. If something is missing, it can be added in as little as one line.

A subscription API allows listening for changes to the database.

# Usage

```javascript
Expand All @@ -35,8 +37,6 @@ Primitive keys are automatically converted to the arrays required by Deno KV.

Deno KV does not provide a `db.clear` function. Denodata does.

```javascript

```javascript
await db.set("mykey", "myvalue");
await (async () => { const {key,value,version} = await db.get("mykey")})();
Expand Down Expand Up @@ -129,6 +129,12 @@ await (async () => {
console.log(value); // prints Book instance, less efficient, large index scans because of lack of cname
}
})();
db.subscribe({set: {key:1,value:1}},async (value) => {
console.log(value); // prints {key:1,value:1} so long as db.set(1,1) is called
});
db.subscribe({delete: (key) => key<10 ? key : undefined},async (value) => {
console.log(value); // prints key so long as db.delete(key) is called with value less than 10
});
```

# Installation
Expand Down Expand Up @@ -242,6 +248,11 @@ Notes:
- Works like `Deno KV.set`. Does not manage indexes or do specialized serialization.
- See notes at start of API section regarding `key` and `value` types.

`db.subscribe(on:{delete?:any,patch?:any,put?:any,set?:any},callback:(arg1:any,arg2?:any)=>void,{cname?:string,metadata?:{[key:string]:any}}):void`

- Calls the `callback` with the same signature as `db.delete`, `db.patch`, `db.put`, or `db.set` if the value or key and value in the `on` pattern, as well as `cname` and `metadata` match the call to `db.delete`, `db.patch`, `db.put`, or `db.set`.


# Key and Value Space

## Keys
Expand Down Expand Up @@ -431,7 +442,7 @@ The following operators are supported in patterns.
# Testing

- `constants.js ... 100.000% (3/3)`
- `denodata.ts ... index.js ... 90.196% (690/765)`
- `denodata.ts ... 82.809% (684/826)`
- `operators.ts ... ... 95.330% (347/364)`

# Release History (Reverse Chronological Order)
Expand All @@ -440,6 +451,10 @@ The following operators are supported in patterns.
- Beta commenced when unit test coverage first exceeded 90%
- The exposed API is stable. Additional features may be exposed.


2023-08-09 v0.0.27 (Beta)
- Exposed subscription API. Test coverage dropped below 90% as a result.

2023-07-28 v0.0.26 (Beta)
- Documentation corrections.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "denodata",
"version": "0.0.26",
"description": "Generalized indexing and search for Deno KV",
"version": "0.0.27",
"description": "Generalized indexing, search, and change subscriptions for Deno KV",
"type": "module",
"main": "mod.ts",
"scripts": {
Expand Down
64 changes: 64 additions & 0 deletions src/denodata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,70 @@ const test = async (deno) => {
expect(sum.value.value).toEqual(1n);
})

deno.test("subscribe delete", async (t) => {
return new Promise(async (resolve,reject) => {
await db.subscribe({delete: 1},(value) => {
try {
expect(value).toEqual(1);
resolve();
} catch(e) {
reject(e);
}
});
await db.delete(1);
});
})

deno.test("subscribe delete - function test", async (t) => {
return new Promise(async (resolve,reject) => {
await db.subscribe({delete: (value) => value===1 ? value : undefined},(value) => {
try {
expect(value).toEqual(1);
resolve();
} catch(e) {
reject(e);
}
});
await db.delete(1);
});
})

deno.test("subscribe set", async (t) => {
return new Promise(async (resolve,reject) => {
db.subscribe({set: {key:1,value:1}},async (key,value) => {
try {
expect(key).toEqual(1);
expect(value).toEqual(1);
resolve();
} catch(e) {
reject(e);
} finally {
await db.delete(1);
}
});
await db.set(1,1);
});
})

deno.test("subscribe set - function test", async (t) => {
return new Promise(async (resolve,reject) => {
db.subscribe({set: {key:1,value:(value) => value===1 ? value : undefined}},async (key,value) => {
try {
expect(key).toEqual(1);
expect(value).toEqual(1);
resolve();
} catch(e) {
reject(e);
} finally {
await db.delete(1);
}
});
await db.set(1,1);
});
})



return async () => {
const errors = [],
start = Date.now();
Expand Down
Loading

0 comments on commit 5a851f7

Please sign in to comment.