silosdk 0.0.1 → 0.0.2
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/dist/source.cjs +161 -0
- package/dist/source.d.cts +29 -0
- package/dist/source.d.mts +29 -0
- package/dist/source.mjs +161 -0
- package/package.json +19 -21
- package/README.md +0 -3
- package/dist/_virtual/rolldown_runtime.cjs +0 -29
- package/dist/cli/d1.cjs +0 -93
- package/dist/cli/d1.mjs +0 -92
- package/dist/cli/index.cjs +0 -93
- package/dist/cli/index.d.cts +0 -1
- package/dist/cli/index.d.mts +0 -1
- package/dist/cli/index.mjs +0 -94
- package/dist/cli/init.cjs +0 -134
- package/dist/cli/init.mjs +0 -133
- package/dist/cli/kv.cjs +0 -63
- package/dist/cli/kv.mjs +0 -60
- package/dist/cli/r2.cjs +0 -83
- package/dist/cli/r2.mjs +0 -82
- package/dist/cli/wrangler.cjs +0 -93
- package/dist/cli/wrangler.mjs +0 -89
- package/dist/local/adapters/cloudflare.cjs +0 -200
- package/dist/local/adapters/cloudflare.d.cts +0 -50
- package/dist/local/adapters/cloudflare.d.mts +0 -50
- package/dist/local/adapters/cloudflare.mjs +0 -200
- package/dist/local/auth-context.cjs +0 -14
- package/dist/local/auth-context.d.cts +0 -7
- package/dist/local/auth-context.d.mts +0 -7
- package/dist/local/auth-context.mjs +0 -12
- package/dist/local/auth.cjs +0 -109
- package/dist/local/auth.d.cts +0 -26
- package/dist/local/auth.d.mts +0 -26
- package/dist/local/auth.mjs +0 -99
- package/dist/local/commit.cjs +0 -350
- package/dist/local/commit.d.cts +0 -59
- package/dist/local/commit.d.mts +0 -59
- package/dist/local/commit.mjs +0 -349
- package/dist/local/config.cjs +0 -17
- package/dist/local/config.mjs +0 -15
- package/dist/local/index.cjs +0 -16
- package/dist/local/index.d.cts +0 -10
- package/dist/local/index.d.mts +0 -10
- package/dist/local/index.mjs +0 -9
- package/dist/local/provider.cjs +0 -204
- package/dist/local/provider.d.cts +0 -25
- package/dist/local/provider.d.mts +0 -25
- package/dist/local/provider.mjs +0 -203
- package/dist/local/query-store.cjs +0 -276
- package/dist/local/query-store.mjs +0 -274
- package/dist/local/storage.cjs +0 -71
- package/dist/local/storage.d.cts +0 -7
- package/dist/local/storage.d.mts +0 -7
- package/dist/local/storage.mjs +0 -68
- package/dist/local/sync.cjs +0 -124
- package/dist/local/sync.d.cts +0 -36
- package/dist/local/sync.d.mts +0 -36
- package/dist/local/sync.mjs +0 -122
- package/dist/local/view.cjs +0 -257
- package/dist/local/view.d.cts +0 -24
- package/dist/local/view.d.mts +0 -24
- package/dist/local/view.mjs +0 -254
- package/dist/package.cjs +0 -11
- package/dist/package.mjs +0 -5
- package/dist/schema/index.cjs +0 -276
- package/dist/schema/index.d.cts +0 -207
- package/dist/schema/index.d.mts +0 -207
- package/dist/schema/index.mjs +0 -265
- package/dist/server/auth.cjs +0 -132
- package/dist/server/auth.d.cts +0 -49
- package/dist/server/auth.d.mts +0 -49
- package/dist/server/auth.mjs +0 -122
- package/dist/server/d1.cjs +0 -120
- package/dist/server/d1.mjs +0 -116
- package/dist/server/do.cjs +0 -132
- package/dist/server/do.d.cts +0 -21
- package/dist/server/do.d.mts +0 -21
- package/dist/server/do.mjs +0 -131
- package/dist/server/index.cjs +0 -355
- package/dist/server/index.d.cts +0 -65
- package/dist/server/index.d.mts +0 -65
- package/dist/server/index.mjs +0 -348
- package/dist/server/protect.cjs +0 -34
- package/dist/server/protect.d.cts +0 -32
- package/dist/server/protect.d.mts +0 -32
- package/dist/server/protect.mjs +0 -33
- package/dist/server/r2.cjs +0 -58
- package/dist/server/r2.d.cts +0 -4
- package/dist/server/r2.d.mts +0 -4
- package/dist/server/r2.mjs +0 -53
package/dist/source.cjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
let expo_sqlite = require("expo-sqlite");
|
|
2
|
+
|
|
3
|
+
//#region src/source/index.ts
|
|
4
|
+
const sourceNames = /* @__PURE__ */ new Set();
|
|
5
|
+
const sourceNamePattern = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
6
|
+
function source(name, defaults) {
|
|
7
|
+
assertSourceName(name);
|
|
8
|
+
assertFlatDefaults(name, defaults);
|
|
9
|
+
if (sourceNames.has(name)) throw new Error(`Source "${name}" is already registered. Define each source once and import the existing source instead.`);
|
|
10
|
+
sourceNames.add(name);
|
|
11
|
+
const schema = createSourceSchema(name, defaults);
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
defaults,
|
|
15
|
+
schema,
|
|
16
|
+
collectionOptions(options = {}) {
|
|
17
|
+
return {
|
|
18
|
+
id: name,
|
|
19
|
+
getKey: (item) => item.id,
|
|
20
|
+
schema,
|
|
21
|
+
startSync: options.startSync,
|
|
22
|
+
sync: { sync({ begin, write, commit, markReady, truncate }) {
|
|
23
|
+
const rows = loadRows(name);
|
|
24
|
+
begin();
|
|
25
|
+
truncate();
|
|
26
|
+
for (const row of rows) write({
|
|
27
|
+
type: "insert",
|
|
28
|
+
value: row
|
|
29
|
+
});
|
|
30
|
+
commit();
|
|
31
|
+
markReady();
|
|
32
|
+
} },
|
|
33
|
+
onInsert: async ({ transaction }) => {
|
|
34
|
+
await insertRows(name, transaction.mutations.map((mutation) => mutation.modified));
|
|
35
|
+
},
|
|
36
|
+
onUpdate: async ({ transaction }) => {
|
|
37
|
+
await updateRows(name, transaction.mutations);
|
|
38
|
+
},
|
|
39
|
+
onDelete: async ({ transaction }) => {
|
|
40
|
+
await deleteRows(name, transaction.mutations.map((mutation) => mutation.key));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function assertSourceName(name) {
|
|
47
|
+
if (!sourceNamePattern.test(name)) throw new Error(`Source names must start with a letter and contain only letters, numbers, underscores, or hyphens. Received "${name}".`);
|
|
48
|
+
}
|
|
49
|
+
function assertFlatDefaults(sourceName, defaults) {
|
|
50
|
+
for (const [field, defaultValue] of Object.entries(defaults)) if (!isFieldValue(resolveDefault(defaultValue))) throw new Error(`Source "${sourceName}" field "${field}" must default to a string, number, boolean, null, or a function returning one of those values.`);
|
|
51
|
+
}
|
|
52
|
+
function createSourceSchema(sourceName, defaults) {
|
|
53
|
+
return { "~standard": {
|
|
54
|
+
version: 1,
|
|
55
|
+
vendor: "silo",
|
|
56
|
+
validate(value) {
|
|
57
|
+
if (!isRecord(value)) return failure("Expected a flat source row object.");
|
|
58
|
+
if (typeof value.id !== "string") return failure("Expected source row field \"id\" to be a string.", ["id"]);
|
|
59
|
+
const row = { id: value.id };
|
|
60
|
+
for (const [field, fieldValue] of Object.entries(value)) {
|
|
61
|
+
if (field === "id") continue;
|
|
62
|
+
if (!isFieldValue(fieldValue)) return failure(`Source "${sourceName}" field "${field}" must be a string, number, boolean, or null.`, [field]);
|
|
63
|
+
row[field] = fieldValue;
|
|
64
|
+
}
|
|
65
|
+
for (const [field, defaultValue] of Object.entries(defaults)) {
|
|
66
|
+
if (field in row) continue;
|
|
67
|
+
row[field] = resolveDefault(defaultValue);
|
|
68
|
+
}
|
|
69
|
+
return { value: row };
|
|
70
|
+
}
|
|
71
|
+
} };
|
|
72
|
+
}
|
|
73
|
+
function resolveDefault(value) {
|
|
74
|
+
const resolved = typeof value === "function" ? value() : value;
|
|
75
|
+
if (!isFieldValue(resolved)) throw new Error("Source defaults must resolve to a string, number, boolean, or null.");
|
|
76
|
+
return resolved;
|
|
77
|
+
}
|
|
78
|
+
function isRecord(value) {
|
|
79
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
80
|
+
}
|
|
81
|
+
function isFieldValue(value) {
|
|
82
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null;
|
|
83
|
+
}
|
|
84
|
+
function failure(message, path) {
|
|
85
|
+
return { issues: [{
|
|
86
|
+
message,
|
|
87
|
+
path
|
|
88
|
+
}] };
|
|
89
|
+
}
|
|
90
|
+
let database;
|
|
91
|
+
function getDatabase() {
|
|
92
|
+
if (!database) {
|
|
93
|
+
database = (0, expo_sqlite.openDatabaseSync)("silo.db");
|
|
94
|
+
database.execSync(`
|
|
95
|
+
PRAGMA journal_mode = WAL;
|
|
96
|
+
|
|
97
|
+
CREATE TABLE IF NOT EXISTS sources (
|
|
98
|
+
source TEXT NOT NULL,
|
|
99
|
+
id TEXT NOT NULL,
|
|
100
|
+
data TEXT NOT NULL,
|
|
101
|
+
createdAt TEXT NOT NULL,
|
|
102
|
+
updatedAt TEXT NOT NULL,
|
|
103
|
+
version INTEGER NOT NULL DEFAULT 1,
|
|
104
|
+
PRIMARY KEY (source, id)
|
|
105
|
+
);
|
|
106
|
+
`);
|
|
107
|
+
}
|
|
108
|
+
return database;
|
|
109
|
+
}
|
|
110
|
+
function loadRows(sourceName) {
|
|
111
|
+
return getDatabase().getAllSync(`SELECT id, data FROM sources WHERE source = ?`, [sourceName]).map((row) => ({
|
|
112
|
+
id: row.id,
|
|
113
|
+
...JSON.parse(row.data)
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
function insertRows(sourceName, rows) {
|
|
117
|
+
const db = getDatabase();
|
|
118
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
119
|
+
db.withTransactionSync(() => {
|
|
120
|
+
for (const row of rows) {
|
|
121
|
+
const { id, ...data } = row;
|
|
122
|
+
db.runSync(`INSERT INTO sources (source, id, data, createdAt, updatedAt, version)
|
|
123
|
+
VALUES (?, ?, ?, ?, ?, 1)`, [
|
|
124
|
+
sourceName,
|
|
125
|
+
id,
|
|
126
|
+
JSON.stringify(data),
|
|
127
|
+
timestamp,
|
|
128
|
+
timestamp
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return Promise.resolve();
|
|
133
|
+
}
|
|
134
|
+
function updateRows(sourceName, mutations) {
|
|
135
|
+
const db = getDatabase();
|
|
136
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
137
|
+
db.withTransactionSync(() => {
|
|
138
|
+
for (const mutation of mutations) {
|
|
139
|
+
const { id, ...data } = mutation.modified;
|
|
140
|
+
db.runSync(`UPDATE sources
|
|
141
|
+
SET data = ?, updatedAt = ?, version = version + 1
|
|
142
|
+
WHERE source = ? AND id = ?`, [
|
|
143
|
+
JSON.stringify(data),
|
|
144
|
+
timestamp,
|
|
145
|
+
sourceName,
|
|
146
|
+
String(mutation.key)
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return Promise.resolve();
|
|
151
|
+
}
|
|
152
|
+
function deleteRows(sourceName, ids) {
|
|
153
|
+
const db = getDatabase();
|
|
154
|
+
db.withTransactionSync(() => {
|
|
155
|
+
for (const id of ids) db.runSync(`DELETE FROM sources WHERE source = ? AND id = ?`, [sourceName, String(id)]);
|
|
156
|
+
});
|
|
157
|
+
return Promise.resolve();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
exports.source = source;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { CollectionConfig } from "@tanstack/react-db";
|
|
3
|
+
|
|
4
|
+
//#region src/source/index.d.ts
|
|
5
|
+
type FieldValue = string | number | boolean | null;
|
|
6
|
+
type OptionalKeys<T extends object> = { [K in keyof T]-?: object extends Pick<T, K> ? K : never }[keyof T];
|
|
7
|
+
type RequiredKeys<T extends object> = Exclude<keyof T, OptionalKeys<T>>;
|
|
8
|
+
type InvalidDocumentKeys<T extends object> = { [K in keyof T]-?: Exclude<T[K], undefined> extends FieldValue ? never : K }[keyof T];
|
|
9
|
+
type DocumentData<T extends object> = InvalidDocumentKeys<T> extends never ? T : never;
|
|
10
|
+
type DefaultValue<T> = Exclude<T, undefined> | (() => Exclude<T, undefined>);
|
|
11
|
+
type SourceDefaults<T extends object> = { [K in RequiredKeys<DocumentData<T>>]: DefaultValue<DocumentData<T>[K]> } & { [K in OptionalKeys<DocumentData<T>>]?: DefaultValue<DocumentData<T>[K]> };
|
|
12
|
+
type SourceRow<T extends object> = {
|
|
13
|
+
id: string;
|
|
14
|
+
} & DocumentData<T>;
|
|
15
|
+
type SourceInput<T extends object> = {
|
|
16
|
+
id: string;
|
|
17
|
+
} & Partial<DocumentData<T>>;
|
|
18
|
+
type SourceCollectionOptions<T extends object> = {
|
|
19
|
+
startSync?: boolean;
|
|
20
|
+
};
|
|
21
|
+
type Source<T extends object> = {
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly defaults: SourceDefaults<T>;
|
|
24
|
+
readonly schema: StandardSchemaV1<SourceInput<T>, SourceRow<T>>;
|
|
25
|
+
collectionOptions(options?: SourceCollectionOptions<T>): CollectionConfig<SourceRow<T>, string, StandardSchemaV1<SourceInput<T>, SourceRow<T>>>;
|
|
26
|
+
};
|
|
27
|
+
declare function source<T extends object>(name: string, defaults: SourceDefaults<T>): Source<T>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { DefaultValue, DocumentData, FieldValue, Source, SourceCollectionOptions, SourceDefaults, SourceInput, SourceRow, source };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { CollectionConfig } from "@tanstack/react-db";
|
|
3
|
+
|
|
4
|
+
//#region src/source/index.d.ts
|
|
5
|
+
type FieldValue = string | number | boolean | null;
|
|
6
|
+
type OptionalKeys<T extends object> = { [K in keyof T]-?: object extends Pick<T, K> ? K : never }[keyof T];
|
|
7
|
+
type RequiredKeys<T extends object> = Exclude<keyof T, OptionalKeys<T>>;
|
|
8
|
+
type InvalidDocumentKeys<T extends object> = { [K in keyof T]-?: Exclude<T[K], undefined> extends FieldValue ? never : K }[keyof T];
|
|
9
|
+
type DocumentData<T extends object> = InvalidDocumentKeys<T> extends never ? T : never;
|
|
10
|
+
type DefaultValue<T> = Exclude<T, undefined> | (() => Exclude<T, undefined>);
|
|
11
|
+
type SourceDefaults<T extends object> = { [K in RequiredKeys<DocumentData<T>>]: DefaultValue<DocumentData<T>[K]> } & { [K in OptionalKeys<DocumentData<T>>]?: DefaultValue<DocumentData<T>[K]> };
|
|
12
|
+
type SourceRow<T extends object> = {
|
|
13
|
+
id: string;
|
|
14
|
+
} & DocumentData<T>;
|
|
15
|
+
type SourceInput<T extends object> = {
|
|
16
|
+
id: string;
|
|
17
|
+
} & Partial<DocumentData<T>>;
|
|
18
|
+
type SourceCollectionOptions<T extends object> = {
|
|
19
|
+
startSync?: boolean;
|
|
20
|
+
};
|
|
21
|
+
type Source<T extends object> = {
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly defaults: SourceDefaults<T>;
|
|
24
|
+
readonly schema: StandardSchemaV1<SourceInput<T>, SourceRow<T>>;
|
|
25
|
+
collectionOptions(options?: SourceCollectionOptions<T>): CollectionConfig<SourceRow<T>, string, StandardSchemaV1<SourceInput<T>, SourceRow<T>>>;
|
|
26
|
+
};
|
|
27
|
+
declare function source<T extends object>(name: string, defaults: SourceDefaults<T>): Source<T>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { DefaultValue, DocumentData, FieldValue, Source, SourceCollectionOptions, SourceDefaults, SourceInput, SourceRow, source };
|
package/dist/source.mjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { openDatabaseSync } from "expo-sqlite";
|
|
2
|
+
|
|
3
|
+
//#region src/source/index.ts
|
|
4
|
+
const sourceNames = /* @__PURE__ */ new Set();
|
|
5
|
+
const sourceNamePattern = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
6
|
+
function source(name, defaults) {
|
|
7
|
+
assertSourceName(name);
|
|
8
|
+
assertFlatDefaults(name, defaults);
|
|
9
|
+
if (sourceNames.has(name)) throw new Error(`Source "${name}" is already registered. Define each source once and import the existing source instead.`);
|
|
10
|
+
sourceNames.add(name);
|
|
11
|
+
const schema = createSourceSchema(name, defaults);
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
defaults,
|
|
15
|
+
schema,
|
|
16
|
+
collectionOptions(options = {}) {
|
|
17
|
+
return {
|
|
18
|
+
id: name,
|
|
19
|
+
getKey: (item) => item.id,
|
|
20
|
+
schema,
|
|
21
|
+
startSync: options.startSync,
|
|
22
|
+
sync: { sync({ begin, write, commit, markReady, truncate }) {
|
|
23
|
+
const rows = loadRows(name);
|
|
24
|
+
begin();
|
|
25
|
+
truncate();
|
|
26
|
+
for (const row of rows) write({
|
|
27
|
+
type: "insert",
|
|
28
|
+
value: row
|
|
29
|
+
});
|
|
30
|
+
commit();
|
|
31
|
+
markReady();
|
|
32
|
+
} },
|
|
33
|
+
onInsert: async ({ transaction }) => {
|
|
34
|
+
await insertRows(name, transaction.mutations.map((mutation) => mutation.modified));
|
|
35
|
+
},
|
|
36
|
+
onUpdate: async ({ transaction }) => {
|
|
37
|
+
await updateRows(name, transaction.mutations);
|
|
38
|
+
},
|
|
39
|
+
onDelete: async ({ transaction }) => {
|
|
40
|
+
await deleteRows(name, transaction.mutations.map((mutation) => mutation.key));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function assertSourceName(name) {
|
|
47
|
+
if (!sourceNamePattern.test(name)) throw new Error(`Source names must start with a letter and contain only letters, numbers, underscores, or hyphens. Received "${name}".`);
|
|
48
|
+
}
|
|
49
|
+
function assertFlatDefaults(sourceName, defaults) {
|
|
50
|
+
for (const [field, defaultValue] of Object.entries(defaults)) if (!isFieldValue(resolveDefault(defaultValue))) throw new Error(`Source "${sourceName}" field "${field}" must default to a string, number, boolean, null, or a function returning one of those values.`);
|
|
51
|
+
}
|
|
52
|
+
function createSourceSchema(sourceName, defaults) {
|
|
53
|
+
return { "~standard": {
|
|
54
|
+
version: 1,
|
|
55
|
+
vendor: "silo",
|
|
56
|
+
validate(value) {
|
|
57
|
+
if (!isRecord(value)) return failure("Expected a flat source row object.");
|
|
58
|
+
if (typeof value.id !== "string") return failure("Expected source row field \"id\" to be a string.", ["id"]);
|
|
59
|
+
const row = { id: value.id };
|
|
60
|
+
for (const [field, fieldValue] of Object.entries(value)) {
|
|
61
|
+
if (field === "id") continue;
|
|
62
|
+
if (!isFieldValue(fieldValue)) return failure(`Source "${sourceName}" field "${field}" must be a string, number, boolean, or null.`, [field]);
|
|
63
|
+
row[field] = fieldValue;
|
|
64
|
+
}
|
|
65
|
+
for (const [field, defaultValue] of Object.entries(defaults)) {
|
|
66
|
+
if (field in row) continue;
|
|
67
|
+
row[field] = resolveDefault(defaultValue);
|
|
68
|
+
}
|
|
69
|
+
return { value: row };
|
|
70
|
+
}
|
|
71
|
+
} };
|
|
72
|
+
}
|
|
73
|
+
function resolveDefault(value) {
|
|
74
|
+
const resolved = typeof value === "function" ? value() : value;
|
|
75
|
+
if (!isFieldValue(resolved)) throw new Error("Source defaults must resolve to a string, number, boolean, or null.");
|
|
76
|
+
return resolved;
|
|
77
|
+
}
|
|
78
|
+
function isRecord(value) {
|
|
79
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
80
|
+
}
|
|
81
|
+
function isFieldValue(value) {
|
|
82
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null;
|
|
83
|
+
}
|
|
84
|
+
function failure(message, path) {
|
|
85
|
+
return { issues: [{
|
|
86
|
+
message,
|
|
87
|
+
path
|
|
88
|
+
}] };
|
|
89
|
+
}
|
|
90
|
+
let database;
|
|
91
|
+
function getDatabase() {
|
|
92
|
+
if (!database) {
|
|
93
|
+
database = openDatabaseSync("silo.db");
|
|
94
|
+
database.execSync(`
|
|
95
|
+
PRAGMA journal_mode = WAL;
|
|
96
|
+
|
|
97
|
+
CREATE TABLE IF NOT EXISTS sources (
|
|
98
|
+
source TEXT NOT NULL,
|
|
99
|
+
id TEXT NOT NULL,
|
|
100
|
+
data TEXT NOT NULL,
|
|
101
|
+
createdAt TEXT NOT NULL,
|
|
102
|
+
updatedAt TEXT NOT NULL,
|
|
103
|
+
version INTEGER NOT NULL DEFAULT 1,
|
|
104
|
+
PRIMARY KEY (source, id)
|
|
105
|
+
);
|
|
106
|
+
`);
|
|
107
|
+
}
|
|
108
|
+
return database;
|
|
109
|
+
}
|
|
110
|
+
function loadRows(sourceName) {
|
|
111
|
+
return getDatabase().getAllSync(`SELECT id, data FROM sources WHERE source = ?`, [sourceName]).map((row) => ({
|
|
112
|
+
id: row.id,
|
|
113
|
+
...JSON.parse(row.data)
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
function insertRows(sourceName, rows) {
|
|
117
|
+
const db = getDatabase();
|
|
118
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
119
|
+
db.withTransactionSync(() => {
|
|
120
|
+
for (const row of rows) {
|
|
121
|
+
const { id, ...data } = row;
|
|
122
|
+
db.runSync(`INSERT INTO sources (source, id, data, createdAt, updatedAt, version)
|
|
123
|
+
VALUES (?, ?, ?, ?, ?, 1)`, [
|
|
124
|
+
sourceName,
|
|
125
|
+
id,
|
|
126
|
+
JSON.stringify(data),
|
|
127
|
+
timestamp,
|
|
128
|
+
timestamp
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return Promise.resolve();
|
|
133
|
+
}
|
|
134
|
+
function updateRows(sourceName, mutations) {
|
|
135
|
+
const db = getDatabase();
|
|
136
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
137
|
+
db.withTransactionSync(() => {
|
|
138
|
+
for (const mutation of mutations) {
|
|
139
|
+
const { id, ...data } = mutation.modified;
|
|
140
|
+
db.runSync(`UPDATE sources
|
|
141
|
+
SET data = ?, updatedAt = ?, version = version + 1
|
|
142
|
+
WHERE source = ? AND id = ?`, [
|
|
143
|
+
JSON.stringify(data),
|
|
144
|
+
timestamp,
|
|
145
|
+
sourceName,
|
|
146
|
+
String(mutation.key)
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return Promise.resolve();
|
|
151
|
+
}
|
|
152
|
+
function deleteRows(sourceName, ids) {
|
|
153
|
+
const db = getDatabase();
|
|
154
|
+
db.withTransactionSync(() => {
|
|
155
|
+
for (const id of ids) db.runSync(`DELETE FROM sources WHERE source = ? AND id = ?`, [sourceName, String(id)]);
|
|
156
|
+
});
|
|
157
|
+
return Promise.resolve();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
export { source };
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "silosdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsdown",
|
|
7
7
|
"watch": "tsdown --watch",
|
|
8
|
+
"test": "vitest run",
|
|
8
9
|
"publish": "tsdown && npm publish"
|
|
9
10
|
},
|
|
10
|
-
"packageManager": "yarn@
|
|
11
|
+
"packageManager": "yarn@1.22.22",
|
|
11
12
|
"files": [
|
|
12
13
|
"dist/"
|
|
13
14
|
],
|
|
14
|
-
"bin": {
|
|
15
|
-
"silo": "./dist/cli/index.mjs"
|
|
16
|
-
},
|
|
17
15
|
"devDependencies": {
|
|
18
|
-
"@
|
|
16
|
+
"@tanstack/react-db": "^0.1.86",
|
|
17
|
+
"@tanstack/react-query": "^5.101.0",
|
|
19
18
|
"@types/node": "^25.3.0",
|
|
20
19
|
"@types/react": "^19",
|
|
21
|
-
"expo-secure-store": "*",
|
|
22
20
|
"expo-sqlite": "*",
|
|
23
21
|
"prettier": "^3.7.4",
|
|
22
|
+
"react-native": "0.85.0",
|
|
24
23
|
"tsdown": "^0.17.2",
|
|
25
|
-
"typescript": "^5.9.3"
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vitest": "^4.0.18"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@standard-schema/spec": "^1.0.0",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"@tanstack/react-db": "^0.1.86",
|
|
30
|
+
"@tanstack/react-query": "^5.101.0",
|
|
31
31
|
"nanoid": "5.1.6"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"expo-secure-store": "*",
|
|
35
34
|
"expo-sqlite": "*",
|
|
36
|
-
"react": "^19"
|
|
35
|
+
"react": "^19",
|
|
36
|
+
"react-native": "0.85.x"
|
|
37
37
|
},
|
|
38
38
|
"prettier": {
|
|
39
39
|
"semi": false,
|
|
@@ -42,16 +42,14 @@
|
|
|
42
42
|
"printWidth": 80,
|
|
43
43
|
"tabWidth": 2
|
|
44
44
|
},
|
|
45
|
+
"main": "./dist/source.cjs",
|
|
46
|
+
"module": "./dist/source.mjs",
|
|
47
|
+
"types": "./dist/source.d.cts",
|
|
45
48
|
"exports": {
|
|
46
|
-
"./
|
|
47
|
-
"types": "./dist/
|
|
48
|
-
"import": "./dist/
|
|
49
|
-
"require": "./dist/
|
|
50
|
-
},
|
|
51
|
-
"./local": "./dist/local/index.cjs",
|
|
52
|
-
"./server": {
|
|
53
|
-
"types": "./dist/server/index.d.mts",
|
|
54
|
-
"import": "./dist/server/index.mjs"
|
|
49
|
+
"./source": {
|
|
50
|
+
"types": "./dist/source.d.mts",
|
|
51
|
+
"import": "./dist/source.mjs",
|
|
52
|
+
"require": "./dist/source.cjs"
|
|
55
53
|
},
|
|
56
54
|
"./package.json": "./package.json"
|
|
57
55
|
}
|
package/README.md
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
//#region rolldown:runtime
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
-
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
-
__defProp(to, key, {
|
|
14
|
-
get: ((k) => from[k]).bind(null, key),
|
|
15
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
-
value: mod,
|
|
24
|
-
enumerable: true
|
|
25
|
-
}) : target, mod));
|
|
26
|
-
|
|
27
|
-
//#endregion
|
|
28
|
-
|
|
29
|
-
exports.__toESM = __toESM;
|
package/dist/cli/d1.cjs
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
-
const require_wrangler = require('./wrangler.cjs');
|
|
3
|
-
let node_fs = require("node:fs");
|
|
4
|
-
let node_path = require("node:path");
|
|
5
|
-
|
|
6
|
-
//#region src/cli/d1.ts
|
|
7
|
-
/**
|
|
8
|
-
* Entry point for `silo d1 <subcommand> [args]`
|
|
9
|
-
* Currently handles: create
|
|
10
|
-
* Supports fallback: --id <uuid>
|
|
11
|
-
*/
|
|
12
|
-
async function runD1(args) {
|
|
13
|
-
const idFlagIndex = args.indexOf("--id");
|
|
14
|
-
if (idFlagIndex !== -1) {
|
|
15
|
-
const uuid = args[idFlagIndex + 1];
|
|
16
|
-
if (!uuid) {
|
|
17
|
-
console.error("Error: --id requires a UUID argument");
|
|
18
|
-
process.exit(1);
|
|
19
|
-
}
|
|
20
|
-
await patchWranglerWithId(uuid);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
await d1Create();
|
|
24
|
-
}
|
|
25
|
-
async function d1Create() {
|
|
26
|
-
const wranglerPath = (0, node_path.resolve)(require_wrangler.siloDir(), "wrangler.jsonc");
|
|
27
|
-
let wranglerSource;
|
|
28
|
-
try {
|
|
29
|
-
wranglerSource = (0, node_fs.readFileSync)(wranglerPath, "utf8");
|
|
30
|
-
} catch {
|
|
31
|
-
console.error("Error: silo/wrangler.jsonc not found. Run `silo init` first.");
|
|
32
|
-
process.exit(1);
|
|
33
|
-
}
|
|
34
|
-
const dbName = extractDatabaseName(wranglerSource);
|
|
35
|
-
if (!dbName) {
|
|
36
|
-
console.error("Error: Could not find database_name in silo/wrangler.jsonc.");
|
|
37
|
-
process.exit(1);
|
|
38
|
-
}
|
|
39
|
-
console.log(`Creating D1 database: ${dbName} ...`);
|
|
40
|
-
const { stdout, code } = await require_wrangler.runWranglerCaptured([
|
|
41
|
-
"d1",
|
|
42
|
-
"create",
|
|
43
|
-
dbName
|
|
44
|
-
]);
|
|
45
|
-
if (code !== 0) {
|
|
46
|
-
console.error("wrangler d1 create failed.");
|
|
47
|
-
process.exit(1);
|
|
48
|
-
}
|
|
49
|
-
const match = stdout.match(/database_id\s*=\s*"([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"/i);
|
|
50
|
-
if (!match) {
|
|
51
|
-
console.log("\nRaw wrangler output:\n");
|
|
52
|
-
console.log(stdout);
|
|
53
|
-
console.error("\nCould not parse database_id from wrangler output.");
|
|
54
|
-
console.error("To patch manually, run: silo d1 create --id <uuid>");
|
|
55
|
-
process.exit(1);
|
|
56
|
-
}
|
|
57
|
-
const uuid = match[1];
|
|
58
|
-
await patchWranglerWithId(uuid);
|
|
59
|
-
}
|
|
60
|
-
async function patchWranglerWithId(uuid) {
|
|
61
|
-
const wranglerPath = (0, node_path.resolve)(require_wrangler.siloDir(), "wrangler.jsonc");
|
|
62
|
-
let source;
|
|
63
|
-
try {
|
|
64
|
-
source = (0, node_fs.readFileSync)(wranglerPath, "utf8");
|
|
65
|
-
} catch {
|
|
66
|
-
console.error("Error: silo/wrangler.jsonc not found. Run `silo init` first.");
|
|
67
|
-
process.exit(1);
|
|
68
|
-
}
|
|
69
|
-
const placeholder = "TODO: run `silo d1 create` and paste the id here";
|
|
70
|
-
if (!source.includes(placeholder)) {
|
|
71
|
-
console.error(`Error: Could not find the placeholder "${placeholder}" in silo/wrangler.jsonc.`);
|
|
72
|
-
console.error("Has database_id already been set?");
|
|
73
|
-
process.exit(1);
|
|
74
|
-
}
|
|
75
|
-
(0, node_fs.writeFileSync)(wranglerPath, source.replace(placeholder, uuid), "utf8");
|
|
76
|
-
console.log(`Patched silo/wrangler.jsonc with database_id = "${uuid}"`);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Strips single-line // comments and extracts database_name from wrangler.jsonc.
|
|
80
|
-
* Uses a regex rather than a full JSONC parser to avoid a dependency.
|
|
81
|
-
*/
|
|
82
|
-
function extractDatabaseName(source) {
|
|
83
|
-
const stripped = source.replace(/\/\/[^\n]*/g, "");
|
|
84
|
-
try {
|
|
85
|
-
const db = JSON.parse(stripped)?.d1_databases?.[0];
|
|
86
|
-
return typeof db?.database_name === "string" ? db.database_name : null;
|
|
87
|
-
} catch {
|
|
88
|
-
return source.match(/"database_name"\s*:\s*"([^"]+)"/)?.[1] ?? null;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
//#endregion
|
|
93
|
-
exports.runD1 = runD1;
|
package/dist/cli/d1.mjs
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { runWranglerCaptured, siloDir } from "./wrangler.mjs";
|
|
2
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
|
-
|
|
5
|
-
//#region src/cli/d1.ts
|
|
6
|
-
/**
|
|
7
|
-
* Entry point for `silo d1 <subcommand> [args]`
|
|
8
|
-
* Currently handles: create
|
|
9
|
-
* Supports fallback: --id <uuid>
|
|
10
|
-
*/
|
|
11
|
-
async function runD1(args) {
|
|
12
|
-
const idFlagIndex = args.indexOf("--id");
|
|
13
|
-
if (idFlagIndex !== -1) {
|
|
14
|
-
const uuid = args[idFlagIndex + 1];
|
|
15
|
-
if (!uuid) {
|
|
16
|
-
console.error("Error: --id requires a UUID argument");
|
|
17
|
-
process.exit(1);
|
|
18
|
-
}
|
|
19
|
-
await patchWranglerWithId(uuid);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
await d1Create();
|
|
23
|
-
}
|
|
24
|
-
async function d1Create() {
|
|
25
|
-
const wranglerPath = resolve(siloDir(), "wrangler.jsonc");
|
|
26
|
-
let wranglerSource;
|
|
27
|
-
try {
|
|
28
|
-
wranglerSource = readFileSync(wranglerPath, "utf8");
|
|
29
|
-
} catch {
|
|
30
|
-
console.error("Error: silo/wrangler.jsonc not found. Run `silo init` first.");
|
|
31
|
-
process.exit(1);
|
|
32
|
-
}
|
|
33
|
-
const dbName = extractDatabaseName(wranglerSource);
|
|
34
|
-
if (!dbName) {
|
|
35
|
-
console.error("Error: Could not find database_name in silo/wrangler.jsonc.");
|
|
36
|
-
process.exit(1);
|
|
37
|
-
}
|
|
38
|
-
console.log(`Creating D1 database: ${dbName} ...`);
|
|
39
|
-
const { stdout, code } = await runWranglerCaptured([
|
|
40
|
-
"d1",
|
|
41
|
-
"create",
|
|
42
|
-
dbName
|
|
43
|
-
]);
|
|
44
|
-
if (code !== 0) {
|
|
45
|
-
console.error("wrangler d1 create failed.");
|
|
46
|
-
process.exit(1);
|
|
47
|
-
}
|
|
48
|
-
const match = stdout.match(/database_id\s*=\s*"([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"/i);
|
|
49
|
-
if (!match) {
|
|
50
|
-
console.log("\nRaw wrangler output:\n");
|
|
51
|
-
console.log(stdout);
|
|
52
|
-
console.error("\nCould not parse database_id from wrangler output.");
|
|
53
|
-
console.error("To patch manually, run: silo d1 create --id <uuid>");
|
|
54
|
-
process.exit(1);
|
|
55
|
-
}
|
|
56
|
-
const uuid = match[1];
|
|
57
|
-
await patchWranglerWithId(uuid);
|
|
58
|
-
}
|
|
59
|
-
async function patchWranglerWithId(uuid) {
|
|
60
|
-
const wranglerPath = resolve(siloDir(), "wrangler.jsonc");
|
|
61
|
-
let source;
|
|
62
|
-
try {
|
|
63
|
-
source = readFileSync(wranglerPath, "utf8");
|
|
64
|
-
} catch {
|
|
65
|
-
console.error("Error: silo/wrangler.jsonc not found. Run `silo init` first.");
|
|
66
|
-
process.exit(1);
|
|
67
|
-
}
|
|
68
|
-
const placeholder = "TODO: run `silo d1 create` and paste the id here";
|
|
69
|
-
if (!source.includes(placeholder)) {
|
|
70
|
-
console.error(`Error: Could not find the placeholder "${placeholder}" in silo/wrangler.jsonc.`);
|
|
71
|
-
console.error("Has database_id already been set?");
|
|
72
|
-
process.exit(1);
|
|
73
|
-
}
|
|
74
|
-
writeFileSync(wranglerPath, source.replace(placeholder, uuid), "utf8");
|
|
75
|
-
console.log(`Patched silo/wrangler.jsonc with database_id = "${uuid}"`);
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Strips single-line // comments and extracts database_name from wrangler.jsonc.
|
|
79
|
-
* Uses a regex rather than a full JSONC parser to avoid a dependency.
|
|
80
|
-
*/
|
|
81
|
-
function extractDatabaseName(source) {
|
|
82
|
-
const stripped = source.replace(/\/\/[^\n]*/g, "");
|
|
83
|
-
try {
|
|
84
|
-
const db = JSON.parse(stripped)?.d1_databases?.[0];
|
|
85
|
-
return typeof db?.database_name === "string" ? db.database_name : null;
|
|
86
|
-
} catch {
|
|
87
|
-
return source.match(/"database_name"\s*:\s*"([^"]+)"/)?.[1] ?? null;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
//#endregion
|
|
92
|
-
export { runD1 };
|