silosdk 0.0.7 → 0.0.8
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
CHANGED
|
@@ -25,14 +25,14 @@ TanStack owns:
|
|
|
25
25
|
yarn add silosdk @tanstack/react-db @tanstack/react-query expo-crypto expo-sqlite expo-file-system
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
##
|
|
28
|
+
## Stores
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Stores define SQLite-backed document collections and a link collection for
|
|
31
31
|
relationships between them. They produce options for TanStack DB collections.
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
34
|
import { createCollection } from '@tanstack/react-db'
|
|
35
|
-
import {
|
|
35
|
+
import { createID, createStore } from 'silosdk/store'
|
|
36
36
|
|
|
37
37
|
type Post = {
|
|
38
38
|
title: string
|
|
@@ -45,7 +45,7 @@ type Tag = {
|
|
|
45
45
|
name: string
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export const
|
|
48
|
+
export const store = createStore<{
|
|
49
49
|
posts: Post
|
|
50
50
|
tags: Tag
|
|
51
51
|
}>({
|
|
@@ -60,9 +60,9 @@ export const datastore = createDatastore<{
|
|
|
60
60
|
},
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
export const posts = createCollection(
|
|
64
|
-
export const tags = createCollection(
|
|
65
|
-
export const links = createCollection(
|
|
63
|
+
export const posts = createCollection(store.collectionOptions('posts'))
|
|
64
|
+
export const tags = createCollection(store.collectionOptions('tags'))
|
|
65
|
+
export const links = createCollection(store.linkCollectionOptions())
|
|
66
66
|
```
|
|
67
67
|
|
|
68
68
|
Query and mutate with TanStack DB directly:
|
|
@@ -149,8 +149,8 @@ a different semantic relationship.
|
|
|
149
149
|
|
|
150
150
|
## Sources
|
|
151
151
|
|
|
152
|
-
The lower-level `source()` API remains available when you
|
|
153
|
-
collection definition.
|
|
152
|
+
The lower-level `source()` API remains available from `silosdk/store` when you
|
|
153
|
+
only need one collection definition.
|
|
154
154
|
|
|
155
155
|
## Settings
|
|
156
156
|
|
|
@@ -228,7 +228,8 @@ empty, invalid, unknown, or missing on disk.
|
|
|
228
228
|
Silo exposes definition objects and option factories:
|
|
229
229
|
|
|
230
230
|
```ts
|
|
231
|
-
createCollection(
|
|
231
|
+
createCollection(store.collectionOptions('posts'))
|
|
232
|
+
createCollection(store.linkCollectionOptions())
|
|
232
233
|
useQuery(theme.queryOptions())
|
|
233
234
|
useMutation(theme.mutationOptions({ queryClient }))
|
|
234
235
|
useQuery(media.queryOptions(ref))
|
|
@@ -2,7 +2,7 @@ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
|
2
2
|
let expo_crypto = require("expo-crypto");
|
|
3
3
|
let expo_sqlite = require("expo-sqlite");
|
|
4
4
|
|
|
5
|
-
//#region src/
|
|
5
|
+
//#region src/store/index.ts
|
|
6
6
|
const sourceNames = /* @__PURE__ */ new Set();
|
|
7
7
|
const sourceNamePattern = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
8
8
|
const reservedDocumentFields = new Set([
|
|
@@ -14,18 +14,18 @@ const deletedDocs = /* @__PURE__ */ new WeakSet();
|
|
|
14
14
|
function createID() {
|
|
15
15
|
return (0, expo_crypto.randomUUID)();
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function createStore(defaults) {
|
|
18
18
|
const names = Object.keys(defaults);
|
|
19
19
|
const nameSet = new Set(names);
|
|
20
20
|
for (const name of names) {
|
|
21
21
|
assertSourceName(name);
|
|
22
22
|
assertFlatDefaults(name, defaults[name]);
|
|
23
23
|
}
|
|
24
|
-
if (new Set(names).size !== names.length) throw new Error("
|
|
24
|
+
if (new Set(names).size !== names.length) throw new Error("Store collection names must be unique.");
|
|
25
25
|
return {
|
|
26
26
|
names,
|
|
27
27
|
collectionOptions(name, options = {}) {
|
|
28
|
-
|
|
28
|
+
assertStoreCollectionName(nameSet, name);
|
|
29
29
|
return createSourceCollectionOptions(name, defaults[name], options);
|
|
30
30
|
},
|
|
31
31
|
linkCollectionOptions(options = {}) {
|
|
@@ -136,8 +136,8 @@ function createLinkCollectionOptions(names, nameSet, options = {}) {
|
|
|
136
136
|
function assertSourceName(name) {
|
|
137
137
|
if (!sourceNamePattern.test(name)) throw new Error(`Source names must start with a letter and contain only letters, numbers, underscores, or hyphens. Received "${name}".`);
|
|
138
138
|
}
|
|
139
|
-
function
|
|
140
|
-
if (!nameSet.has(name)) throw new Error(`
|
|
139
|
+
function assertStoreCollectionName(nameSet, name) {
|
|
140
|
+
if (!nameSet.has(name)) throw new Error(`Store collection "${name}" is not registered.`);
|
|
141
141
|
}
|
|
142
142
|
function assertLinkCollectionName(nameSet, name) {
|
|
143
143
|
if (!nameSet.has(name)) throw new Error(`Cannot link unknown collection "${name}".`);
|
|
@@ -427,6 +427,6 @@ function deleteLinksForSourceRow(sourceName, id) {
|
|
|
427
427
|
}
|
|
428
428
|
|
|
429
429
|
//#endregion
|
|
430
|
-
exports.createDatastore = createDatastore;
|
|
431
430
|
exports.createID = createID;
|
|
431
|
+
exports.createStore = createStore;
|
|
432
432
|
exports.source = source;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
import { CollectionConfig } from "@tanstack/react-db";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/store/index.d.ts
|
|
5
5
|
type FieldValue = string | number | boolean | null;
|
|
6
6
|
type OptionalKeys<T extends object> = { [K in keyof T]-?: object extends Pick<T, K> ? K : never }[keyof T];
|
|
7
7
|
type RequiredKeys<T extends object> = Exclude<keyof T, OptionalKeys<T>>;
|
|
@@ -22,7 +22,7 @@ type SourceRow<T extends object> = Doc<T>;
|
|
|
22
22
|
type SourceCollectionOptions<T extends object> = {
|
|
23
23
|
startSync?: boolean;
|
|
24
24
|
};
|
|
25
|
-
type
|
|
25
|
+
type StoreDefaults<TCollections extends Record<string, object>> = { [Name in keyof TCollections]: SourceDefaults<TCollections[Name]> };
|
|
26
26
|
type LinkRow<TName extends string = string> = {
|
|
27
27
|
readonly id: string;
|
|
28
28
|
readonly collections: string;
|
|
@@ -36,7 +36,7 @@ type LinkUtils<TName extends string = string> = {
|
|
|
36
36
|
unlink(collectionA: TName, idA: string, collectionB: TName, idB: string): Promise<void>;
|
|
37
37
|
has(collectionA: TName, idA: string, collectionB: TName, idB: string): boolean;
|
|
38
38
|
};
|
|
39
|
-
type
|
|
39
|
+
type Store<TCollections extends Record<string, object>> = {
|
|
40
40
|
readonly names: ReadonlyArray<keyof TCollections & string>;
|
|
41
41
|
collectionOptions<Name$1 extends keyof TCollections & string>(name: Name$1, options?: SourceCollectionOptions<TCollections[Name$1]>): CollectionConfig<SourceRow<TCollections[Name$1]>, string, StandardSchemaV1<any, SourceRow<TCollections[Name$1]>>> & {
|
|
42
42
|
schema: StandardSchemaV1<any, SourceRow<TCollections[Name$1]>>;
|
|
@@ -55,7 +55,7 @@ type Source<T extends object> = {
|
|
|
55
55
|
};
|
|
56
56
|
};
|
|
57
57
|
declare function createID(): string;
|
|
58
|
-
declare function
|
|
58
|
+
declare function createStore<TCollections extends Record<string, object>>(defaults: StoreDefaults<TCollections>): Store<TCollections>;
|
|
59
59
|
declare function source<T extends object>(name: string, defaults: SourceDefaults<T>): Source<T>;
|
|
60
60
|
//#endregion
|
|
61
|
-
export {
|
|
61
|
+
export { DefaultValue, Doc, DocumentData, FieldValue, LinkRow, LinkUtils, Source, SourceCollectionOptions, SourceDefaults, SourceInput, SourceRow, Store, StoreDefaults, createID, createStore, source };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
import { CollectionConfig } from "@tanstack/react-db";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/store/index.d.ts
|
|
5
5
|
type FieldValue = string | number | boolean | null;
|
|
6
6
|
type OptionalKeys<T extends object> = { [K in keyof T]-?: object extends Pick<T, K> ? K : never }[keyof T];
|
|
7
7
|
type RequiredKeys<T extends object> = Exclude<keyof T, OptionalKeys<T>>;
|
|
@@ -22,7 +22,7 @@ type SourceRow<T extends object> = Doc<T>;
|
|
|
22
22
|
type SourceCollectionOptions<T extends object> = {
|
|
23
23
|
startSync?: boolean;
|
|
24
24
|
};
|
|
25
|
-
type
|
|
25
|
+
type StoreDefaults<TCollections extends Record<string, object>> = { [Name in keyof TCollections]: SourceDefaults<TCollections[Name]> };
|
|
26
26
|
type LinkRow<TName extends string = string> = {
|
|
27
27
|
readonly id: string;
|
|
28
28
|
readonly collections: string;
|
|
@@ -36,7 +36,7 @@ type LinkUtils<TName extends string = string> = {
|
|
|
36
36
|
unlink(collectionA: TName, idA: string, collectionB: TName, idB: string): Promise<void>;
|
|
37
37
|
has(collectionA: TName, idA: string, collectionB: TName, idB: string): boolean;
|
|
38
38
|
};
|
|
39
|
-
type
|
|
39
|
+
type Store<TCollections extends Record<string, object>> = {
|
|
40
40
|
readonly names: ReadonlyArray<keyof TCollections & string>;
|
|
41
41
|
collectionOptions<Name$1 extends keyof TCollections & string>(name: Name$1, options?: SourceCollectionOptions<TCollections[Name$1]>): CollectionConfig<SourceRow<TCollections[Name$1]>, string, StandardSchemaV1<any, SourceRow<TCollections[Name$1]>>> & {
|
|
42
42
|
schema: StandardSchemaV1<any, SourceRow<TCollections[Name$1]>>;
|
|
@@ -55,7 +55,7 @@ type Source<T extends object> = {
|
|
|
55
55
|
};
|
|
56
56
|
};
|
|
57
57
|
declare function createID(): string;
|
|
58
|
-
declare function
|
|
58
|
+
declare function createStore<TCollections extends Record<string, object>>(defaults: StoreDefaults<TCollections>): Store<TCollections>;
|
|
59
59
|
declare function source<T extends object>(name: string, defaults: SourceDefaults<T>): Source<T>;
|
|
60
60
|
//#endregion
|
|
61
|
-
export {
|
|
61
|
+
export { DefaultValue, Doc, DocumentData, FieldValue, LinkRow, LinkUtils, Source, SourceCollectionOptions, SourceDefaults, SourceInput, SourceRow, Store, StoreDefaults, createID, createStore, source };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "expo-crypto";
|
|
2
2
|
import { openDatabaseSync } from "expo-sqlite";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/store/index.ts
|
|
5
5
|
const sourceNames = /* @__PURE__ */ new Set();
|
|
6
6
|
const sourceNamePattern = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
7
7
|
const reservedDocumentFields = new Set([
|
|
@@ -13,18 +13,18 @@ const deletedDocs = /* @__PURE__ */ new WeakSet();
|
|
|
13
13
|
function createID() {
|
|
14
14
|
return randomUUID();
|
|
15
15
|
}
|
|
16
|
-
function
|
|
16
|
+
function createStore(defaults) {
|
|
17
17
|
const names = Object.keys(defaults);
|
|
18
18
|
const nameSet = new Set(names);
|
|
19
19
|
for (const name of names) {
|
|
20
20
|
assertSourceName(name);
|
|
21
21
|
assertFlatDefaults(name, defaults[name]);
|
|
22
22
|
}
|
|
23
|
-
if (new Set(names).size !== names.length) throw new Error("
|
|
23
|
+
if (new Set(names).size !== names.length) throw new Error("Store collection names must be unique.");
|
|
24
24
|
return {
|
|
25
25
|
names,
|
|
26
26
|
collectionOptions(name, options = {}) {
|
|
27
|
-
|
|
27
|
+
assertStoreCollectionName(nameSet, name);
|
|
28
28
|
return createSourceCollectionOptions(name, defaults[name], options);
|
|
29
29
|
},
|
|
30
30
|
linkCollectionOptions(options = {}) {
|
|
@@ -135,8 +135,8 @@ function createLinkCollectionOptions(names, nameSet, options = {}) {
|
|
|
135
135
|
function assertSourceName(name) {
|
|
136
136
|
if (!sourceNamePattern.test(name)) throw new Error(`Source names must start with a letter and contain only letters, numbers, underscores, or hyphens. Received "${name}".`);
|
|
137
137
|
}
|
|
138
|
-
function
|
|
139
|
-
if (!nameSet.has(name)) throw new Error(`
|
|
138
|
+
function assertStoreCollectionName(nameSet, name) {
|
|
139
|
+
if (!nameSet.has(name)) throw new Error(`Store collection "${name}" is not registered.`);
|
|
140
140
|
}
|
|
141
141
|
function assertLinkCollectionName(nameSet, name) {
|
|
142
142
|
if (!nameSet.has(name)) throw new Error(`Cannot link unknown collection "${name}".`);
|
|
@@ -426,4 +426,4 @@ function deleteLinksForSourceRow(sourceName, id) {
|
|
|
426
426
|
}
|
|
427
427
|
|
|
428
428
|
//#endregion
|
|
429
|
-
export {
|
|
429
|
+
export { createID, createStore, source };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "silosdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsdown",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"printWidth": 80,
|
|
46
46
|
"tabWidth": 2
|
|
47
47
|
},
|
|
48
|
-
"main": "./dist/
|
|
49
|
-
"module": "./dist/
|
|
50
|
-
"types": "./dist/
|
|
48
|
+
"main": "./dist/store.cjs",
|
|
49
|
+
"module": "./dist/store.mjs",
|
|
50
|
+
"types": "./dist/store.d.cts",
|
|
51
51
|
"exports": {
|
|
52
|
-
"./
|
|
53
|
-
"types": "./dist/
|
|
54
|
-
"import": "./dist/
|
|
55
|
-
"require": "./dist/
|
|
52
|
+
"./store": {
|
|
53
|
+
"types": "./dist/store.d.mts",
|
|
54
|
+
"import": "./dist/store.mjs",
|
|
55
|
+
"require": "./dist/store.cjs"
|
|
56
56
|
},
|
|
57
57
|
"./settings": {
|
|
58
58
|
"types": "./dist/settings.d.mts",
|