silosdk 0.0.0 → 0.0.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 +3 -1
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/cli/d1.cjs +93 -0
- package/dist/cli/d1.mjs +92 -0
- package/dist/cli/index.cjs +93 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +94 -0
- package/dist/cli/init.cjs +134 -0
- package/dist/cli/init.mjs +133 -0
- package/dist/cli/kv.cjs +63 -0
- package/dist/cli/kv.mjs +60 -0
- package/dist/cli/r2.cjs +83 -0
- package/dist/cli/r2.mjs +82 -0
- package/dist/cli/wrangler.cjs +93 -0
- package/dist/cli/wrangler.mjs +89 -0
- package/dist/local/adapters/cloudflare.cjs +200 -0
- package/dist/local/adapters/cloudflare.d.cts +50 -0
- package/dist/local/adapters/cloudflare.d.mts +50 -0
- package/dist/local/adapters/cloudflare.mjs +200 -0
- package/dist/local/auth-context.cjs +14 -0
- package/dist/local/auth-context.d.cts +7 -0
- package/dist/local/auth-context.d.mts +7 -0
- package/dist/local/auth-context.mjs +12 -0
- package/dist/local/auth.cjs +109 -0
- package/dist/local/auth.d.cts +26 -0
- package/dist/local/auth.d.mts +26 -0
- package/dist/local/auth.mjs +99 -0
- package/dist/local/commit.cjs +350 -0
- package/dist/local/commit.d.cts +59 -0
- package/dist/local/commit.d.mts +59 -0
- package/dist/local/commit.mjs +349 -0
- package/dist/local/config.cjs +17 -0
- package/dist/local/config.mjs +15 -0
- package/dist/local/index.cjs +16 -0
- package/dist/local/index.d.cts +10 -0
- package/dist/local/index.d.mts +10 -0
- package/dist/local/index.mjs +9 -0
- package/dist/local/provider.cjs +204 -0
- package/dist/local/provider.d.cts +25 -0
- package/dist/local/provider.d.mts +25 -0
- package/dist/local/provider.mjs +203 -0
- package/dist/local/query-store.cjs +276 -0
- package/dist/local/query-store.mjs +274 -0
- package/dist/local/storage.cjs +71 -0
- package/dist/local/storage.d.cts +7 -0
- package/dist/local/storage.d.mts +7 -0
- package/dist/local/storage.mjs +68 -0
- package/dist/local/sync.cjs +124 -0
- package/dist/local/sync.d.cts +36 -0
- package/dist/local/sync.d.mts +36 -0
- package/dist/local/sync.mjs +122 -0
- package/dist/local/view.cjs +257 -0
- package/dist/local/view.d.cts +24 -0
- package/dist/local/view.d.mts +24 -0
- package/dist/local/view.mjs +254 -0
- package/dist/package.cjs +11 -0
- package/dist/package.mjs +5 -0
- package/dist/schema/index.cjs +276 -0
- package/dist/schema/index.d.cts +207 -0
- package/dist/schema/index.d.mts +207 -0
- package/dist/schema/index.mjs +265 -0
- package/dist/server/auth.cjs +132 -0
- package/dist/server/auth.d.cts +49 -0
- package/dist/server/auth.d.mts +49 -0
- package/dist/server/auth.mjs +122 -0
- package/dist/server/d1.cjs +120 -0
- package/dist/server/d1.mjs +116 -0
- package/dist/server/do.cjs +132 -0
- package/dist/server/do.d.cts +21 -0
- package/dist/server/do.d.mts +21 -0
- package/dist/server/do.mjs +131 -0
- package/dist/server/index.cjs +355 -0
- package/dist/server/index.d.cts +65 -0
- package/dist/server/index.d.mts +65 -0
- package/dist/server/index.mjs +348 -0
- package/dist/server/protect.cjs +34 -0
- package/dist/server/protect.d.cts +32 -0
- package/dist/server/protect.d.mts +32 -0
- package/dist/server/protect.mjs +33 -0
- package/dist/server/r2.cjs +58 -0
- package/dist/server/r2.d.cts +4 -0
- package/dist/server/r2.d.mts +4 -0
- package/dist/server/r2.mjs +53 -0
- package/package.json +55 -2
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
const require_package = require('../package.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/schema/index.ts
|
|
4
|
+
function issue(message) {
|
|
5
|
+
return { message };
|
|
6
|
+
}
|
|
7
|
+
function validateString(input, options) {
|
|
8
|
+
const issues = [];
|
|
9
|
+
if (typeof input !== "string") {
|
|
10
|
+
issues.push({ message: "Expected string" });
|
|
11
|
+
return issues;
|
|
12
|
+
}
|
|
13
|
+
if (!options) return issues;
|
|
14
|
+
const { minLength, maxLength } = options;
|
|
15
|
+
if (typeof minLength === "number" && input.length < minLength) issues.push(issue(`Must be at least ${minLength} characters`));
|
|
16
|
+
if (typeof maxLength === "number" && input.length > maxLength) issues.push(issue(`Must be at most ${maxLength} characters`));
|
|
17
|
+
return issues;
|
|
18
|
+
}
|
|
19
|
+
function validateNumber(input, options) {
|
|
20
|
+
const issues = [];
|
|
21
|
+
if (typeof input !== "number") {
|
|
22
|
+
issues.push({ message: "Expected number" });
|
|
23
|
+
return issues;
|
|
24
|
+
}
|
|
25
|
+
if (!options) return issues;
|
|
26
|
+
const { min, max, integer } = options;
|
|
27
|
+
if (typeof min === "number" && input < min) issues.push(issue(`Must be at least ${min}`));
|
|
28
|
+
if (typeof max === "number" && input > max) issues.push(issue(`Must be at most ${max}`));
|
|
29
|
+
if (integer && !Number.isInteger(input)) issues.push(issue("Must be an integer"));
|
|
30
|
+
return issues;
|
|
31
|
+
}
|
|
32
|
+
function validateAsset(input) {
|
|
33
|
+
const issues = [];
|
|
34
|
+
if (typeof input !== "object" || input === null) {
|
|
35
|
+
issues.push({ message: "Expected asset object" });
|
|
36
|
+
return issues;
|
|
37
|
+
}
|
|
38
|
+
const hasKey = typeof input.key === "string" && input.key.length > 0;
|
|
39
|
+
const hasUri = typeof input.uri === "string" && input.uri.length > 0;
|
|
40
|
+
if (!hasKey && !hasUri) issues.push({ message: "Expected either non-empty key or uri" });
|
|
41
|
+
if (input.name !== void 0 && typeof input.name !== "string") issues.push({ message: "name must be a string" });
|
|
42
|
+
if (input.contentType !== void 0 && typeof input.contentType !== "string") issues.push({ message: "contentType must be a string" });
|
|
43
|
+
if (input.size !== void 0 && typeof input.size !== "number") issues.push({ message: "size must be a number" });
|
|
44
|
+
return issues;
|
|
45
|
+
}
|
|
46
|
+
function fieldSchema(type, options) {
|
|
47
|
+
function parse(value) {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
function validate(input) {
|
|
51
|
+
const issues = [];
|
|
52
|
+
if (Array.isArray(type)) {
|
|
53
|
+
if (!type.includes(input)) {
|
|
54
|
+
issues.push({ message: `Expected one of: ${type.join(", ")}` });
|
|
55
|
+
return { issues };
|
|
56
|
+
}
|
|
57
|
+
return { value: parse(input) };
|
|
58
|
+
}
|
|
59
|
+
const def = options?.default;
|
|
60
|
+
if ((input === void 0 || input === null) && def !== void 0) input = typeof def === "function" ? def() : def;
|
|
61
|
+
switch (type) {
|
|
62
|
+
case "string":
|
|
63
|
+
issues.push(...validateString(input, options));
|
|
64
|
+
break;
|
|
65
|
+
case "number":
|
|
66
|
+
issues.push(...validateNumber(input, options));
|
|
67
|
+
break;
|
|
68
|
+
case "boolean":
|
|
69
|
+
if (typeof input !== "boolean") issues.push({ message: "Expected boolean" });
|
|
70
|
+
break;
|
|
71
|
+
case "asset":
|
|
72
|
+
issues.push(...validateAsset(input));
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
if (issues.length > 0) return { issues };
|
|
76
|
+
else return { value: parse(input) };
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
type,
|
|
80
|
+
options,
|
|
81
|
+
hasDefault: Boolean(options && options.default !== void 0),
|
|
82
|
+
validate,
|
|
83
|
+
"~standard": {
|
|
84
|
+
version: 1,
|
|
85
|
+
vendor: require_package.name,
|
|
86
|
+
validate
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function string(options) {
|
|
91
|
+
return fieldSchema("string", options);
|
|
92
|
+
}
|
|
93
|
+
function number(options) {
|
|
94
|
+
return fieldSchema("number", options);
|
|
95
|
+
}
|
|
96
|
+
function boolean(options) {
|
|
97
|
+
return fieldSchema("boolean", options);
|
|
98
|
+
}
|
|
99
|
+
function asset(options) {
|
|
100
|
+
return fieldSchema("asset", options);
|
|
101
|
+
}
|
|
102
|
+
function literal(values, options) {
|
|
103
|
+
return fieldSchema(values, options);
|
|
104
|
+
}
|
|
105
|
+
function viewSchema(type) {
|
|
106
|
+
function parse(value) {
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
function validate(input) {
|
|
110
|
+
const issues = [];
|
|
111
|
+
if (typeof input !== "object" || input === null) {
|
|
112
|
+
issues.push({ message: "Expected object" });
|
|
113
|
+
return { issues };
|
|
114
|
+
}
|
|
115
|
+
const output = {};
|
|
116
|
+
Object.entries(type).forEach(([key, value]) => {
|
|
117
|
+
if (key in input) {
|
|
118
|
+
const res = value.validate(input[key]);
|
|
119
|
+
if ("issues" in res) res.issues?.forEach((issue$1) => {
|
|
120
|
+
issues.push({ message: `${key}: ${issue$1.message}` });
|
|
121
|
+
});
|
|
122
|
+
else output[key] = res.value;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (value.hasDefault) {
|
|
126
|
+
const res = value.validate(void 0);
|
|
127
|
+
if ("issues" in res) res.issues?.forEach((issue$1) => {
|
|
128
|
+
issues.push({ message: `${key}: ${issue$1.message}` });
|
|
129
|
+
});
|
|
130
|
+
else output[key] = res.value;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
return issues.length > 0 ? { issues } : { value: parse(output) };
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
type,
|
|
137
|
+
validate,
|
|
138
|
+
"~standard": {
|
|
139
|
+
version: 1,
|
|
140
|
+
vendor: require_package.name,
|
|
141
|
+
validate
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function view(name$1, type) {
|
|
146
|
+
const schema = viewSchema(type);
|
|
147
|
+
const init = () => {
|
|
148
|
+
const value = {};
|
|
149
|
+
Object.keys(schema.type).forEach((key) => {
|
|
150
|
+
const field = schema.type[key];
|
|
151
|
+
if (field.options?.default !== void 0) {
|
|
152
|
+
const def = field.options.default;
|
|
153
|
+
value[key] = typeof def === "function" ? def() : def;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
return value;
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
name: name$1,
|
|
160
|
+
schema,
|
|
161
|
+
validate: schema["~standard"].validate,
|
|
162
|
+
init
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function jex(field) {
|
|
166
|
+
return `json_extract(data, '$.${field}')`;
|
|
167
|
+
}
|
|
168
|
+
function stableStringify(obj) {
|
|
169
|
+
if (obj === null || obj === void 0) return "";
|
|
170
|
+
if (typeof obj !== "object") return String(obj);
|
|
171
|
+
if (Array.isArray(obj)) return obj.map(stableStringify).join(",");
|
|
172
|
+
return Object.keys(obj).sort().map((k) => `${k}:${stableStringify(obj[k])}`).join("|");
|
|
173
|
+
}
|
|
174
|
+
function compileWhere(where) {
|
|
175
|
+
const params = [];
|
|
176
|
+
const compileObject = (obj) => {
|
|
177
|
+
const parts = [];
|
|
178
|
+
for (const field of Object.keys(obj)) {
|
|
179
|
+
const value = obj[field];
|
|
180
|
+
if (value && Array.isArray(value)) {
|
|
181
|
+
const placeholders = value.map(() => "?").join(", ");
|
|
182
|
+
parts.push(`${jex(field)} IN (${placeholders})`);
|
|
183
|
+
params.push(...value);
|
|
184
|
+
} else if (value && typeof value === "object" && !Array.isArray(value)) for (const op of Object.keys(value)) {
|
|
185
|
+
parts.push(`${jex(field)} ${op} ?`);
|
|
186
|
+
params.push(value[op]);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
parts.push(`${jex(field)} = ?`);
|
|
190
|
+
params.push(value);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return parts.join(" AND ");
|
|
194
|
+
};
|
|
195
|
+
if (Array.isArray(where)) return {
|
|
196
|
+
clause: where.map((option) => `(${compileObject(option)})`).join(" OR "),
|
|
197
|
+
params
|
|
198
|
+
};
|
|
199
|
+
else return {
|
|
200
|
+
clause: compileObject(where),
|
|
201
|
+
params
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
async function getRows(args) {
|
|
205
|
+
const { name: name$1, options, db } = args;
|
|
206
|
+
let statement = `SELECT * FROM views WHERE view = ?`;
|
|
207
|
+
const params = [name$1];
|
|
208
|
+
const ids = [];
|
|
209
|
+
if (options && "parent" in options && options.parent) (await db.getAllAsync("SELECT * FROM relations WHERE parent = ? AND pid = ? AND child = ?", [
|
|
210
|
+
options?.parent?.[0]?.name,
|
|
211
|
+
options.parent[1],
|
|
212
|
+
name$1
|
|
213
|
+
])).forEach((relation) => {
|
|
214
|
+
ids.push(relation.cid);
|
|
215
|
+
});
|
|
216
|
+
if (options && "child" in options && options.child) (await db.getAllAsync("SELECT * FROM relations WHERE child = ? AND cid = ? AND parent = ?", [
|
|
217
|
+
options.child[0].name,
|
|
218
|
+
options.child[1],
|
|
219
|
+
name$1
|
|
220
|
+
])).forEach((relation) => {
|
|
221
|
+
ids.push(relation.pid);
|
|
222
|
+
});
|
|
223
|
+
if (ids.length > 0) {
|
|
224
|
+
statement += ` AND id IN (${ids.map(() => "?").join(",")})`;
|
|
225
|
+
params.push(...ids);
|
|
226
|
+
}
|
|
227
|
+
if (options.where) {
|
|
228
|
+
const { clause, params: whereParams } = compileWhere(options.where);
|
|
229
|
+
statement += ` AND (${clause})`;
|
|
230
|
+
params.push(...whereParams);
|
|
231
|
+
}
|
|
232
|
+
if (options.order) {
|
|
233
|
+
const orderClauses = [];
|
|
234
|
+
for (const [field, direction] of Object.entries(options.order)) orderClauses.push(`${jex(field)} ${direction?.toUpperCase() ?? "ASC"}`);
|
|
235
|
+
if (orderClauses.length > 0) statement += ` ORDER BY ${orderClauses.join(", ")}`;
|
|
236
|
+
}
|
|
237
|
+
if (options.take) {
|
|
238
|
+
statement += ` LIMIT ?`;
|
|
239
|
+
params.push(options.take);
|
|
240
|
+
if (args.offset) {
|
|
241
|
+
statement += ` OFFSET ?`;
|
|
242
|
+
params.push(args.offset);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return await db.getAllAsync(statement, params).then((rows) => rows.map((row) => ({
|
|
246
|
+
...row,
|
|
247
|
+
data: JSON.parse(row.data)
|
|
248
|
+
}))).catch((err) => {
|
|
249
|
+
console.error("DB Query Error:", err);
|
|
250
|
+
}) || [];
|
|
251
|
+
}
|
|
252
|
+
async function getRow(args) {
|
|
253
|
+
const { name: name$1, options, db } = args;
|
|
254
|
+
const statement = `SELECT * FROM views WHERE view = ? AND id = ?`;
|
|
255
|
+
const params = [name$1, options.id];
|
|
256
|
+
return await db.getFirstAsync(statement, params).then((row) => row ? {
|
|
257
|
+
...row,
|
|
258
|
+
data: JSON.parse(row.data)
|
|
259
|
+
} : null).catch((err) => {
|
|
260
|
+
console.error("DB Query Error:", err);
|
|
261
|
+
}) ?? null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
//#endregion
|
|
265
|
+
exports.asset = asset;
|
|
266
|
+
exports.boolean = boolean;
|
|
267
|
+
exports.fieldSchema = fieldSchema;
|
|
268
|
+
exports.getRow = getRow;
|
|
269
|
+
exports.getRows = getRows;
|
|
270
|
+
exports.jex = jex;
|
|
271
|
+
exports.literal = literal;
|
|
272
|
+
exports.number = number;
|
|
273
|
+
exports.stableStringify = stableStringify;
|
|
274
|
+
exports.string = string;
|
|
275
|
+
exports.view = view;
|
|
276
|
+
exports.viewSchema = viewSchema;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { SQLiteDatabase } from "expo-sqlite";
|
|
3
|
+
|
|
4
|
+
//#region src/schema/index.d.ts
|
|
5
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
6
|
+
type BaseViewResult = {
|
|
7
|
+
id: string;
|
|
8
|
+
view: string;
|
|
9
|
+
createdAt: string;
|
|
10
|
+
updatedAt: string;
|
|
11
|
+
};
|
|
12
|
+
interface ViewResult extends BaseViewResult {
|
|
13
|
+
data: string;
|
|
14
|
+
}
|
|
15
|
+
type ParsedViewResult<T> = BaseViewResult & {
|
|
16
|
+
data: T;
|
|
17
|
+
};
|
|
18
|
+
interface Relation {
|
|
19
|
+
parent: string;
|
|
20
|
+
pid: string;
|
|
21
|
+
child: string;
|
|
22
|
+
cid: string;
|
|
23
|
+
}
|
|
24
|
+
type AssetRef = {
|
|
25
|
+
key: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
contentType?: string;
|
|
28
|
+
size?: number;
|
|
29
|
+
};
|
|
30
|
+
type PendingAsset = {
|
|
31
|
+
uri: string;
|
|
32
|
+
name?: string;
|
|
33
|
+
contentType?: string;
|
|
34
|
+
size?: number;
|
|
35
|
+
};
|
|
36
|
+
type AssetValue = AssetRef | PendingAsset;
|
|
37
|
+
type Field = readonly string[] | readonly number[] | 'string' | 'number' | 'boolean' | 'asset';
|
|
38
|
+
type FieldDefinition<T extends Field> = T extends readonly string[] | readonly number[] ? T[number] : T extends 'string' ? string : T extends 'number' ? number : T extends 'boolean' ? boolean : T extends 'asset' ? AssetValue : never;
|
|
39
|
+
type FieldSchema<T extends Field, HasDefault extends boolean = boolean, Input = FieldDefinition<T>> = StandardSchemaV1<Input, FieldDefinition<T>> & {
|
|
40
|
+
type: T;
|
|
41
|
+
options?: T extends readonly string[] | readonly number[] ? {
|
|
42
|
+
default: T[number];
|
|
43
|
+
} : T extends 'string' ? StringOptions : T extends 'number' ? NumberOptions : T extends 'boolean' ? BooleanOptions : T extends 'asset' ? AssetOptions : never;
|
|
44
|
+
hasDefault: HasDefault;
|
|
45
|
+
validate: StandardSchemaV1<Input, FieldDefinition<T>>['~standard']['validate'];
|
|
46
|
+
};
|
|
47
|
+
type RequiredKeys<T extends Record<string, FieldSchema<Field, boolean, any>>> = { [K in keyof T]: T[K] extends FieldSchema<Field, true, any> ? K : never }[keyof T];
|
|
48
|
+
type OptionalKeys<T extends Record<string, FieldSchema<Field, boolean, any>>> = Exclude<keyof T, RequiredKeys<T>>;
|
|
49
|
+
type TypeDefinition<T extends Record<string, FieldSchema<Field, boolean, any>>> = { [K in RequiredKeys<T>]: Prettify<FieldDefinition<T[K]['type']>> } & { [K in OptionalKeys<T>]?: Prettify<FieldDefinition<T[K]['type']>> };
|
|
50
|
+
type ViewSchema<T extends Record<string, FieldSchema<Field, boolean, any>>, Input = TypeDefinition<T>> = StandardSchemaV1<Input, TypeDefinition<T>> & {
|
|
51
|
+
type: T;
|
|
52
|
+
validate: StandardSchemaV1<Input, TypeDefinition<T>>['~standard']['validate'];
|
|
53
|
+
};
|
|
54
|
+
type Issue = {
|
|
55
|
+
message: string;
|
|
56
|
+
};
|
|
57
|
+
type StringOptions = {
|
|
58
|
+
default: string | (() => string);
|
|
59
|
+
minLength?: number;
|
|
60
|
+
maxLength?: number;
|
|
61
|
+
};
|
|
62
|
+
type NumberOptions = {
|
|
63
|
+
default: number | (() => number);
|
|
64
|
+
min?: number;
|
|
65
|
+
max?: number;
|
|
66
|
+
integer?: boolean;
|
|
67
|
+
};
|
|
68
|
+
type BooleanOptions = {
|
|
69
|
+
default: boolean | (() => boolean);
|
|
70
|
+
};
|
|
71
|
+
type LiteralOptions<T extends readonly string[] | readonly number[]> = {
|
|
72
|
+
default: T[number];
|
|
73
|
+
};
|
|
74
|
+
type AssetOptions = {
|
|
75
|
+
default: AssetValue | (() => AssetValue);
|
|
76
|
+
};
|
|
77
|
+
declare function fieldSchema<const T extends Field, O extends (T extends readonly string[] | readonly number[] ? LiteralOptions<T> : T extends 'string' ? StringOptions : T extends 'number' ? NumberOptions : T extends 'boolean' ? BooleanOptions : T extends 'asset' ? AssetOptions : never)>(type: T, options?: O): FieldSchema<T, O extends {
|
|
78
|
+
default: any;
|
|
79
|
+
} ? true : false>;
|
|
80
|
+
declare function string(): FieldSchema<'string', false, string>;
|
|
81
|
+
declare function string(options: StringOptions): FieldSchema<'string', true, string>;
|
|
82
|
+
declare function number(): FieldSchema<'number', false, number>;
|
|
83
|
+
declare function number(options: NumberOptions): FieldSchema<'number', true, number>;
|
|
84
|
+
declare function boolean(): FieldSchema<'boolean', false, boolean>;
|
|
85
|
+
declare function boolean(options: BooleanOptions): FieldSchema<'boolean', true, boolean>;
|
|
86
|
+
declare function asset(): FieldSchema<'asset', false, AssetValue>;
|
|
87
|
+
declare function asset(options: AssetOptions): FieldSchema<'asset', true, AssetValue>;
|
|
88
|
+
declare function literal<const T extends Field & (readonly string[] | readonly number[])>(values: T): FieldSchema<T, false, T[number]>;
|
|
89
|
+
declare function literal<const T extends Field & (readonly string[] | readonly number[])>(values: T, options: LiteralOptions<T>): FieldSchema<T, true, T[number]>;
|
|
90
|
+
interface TypeSchema extends Record<string, FieldSchema<Field, boolean, any>> {}
|
|
91
|
+
declare function viewSchema<Type extends TypeSchema>(type: Type): {
|
|
92
|
+
type: Type;
|
|
93
|
+
validate: (input: unknown) => {
|
|
94
|
+
issues: Issue[];
|
|
95
|
+
value?: undefined;
|
|
96
|
+
} | {
|
|
97
|
+
value: TypeDefinition<Type>;
|
|
98
|
+
issues?: undefined;
|
|
99
|
+
};
|
|
100
|
+
'~standard': {
|
|
101
|
+
version: 1;
|
|
102
|
+
vendor: string;
|
|
103
|
+
validate: (input: unknown) => {
|
|
104
|
+
issues: Issue[];
|
|
105
|
+
value?: undefined;
|
|
106
|
+
} | {
|
|
107
|
+
value: TypeDefinition<Type>;
|
|
108
|
+
issues?: undefined;
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
type ReadRuleContext = {
|
|
113
|
+
user: {
|
|
114
|
+
id: string;
|
|
115
|
+
email: string | null;
|
|
116
|
+
role: string;
|
|
117
|
+
isAnonymous: boolean;
|
|
118
|
+
};
|
|
119
|
+
env: unknown;
|
|
120
|
+
query: Record<string, unknown>;
|
|
121
|
+
};
|
|
122
|
+
type WriteRuleContext = {
|
|
123
|
+
user: {
|
|
124
|
+
id: string;
|
|
125
|
+
email: string | null;
|
|
126
|
+
role: string;
|
|
127
|
+
isAnonymous: boolean;
|
|
128
|
+
};
|
|
129
|
+
env: unknown;
|
|
130
|
+
op: {
|
|
131
|
+
kind: string;
|
|
132
|
+
id?: string;
|
|
133
|
+
value?: unknown;
|
|
134
|
+
[k: string]: unknown;
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
type ProtectedView<Name extends string = string> = {
|
|
138
|
+
/** View name — same as the underlying View.name */
|
|
139
|
+
name: Name;
|
|
140
|
+
/** Marker so createServer can distinguish ProtectedView from View */
|
|
141
|
+
_protected: true;
|
|
142
|
+
_rules: {
|
|
143
|
+
read?: (ctx: ReadRuleContext) => boolean | Promise<boolean>;
|
|
144
|
+
write?: (ctx: WriteRuleContext) => boolean | Promise<boolean>;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
type View<Name extends string = string, Type extends TypeSchema = TypeSchema> = {
|
|
148
|
+
name: Name;
|
|
149
|
+
schema: ViewSchema<Type>;
|
|
150
|
+
validate: ViewSchema<Type>['~standard']['validate'];
|
|
151
|
+
init: () => TypeDefinition<Type>;
|
|
152
|
+
};
|
|
153
|
+
type Infer<V extends View> = TypeDefinition<V['schema']['type']>;
|
|
154
|
+
declare function view<Name extends string, Type extends TypeSchema>(name: Name, type: Type): View<Name, Type>;
|
|
155
|
+
declare function jex(field: string): string;
|
|
156
|
+
declare function stableStringify(obj: any): string;
|
|
157
|
+
type Scope = 'private' | 'public';
|
|
158
|
+
interface ViewOptions {
|
|
159
|
+
id: string;
|
|
160
|
+
scope?: Scope;
|
|
161
|
+
}
|
|
162
|
+
type OrderOption<T extends TypeSchema> = { [K in keyof T]?: 'asc' | 'desc' };
|
|
163
|
+
type Operator<T> = T extends number ? '>' | '<' : T extends string ? 'like' : T extends boolean ? '=' : never;
|
|
164
|
+
type WhereOption<T extends TypeSchema> = { [K in keyof T]?: FieldDefinition<T[K]['type']> | FieldDefinition<T[K]['type']>[] | { [Op in Operator<T[K]['type']>]?: FieldDefinition<T[K]['type']> } };
|
|
165
|
+
interface ViewsOptions<T extends TypeSchema> {
|
|
166
|
+
where?: WhereOption<T> | WhereOption<T>[];
|
|
167
|
+
take?: number;
|
|
168
|
+
order?: OrderOption<T>;
|
|
169
|
+
scope?: Scope;
|
|
170
|
+
}
|
|
171
|
+
interface ViewsOptionsWithChild<T extends TypeSchema> extends ViewsOptions<T> {
|
|
172
|
+
child?: [view: {
|
|
173
|
+
name: string;
|
|
174
|
+
}, id: string];
|
|
175
|
+
}
|
|
176
|
+
interface ViewsOptionsWithParent<T extends TypeSchema> extends ViewsOptions<T> {
|
|
177
|
+
parent?: [view: {
|
|
178
|
+
name: string;
|
|
179
|
+
}, id: string];
|
|
180
|
+
}
|
|
181
|
+
type AnyViewsOptions<T extends TypeSchema> = ViewsOptions<T> | ViewsOptionsWithParent<T> | ViewsOptionsWithChild<T>;
|
|
182
|
+
declare function getRows(args: {
|
|
183
|
+
name: string;
|
|
184
|
+
options: AnyViewsOptions<any>;
|
|
185
|
+
db: SQLiteDatabase;
|
|
186
|
+
offset?: number;
|
|
187
|
+
count?: number;
|
|
188
|
+
}): Promise<{
|
|
189
|
+
data: any;
|
|
190
|
+
id: string;
|
|
191
|
+
view: string;
|
|
192
|
+
createdAt: string;
|
|
193
|
+
updatedAt: string;
|
|
194
|
+
}[]>;
|
|
195
|
+
declare function getRow(args: {
|
|
196
|
+
name: string;
|
|
197
|
+
options: ViewOptions;
|
|
198
|
+
db: SQLiteDatabase;
|
|
199
|
+
}): Promise<{
|
|
200
|
+
data: any;
|
|
201
|
+
id: string;
|
|
202
|
+
view: string;
|
|
203
|
+
createdAt: string;
|
|
204
|
+
updatedAt: string;
|
|
205
|
+
} | null>;
|
|
206
|
+
//#endregion
|
|
207
|
+
export { AnyViewsOptions, AssetRef, AssetValue, BaseViewResult, Field, FieldDefinition, FieldSchema, Infer, OrderOption, ParsedViewResult, PendingAsset, Prettify, ProtectedView, ReadRuleContext, Relation, Scope, TypeDefinition, TypeSchema, View, ViewOptions, ViewResult, ViewSchema, ViewsOptions, ViewsOptionsWithChild, ViewsOptionsWithParent, WhereOption, WriteRuleContext, asset, boolean, fieldSchema, getRow, getRows, jex, literal, number, stableStringify, string, view, viewSchema };
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { SQLiteDatabase } from "expo-sqlite";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/schema/index.d.ts
|
|
5
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
6
|
+
type BaseViewResult = {
|
|
7
|
+
id: string;
|
|
8
|
+
view: string;
|
|
9
|
+
createdAt: string;
|
|
10
|
+
updatedAt: string;
|
|
11
|
+
};
|
|
12
|
+
interface ViewResult extends BaseViewResult {
|
|
13
|
+
data: string;
|
|
14
|
+
}
|
|
15
|
+
type ParsedViewResult<T> = BaseViewResult & {
|
|
16
|
+
data: T;
|
|
17
|
+
};
|
|
18
|
+
interface Relation {
|
|
19
|
+
parent: string;
|
|
20
|
+
pid: string;
|
|
21
|
+
child: string;
|
|
22
|
+
cid: string;
|
|
23
|
+
}
|
|
24
|
+
type AssetRef = {
|
|
25
|
+
key: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
contentType?: string;
|
|
28
|
+
size?: number;
|
|
29
|
+
};
|
|
30
|
+
type PendingAsset = {
|
|
31
|
+
uri: string;
|
|
32
|
+
name?: string;
|
|
33
|
+
contentType?: string;
|
|
34
|
+
size?: number;
|
|
35
|
+
};
|
|
36
|
+
type AssetValue = AssetRef | PendingAsset;
|
|
37
|
+
type Field = readonly string[] | readonly number[] | 'string' | 'number' | 'boolean' | 'asset';
|
|
38
|
+
type FieldDefinition<T extends Field> = T extends readonly string[] | readonly number[] ? T[number] : T extends 'string' ? string : T extends 'number' ? number : T extends 'boolean' ? boolean : T extends 'asset' ? AssetValue : never;
|
|
39
|
+
type FieldSchema<T extends Field, HasDefault extends boolean = boolean, Input = FieldDefinition<T>> = StandardSchemaV1<Input, FieldDefinition<T>> & {
|
|
40
|
+
type: T;
|
|
41
|
+
options?: T extends readonly string[] | readonly number[] ? {
|
|
42
|
+
default: T[number];
|
|
43
|
+
} : T extends 'string' ? StringOptions : T extends 'number' ? NumberOptions : T extends 'boolean' ? BooleanOptions : T extends 'asset' ? AssetOptions : never;
|
|
44
|
+
hasDefault: HasDefault;
|
|
45
|
+
validate: StandardSchemaV1<Input, FieldDefinition<T>>['~standard']['validate'];
|
|
46
|
+
};
|
|
47
|
+
type RequiredKeys<T extends Record<string, FieldSchema<Field, boolean, any>>> = { [K in keyof T]: T[K] extends FieldSchema<Field, true, any> ? K : never }[keyof T];
|
|
48
|
+
type OptionalKeys<T extends Record<string, FieldSchema<Field, boolean, any>>> = Exclude<keyof T, RequiredKeys<T>>;
|
|
49
|
+
type TypeDefinition<T extends Record<string, FieldSchema<Field, boolean, any>>> = { [K in RequiredKeys<T>]: Prettify<FieldDefinition<T[K]['type']>> } & { [K in OptionalKeys<T>]?: Prettify<FieldDefinition<T[K]['type']>> };
|
|
50
|
+
type ViewSchema<T extends Record<string, FieldSchema<Field, boolean, any>>, Input = TypeDefinition<T>> = StandardSchemaV1<Input, TypeDefinition<T>> & {
|
|
51
|
+
type: T;
|
|
52
|
+
validate: StandardSchemaV1<Input, TypeDefinition<T>>['~standard']['validate'];
|
|
53
|
+
};
|
|
54
|
+
type Issue = {
|
|
55
|
+
message: string;
|
|
56
|
+
};
|
|
57
|
+
type StringOptions = {
|
|
58
|
+
default: string | (() => string);
|
|
59
|
+
minLength?: number;
|
|
60
|
+
maxLength?: number;
|
|
61
|
+
};
|
|
62
|
+
type NumberOptions = {
|
|
63
|
+
default: number | (() => number);
|
|
64
|
+
min?: number;
|
|
65
|
+
max?: number;
|
|
66
|
+
integer?: boolean;
|
|
67
|
+
};
|
|
68
|
+
type BooleanOptions = {
|
|
69
|
+
default: boolean | (() => boolean);
|
|
70
|
+
};
|
|
71
|
+
type LiteralOptions<T extends readonly string[] | readonly number[]> = {
|
|
72
|
+
default: T[number];
|
|
73
|
+
};
|
|
74
|
+
type AssetOptions = {
|
|
75
|
+
default: AssetValue | (() => AssetValue);
|
|
76
|
+
};
|
|
77
|
+
declare function fieldSchema<const T extends Field, O extends (T extends readonly string[] | readonly number[] ? LiteralOptions<T> : T extends 'string' ? StringOptions : T extends 'number' ? NumberOptions : T extends 'boolean' ? BooleanOptions : T extends 'asset' ? AssetOptions : never)>(type: T, options?: O): FieldSchema<T, O extends {
|
|
78
|
+
default: any;
|
|
79
|
+
} ? true : false>;
|
|
80
|
+
declare function string(): FieldSchema<'string', false, string>;
|
|
81
|
+
declare function string(options: StringOptions): FieldSchema<'string', true, string>;
|
|
82
|
+
declare function number(): FieldSchema<'number', false, number>;
|
|
83
|
+
declare function number(options: NumberOptions): FieldSchema<'number', true, number>;
|
|
84
|
+
declare function boolean(): FieldSchema<'boolean', false, boolean>;
|
|
85
|
+
declare function boolean(options: BooleanOptions): FieldSchema<'boolean', true, boolean>;
|
|
86
|
+
declare function asset(): FieldSchema<'asset', false, AssetValue>;
|
|
87
|
+
declare function asset(options: AssetOptions): FieldSchema<'asset', true, AssetValue>;
|
|
88
|
+
declare function literal<const T extends Field & (readonly string[] | readonly number[])>(values: T): FieldSchema<T, false, T[number]>;
|
|
89
|
+
declare function literal<const T extends Field & (readonly string[] | readonly number[])>(values: T, options: LiteralOptions<T>): FieldSchema<T, true, T[number]>;
|
|
90
|
+
interface TypeSchema extends Record<string, FieldSchema<Field, boolean, any>> {}
|
|
91
|
+
declare function viewSchema<Type extends TypeSchema>(type: Type): {
|
|
92
|
+
type: Type;
|
|
93
|
+
validate: (input: unknown) => {
|
|
94
|
+
issues: Issue[];
|
|
95
|
+
value?: undefined;
|
|
96
|
+
} | {
|
|
97
|
+
value: TypeDefinition<Type>;
|
|
98
|
+
issues?: undefined;
|
|
99
|
+
};
|
|
100
|
+
'~standard': {
|
|
101
|
+
version: 1;
|
|
102
|
+
vendor: string;
|
|
103
|
+
validate: (input: unknown) => {
|
|
104
|
+
issues: Issue[];
|
|
105
|
+
value?: undefined;
|
|
106
|
+
} | {
|
|
107
|
+
value: TypeDefinition<Type>;
|
|
108
|
+
issues?: undefined;
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
type ReadRuleContext = {
|
|
113
|
+
user: {
|
|
114
|
+
id: string;
|
|
115
|
+
email: string | null;
|
|
116
|
+
role: string;
|
|
117
|
+
isAnonymous: boolean;
|
|
118
|
+
};
|
|
119
|
+
env: unknown;
|
|
120
|
+
query: Record<string, unknown>;
|
|
121
|
+
};
|
|
122
|
+
type WriteRuleContext = {
|
|
123
|
+
user: {
|
|
124
|
+
id: string;
|
|
125
|
+
email: string | null;
|
|
126
|
+
role: string;
|
|
127
|
+
isAnonymous: boolean;
|
|
128
|
+
};
|
|
129
|
+
env: unknown;
|
|
130
|
+
op: {
|
|
131
|
+
kind: string;
|
|
132
|
+
id?: string;
|
|
133
|
+
value?: unknown;
|
|
134
|
+
[k: string]: unknown;
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
type ProtectedView<Name extends string = string> = {
|
|
138
|
+
/** View name — same as the underlying View.name */
|
|
139
|
+
name: Name;
|
|
140
|
+
/** Marker so createServer can distinguish ProtectedView from View */
|
|
141
|
+
_protected: true;
|
|
142
|
+
_rules: {
|
|
143
|
+
read?: (ctx: ReadRuleContext) => boolean | Promise<boolean>;
|
|
144
|
+
write?: (ctx: WriteRuleContext) => boolean | Promise<boolean>;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
type View<Name extends string = string, Type extends TypeSchema = TypeSchema> = {
|
|
148
|
+
name: Name;
|
|
149
|
+
schema: ViewSchema<Type>;
|
|
150
|
+
validate: ViewSchema<Type>['~standard']['validate'];
|
|
151
|
+
init: () => TypeDefinition<Type>;
|
|
152
|
+
};
|
|
153
|
+
type Infer<V extends View> = TypeDefinition<V['schema']['type']>;
|
|
154
|
+
declare function view<Name extends string, Type extends TypeSchema>(name: Name, type: Type): View<Name, Type>;
|
|
155
|
+
declare function jex(field: string): string;
|
|
156
|
+
declare function stableStringify(obj: any): string;
|
|
157
|
+
type Scope = 'private' | 'public';
|
|
158
|
+
interface ViewOptions {
|
|
159
|
+
id: string;
|
|
160
|
+
scope?: Scope;
|
|
161
|
+
}
|
|
162
|
+
type OrderOption<T extends TypeSchema> = { [K in keyof T]?: 'asc' | 'desc' };
|
|
163
|
+
type Operator<T> = T extends number ? '>' | '<' : T extends string ? 'like' : T extends boolean ? '=' : never;
|
|
164
|
+
type WhereOption<T extends TypeSchema> = { [K in keyof T]?: FieldDefinition<T[K]['type']> | FieldDefinition<T[K]['type']>[] | { [Op in Operator<T[K]['type']>]?: FieldDefinition<T[K]['type']> } };
|
|
165
|
+
interface ViewsOptions<T extends TypeSchema> {
|
|
166
|
+
where?: WhereOption<T> | WhereOption<T>[];
|
|
167
|
+
take?: number;
|
|
168
|
+
order?: OrderOption<T>;
|
|
169
|
+
scope?: Scope;
|
|
170
|
+
}
|
|
171
|
+
interface ViewsOptionsWithChild<T extends TypeSchema> extends ViewsOptions<T> {
|
|
172
|
+
child?: [view: {
|
|
173
|
+
name: string;
|
|
174
|
+
}, id: string];
|
|
175
|
+
}
|
|
176
|
+
interface ViewsOptionsWithParent<T extends TypeSchema> extends ViewsOptions<T> {
|
|
177
|
+
parent?: [view: {
|
|
178
|
+
name: string;
|
|
179
|
+
}, id: string];
|
|
180
|
+
}
|
|
181
|
+
type AnyViewsOptions<T extends TypeSchema> = ViewsOptions<T> | ViewsOptionsWithParent<T> | ViewsOptionsWithChild<T>;
|
|
182
|
+
declare function getRows(args: {
|
|
183
|
+
name: string;
|
|
184
|
+
options: AnyViewsOptions<any>;
|
|
185
|
+
db: SQLiteDatabase;
|
|
186
|
+
offset?: number;
|
|
187
|
+
count?: number;
|
|
188
|
+
}): Promise<{
|
|
189
|
+
data: any;
|
|
190
|
+
id: string;
|
|
191
|
+
view: string;
|
|
192
|
+
createdAt: string;
|
|
193
|
+
updatedAt: string;
|
|
194
|
+
}[]>;
|
|
195
|
+
declare function getRow(args: {
|
|
196
|
+
name: string;
|
|
197
|
+
options: ViewOptions;
|
|
198
|
+
db: SQLiteDatabase;
|
|
199
|
+
}): Promise<{
|
|
200
|
+
data: any;
|
|
201
|
+
id: string;
|
|
202
|
+
view: string;
|
|
203
|
+
createdAt: string;
|
|
204
|
+
updatedAt: string;
|
|
205
|
+
} | null>;
|
|
206
|
+
//#endregion
|
|
207
|
+
export { AnyViewsOptions, AssetRef, AssetValue, BaseViewResult, Field, FieldDefinition, FieldSchema, Infer, OrderOption, ParsedViewResult, PendingAsset, Prettify, ProtectedView, ReadRuleContext, Relation, Scope, TypeDefinition, TypeSchema, View, ViewOptions, ViewResult, ViewSchema, ViewsOptions, ViewsOptionsWithChild, ViewsOptionsWithParent, WhereOption, WriteRuleContext, asset, boolean, fieldSchema, getRow, getRows, jex, literal, number, stableStringify, string, view, viewSchema };
|