2023-09-03 19:15:17 +00:00
|
|
|
import "https://esm.sh/fake-indexeddb@4.0.2/auto";
|
|
|
|
import { IndexedDBKvPrimitives } from "./indexeddb_kv_primitives.ts";
|
|
|
|
import { DataStore } from "./datastore.ts";
|
|
|
|
import { DenoKvPrimitives } from "./deno_kv_primitives.ts";
|
|
|
|
import { KvPrimitives } from "./kv_primitives.ts";
|
|
|
|
import { assertEquals } from "https://deno.land/std@0.165.0/testing/asserts.ts";
|
|
|
|
|
|
|
|
async function test(db: KvPrimitives) {
|
2023-10-03 12:16:33 +00:00
|
|
|
const datastore = new DataStore(db, ["ds"], {
|
|
|
|
count: (arr: any[]) => arr.length,
|
|
|
|
});
|
|
|
|
await datastore.set(["user", "peter"], { name: "Peter" });
|
|
|
|
await datastore.set(["user", "hank"], { name: "Hank" });
|
|
|
|
let results = await datastore.query({
|
2023-09-03 19:15:17 +00:00
|
|
|
prefix: ["user"],
|
2023-10-03 12:16:33 +00:00
|
|
|
filter: ["=", ["attr", "name"], ["string", "Peter"]],
|
2023-09-03 19:15:17 +00:00
|
|
|
});
|
|
|
|
assertEquals(results, [{ key: ["user", "peter"], value: { name: "Peter" } }]);
|
2023-10-03 12:16:33 +00:00
|
|
|
await datastore.batchSet<any>([
|
2023-09-03 19:15:17 +00:00
|
|
|
{ key: ["kv", "name"], value: "Zef" },
|
|
|
|
{ key: ["kv", "data"], value: new Uint8Array([1, 2, 3]) },
|
|
|
|
{
|
|
|
|
key: ["kv", "complicated"],
|
|
|
|
value: {
|
|
|
|
name: "Frank",
|
|
|
|
parents: ["John", "Jane"],
|
|
|
|
address: {
|
|
|
|
street: "123 Main St",
|
|
|
|
city: "San Francisco",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2023-10-03 12:16:33 +00:00
|
|
|
assertEquals(await datastore.get(["kv", "name"]), "Zef");
|
|
|
|
assertEquals(await datastore.get(["kv", "data"]), new Uint8Array([1, 2, 3]));
|
|
|
|
results = await datastore.query({
|
2023-09-03 19:15:17 +00:00
|
|
|
prefix: ["kv"],
|
2023-10-03 12:16:33 +00:00
|
|
|
filter: ["=~", ["attr", ""], ["regexp", "Z.f", "i"]],
|
2023-09-03 19:15:17 +00:00
|
|
|
});
|
|
|
|
assertEquals(results, [{ key: ["kv", "name"], value: "Zef" }]);
|
2023-10-03 12:16:33 +00:00
|
|
|
results = await datastore.query({
|
2023-09-03 19:15:17 +00:00
|
|
|
prefix: ["kv"],
|
2023-10-03 12:16:33 +00:00
|
|
|
filter: ["and", ["=", ["attr", "parents"], ["string", "John"]], [
|
2023-09-03 19:15:17 +00:00
|
|
|
"=",
|
2023-10-03 12:16:33 +00:00
|
|
|
["attr", ["attr", "address"], "city"],
|
|
|
|
["string", "San Francisco"],
|
2023-09-03 19:15:17 +00:00
|
|
|
]],
|
2023-10-03 12:16:33 +00:00
|
|
|
select: [
|
|
|
|
{ name: "parents" },
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
expr: ["+", ["attr", "name"], ["string", "!"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "parentCount",
|
|
|
|
expr: ["call", "count", [["attr", "parents"]]],
|
|
|
|
},
|
|
|
|
],
|
2023-09-07 10:33:25 +00:00
|
|
|
});
|
2023-10-03 12:16:33 +00:00
|
|
|
assertEquals(results.length, 1);
|
2023-09-07 10:33:25 +00:00
|
|
|
assertEquals(results[0], {
|
|
|
|
key: ["kv", "complicated"],
|
2023-10-03 12:16:33 +00:00
|
|
|
value: { name: "Frank!", parentCount: 2, parents: ["John", "Jane"] },
|
2023-09-03 19:15:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test("Test Deno KV DataStore", async () => {
|
|
|
|
const tmpFile = await Deno.makeTempFile();
|
2023-10-03 12:16:33 +00:00
|
|
|
const db = new DenoKvPrimitives(await Deno.openKv(tmpFile));
|
2023-09-03 19:15:17 +00:00
|
|
|
await test(db);
|
|
|
|
db.close();
|
|
|
|
await Deno.remove(tmpFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Test IndexDB DataStore", {
|
|
|
|
sanitizeResources: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
}, async () => {
|
|
|
|
const db = new IndexedDBKvPrimitives("test");
|
|
|
|
await db.init();
|
|
|
|
await test(db);
|
|
|
|
db.close();
|
|
|
|
});
|