tablinum 0.1.0 → 0.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 +2 -2
- package/dist/db/{create-localstr.d.ts → create-tablinum.d.ts} +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +15 -15
- package/dist/svelte/collection.svelte.d.ts +20 -0
- package/dist/svelte/database.svelte.d.ts +15 -0
- package/dist/svelte/index.svelte.d.ts +16 -0
- package/dist/svelte/index.svelte.js +2050 -0
- package/dist/svelte/live-query.svelte.d.ts +8 -0
- package/dist/svelte/query.svelte.d.ts +39 -0
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ npm install tablinum
|
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
38
|
import { Effect } from "effect";
|
|
39
|
-
import {
|
|
39
|
+
import { createTablinum, collection, field } from "tablinum";
|
|
40
40
|
|
|
41
41
|
const schema = {
|
|
42
42
|
todos: collection("todos", {
|
|
@@ -46,7 +46,7 @@ const schema = {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
const program = Effect.gen(function* () {
|
|
49
|
-
const db = yield*
|
|
49
|
+
const db = yield* createTablinum({
|
|
50
50
|
schema,
|
|
51
51
|
relays: ["wss://relay.example.com"],
|
|
52
52
|
});
|
|
@@ -2,11 +2,11 @@ import { Effect, Scope } from "effect";
|
|
|
2
2
|
import type { SchemaConfig } from "../schema/types.ts";
|
|
3
3
|
import type { DatabaseHandle } from "./database-handle.ts";
|
|
4
4
|
import { CryptoError, StorageError, ValidationError } from "../errors.ts";
|
|
5
|
-
export interface
|
|
5
|
+
export interface TablinumConfig<S extends SchemaConfig> {
|
|
6
6
|
readonly schema: S;
|
|
7
7
|
readonly relays: readonly string[];
|
|
8
8
|
readonly privateKey?: Uint8Array | undefined;
|
|
9
9
|
readonly dbName?: string | undefined;
|
|
10
10
|
readonly onSyncError?: ((error: Error) => void) | undefined;
|
|
11
11
|
}
|
|
12
|
-
export declare function
|
|
12
|
+
export declare function createTablinum<S extends SchemaConfig>(config: TablinumConfig<S>): Effect.Effect<DatabaseHandle<S>, ValidationError | StorageError | CryptoError, Scope.Scope>;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ export { collection } from "./schema/collection.ts";
|
|
|
3
3
|
export type { CollectionDef, CollectionFields } from "./schema/collection.ts";
|
|
4
4
|
export type { FieldDef, FieldKind } from "./schema/field.ts";
|
|
5
5
|
export type { InferRecord, SchemaConfig } from "./schema/types.ts";
|
|
6
|
-
export {
|
|
7
|
-
export type {
|
|
6
|
+
export { createTablinum } from "./db/create-tablinum.ts";
|
|
7
|
+
export type { TablinumConfig } from "./db/create-tablinum.ts";
|
|
8
8
|
export type { DatabaseHandle, SyncStatus } from "./db/database-handle.ts";
|
|
9
9
|
export type { CollectionHandle } from "./crud/collection-handle.ts";
|
|
10
10
|
export type { CollectionOptions } from "./schema/collection.ts";
|
package/dist/index.js
CHANGED
|
@@ -48,7 +48,7 @@ function collection(name, fields, options) {
|
|
|
48
48
|
}
|
|
49
49
|
return { _tag: "CollectionDef", name, fields, indices };
|
|
50
50
|
}
|
|
51
|
-
// src/db/create-
|
|
51
|
+
// src/db/create-tablinum.ts
|
|
52
52
|
import { Effect as Effect14, PubSub as PubSub2, Ref as Ref5 } from "effect";
|
|
53
53
|
|
|
54
54
|
// src/schema/validate.ts
|
|
@@ -160,7 +160,7 @@ function buildPartialValidator(collectionName, def) {
|
|
|
160
160
|
// src/storage/idb.ts
|
|
161
161
|
import { Effect as Effect2 } from "effect";
|
|
162
162
|
import { openDB } from "idb";
|
|
163
|
-
var DB_NAME = "
|
|
163
|
+
var DB_NAME = "tablinum";
|
|
164
164
|
function storeName(collection2) {
|
|
165
165
|
return `col_${collection2}`;
|
|
166
166
|
}
|
|
@@ -1665,7 +1665,7 @@ function createSyncHandle(storage, giftWrapHandle, relay, publishQueue, syncStat
|
|
|
1665
1665
|
const result = yield* Effect13.result(relay.publish(giftWrap.event, relayUrls));
|
|
1666
1666
|
if (result._tag === "Failure") {
|
|
1667
1667
|
yield* publishQueue.enqueue(giftWrap.id);
|
|
1668
|
-
console.error("[
|
|
1668
|
+
console.error("[tablinum:publishLocal] relay error:", result.failure);
|
|
1669
1669
|
if (onSyncError)
|
|
1670
1670
|
onSyncError(result.failure);
|
|
1671
1671
|
}
|
|
@@ -1685,19 +1685,19 @@ function createSyncHandle(storage, giftWrapHandle, relay, publishQueue, syncStat
|
|
|
1685
1685
|
}));
|
|
1686
1686
|
}));
|
|
1687
1687
|
if (subResult._tag === "Failure") {
|
|
1688
|
-
console.error("[
|
|
1688
|
+
console.error("[tablinum:subscribe] failed for", url, subResult.failure);
|
|
1689
1689
|
if (onSyncError)
|
|
1690
1690
|
onSyncError(subResult.failure);
|
|
1691
1691
|
} else {
|
|
1692
|
-
console.log("[
|
|
1692
|
+
console.log("[tablinum:subscribe] listening on", url);
|
|
1693
1693
|
}
|
|
1694
1694
|
}
|
|
1695
1695
|
})
|
|
1696
1696
|
};
|
|
1697
1697
|
}
|
|
1698
1698
|
|
|
1699
|
-
// src/db/create-
|
|
1700
|
-
function
|
|
1699
|
+
// src/db/create-tablinum.ts
|
|
1700
|
+
function createTablinum(config) {
|
|
1701
1701
|
return Effect14.gen(function* () {
|
|
1702
1702
|
if (!config.relays || config.relays.length === 0) {
|
|
1703
1703
|
return yield* new ValidationError({
|
|
@@ -1711,7 +1711,7 @@ function createLocalstr(config) {
|
|
|
1711
1711
|
});
|
|
1712
1712
|
}
|
|
1713
1713
|
let resolvedKey = config.privateKey;
|
|
1714
|
-
const storageKeyName = `
|
|
1714
|
+
const storageKeyName = `tablinum-key-${config.dbName ?? "tablinum"}`;
|
|
1715
1715
|
if (!resolvedKey && typeof globalThis.localStorage !== "undefined") {
|
|
1716
1716
|
const saved = globalThis.localStorage.getItem(storageKeyName);
|
|
1717
1717
|
if (saved && saved.length === 64) {
|
|
@@ -1737,7 +1737,7 @@ function createLocalstr(config) {
|
|
|
1737
1737
|
const syncStatus = yield* createSyncStatusHandle();
|
|
1738
1738
|
const syncHandle = createSyncHandle(storage, giftWrapHandle, relayHandle, publishQueue, syncStatus, watchCtx, config.relays, identity.publicKey, config.onSyncError);
|
|
1739
1739
|
const onWrite = (event) => Effect14.gen(function* () {
|
|
1740
|
-
console.log("[
|
|
1740
|
+
console.log("[tablinum:onWrite]", event.kind, event.collection, event.recordId);
|
|
1741
1741
|
const content = event.kind === "delete" ? JSON.stringify({ _deleted: true }) : JSON.stringify(event.data);
|
|
1742
1742
|
const dTag = `${event.collection}:${event.recordId}`;
|
|
1743
1743
|
const wrapResult = yield* Effect14.result(giftWrapHandle.wrap({
|
|
@@ -1748,13 +1748,13 @@ function createLocalstr(config) {
|
|
|
1748
1748
|
}));
|
|
1749
1749
|
if (wrapResult._tag === "Success") {
|
|
1750
1750
|
const gw = wrapResult.success;
|
|
1751
|
-
console.log("[
|
|
1751
|
+
console.log("[tablinum:onWrite] gift wrap created:", gw.id, "kind:", gw.kind, "tags:", JSON.stringify(gw.tags));
|
|
1752
1752
|
yield* storage.putGiftWrap({
|
|
1753
1753
|
id: gw.id,
|
|
1754
1754
|
event: gw,
|
|
1755
1755
|
createdAt: gw.created_at
|
|
1756
1756
|
});
|
|
1757
|
-
console.log("[
|
|
1757
|
+
console.log("[tablinum:onWrite] gift wrap stored, publishing...");
|
|
1758
1758
|
const publishEffect = Effect14.gen(function* () {
|
|
1759
1759
|
const pubResult = yield* Effect14.result(syncHandle.publishLocal({
|
|
1760
1760
|
id: gw.id,
|
|
@@ -1763,17 +1763,17 @@ function createLocalstr(config) {
|
|
|
1763
1763
|
}));
|
|
1764
1764
|
if (pubResult._tag === "Failure") {
|
|
1765
1765
|
const err = pubResult.failure;
|
|
1766
|
-
console.error("[
|
|
1766
|
+
console.error("[tablinum:publish] failed:", err);
|
|
1767
1767
|
if (config.onSyncError)
|
|
1768
1768
|
config.onSyncError(err);
|
|
1769
1769
|
} else {
|
|
1770
|
-
console.log("[
|
|
1770
|
+
console.log("[tablinum:publish] success");
|
|
1771
1771
|
}
|
|
1772
1772
|
});
|
|
1773
1773
|
yield* Effect14.forkDetach(publishEffect);
|
|
1774
1774
|
} else {
|
|
1775
1775
|
const err = wrapResult.failure;
|
|
1776
|
-
console.error("[
|
|
1776
|
+
console.error("[tablinum:onWrite] wrap failed:", err);
|
|
1777
1777
|
if (config.onSyncError)
|
|
1778
1778
|
config.onSyncError(err);
|
|
1779
1779
|
}
|
|
@@ -1821,7 +1821,7 @@ function createLocalstr(config) {
|
|
|
1821
1821
|
}
|
|
1822
1822
|
export {
|
|
1823
1823
|
field,
|
|
1824
|
-
|
|
1824
|
+
createTablinum,
|
|
1825
1825
|
collection,
|
|
1826
1826
|
ValidationError,
|
|
1827
1827
|
SyncError,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CollectionDef, CollectionFields } from "../schema/collection.ts";
|
|
2
|
+
import type { InferRecord } from "../schema/types.ts";
|
|
3
|
+
import type { CollectionHandle } from "../crud/collection-handle.ts";
|
|
4
|
+
import { type SvelteWhereClause, type SvelteOrderByBuilder } from "./query.svelte.ts";
|
|
5
|
+
export declare class Collection<C extends CollectionDef<CollectionFields>> {
|
|
6
|
+
#private;
|
|
7
|
+
items: any;
|
|
8
|
+
error: any;
|
|
9
|
+
constructor(handle: CollectionHandle<C>);
|
|
10
|
+
add: (data: Omit<InferRecord<C>, "id">) => Promise<string>;
|
|
11
|
+
update: (id: string, data: Partial<Omit<InferRecord<C>, "id">>) => Promise<void>;
|
|
12
|
+
delete: (id: string) => Promise<void>;
|
|
13
|
+
get: (id: string) => Promise<InferRecord<C>>;
|
|
14
|
+
first: () => Promise<InferRecord<C> | null>;
|
|
15
|
+
count: () => Promise<number>;
|
|
16
|
+
where: (field: string & keyof Omit<InferRecord<C>, "id">) => SvelteWhereClause<InferRecord<C>>;
|
|
17
|
+
orderBy: (field: string & keyof Omit<InferRecord<C>, "id">) => SvelteOrderByBuilder<InferRecord<C>>;
|
|
18
|
+
/** @internal Called by Database.close() */
|
|
19
|
+
_destroy(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Scope } from "effect";
|
|
2
|
+
import type { SchemaConfig } from "../schema/types.ts";
|
|
3
|
+
import type { DatabaseHandle } from "../db/database-handle.ts";
|
|
4
|
+
import { Collection } from "./collection.svelte.ts";
|
|
5
|
+
export declare class Database<S extends SchemaConfig> {
|
|
6
|
+
#private;
|
|
7
|
+
status: any;
|
|
8
|
+
error: any;
|
|
9
|
+
constructor(handle: DatabaseHandle<S>, scope: Scope.Closeable);
|
|
10
|
+
collection<K extends string & keyof S>(name: K): Collection<S[K]>;
|
|
11
|
+
exportKey(): string;
|
|
12
|
+
close: () => Promise<void>;
|
|
13
|
+
sync: () => Promise<void>;
|
|
14
|
+
rebuild: () => Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SchemaConfig } from "../schema/types.ts";
|
|
2
|
+
import { type TablinumConfig } from "../db/create-tablinum.ts";
|
|
3
|
+
import { Database } from "./database.svelte.ts";
|
|
4
|
+
export { field } from "../schema/field.ts";
|
|
5
|
+
export { collection } from "../schema/collection.ts";
|
|
6
|
+
export type { CollectionDef, CollectionFields } from "../schema/collection.ts";
|
|
7
|
+
export type { FieldDef, FieldKind } from "../schema/field.ts";
|
|
8
|
+
export type { InferRecord, SchemaConfig } from "../schema/types.ts";
|
|
9
|
+
export type { TablinumConfig } from "../db/create-tablinum.ts";
|
|
10
|
+
export type { SyncStatus } from "../db/database-handle.ts";
|
|
11
|
+
export { ValidationError, StorageError, CryptoError, RelayError, SyncError, NotFoundError, ClosedError, } from "../errors.ts";
|
|
12
|
+
export { Database } from "./database.svelte.ts";
|
|
13
|
+
export { Collection } from "./collection.svelte.ts";
|
|
14
|
+
export { LiveQuery } from "./live-query.svelte.ts";
|
|
15
|
+
export type { SvelteQueryBuilder, SvelteWhereClause, SvelteOrderByBuilder, } from "./query.svelte.ts";
|
|
16
|
+
export declare function createTablinum<S extends SchemaConfig>(config: TablinumConfig<S>): Promise<Database<S>>;
|