silosdk 0.0.7 → 0.0.9

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
- ## Datastores
28
+ ## Stores
29
29
 
30
- Datastores define SQLite-backed document collections and a link collection for
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 { createDatastore, createID } from 'silosdk/source'
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 datastore = createDatastore<{
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(datastore.collectionOptions('posts'))
64
- export const tags = createCollection(datastore.collectionOptions('tags'))
65
- export const links = createCollection(datastore.linkCollectionOptions())
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 only need one
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
 
@@ -223,16 +223,30 @@ const { data: uri } = useQuery(media.queryOptions(post.coverImage))
223
223
  `media.queryOptions()` returns `string | null`. It returns `null` when the ref is
224
224
  empty, invalid, unknown, or missing on disk.
225
225
 
226
+ Delete a media ref when your app no longer stores it:
227
+
228
+ ```tsx
229
+ const deleteMedia = useMutation(media.deleteMutationOptions({ queryClient }))
230
+
231
+ await deleteMedia.mutateAsync(coverImage)
232
+ ```
233
+
234
+ Deletion removes Silo's copied file and media mapping only. Remove the ref from
235
+ your own source rows or settings separately. Invalid or unknown refs are safe and
236
+ return `false`.
237
+
226
238
  ## API Shape
227
239
 
228
240
  Silo exposes definition objects and option factories:
229
241
 
230
242
  ```ts
231
- createCollection(postsSource.collectionOptions())
243
+ createCollection(store.collectionOptions('posts'))
244
+ createCollection(store.linkCollectionOptions())
232
245
  useQuery(theme.queryOptions())
233
246
  useMutation(theme.mutationOptions({ queryClient }))
234
247
  useQuery(media.queryOptions(ref))
235
248
  useMutation(media.mutationOptions({ queryClient }))
249
+ useMutation(media.deleteMutationOptions({ queryClient }))
236
250
  ```
237
251
 
238
252
  There is no Silo provider and no Silo hook wrapper API.
package/dist/media.cjs CHANGED
@@ -25,6 +25,23 @@ const media = {
25
25
  return imported.ref;
26
26
  }
27
27
  });
28
+ },
29
+ delete(ref) {
30
+ return deleteMedia(ref);
31
+ },
32
+ deleteMutationOptions(options = {}) {
33
+ return (0, __tanstack_react_query.mutationOptions)({
34
+ mutationKey: [
35
+ "silo",
36
+ "media",
37
+ "delete"
38
+ ],
39
+ mutationFn: async (ref) => {
40
+ const deleted = await deleteMedia(ref);
41
+ if (isMediaRef(ref)) options.queryClient?.setQueryData(mediaQueryKey(ref), null);
42
+ return deleted;
43
+ }
44
+ });
28
45
  }
29
46
  };
30
47
  function isMediaRef(value) {
@@ -71,10 +88,22 @@ async function importMedia(input) {
71
88
  }
72
89
  function resolveMediaUri(ref) {
73
90
  if (!isMediaRef(ref)) return null;
74
- const row = getDatabase().getFirstSync(`SELECT ref, uri FROM media_files WHERE ref = ?`, [ref]);
91
+ const row = getMediaFile(ref);
75
92
  if (!row) return null;
76
93
  return new expo_file_system.File(row.uri).exists ? row.uri : null;
77
94
  }
95
+ async function deleteMedia(ref) {
96
+ if (!isMediaRef(ref)) return false;
97
+ const row = getMediaFile(ref);
98
+ if (!row) return false;
99
+ const file = new expo_file_system.File(row.uri);
100
+ if (file.exists) await file.delete();
101
+ deleteMediaFileRow(ref);
102
+ return true;
103
+ }
104
+ function getMediaFile(ref) {
105
+ return getDatabase().getFirstSync(`SELECT ref, uri FROM media_files WHERE ref = ?`, [ref]);
106
+ }
78
107
  function createMediaRef(kind, id) {
79
108
  return `media://silo/${kindDirectoryName(kind)}/${id}`;
80
109
  }
@@ -129,6 +158,9 @@ function insertMediaFile(file) {
129
158
  JSON.stringify(file.data)
130
159
  ]);
131
160
  }
161
+ function deleteMediaFileRow(ref) {
162
+ getDatabase().runSync(`DELETE FROM media_files WHERE ref = ?`, [ref]);
163
+ }
132
164
 
133
165
  //#endregion
134
166
  exports.isMediaRef = isMediaRef;
package/dist/media.d.cts CHANGED
@@ -16,6 +16,7 @@ type MediaImportInput = {
16
16
  mimeType?: string | null;
17
17
  data?: MediaData;
18
18
  };
19
+ type MediaDeleteInput = MediaRef | string | null | undefined;
19
20
  type MediaQueryKey = readonly ['silo', 'media', MediaRef | string | null];
20
21
  type MediaMutationOptions = {
21
22
  queryClient?: QueryClient;
@@ -23,7 +24,9 @@ type MediaMutationOptions = {
23
24
  declare const media: {
24
25
  queryOptions(ref: MediaRef | string | null | undefined): UseQueryOptions<string | null, Error, string | null, MediaQueryKey>;
25
26
  mutationOptions(options?: MediaMutationOptions): UseMutationOptions<MediaRef, Error, MediaImportInput>;
27
+ delete(ref: MediaDeleteInput): Promise<boolean>;
28
+ deleteMutationOptions(options?: MediaMutationOptions): UseMutationOptions<boolean, Error, MediaDeleteInput>;
26
29
  };
27
30
  declare function isMediaRef(value: unknown): value is MediaRef;
28
31
  //#endregion
29
- export { MediaData, MediaImportInput, MediaJson, MediaKind, MediaMutationOptions, MediaQueryKey, MediaRef, isMediaRef, media };
32
+ export { MediaData, MediaDeleteInput, MediaImportInput, MediaJson, MediaKind, MediaMutationOptions, MediaQueryKey, MediaRef, isMediaRef, media };
package/dist/media.d.mts CHANGED
@@ -16,6 +16,7 @@ type MediaImportInput = {
16
16
  mimeType?: string | null;
17
17
  data?: MediaData;
18
18
  };
19
+ type MediaDeleteInput = MediaRef | string | null | undefined;
19
20
  type MediaQueryKey = readonly ['silo', 'media', MediaRef | string | null];
20
21
  type MediaMutationOptions = {
21
22
  queryClient?: QueryClient;
@@ -23,7 +24,9 @@ type MediaMutationOptions = {
23
24
  declare const media: {
24
25
  queryOptions(ref: MediaRef | string | null | undefined): UseQueryOptions<string | null, Error, string | null, MediaQueryKey>;
25
26
  mutationOptions(options?: MediaMutationOptions): UseMutationOptions<MediaRef, Error, MediaImportInput>;
27
+ delete(ref: MediaDeleteInput): Promise<boolean>;
28
+ deleteMutationOptions(options?: MediaMutationOptions): UseMutationOptions<boolean, Error, MediaDeleteInput>;
26
29
  };
27
30
  declare function isMediaRef(value: unknown): value is MediaRef;
28
31
  //#endregion
29
- export { MediaData, MediaImportInput, MediaJson, MediaKind, MediaMutationOptions, MediaQueryKey, MediaRef, isMediaRef, media };
32
+ export { MediaData, MediaDeleteInput, MediaImportInput, MediaJson, MediaKind, MediaMutationOptions, MediaQueryKey, MediaRef, isMediaRef, media };
package/dist/media.mjs CHANGED
@@ -24,6 +24,23 @@ const media = {
24
24
  return imported.ref;
25
25
  }
26
26
  });
27
+ },
28
+ delete(ref) {
29
+ return deleteMedia(ref);
30
+ },
31
+ deleteMutationOptions(options = {}) {
32
+ return mutationOptions({
33
+ mutationKey: [
34
+ "silo",
35
+ "media",
36
+ "delete"
37
+ ],
38
+ mutationFn: async (ref) => {
39
+ const deleted = await deleteMedia(ref);
40
+ if (isMediaRef(ref)) options.queryClient?.setQueryData(mediaQueryKey(ref), null);
41
+ return deleted;
42
+ }
43
+ });
27
44
  }
28
45
  };
29
46
  function isMediaRef(value) {
@@ -70,10 +87,22 @@ async function importMedia(input) {
70
87
  }
71
88
  function resolveMediaUri(ref) {
72
89
  if (!isMediaRef(ref)) return null;
73
- const row = getDatabase().getFirstSync(`SELECT ref, uri FROM media_files WHERE ref = ?`, [ref]);
90
+ const row = getMediaFile(ref);
74
91
  if (!row) return null;
75
92
  return new File(row.uri).exists ? row.uri : null;
76
93
  }
94
+ async function deleteMedia(ref) {
95
+ if (!isMediaRef(ref)) return false;
96
+ const row = getMediaFile(ref);
97
+ if (!row) return false;
98
+ const file = new File(row.uri);
99
+ if (file.exists) await file.delete();
100
+ deleteMediaFileRow(ref);
101
+ return true;
102
+ }
103
+ function getMediaFile(ref) {
104
+ return getDatabase().getFirstSync(`SELECT ref, uri FROM media_files WHERE ref = ?`, [ref]);
105
+ }
77
106
  function createMediaRef(kind, id) {
78
107
  return `media://silo/${kindDirectoryName(kind)}/${id}`;
79
108
  }
@@ -128,6 +157,9 @@ function insertMediaFile(file) {
128
157
  JSON.stringify(file.data)
129
158
  ]);
130
159
  }
160
+ function deleteMediaFileRow(ref) {
161
+ getDatabase().runSync(`DELETE FROM media_files WHERE ref = ?`, [ref]);
162
+ }
131
163
 
132
164
  //#endregion
133
165
  export { isMediaRef, media };
@@ -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/source/index.ts
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 createDatastore(defaults) {
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("Datastore collection names must be unique.");
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
- assertDatastoreCollectionName(nameSet, name);
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 assertDatastoreCollectionName(nameSet, name) {
140
- if (!nameSet.has(name)) throw new Error(`Datastore collection "${name}" is not registered.`);
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/source/index.d.ts
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 DatastoreDefaults<TCollections extends Record<string, object>> = { [Name in keyof TCollections]: SourceDefaults<TCollections[Name]> };
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 Datastore<TCollections extends Record<string, object>> = {
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 createDatastore<TCollections extends Record<string, object>>(defaults: DatastoreDefaults<TCollections>): Datastore<TCollections>;
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 { Datastore, DatastoreDefaults, DefaultValue, Doc, DocumentData, FieldValue, LinkRow, LinkUtils, Source, SourceCollectionOptions, SourceDefaults, SourceInput, SourceRow, createDatastore, createID, source };
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/source/index.d.ts
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 DatastoreDefaults<TCollections extends Record<string, object>> = { [Name in keyof TCollections]: SourceDefaults<TCollections[Name]> };
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 Datastore<TCollections extends Record<string, object>> = {
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 createDatastore<TCollections extends Record<string, object>>(defaults: DatastoreDefaults<TCollections>): Datastore<TCollections>;
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 { Datastore, DatastoreDefaults, DefaultValue, Doc, DocumentData, FieldValue, LinkRow, LinkUtils, Source, SourceCollectionOptions, SourceDefaults, SourceInput, SourceRow, createDatastore, createID, source };
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/source/index.ts
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 createDatastore(defaults) {
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("Datastore collection names must be unique.");
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
- assertDatastoreCollectionName(nameSet, name);
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 assertDatastoreCollectionName(nameSet, name) {
139
- if (!nameSet.has(name)) throw new Error(`Datastore collection "${name}" is not registered.`);
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 { createDatastore, createID, source };
429
+ export { createID, createStore, source };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "silosdk",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
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/source.cjs",
49
- "module": "./dist/source.mjs",
50
- "types": "./dist/source.d.cts",
48
+ "main": "./dist/store.cjs",
49
+ "module": "./dist/store.mjs",
50
+ "types": "./dist/store.d.cts",
51
51
  "exports": {
52
- "./source": {
53
- "types": "./dist/source.d.mts",
54
- "import": "./dist/source.mjs",
55
- "require": "./dist/source.cjs"
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",