@onyx.dev/onyx-database 0.2.10 → 0.3.0
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 +126 -2
- package/dist/gen/cli/generate.cjs +1294 -112
- package/dist/gen/cli/generate.cjs.map +1 -1
- package/dist/index.cjs +109 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +138 -1
- package/dist/index.d.ts +138 -1
- package/dist/index.js +109 -1
- package/dist/index.js.map +1 -1
- package/dist/schema/cli/schema.cjs +1698 -0
- package/dist/schema/cli/schema.cjs.map +1 -0
- package/package.json +4 -2
|
@@ -0,0 +1,1698 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var path = require('path');
|
|
5
|
+
var process = require('process');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
10
|
+
var process__default = /*#__PURE__*/_interopDefault(process);
|
|
11
|
+
|
|
12
|
+
// src/config/defaults.ts
|
|
13
|
+
var DEFAULT_BASE_URL = "https://api.onyx.dev";
|
|
14
|
+
var sanitizeBaseUrl = (u) => u.replace(/\/+$/, "");
|
|
15
|
+
|
|
16
|
+
// src/errors/config-error.ts
|
|
17
|
+
var OnyxConfigError = class extends Error {
|
|
18
|
+
name = "OnyxConfigError";
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/config/chain.ts
|
|
25
|
+
var gProcess = globalThis.process;
|
|
26
|
+
var isNode = !!gProcess?.versions?.node;
|
|
27
|
+
var dbg = (...args) => {
|
|
28
|
+
if (gProcess?.env?.ONYX_DEBUG == "true") {
|
|
29
|
+
const fmt = (v) => {
|
|
30
|
+
if (typeof v === "string") return v;
|
|
31
|
+
try {
|
|
32
|
+
return JSON.stringify(v);
|
|
33
|
+
} catch {
|
|
34
|
+
return String(v);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
gProcess.stderr?.write?.(`[onyx-config] ${args.map(fmt).join(" ")}
|
|
38
|
+
`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function dropUndefined(obj) {
|
|
42
|
+
if (!obj) return {};
|
|
43
|
+
const out = {};
|
|
44
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
45
|
+
if (v !== void 0) out[k] = v;
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
async function nodeImport(spec) {
|
|
50
|
+
return import(
|
|
51
|
+
/* @vite-ignore */
|
|
52
|
+
spec
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
function readEnv(targetId) {
|
|
56
|
+
if (!gProcess?.env) return {};
|
|
57
|
+
const env = gProcess.env;
|
|
58
|
+
const pick = (...keys) => {
|
|
59
|
+
for (const k of keys) {
|
|
60
|
+
const v = env[k];
|
|
61
|
+
if (typeof v === "string") {
|
|
62
|
+
const cleaned = v.replace(/[\r\n]+/g, "").trim();
|
|
63
|
+
if (cleaned !== "") return cleaned;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
};
|
|
68
|
+
const envId = pick("ONYX_DATABASE_ID");
|
|
69
|
+
if (targetId && envId !== targetId) return {};
|
|
70
|
+
const res = dropUndefined({
|
|
71
|
+
baseUrl: pick("ONYX_DATABASE_BASE_URL"),
|
|
72
|
+
databaseId: envId,
|
|
73
|
+
apiKey: pick("ONYX_DATABASE_API_KEY"),
|
|
74
|
+
apiSecret: pick("ONYX_DATABASE_API_SECRET")
|
|
75
|
+
});
|
|
76
|
+
if (Object.keys(res).length === 0) return {};
|
|
77
|
+
dbg("env:", mask(res));
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
async function readProjectFile(databaseId) {
|
|
81
|
+
if (!isNode) return {};
|
|
82
|
+
const fs = await nodeImport("node:fs/promises");
|
|
83
|
+
const path2 = await nodeImport("node:path");
|
|
84
|
+
const cwd = gProcess?.cwd?.() ?? ".";
|
|
85
|
+
const tryRead = async (p) => {
|
|
86
|
+
const txt = await fs.readFile(p, "utf8");
|
|
87
|
+
const sanitized = txt.replace(/[\r\n]+/g, "");
|
|
88
|
+
const json = dropUndefined(JSON.parse(sanitized));
|
|
89
|
+
dbg("project file:", p, "\u2192", mask(json));
|
|
90
|
+
return json;
|
|
91
|
+
};
|
|
92
|
+
if (databaseId) {
|
|
93
|
+
const specific = path2.resolve(cwd, `onyx-database-${databaseId}.json`);
|
|
94
|
+
try {
|
|
95
|
+
return await tryRead(specific);
|
|
96
|
+
} catch {
|
|
97
|
+
dbg("project file not found:", specific);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const fallback = path2.resolve(cwd, "onyx-database.json");
|
|
101
|
+
try {
|
|
102
|
+
return await tryRead(fallback);
|
|
103
|
+
} catch {
|
|
104
|
+
dbg("project file not found:", fallback);
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function readHomeProfile(databaseId) {
|
|
109
|
+
if (!isNode) return {};
|
|
110
|
+
const fs = await nodeImport("node:fs/promises");
|
|
111
|
+
const os = await nodeImport("node:os");
|
|
112
|
+
const path2 = await nodeImport("node:path");
|
|
113
|
+
const home = os.homedir();
|
|
114
|
+
const dir = path2.join(home, ".onyx");
|
|
115
|
+
const fileExists = async (p) => {
|
|
116
|
+
try {
|
|
117
|
+
await fs.access(p);
|
|
118
|
+
return true;
|
|
119
|
+
} catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const readProfile = async (p) => {
|
|
124
|
+
try {
|
|
125
|
+
const txt = await fs.readFile(p, "utf8");
|
|
126
|
+
const sanitized = txt.replace(/[\r\n]+/g, "");
|
|
127
|
+
const json = dropUndefined(JSON.parse(sanitized));
|
|
128
|
+
dbg("home profile used:", p, "\u2192", mask(json));
|
|
129
|
+
return json;
|
|
130
|
+
} catch (e) {
|
|
131
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
132
|
+
throw new OnyxConfigError(`Failed to read ${p}: ${msg}`);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
if (databaseId) {
|
|
136
|
+
const specific = `${dir}/onyx-database-${databaseId}.json`;
|
|
137
|
+
if (await fileExists(specific)) return readProfile(specific);
|
|
138
|
+
dbg("no specific profile:", specific);
|
|
139
|
+
}
|
|
140
|
+
const defaultInDir = `${dir}/onyx-database.json`;
|
|
141
|
+
if (await fileExists(defaultInDir)) return readProfile(defaultInDir);
|
|
142
|
+
dbg("no default profile in dir:", defaultInDir);
|
|
143
|
+
const defaultInHome = `${home}/onyx-database.json`;
|
|
144
|
+
if (await fileExists(defaultInHome)) return readProfile(defaultInHome);
|
|
145
|
+
dbg("no home-root fallback:", defaultInHome);
|
|
146
|
+
if (!await fileExists(dir)) {
|
|
147
|
+
dbg("~/.onyx does not exist:", dir);
|
|
148
|
+
return {};
|
|
149
|
+
}
|
|
150
|
+
const files = await fs.readdir(dir).catch(() => []);
|
|
151
|
+
const matches = files.filter((f) => f.startsWith("onyx-database-") && f.endsWith(".json"));
|
|
152
|
+
if (matches.length === 1) {
|
|
153
|
+
const only = `${dir}/${matches[0]}`;
|
|
154
|
+
return readProfile(only);
|
|
155
|
+
}
|
|
156
|
+
if (matches.length > 1) {
|
|
157
|
+
throw new OnyxConfigError(
|
|
158
|
+
"Multiple ~/.onyx/onyx-database-*.json profiles found. Specify databaseId via env or provide ./onyx-database.json."
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
dbg("no usable home profiles found in", dir);
|
|
162
|
+
return {};
|
|
163
|
+
}
|
|
164
|
+
async function readConfigPath(p) {
|
|
165
|
+
if (!isNode) return {};
|
|
166
|
+
const fs = await nodeImport("node:fs/promises");
|
|
167
|
+
const path2 = await nodeImport("node:path");
|
|
168
|
+
const cwd = gProcess?.cwd?.() ?? ".";
|
|
169
|
+
const resolved = path2.isAbsolute(p) ? p : path2.resolve(cwd, p);
|
|
170
|
+
try {
|
|
171
|
+
const txt = await fs.readFile(resolved, "utf8");
|
|
172
|
+
const sanitized = txt.replace(/[\r\n]+/g, "");
|
|
173
|
+
const json = dropUndefined(JSON.parse(sanitized));
|
|
174
|
+
dbg("config path:", resolved, "\u2192", mask(json));
|
|
175
|
+
return json;
|
|
176
|
+
} catch (e) {
|
|
177
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
178
|
+
throw new OnyxConfigError(`Failed to read ${resolved}: ${msg}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async function resolveConfig(input) {
|
|
182
|
+
const configPath = gProcess?.env?.ONYX_CONFIG_PATH;
|
|
183
|
+
const env = readEnv(input?.databaseId);
|
|
184
|
+
let cfgPath = {};
|
|
185
|
+
if (configPath) {
|
|
186
|
+
cfgPath = await readConfigPath(configPath);
|
|
187
|
+
}
|
|
188
|
+
const targetId = input?.databaseId ?? env.databaseId ?? cfgPath.databaseId;
|
|
189
|
+
let haveDbId = !!(input?.databaseId ?? env.databaseId ?? cfgPath.databaseId);
|
|
190
|
+
let haveApiKey = !!(input?.apiKey ?? env.apiKey ?? cfgPath.apiKey);
|
|
191
|
+
let haveApiSecret = !!(input?.apiSecret ?? env.apiSecret ?? cfgPath.apiSecret);
|
|
192
|
+
let project = {};
|
|
193
|
+
if (!(haveDbId && haveApiKey && haveApiSecret)) {
|
|
194
|
+
project = await readProjectFile(targetId);
|
|
195
|
+
if (project.databaseId) haveDbId = true;
|
|
196
|
+
if (project.apiKey) haveApiKey = true;
|
|
197
|
+
if (project.apiSecret) haveApiSecret = true;
|
|
198
|
+
}
|
|
199
|
+
let home = {};
|
|
200
|
+
if (!(haveDbId && haveApiKey && haveApiSecret)) {
|
|
201
|
+
home = await readHomeProfile(targetId);
|
|
202
|
+
}
|
|
203
|
+
const merged = {
|
|
204
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
205
|
+
...dropUndefined(home),
|
|
206
|
+
...dropUndefined(project),
|
|
207
|
+
...dropUndefined(cfgPath),
|
|
208
|
+
...dropUndefined(env),
|
|
209
|
+
...dropUndefined(input)
|
|
210
|
+
};
|
|
211
|
+
dbg("merged (pre-validate):", mask(merged));
|
|
212
|
+
const baseUrl = sanitizeBaseUrl(merged.baseUrl ?? DEFAULT_BASE_URL);
|
|
213
|
+
const databaseId = merged.databaseId ?? "";
|
|
214
|
+
const apiKey = merged.apiKey ?? "";
|
|
215
|
+
const apiSecret = merged.apiSecret ?? "";
|
|
216
|
+
const gfetch = globalThis.fetch;
|
|
217
|
+
const fetchImpl = merged.fetch ?? (typeof gfetch === "function" ? (u, i) => gfetch(u, i) : async () => {
|
|
218
|
+
throw new OnyxConfigError("No fetch available; provide OnyxConfig.fetch");
|
|
219
|
+
});
|
|
220
|
+
const missing = [];
|
|
221
|
+
if (!databaseId) missing.push("databaseId");
|
|
222
|
+
if (!apiKey) missing.push("apiKey");
|
|
223
|
+
if (!apiSecret) missing.push("apiSecret");
|
|
224
|
+
if (missing.length) {
|
|
225
|
+
dbg("validation failed. merged:", mask(merged));
|
|
226
|
+
const sources = [
|
|
227
|
+
"env",
|
|
228
|
+
configPath ?? "env ONYX_CONFIG_PATH",
|
|
229
|
+
...isNode ? [
|
|
230
|
+
"./onyx-database-<databaseId>.json",
|
|
231
|
+
"./onyx-database.json",
|
|
232
|
+
"~/.onyx/onyx-database-<databaseId>.json",
|
|
233
|
+
"~/.onyx/onyx-database.json",
|
|
234
|
+
"~/onyx-database.json"
|
|
235
|
+
] : [],
|
|
236
|
+
"explicit config"
|
|
237
|
+
];
|
|
238
|
+
throw new OnyxConfigError(
|
|
239
|
+
`Missing required config: ${missing.join(", ")}. Sources: ${sources.join(", ")}`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
const resolved = { baseUrl, databaseId, apiKey, apiSecret, fetch: fetchImpl };
|
|
243
|
+
const source = {
|
|
244
|
+
databaseId: input?.databaseId ? "explicit config" : env.databaseId ? "env" : cfgPath.databaseId ? "env ONYX_CONFIG_PATH" : project.databaseId ? "project file" : home.databaseId ? "home profile" : "unknown",
|
|
245
|
+
apiKey: input?.apiKey ? "explicit config" : env.apiKey ? "env" : cfgPath.apiKey ? "env ONYX_CONFIG_PATH" : project.apiKey ? "project file" : home.apiKey ? "home profile" : "unknown",
|
|
246
|
+
apiSecret: input?.apiSecret ? "explicit config" : env.apiSecret ? "env" : cfgPath.apiSecret ? "env ONYX_CONFIG_PATH" : project.apiSecret ? "project file" : home.apiSecret ? "home profile" : "unknown"
|
|
247
|
+
};
|
|
248
|
+
dbg("credential source:", JSON.stringify(source));
|
|
249
|
+
dbg("resolved:", mask(resolved));
|
|
250
|
+
return resolved;
|
|
251
|
+
}
|
|
252
|
+
function mask(obj) {
|
|
253
|
+
if (!obj) return obj;
|
|
254
|
+
const clone = { ...obj };
|
|
255
|
+
if (typeof clone.apiKey === "string") clone.apiKey = "***";
|
|
256
|
+
if (typeof clone.apiSecret === "string") clone.apiSecret = "***";
|
|
257
|
+
return clone;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/errors/http-error.ts
|
|
261
|
+
var OnyxHttpError = class extends Error {
|
|
262
|
+
name = "OnyxHttpError";
|
|
263
|
+
status;
|
|
264
|
+
statusText;
|
|
265
|
+
body;
|
|
266
|
+
rawBody;
|
|
267
|
+
constructor(message, status, statusText, body, rawBody) {
|
|
268
|
+
super(message);
|
|
269
|
+
this.status = status;
|
|
270
|
+
this.statusText = statusText;
|
|
271
|
+
this.body = body;
|
|
272
|
+
this.rawBody = rawBody;
|
|
273
|
+
}
|
|
274
|
+
toJSON() {
|
|
275
|
+
return {
|
|
276
|
+
name: this.name,
|
|
277
|
+
message: this.message,
|
|
278
|
+
status: this.status,
|
|
279
|
+
statusText: this.statusText,
|
|
280
|
+
body: this.body,
|
|
281
|
+
rawBody: this.rawBody,
|
|
282
|
+
stack: this.stack
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
286
|
+
return this.toJSON();
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// src/core/http.ts
|
|
291
|
+
function parseJsonAllowNaN(txt) {
|
|
292
|
+
try {
|
|
293
|
+
return JSON.parse(txt);
|
|
294
|
+
} catch {
|
|
295
|
+
const fixed = txt.replace(/(:\s*)(NaN|Infinity|-Infinity)(\s*[,}])/g, "$1null$3");
|
|
296
|
+
return JSON.parse(fixed);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
var HttpClient = class {
|
|
300
|
+
baseUrl;
|
|
301
|
+
apiKey;
|
|
302
|
+
apiSecret;
|
|
303
|
+
fetchImpl;
|
|
304
|
+
defaults;
|
|
305
|
+
requestLoggingEnabled;
|
|
306
|
+
responseLoggingEnabled;
|
|
307
|
+
constructor(opts) {
|
|
308
|
+
if (!opts.baseUrl || opts.baseUrl.trim() === "") {
|
|
309
|
+
throw new OnyxConfigError("baseUrl is required");
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
new URL(opts.baseUrl);
|
|
313
|
+
} catch {
|
|
314
|
+
throw new OnyxConfigError("baseUrl must include protocol, e.g. https://");
|
|
315
|
+
}
|
|
316
|
+
this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
317
|
+
this.apiKey = opts.apiKey;
|
|
318
|
+
this.apiSecret = opts.apiSecret;
|
|
319
|
+
const gfetch = globalThis.fetch;
|
|
320
|
+
if (opts.fetchImpl) {
|
|
321
|
+
this.fetchImpl = opts.fetchImpl;
|
|
322
|
+
} else if (typeof gfetch === "function") {
|
|
323
|
+
this.fetchImpl = (url, init) => gfetch(url, init);
|
|
324
|
+
} else {
|
|
325
|
+
throw new Error("global fetch is not available; provide OnyxConfig.fetch");
|
|
326
|
+
}
|
|
327
|
+
this.defaults = Object.assign({}, opts.defaultHeaders);
|
|
328
|
+
const envDebug = globalThis.process?.env?.ONYX_DEBUG === "true";
|
|
329
|
+
this.requestLoggingEnabled = !!opts.requestLoggingEnabled || envDebug;
|
|
330
|
+
this.responseLoggingEnabled = !!opts.responseLoggingEnabled || envDebug;
|
|
331
|
+
}
|
|
332
|
+
headers(extra) {
|
|
333
|
+
const extras = { ...extra ?? {} };
|
|
334
|
+
delete extras["x-onyx-key"];
|
|
335
|
+
delete extras["x-onyx-secret"];
|
|
336
|
+
return {
|
|
337
|
+
"x-onyx-key": this.apiKey,
|
|
338
|
+
"x-onyx-secret": this.apiSecret,
|
|
339
|
+
"Accept": "application/json",
|
|
340
|
+
"Content-Type": "application/json",
|
|
341
|
+
...this.defaults,
|
|
342
|
+
...extras
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
async request(method, path2, body, extraHeaders) {
|
|
346
|
+
if (!path2.startsWith("/")) {
|
|
347
|
+
throw new OnyxConfigError("path must start with /");
|
|
348
|
+
}
|
|
349
|
+
const url = `${this.baseUrl}${path2}`;
|
|
350
|
+
const headers = this.headers({
|
|
351
|
+
...method === "DELETE" ? { Prefer: "return=representation" } : {},
|
|
352
|
+
...extraHeaders ?? {}
|
|
353
|
+
});
|
|
354
|
+
const hasExplicitContentType = extraHeaders && "Content-Type" in extraHeaders || Object.prototype.hasOwnProperty.call(this.defaults, "Content-Type");
|
|
355
|
+
if (body == null && !hasExplicitContentType) delete headers["Content-Type"];
|
|
356
|
+
if (this.requestLoggingEnabled) {
|
|
357
|
+
console.log(`${method} ${url}`);
|
|
358
|
+
if (body != null) {
|
|
359
|
+
const logBody = typeof body === "string" ? body : JSON.stringify(body);
|
|
360
|
+
console.log(logBody);
|
|
361
|
+
}
|
|
362
|
+
const headerLog = { ...headers, "x-onyx-secret": "[REDACTED]" };
|
|
363
|
+
console.log("Headers:", headerLog);
|
|
364
|
+
}
|
|
365
|
+
const payload = body == null ? void 0 : typeof body === "string" ? body : JSON.stringify(body);
|
|
366
|
+
const init = {
|
|
367
|
+
method,
|
|
368
|
+
headers,
|
|
369
|
+
body: payload
|
|
370
|
+
};
|
|
371
|
+
const isQuery = path2.includes("/query/") && !/\/query\/(?:update|delete)\//.test(path2);
|
|
372
|
+
const canRetry = method === "GET" || isQuery;
|
|
373
|
+
const maxAttempts = canRetry ? 3 : 1;
|
|
374
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
375
|
+
try {
|
|
376
|
+
const res = await this.fetchImpl(url, init);
|
|
377
|
+
const contentType = res.headers.get("Content-Type") || "";
|
|
378
|
+
const raw = await res.text();
|
|
379
|
+
if (this.responseLoggingEnabled) {
|
|
380
|
+
const statusLine = `${res.status} ${res.statusText}`.trim();
|
|
381
|
+
console.log(statusLine);
|
|
382
|
+
if (raw.trim().length > 0) {
|
|
383
|
+
console.log(raw);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const isJson = raw.trim().length > 0 && (contentType.includes("application/json") || /^[\[{]/.test(raw.trim()));
|
|
387
|
+
const data = isJson ? parseJsonAllowNaN(raw) : raw;
|
|
388
|
+
if (!res.ok) {
|
|
389
|
+
const msg = typeof data === "object" && data !== null && "error" in data && typeof data.error?.message === "string" ? String(data.error.message) : `${res.status} ${res.statusText}`;
|
|
390
|
+
if (canRetry && res.status >= 500 && attempt + 1 < maxAttempts) {
|
|
391
|
+
await new Promise((r) => setTimeout(r, 100 * 2 ** attempt));
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
throw new OnyxHttpError(msg, res.status, res.statusText, data, raw);
|
|
395
|
+
}
|
|
396
|
+
return data;
|
|
397
|
+
} catch (err) {
|
|
398
|
+
const retryable = canRetry && (!(err instanceof OnyxHttpError) || err.status >= 500);
|
|
399
|
+
if (attempt + 1 < maxAttempts && retryable) {
|
|
400
|
+
await new Promise((r) => setTimeout(r, 100 * 2 ** attempt));
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
throw err;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
throw new Error("Request failed after retries");
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
// src/core/stream.ts
|
|
411
|
+
var debug = (...args) => {
|
|
412
|
+
if (globalThis.process?.env?.ONYX_STREAM_DEBUG == "true")
|
|
413
|
+
console.log("[onyx-stream]", ...args);
|
|
414
|
+
};
|
|
415
|
+
async function openJsonLinesStream(fetchImpl, url, init = {}, handlers = {}) {
|
|
416
|
+
const decoder = new TextDecoder("utf-8");
|
|
417
|
+
let buffer = "";
|
|
418
|
+
let canceled = false;
|
|
419
|
+
let currentReader = null;
|
|
420
|
+
let retryCount = 0;
|
|
421
|
+
const maxRetries = 4;
|
|
422
|
+
const processLine = (line) => {
|
|
423
|
+
const trimmed = line.trim();
|
|
424
|
+
debug("line", trimmed);
|
|
425
|
+
if (!trimmed || trimmed.startsWith(":")) return;
|
|
426
|
+
const jsonLine = trimmed.startsWith("data:") ? trimmed.slice(5).trim() : trimmed;
|
|
427
|
+
let obj;
|
|
428
|
+
try {
|
|
429
|
+
obj = parseJsonAllowNaN(jsonLine);
|
|
430
|
+
} catch {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const rawAction = obj.action ?? obj.event ?? obj.type ?? obj.eventType ?? obj.changeType;
|
|
434
|
+
const entity = obj.entity;
|
|
435
|
+
const action = rawAction?.toUpperCase();
|
|
436
|
+
if (action === "CREATE" || action === "CREATED" || action === "ADDED" || action === "ADD" || action === "INSERT" || action === "INSERTED")
|
|
437
|
+
handlers.onItemAdded?.(entity);
|
|
438
|
+
else if (action === "UPDATE" || action === "UPDATED")
|
|
439
|
+
handlers.onItemUpdated?.(entity);
|
|
440
|
+
else if (action === "DELETE" || action === "DELETED" || action === "REMOVE" || action === "REMOVED")
|
|
441
|
+
handlers.onItemDeleted?.(entity);
|
|
442
|
+
const canonical = action === "ADDED" || action === "ADD" || action === "CREATE" || action === "CREATED" || action === "INSERT" || action === "INSERTED" ? "CREATE" : action === "UPDATED" || action === "UPDATE" ? "UPDATE" : action === "DELETED" || action === "DELETE" || action === "REMOVE" || action === "REMOVED" ? "DELETE" : action;
|
|
443
|
+
if (canonical && canonical !== "KEEP_ALIVE")
|
|
444
|
+
handlers.onItem?.(entity ?? null, canonical);
|
|
445
|
+
debug("dispatch", canonical, entity);
|
|
446
|
+
};
|
|
447
|
+
const connect = async () => {
|
|
448
|
+
if (canceled) return;
|
|
449
|
+
debug("connecting", url);
|
|
450
|
+
try {
|
|
451
|
+
const res = await fetchImpl(url, {
|
|
452
|
+
method: init.method ?? "PUT",
|
|
453
|
+
headers: init.headers ?? {},
|
|
454
|
+
body: init.body
|
|
455
|
+
});
|
|
456
|
+
debug("response", res.status, res.statusText);
|
|
457
|
+
if (!res.ok) {
|
|
458
|
+
const raw = await res.text();
|
|
459
|
+
let parsed = raw;
|
|
460
|
+
try {
|
|
461
|
+
parsed = parseJsonAllowNaN(raw);
|
|
462
|
+
} catch {
|
|
463
|
+
}
|
|
464
|
+
debug("non-ok", res.status);
|
|
465
|
+
throw new OnyxHttpError(`${res.status} ${res.statusText}`, res.status, res.statusText, parsed, raw);
|
|
466
|
+
}
|
|
467
|
+
const body = res.body;
|
|
468
|
+
if (!body || typeof body.getReader !== "function") {
|
|
469
|
+
debug("no reader");
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
currentReader = body.getReader();
|
|
473
|
+
debug("connected");
|
|
474
|
+
retryCount = 0;
|
|
475
|
+
pump();
|
|
476
|
+
} catch (err) {
|
|
477
|
+
debug("connect error", err);
|
|
478
|
+
if (canceled) return;
|
|
479
|
+
if (retryCount >= maxRetries) return;
|
|
480
|
+
const delay = Math.min(1e3 * 2 ** retryCount, 3e4);
|
|
481
|
+
retryCount++;
|
|
482
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
483
|
+
void connect();
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
const pump = () => {
|
|
487
|
+
if (canceled || !currentReader) return;
|
|
488
|
+
currentReader.read().then(({ done, value }) => {
|
|
489
|
+
if (canceled) return;
|
|
490
|
+
debug("chunk", { done, length: value?.length ?? 0 });
|
|
491
|
+
if (done) {
|
|
492
|
+
debug("done");
|
|
493
|
+
void connect();
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
buffer += decoder.decode(value, { stream: true });
|
|
497
|
+
const lines = buffer.split("\n");
|
|
498
|
+
buffer = lines.pop() ?? "";
|
|
499
|
+
for (const line of lines) processLine(line);
|
|
500
|
+
pump();
|
|
501
|
+
}).catch((err) => {
|
|
502
|
+
debug("pump error", err);
|
|
503
|
+
if (!canceled) void connect();
|
|
504
|
+
});
|
|
505
|
+
};
|
|
506
|
+
await connect();
|
|
507
|
+
return {
|
|
508
|
+
cancel() {
|
|
509
|
+
if (canceled) return;
|
|
510
|
+
canceled = true;
|
|
511
|
+
try {
|
|
512
|
+
currentReader?.cancel();
|
|
513
|
+
} catch {
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// src/builders/query-results.ts
|
|
520
|
+
var QueryResults = class extends Array {
|
|
521
|
+
/** Token for the next page of results or null. */
|
|
522
|
+
nextPage;
|
|
523
|
+
fetcher;
|
|
524
|
+
/**
|
|
525
|
+
* @param records - Records in the current page.
|
|
526
|
+
* @param nextPage - Token representing the next page.
|
|
527
|
+
* @param fetcher - Function used to fetch the next page when needed.
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* const results = new QueryResults(users, token, t => fetchMore(t));
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
constructor(records, nextPage, fetcher) {
|
|
534
|
+
const items = (() => {
|
|
535
|
+
if (records == null) return [];
|
|
536
|
+
if (Array.isArray(records)) return records;
|
|
537
|
+
if (typeof records[Symbol.iterator] === "function") {
|
|
538
|
+
return Array.from(records);
|
|
539
|
+
}
|
|
540
|
+
if (typeof records.length === "number") {
|
|
541
|
+
return Array.from(records);
|
|
542
|
+
}
|
|
543
|
+
return [records];
|
|
544
|
+
})();
|
|
545
|
+
super(...items);
|
|
546
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
547
|
+
this.nextPage = nextPage;
|
|
548
|
+
this.fetcher = fetcher;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Returns the first record in the result set.
|
|
552
|
+
* @throws Error if the result set is empty.
|
|
553
|
+
* @example
|
|
554
|
+
* ```ts
|
|
555
|
+
* const user = results.first();
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
first() {
|
|
559
|
+
if (this.length === 0) throw new Error("QueryResults is empty");
|
|
560
|
+
return this[0];
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Returns the first record or `null` if the result set is empty.
|
|
564
|
+
* @example
|
|
565
|
+
* ```ts
|
|
566
|
+
* const user = results.firstOrNull();
|
|
567
|
+
* ```
|
|
568
|
+
*/
|
|
569
|
+
firstOrNull() {
|
|
570
|
+
return this.length > 0 ? this[0] : null;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Checks whether the current page has no records.
|
|
574
|
+
* @example
|
|
575
|
+
* ```ts
|
|
576
|
+
* if (results.isEmpty()) console.log('no data');
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
isEmpty() {
|
|
580
|
+
return this.length === 0;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Number of records on the current page.
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* console.log(results.size());
|
|
587
|
+
* ```
|
|
588
|
+
*/
|
|
589
|
+
size() {
|
|
590
|
+
return this.length;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Iterates over each record on the current page only.
|
|
594
|
+
* @param action - Function to invoke for each record.
|
|
595
|
+
* @param thisArg - Optional `this` binding for the callback.
|
|
596
|
+
* @example
|
|
597
|
+
* ```ts
|
|
598
|
+
* results.forEachOnPage(u => console.log(u.id));
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
forEachOnPage(action, thisArg) {
|
|
602
|
+
super.forEach((value, index) => {
|
|
603
|
+
action.call(thisArg, value, index, this);
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Iterates over every record across all pages sequentially.
|
|
608
|
+
* @param action - Function executed for each record. Returning `false`
|
|
609
|
+
* stops iteration early.
|
|
610
|
+
* @param thisArg - Optional `this` binding for the callback.
|
|
611
|
+
* @example
|
|
612
|
+
* ```ts
|
|
613
|
+
* await results.forEach(u => {
|
|
614
|
+
* console.log(u.id);
|
|
615
|
+
* });
|
|
616
|
+
* ```
|
|
617
|
+
*/
|
|
618
|
+
forEach(action, thisArg) {
|
|
619
|
+
let index = 0;
|
|
620
|
+
return this.forEachAll(async (item) => {
|
|
621
|
+
const result = await action.call(thisArg, item, index, this);
|
|
622
|
+
index += 1;
|
|
623
|
+
return result;
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Iterates over every record across all pages sequentially.
|
|
628
|
+
* @param action - Function executed for each record. Returning `false`
|
|
629
|
+
* stops iteration early.
|
|
630
|
+
* @example
|
|
631
|
+
* ```ts
|
|
632
|
+
* await results.forEachAll(u => {
|
|
633
|
+
* if (u.disabled) return false;
|
|
634
|
+
* });
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
async forEachAll(action) {
|
|
638
|
+
await this.forEachPage(async (records) => {
|
|
639
|
+
for (const r of records) {
|
|
640
|
+
const res = await action(r);
|
|
641
|
+
if (res === false) return false;
|
|
642
|
+
}
|
|
643
|
+
return true;
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Iterates page by page across the result set.
|
|
648
|
+
* @param action - Function invoked with each page of records. Returning
|
|
649
|
+
* `false` stops iteration.
|
|
650
|
+
* @example
|
|
651
|
+
* ```ts
|
|
652
|
+
* await results.forEachPage(page => {
|
|
653
|
+
* console.log(page.length);
|
|
654
|
+
* });
|
|
655
|
+
* ```
|
|
656
|
+
*/
|
|
657
|
+
async forEachPage(action) {
|
|
658
|
+
let page = this;
|
|
659
|
+
while (page) {
|
|
660
|
+
const cont = await action(Array.from(page));
|
|
661
|
+
if (cont === false) return;
|
|
662
|
+
if (page.nextPage && page.fetcher) {
|
|
663
|
+
page = await page.fetcher(page.nextPage);
|
|
664
|
+
} else {
|
|
665
|
+
page = null;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Collects all records from every page into a single array.
|
|
671
|
+
* @returns All records.
|
|
672
|
+
* @example
|
|
673
|
+
* ```ts
|
|
674
|
+
* const allUsers = await results.getAllRecords();
|
|
675
|
+
* ```
|
|
676
|
+
*/
|
|
677
|
+
async getAllRecords() {
|
|
678
|
+
const all = [];
|
|
679
|
+
await this.forEachPage((records) => {
|
|
680
|
+
all.push(...records);
|
|
681
|
+
});
|
|
682
|
+
return all;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Filters all records using the provided predicate.
|
|
686
|
+
* @param predicate - Function used to test each record.
|
|
687
|
+
* @example
|
|
688
|
+
* ```ts
|
|
689
|
+
* const enabled = await results.filterAll(u => u.enabled);
|
|
690
|
+
* ```
|
|
691
|
+
*/
|
|
692
|
+
async filterAll(predicate) {
|
|
693
|
+
const all = await this.getAllRecords();
|
|
694
|
+
return all.filter(predicate);
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Maps all records using the provided transform.
|
|
698
|
+
* @param transform - Mapping function.
|
|
699
|
+
* @example
|
|
700
|
+
* ```ts
|
|
701
|
+
* const names = await results.mapAll(u => u.name);
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
async mapAll(transform) {
|
|
705
|
+
const all = await this.getAllRecords();
|
|
706
|
+
return all.map(transform);
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Extracts values for a field across all records.
|
|
710
|
+
* @param field - Name of the field to pluck.
|
|
711
|
+
* @example
|
|
712
|
+
* ```ts
|
|
713
|
+
* const ids = await results.values('id');
|
|
714
|
+
* ```
|
|
715
|
+
*/
|
|
716
|
+
// @ts-expect-error overriding Array#values
|
|
717
|
+
async values(field) {
|
|
718
|
+
const all = await this.getAllRecords();
|
|
719
|
+
return all.map((r) => r[field]);
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Maximum value produced by the selector across all records.
|
|
723
|
+
* @param selector - Function extracting a numeric value.
|
|
724
|
+
* @example
|
|
725
|
+
* ```ts
|
|
726
|
+
* const maxAge = await results.maxOfDouble(u => u.age);
|
|
727
|
+
* ```
|
|
728
|
+
*/
|
|
729
|
+
async maxOfDouble(selector) {
|
|
730
|
+
const all = await this.getAllRecords();
|
|
731
|
+
return all.reduce((max, r) => Math.max(max, selector(r)), -Infinity);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Minimum value produced by the selector across all records.
|
|
735
|
+
* @param selector - Function extracting a numeric value.
|
|
736
|
+
* @example
|
|
737
|
+
* ```ts
|
|
738
|
+
* const minAge = await results.minOfDouble(u => u.age);
|
|
739
|
+
* ```
|
|
740
|
+
*/
|
|
741
|
+
async minOfDouble(selector) {
|
|
742
|
+
const all = await this.getAllRecords();
|
|
743
|
+
return all.reduce((min, r) => Math.min(min, selector(r)), Infinity);
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Sum of values produced by the selector across all records.
|
|
747
|
+
* @param selector - Function extracting a numeric value.
|
|
748
|
+
* @example
|
|
749
|
+
* ```ts
|
|
750
|
+
* const total = await results.sumOfDouble(u => u.score);
|
|
751
|
+
* ```
|
|
752
|
+
*/
|
|
753
|
+
async sumOfDouble(selector) {
|
|
754
|
+
const all = await this.getAllRecords();
|
|
755
|
+
return all.reduce((sum, r) => sum + selector(r), 0);
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Maximum float value from the selector.
|
|
759
|
+
* @param selector - Function extracting a numeric value.
|
|
760
|
+
*/
|
|
761
|
+
async maxOfFloat(selector) {
|
|
762
|
+
return this.maxOfDouble(selector);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Minimum float value from the selector.
|
|
766
|
+
* @param selector - Function extracting a numeric value.
|
|
767
|
+
*/
|
|
768
|
+
async minOfFloat(selector) {
|
|
769
|
+
return this.minOfDouble(selector);
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Sum of float values from the selector.
|
|
773
|
+
* @param selector - Function extracting a numeric value.
|
|
774
|
+
*/
|
|
775
|
+
async sumOfFloat(selector) {
|
|
776
|
+
return this.sumOfDouble(selector);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Maximum integer value from the selector.
|
|
780
|
+
* @param selector - Function extracting a numeric value.
|
|
781
|
+
*/
|
|
782
|
+
async maxOfInt(selector) {
|
|
783
|
+
return this.maxOfDouble(selector);
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Minimum integer value from the selector.
|
|
787
|
+
* @param selector - Function extracting a numeric value.
|
|
788
|
+
*/
|
|
789
|
+
async minOfInt(selector) {
|
|
790
|
+
return this.minOfDouble(selector);
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Sum of integer values from the selector.
|
|
794
|
+
* @param selector - Function extracting a numeric value.
|
|
795
|
+
*/
|
|
796
|
+
async sumOfInt(selector) {
|
|
797
|
+
return this.sumOfDouble(selector);
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Maximum long value from the selector.
|
|
801
|
+
* @param selector - Function extracting a numeric value.
|
|
802
|
+
*/
|
|
803
|
+
async maxOfLong(selector) {
|
|
804
|
+
return this.maxOfDouble(selector);
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Minimum long value from the selector.
|
|
808
|
+
* @param selector - Function extracting a numeric value.
|
|
809
|
+
*/
|
|
810
|
+
async minOfLong(selector) {
|
|
811
|
+
return this.minOfDouble(selector);
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Sum of long values from the selector.
|
|
815
|
+
* @param selector - Function extracting a numeric value.
|
|
816
|
+
*/
|
|
817
|
+
async sumOfLong(selector) {
|
|
818
|
+
return this.sumOfDouble(selector);
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Sum of bigint values from the selector.
|
|
822
|
+
* @param selector - Function extracting a bigint value.
|
|
823
|
+
* @example
|
|
824
|
+
* ```ts
|
|
825
|
+
* const total = await results.sumOfBigInt(u => u.balance);
|
|
826
|
+
* ```
|
|
827
|
+
*/
|
|
828
|
+
async sumOfBigInt(selector) {
|
|
829
|
+
const all = await this.getAllRecords();
|
|
830
|
+
return all.reduce((sum, r) => sum + selector(r), 0n);
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Executes an action for each page in parallel.
|
|
834
|
+
* @param action - Function executed for each record concurrently.
|
|
835
|
+
* @example
|
|
836
|
+
* ```ts
|
|
837
|
+
* await results.forEachPageParallel(async u => sendEmail(u));
|
|
838
|
+
* ```
|
|
839
|
+
*/
|
|
840
|
+
async forEachPageParallel(action) {
|
|
841
|
+
await this.forEachPage((records) => Promise.all(records.map(action)).then(() => true));
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
// src/builders/cascade-relationship-builder.ts
|
|
846
|
+
var CascadeRelationshipBuilder = class {
|
|
847
|
+
graphName;
|
|
848
|
+
typeName;
|
|
849
|
+
target;
|
|
850
|
+
/**
|
|
851
|
+
* Set the graph name component.
|
|
852
|
+
*
|
|
853
|
+
* @param name Graph name or namespace.
|
|
854
|
+
* @example
|
|
855
|
+
* ```ts
|
|
856
|
+
* builder.graph('programs');
|
|
857
|
+
* ```
|
|
858
|
+
*/
|
|
859
|
+
graph(name) {
|
|
860
|
+
this.graphName = name;
|
|
861
|
+
return this;
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Set the graph type component.
|
|
865
|
+
*
|
|
866
|
+
* @param type Graph type to target.
|
|
867
|
+
* @example
|
|
868
|
+
* ```ts
|
|
869
|
+
* builder.graphType('StreamingProgram');
|
|
870
|
+
* ```
|
|
871
|
+
*/
|
|
872
|
+
graphType(type) {
|
|
873
|
+
this.typeName = type;
|
|
874
|
+
return this;
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Set the target field for the relationship.
|
|
878
|
+
*
|
|
879
|
+
* @param field Target field name.
|
|
880
|
+
* @example
|
|
881
|
+
* ```ts
|
|
882
|
+
* builder.targetField('channelId');
|
|
883
|
+
* ```
|
|
884
|
+
*/
|
|
885
|
+
targetField(field) {
|
|
886
|
+
this.target = field;
|
|
887
|
+
return this;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Produce the cascade relationship string using the provided source field.
|
|
891
|
+
*
|
|
892
|
+
* @param field Source field name.
|
|
893
|
+
* @example
|
|
894
|
+
* ```ts
|
|
895
|
+
* const rel = builder
|
|
896
|
+
* .graph('programs')
|
|
897
|
+
* .graphType('StreamingProgram')
|
|
898
|
+
* .targetField('channelId')
|
|
899
|
+
* .sourceField('id');
|
|
900
|
+
* // rel === 'programs:StreamingProgram(channelId, id)'
|
|
901
|
+
* ```
|
|
902
|
+
*/
|
|
903
|
+
sourceField(field) {
|
|
904
|
+
if (!this.graphName || !this.typeName || !this.target) {
|
|
905
|
+
throw new Error("Cascade relationship requires graph, type, target, and source fields");
|
|
906
|
+
}
|
|
907
|
+
return `${this.graphName}:${this.typeName}(${this.target}, ${field})`;
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
|
|
911
|
+
// src/errors/onyx-error.ts
|
|
912
|
+
var OnyxError = class extends Error {
|
|
913
|
+
name = "OnyxError";
|
|
914
|
+
constructor(message) {
|
|
915
|
+
super(message);
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
// src/impl/onyx.ts
|
|
920
|
+
var DEFAULT_CACHE_TTL = 5 * 60 * 1e3;
|
|
921
|
+
var cachedCfg = null;
|
|
922
|
+
function resolveConfigWithCache(config) {
|
|
923
|
+
const ttl = config?.ttl ?? DEFAULT_CACHE_TTL;
|
|
924
|
+
const now = Date.now();
|
|
925
|
+
if (cachedCfg && cachedCfg.expires > now) {
|
|
926
|
+
return cachedCfg.promise;
|
|
927
|
+
}
|
|
928
|
+
const { ttl: _ttl, requestLoggingEnabled: _reqLog, responseLoggingEnabled: _resLog, ...rest } = config ?? {};
|
|
929
|
+
const promise = resolveConfig(rest);
|
|
930
|
+
cachedCfg = { promise, expires: now + ttl };
|
|
931
|
+
return promise;
|
|
932
|
+
}
|
|
933
|
+
function clearCacheConfig() {
|
|
934
|
+
cachedCfg = null;
|
|
935
|
+
}
|
|
936
|
+
function toSingleCondition(criteria) {
|
|
937
|
+
return { conditionType: "SingleCondition", criteria };
|
|
938
|
+
}
|
|
939
|
+
function flattenStrings(values) {
|
|
940
|
+
const flat = [];
|
|
941
|
+
for (const value of values) {
|
|
942
|
+
if (Array.isArray(value)) {
|
|
943
|
+
flat.push(...value);
|
|
944
|
+
} else if (typeof value === "string") {
|
|
945
|
+
flat.push(value);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return flat;
|
|
949
|
+
}
|
|
950
|
+
function toCondition(input) {
|
|
951
|
+
if (typeof input.toCondition === "function") {
|
|
952
|
+
return input.toCondition();
|
|
953
|
+
}
|
|
954
|
+
const c = input;
|
|
955
|
+
if (c && typeof c.field === "string" && typeof c.operator === "string") {
|
|
956
|
+
return toSingleCondition(c);
|
|
957
|
+
}
|
|
958
|
+
throw new Error("Invalid condition passed to builder.");
|
|
959
|
+
}
|
|
960
|
+
function serializeDates(value) {
|
|
961
|
+
if (value instanceof Date) return value.toISOString();
|
|
962
|
+
if (Array.isArray(value)) return value.map(serializeDates);
|
|
963
|
+
if (value && typeof value === "object") {
|
|
964
|
+
const out = {};
|
|
965
|
+
for (const [k, v] of Object.entries(value)) {
|
|
966
|
+
out[k] = serializeDates(v);
|
|
967
|
+
}
|
|
968
|
+
return out;
|
|
969
|
+
}
|
|
970
|
+
return value;
|
|
971
|
+
}
|
|
972
|
+
function normalizeSecretMetadata(input) {
|
|
973
|
+
return { ...input, updatedAt: new Date(input.updatedAt) };
|
|
974
|
+
}
|
|
975
|
+
function normalizeSecretRecord(input) {
|
|
976
|
+
return { ...input, updatedAt: new Date(input.updatedAt) };
|
|
977
|
+
}
|
|
978
|
+
function normalizeDate(value) {
|
|
979
|
+
if (value == null) return void 0;
|
|
980
|
+
if (value instanceof Date) return value;
|
|
981
|
+
const ts = new Date(value);
|
|
982
|
+
return Number.isNaN(ts.getTime()) ? void 0 : ts;
|
|
983
|
+
}
|
|
984
|
+
function normalizeSchemaRevision(input, fallbackDatabaseId) {
|
|
985
|
+
const { meta, createdAt, publishedAt, revisionId, ...rest } = input;
|
|
986
|
+
const mergedMeta = {
|
|
987
|
+
revisionId: meta?.revisionId ?? revisionId,
|
|
988
|
+
createdAt: normalizeDate(meta?.createdAt ?? createdAt),
|
|
989
|
+
publishedAt: normalizeDate(meta?.publishedAt ?? publishedAt)
|
|
990
|
+
};
|
|
991
|
+
const cleanedMeta = mergedMeta.revisionId || mergedMeta.createdAt || mergedMeta.publishedAt ? mergedMeta : void 0;
|
|
992
|
+
return {
|
|
993
|
+
...rest,
|
|
994
|
+
databaseId: input.databaseId ?? fallbackDatabaseId,
|
|
995
|
+
meta: cleanedMeta,
|
|
996
|
+
entities: input.entities ?? []
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
var OnyxDatabaseImpl = class {
|
|
1000
|
+
cfgPromise;
|
|
1001
|
+
resolved = null;
|
|
1002
|
+
http = null;
|
|
1003
|
+
streams = /* @__PURE__ */ new Set();
|
|
1004
|
+
requestLoggingEnabled;
|
|
1005
|
+
responseLoggingEnabled;
|
|
1006
|
+
defaultPartition;
|
|
1007
|
+
constructor(config) {
|
|
1008
|
+
this.requestLoggingEnabled = !!config?.requestLoggingEnabled;
|
|
1009
|
+
this.responseLoggingEnabled = !!config?.responseLoggingEnabled;
|
|
1010
|
+
this.defaultPartition = config?.partition;
|
|
1011
|
+
this.cfgPromise = resolveConfigWithCache(config);
|
|
1012
|
+
}
|
|
1013
|
+
async ensureClient() {
|
|
1014
|
+
if (!this.resolved) {
|
|
1015
|
+
this.resolved = await this.cfgPromise;
|
|
1016
|
+
}
|
|
1017
|
+
if (!this.http) {
|
|
1018
|
+
this.http = new HttpClient({
|
|
1019
|
+
baseUrl: this.resolved.baseUrl,
|
|
1020
|
+
apiKey: this.resolved.apiKey,
|
|
1021
|
+
apiSecret: this.resolved.apiSecret,
|
|
1022
|
+
fetchImpl: this.resolved.fetch,
|
|
1023
|
+
requestLoggingEnabled: this.requestLoggingEnabled,
|
|
1024
|
+
responseLoggingEnabled: this.responseLoggingEnabled
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
return {
|
|
1028
|
+
http: this.http,
|
|
1029
|
+
fetchImpl: this.resolved.fetch,
|
|
1030
|
+
baseUrl: this.resolved.baseUrl,
|
|
1031
|
+
databaseId: this.resolved.databaseId
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
registerStream(handle) {
|
|
1035
|
+
this.streams.add(handle);
|
|
1036
|
+
return {
|
|
1037
|
+
cancel: () => {
|
|
1038
|
+
try {
|
|
1039
|
+
handle.cancel();
|
|
1040
|
+
} finally {
|
|
1041
|
+
this.streams.delete(handle);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
/** -------- IOnyxDatabase -------- */
|
|
1047
|
+
from(table) {
|
|
1048
|
+
return new QueryBuilderImpl(this, String(table), this.defaultPartition);
|
|
1049
|
+
}
|
|
1050
|
+
select(...fields) {
|
|
1051
|
+
const qb = new QueryBuilderImpl(
|
|
1052
|
+
this,
|
|
1053
|
+
null,
|
|
1054
|
+
this.defaultPartition
|
|
1055
|
+
);
|
|
1056
|
+
qb.select(...fields);
|
|
1057
|
+
return qb;
|
|
1058
|
+
}
|
|
1059
|
+
cascade(...relationships) {
|
|
1060
|
+
const cb = new CascadeBuilderImpl(this);
|
|
1061
|
+
return cb.cascade(...relationships);
|
|
1062
|
+
}
|
|
1063
|
+
cascadeBuilder() {
|
|
1064
|
+
return new CascadeRelationshipBuilder();
|
|
1065
|
+
}
|
|
1066
|
+
// Impl
|
|
1067
|
+
save(table, entityOrEntities, options) {
|
|
1068
|
+
if (arguments.length === 1) {
|
|
1069
|
+
return new SaveBuilderImpl(this, table);
|
|
1070
|
+
}
|
|
1071
|
+
return this._saveInternal(table, entityOrEntities, options);
|
|
1072
|
+
}
|
|
1073
|
+
async batchSave(table, entities, batchSize = 1e3, options) {
|
|
1074
|
+
for (let i = 0; i < entities.length; i += batchSize) {
|
|
1075
|
+
const chunk = entities.slice(i, i + batchSize);
|
|
1076
|
+
if (chunk.length) {
|
|
1077
|
+
await this._saveInternal(String(table), chunk, options);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
async findById(table, primaryKey, options) {
|
|
1082
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1083
|
+
const params = new URLSearchParams();
|
|
1084
|
+
const partition = options?.partition ?? this.defaultPartition;
|
|
1085
|
+
if (partition) params.append("partition", partition);
|
|
1086
|
+
if (options?.resolvers?.length) params.append("resolvers", options.resolvers.join(","));
|
|
1087
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/${encodeURIComponent(
|
|
1088
|
+
String(table)
|
|
1089
|
+
)}/${encodeURIComponent(primaryKey)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1090
|
+
try {
|
|
1091
|
+
return await http.request("GET", path2);
|
|
1092
|
+
} catch (err) {
|
|
1093
|
+
if (err instanceof OnyxHttpError && err.status === 404) return null;
|
|
1094
|
+
throw err;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
async delete(table, primaryKey, options) {
|
|
1098
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1099
|
+
const params = new URLSearchParams();
|
|
1100
|
+
const partition = options?.partition ?? this.defaultPartition;
|
|
1101
|
+
if (partition) params.append("partition", partition);
|
|
1102
|
+
if (options?.relationships?.length) {
|
|
1103
|
+
params.append("relationships", options.relationships.map(encodeURIComponent).join(","));
|
|
1104
|
+
}
|
|
1105
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/${encodeURIComponent(
|
|
1106
|
+
table
|
|
1107
|
+
)}/${encodeURIComponent(primaryKey)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1108
|
+
return http.request("DELETE", path2);
|
|
1109
|
+
}
|
|
1110
|
+
async saveDocument(doc) {
|
|
1111
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1112
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/document`;
|
|
1113
|
+
return http.request("PUT", path2, serializeDates(doc));
|
|
1114
|
+
}
|
|
1115
|
+
async getDocument(documentId, options) {
|
|
1116
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1117
|
+
const params = new URLSearchParams();
|
|
1118
|
+
if (options?.width != null) params.append("width", String(options.width));
|
|
1119
|
+
if (options?.height != null) params.append("height", String(options.height));
|
|
1120
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/document/${encodeURIComponent(
|
|
1121
|
+
documentId
|
|
1122
|
+
)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1123
|
+
return http.request("GET", path2);
|
|
1124
|
+
}
|
|
1125
|
+
async deleteDocument(documentId) {
|
|
1126
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1127
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/document/${encodeURIComponent(
|
|
1128
|
+
documentId
|
|
1129
|
+
)}`;
|
|
1130
|
+
return http.request("DELETE", path2);
|
|
1131
|
+
}
|
|
1132
|
+
async getSchema(options) {
|
|
1133
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1134
|
+
const params = new URLSearchParams();
|
|
1135
|
+
const tables = options?.tables;
|
|
1136
|
+
const tableList = Array.isArray(tables) ? tables : typeof tables === "string" ? tables.split(",") : [];
|
|
1137
|
+
const normalizedTables = tableList.map((t) => t.trim()).filter(Boolean);
|
|
1138
|
+
if (normalizedTables.length) {
|
|
1139
|
+
params.append("tables", normalizedTables.map(encodeURIComponent).join(","));
|
|
1140
|
+
}
|
|
1141
|
+
const path2 = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
|
|
1142
|
+
const res = await http.request("GET", path2);
|
|
1143
|
+
return normalizeSchemaRevision(res, databaseId);
|
|
1144
|
+
}
|
|
1145
|
+
async getSchemaHistory() {
|
|
1146
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1147
|
+
const path2 = `/schemas/history/${encodeURIComponent(databaseId)}`;
|
|
1148
|
+
const res = await http.request("GET", path2);
|
|
1149
|
+
return Array.isArray(res) ? res.map((entry) => normalizeSchemaRevision(entry, databaseId)) : [];
|
|
1150
|
+
}
|
|
1151
|
+
async updateSchema(schema, options) {
|
|
1152
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1153
|
+
const params = new URLSearchParams();
|
|
1154
|
+
if (options?.publish) params.append("publish", "true");
|
|
1155
|
+
const path2 = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
|
|
1156
|
+
const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
|
|
1157
|
+
const res = await http.request("PUT", path2, serializeDates(body));
|
|
1158
|
+
return normalizeSchemaRevision(res, databaseId);
|
|
1159
|
+
}
|
|
1160
|
+
async validateSchema(schema) {
|
|
1161
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1162
|
+
const path2 = `/schemas/${encodeURIComponent(databaseId)}/validate`;
|
|
1163
|
+
const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
|
|
1164
|
+
const res = await http.request("POST", path2, serializeDates(body));
|
|
1165
|
+
const normalizedSchema = res.schema ? normalizeSchemaRevision(res.schema, databaseId) : void 0;
|
|
1166
|
+
return {
|
|
1167
|
+
...res,
|
|
1168
|
+
valid: res.valid ?? true,
|
|
1169
|
+
schema: normalizedSchema
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
async listSecrets() {
|
|
1173
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1174
|
+
const path2 = `/database/${encodeURIComponent(databaseId)}/secret`;
|
|
1175
|
+
const response = await http.request(
|
|
1176
|
+
"GET",
|
|
1177
|
+
path2,
|
|
1178
|
+
void 0,
|
|
1179
|
+
{ "Content-Type": "application/json" }
|
|
1180
|
+
);
|
|
1181
|
+
const records = (response.records ?? []).map(normalizeSecretMetadata);
|
|
1182
|
+
return {
|
|
1183
|
+
...response,
|
|
1184
|
+
records,
|
|
1185
|
+
meta: response.meta ?? { totalRecords: records.length }
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
async getSecret(key) {
|
|
1189
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1190
|
+
const path2 = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
|
|
1191
|
+
const record = await http.request("GET", path2, void 0, {
|
|
1192
|
+
"Content-Type": "application/json"
|
|
1193
|
+
});
|
|
1194
|
+
return normalizeSecretRecord(record);
|
|
1195
|
+
}
|
|
1196
|
+
async putSecret(key, input) {
|
|
1197
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1198
|
+
const path2 = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
|
|
1199
|
+
const response = await http.request("PUT", path2, serializeDates(input));
|
|
1200
|
+
return normalizeSecretMetadata(response);
|
|
1201
|
+
}
|
|
1202
|
+
async deleteSecret(key) {
|
|
1203
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1204
|
+
const path2 = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
|
|
1205
|
+
const response = await http.request(
|
|
1206
|
+
"DELETE",
|
|
1207
|
+
path2
|
|
1208
|
+
);
|
|
1209
|
+
const deletedKey = response && typeof response === "object" && "key" in response ? response.key : void 0;
|
|
1210
|
+
return { key: deletedKey ?? key };
|
|
1211
|
+
}
|
|
1212
|
+
close() {
|
|
1213
|
+
for (const h of Array.from(this.streams)) {
|
|
1214
|
+
try {
|
|
1215
|
+
h.cancel();
|
|
1216
|
+
} catch {
|
|
1217
|
+
} finally {
|
|
1218
|
+
this.streams.delete(h);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
/** -------- internal helpers used by builders -------- */
|
|
1223
|
+
async _count(table, select, partition) {
|
|
1224
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1225
|
+
const params = new URLSearchParams();
|
|
1226
|
+
const p = partition ?? this.defaultPartition;
|
|
1227
|
+
if (p) params.append("partition", p);
|
|
1228
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/query/count/${encodeURIComponent(
|
|
1229
|
+
table
|
|
1230
|
+
)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1231
|
+
return http.request("PUT", path2, serializeDates(select));
|
|
1232
|
+
}
|
|
1233
|
+
async _queryPage(table, select, opts = {}) {
|
|
1234
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1235
|
+
const params = new URLSearchParams();
|
|
1236
|
+
if (opts.pageSize != null) params.append("pageSize", String(opts.pageSize));
|
|
1237
|
+
if (opts.nextPage) params.append("nextPage", opts.nextPage);
|
|
1238
|
+
const p = opts.partition ?? this.defaultPartition;
|
|
1239
|
+
if (p) params.append("partition", p);
|
|
1240
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/query/${encodeURIComponent(
|
|
1241
|
+
table
|
|
1242
|
+
)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1243
|
+
return http.request("PUT", path2, serializeDates(select));
|
|
1244
|
+
}
|
|
1245
|
+
async _update(table, update, partition) {
|
|
1246
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1247
|
+
const params = new URLSearchParams();
|
|
1248
|
+
const p = partition ?? this.defaultPartition;
|
|
1249
|
+
if (p) params.append("partition", p);
|
|
1250
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/query/update/${encodeURIComponent(
|
|
1251
|
+
table
|
|
1252
|
+
)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1253
|
+
return http.request("PUT", path2, serializeDates(update));
|
|
1254
|
+
}
|
|
1255
|
+
async _deleteByQuery(table, select, partition) {
|
|
1256
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1257
|
+
const params = new URLSearchParams();
|
|
1258
|
+
const p = partition ?? this.defaultPartition;
|
|
1259
|
+
if (p) params.append("partition", p);
|
|
1260
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/query/delete/${encodeURIComponent(
|
|
1261
|
+
table
|
|
1262
|
+
)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1263
|
+
return http.request("PUT", path2, serializeDates(select));
|
|
1264
|
+
}
|
|
1265
|
+
async _stream(table, select, includeQueryResults, keepAlive, handlers) {
|
|
1266
|
+
const { http, baseUrl, databaseId, fetchImpl } = await this.ensureClient();
|
|
1267
|
+
const params = new URLSearchParams();
|
|
1268
|
+
if (includeQueryResults) params.append("includeQueryResults", "true");
|
|
1269
|
+
if (keepAlive) params.append("keepAlive", "true");
|
|
1270
|
+
const url = `${baseUrl}/data/${encodeURIComponent(databaseId)}/query/stream/${encodeURIComponent(
|
|
1271
|
+
table
|
|
1272
|
+
)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1273
|
+
const handle = await openJsonLinesStream(
|
|
1274
|
+
fetchImpl,
|
|
1275
|
+
url,
|
|
1276
|
+
{
|
|
1277
|
+
method: "PUT",
|
|
1278
|
+
headers: http.headers({
|
|
1279
|
+
Accept: "application/x-ndjson",
|
|
1280
|
+
"Content-Type": "application/json"
|
|
1281
|
+
}),
|
|
1282
|
+
body: JSON.stringify(serializeDates(select))
|
|
1283
|
+
},
|
|
1284
|
+
handlers
|
|
1285
|
+
);
|
|
1286
|
+
return this.registerStream(handle);
|
|
1287
|
+
}
|
|
1288
|
+
async _saveInternal(table, entityOrEntities, options) {
|
|
1289
|
+
const { http, databaseId } = await this.ensureClient();
|
|
1290
|
+
const params = new URLSearchParams();
|
|
1291
|
+
if (options?.relationships?.length) {
|
|
1292
|
+
params.append("relationships", options.relationships.map(encodeURIComponent).join(","));
|
|
1293
|
+
}
|
|
1294
|
+
const path2 = `/data/${encodeURIComponent(databaseId)}/${encodeURIComponent(table)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1295
|
+
return http.request("PUT", path2, serializeDates(entityOrEntities));
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
var QueryBuilderImpl = class {
|
|
1299
|
+
db;
|
|
1300
|
+
table;
|
|
1301
|
+
fields = null;
|
|
1302
|
+
resolvers = null;
|
|
1303
|
+
conditions = null;
|
|
1304
|
+
sort = null;
|
|
1305
|
+
limitValue = null;
|
|
1306
|
+
distinctValue = false;
|
|
1307
|
+
groupByValues = null;
|
|
1308
|
+
partitionValue;
|
|
1309
|
+
pageSizeValue = null;
|
|
1310
|
+
nextPageValue = null;
|
|
1311
|
+
mode = "select";
|
|
1312
|
+
updates = null;
|
|
1313
|
+
onItemAddedListener = null;
|
|
1314
|
+
onItemUpdatedListener = null;
|
|
1315
|
+
onItemDeletedListener = null;
|
|
1316
|
+
onItemListener = null;
|
|
1317
|
+
constructor(db, table, partition) {
|
|
1318
|
+
this.db = db;
|
|
1319
|
+
this.table = table;
|
|
1320
|
+
this.partitionValue = partition;
|
|
1321
|
+
}
|
|
1322
|
+
ensureTable() {
|
|
1323
|
+
if (!this.table) throw new Error("Table is not defined. Call from(<table>) first.");
|
|
1324
|
+
return this.table;
|
|
1325
|
+
}
|
|
1326
|
+
toSelectQuery() {
|
|
1327
|
+
return {
|
|
1328
|
+
type: "SelectQuery",
|
|
1329
|
+
fields: this.fields,
|
|
1330
|
+
conditions: this.conditions,
|
|
1331
|
+
sort: this.sort,
|
|
1332
|
+
limit: this.limitValue,
|
|
1333
|
+
distinct: this.distinctValue,
|
|
1334
|
+
groupBy: this.groupByValues,
|
|
1335
|
+
partition: this.partitionValue ?? null,
|
|
1336
|
+
resolvers: this.resolvers
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
from(table) {
|
|
1340
|
+
this.table = table;
|
|
1341
|
+
return this;
|
|
1342
|
+
}
|
|
1343
|
+
select(...fields) {
|
|
1344
|
+
const flat = flattenStrings(fields);
|
|
1345
|
+
this.fields = flat.length > 0 ? flat : null;
|
|
1346
|
+
return this;
|
|
1347
|
+
}
|
|
1348
|
+
resolve(...values) {
|
|
1349
|
+
const flat = flattenStrings(values);
|
|
1350
|
+
this.resolvers = flat.length > 0 ? flat : null;
|
|
1351
|
+
return this;
|
|
1352
|
+
}
|
|
1353
|
+
where(condition) {
|
|
1354
|
+
const c = toCondition(condition);
|
|
1355
|
+
if (!this.conditions) {
|
|
1356
|
+
this.conditions = c;
|
|
1357
|
+
} else {
|
|
1358
|
+
this.conditions = {
|
|
1359
|
+
conditionType: "CompoundCondition",
|
|
1360
|
+
operator: "AND",
|
|
1361
|
+
conditions: [this.conditions, c]
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
return this;
|
|
1365
|
+
}
|
|
1366
|
+
and(condition) {
|
|
1367
|
+
const c = toCondition(condition);
|
|
1368
|
+
if (!this.conditions) {
|
|
1369
|
+
this.conditions = c;
|
|
1370
|
+
} else if (this.conditions.conditionType === "CompoundCondition" && this.conditions.operator === "AND") {
|
|
1371
|
+
this.conditions.conditions.push(c);
|
|
1372
|
+
} else {
|
|
1373
|
+
this.conditions = {
|
|
1374
|
+
conditionType: "CompoundCondition",
|
|
1375
|
+
operator: "AND",
|
|
1376
|
+
conditions: [this.conditions, c]
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
return this;
|
|
1380
|
+
}
|
|
1381
|
+
or(condition) {
|
|
1382
|
+
const c = toCondition(condition);
|
|
1383
|
+
if (!this.conditions) {
|
|
1384
|
+
this.conditions = c;
|
|
1385
|
+
} else if (this.conditions.conditionType === "CompoundCondition" && this.conditions.operator === "OR") {
|
|
1386
|
+
this.conditions.conditions.push(c);
|
|
1387
|
+
} else {
|
|
1388
|
+
this.conditions = {
|
|
1389
|
+
conditionType: "CompoundCondition",
|
|
1390
|
+
operator: "OR",
|
|
1391
|
+
conditions: [this.conditions, c]
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
return this;
|
|
1395
|
+
}
|
|
1396
|
+
orderBy(...sorts) {
|
|
1397
|
+
this.sort = sorts;
|
|
1398
|
+
return this;
|
|
1399
|
+
}
|
|
1400
|
+
groupBy(...fields) {
|
|
1401
|
+
this.groupByValues = fields.length ? fields : null;
|
|
1402
|
+
return this;
|
|
1403
|
+
}
|
|
1404
|
+
distinct() {
|
|
1405
|
+
this.distinctValue = true;
|
|
1406
|
+
return this;
|
|
1407
|
+
}
|
|
1408
|
+
limit(n) {
|
|
1409
|
+
this.limitValue = n;
|
|
1410
|
+
return this;
|
|
1411
|
+
}
|
|
1412
|
+
inPartition(partition) {
|
|
1413
|
+
this.partitionValue = partition;
|
|
1414
|
+
return this;
|
|
1415
|
+
}
|
|
1416
|
+
pageSize(n) {
|
|
1417
|
+
this.pageSizeValue = n;
|
|
1418
|
+
return this;
|
|
1419
|
+
}
|
|
1420
|
+
nextPage(token) {
|
|
1421
|
+
this.nextPageValue = token;
|
|
1422
|
+
return this;
|
|
1423
|
+
}
|
|
1424
|
+
setUpdates(updates) {
|
|
1425
|
+
this.mode = "update";
|
|
1426
|
+
this.updates = updates;
|
|
1427
|
+
return this;
|
|
1428
|
+
}
|
|
1429
|
+
async count() {
|
|
1430
|
+
if (this.mode !== "select") throw new Error("Cannot call count() in update mode.");
|
|
1431
|
+
const table = this.ensureTable();
|
|
1432
|
+
return this.db._count(table, this.toSelectQuery(), this.partitionValue);
|
|
1433
|
+
}
|
|
1434
|
+
async page(options = {}) {
|
|
1435
|
+
if (this.mode !== "select") throw new Error("Cannot call page() in update mode.");
|
|
1436
|
+
const table = this.ensureTable();
|
|
1437
|
+
const final = {
|
|
1438
|
+
pageSize: this.pageSizeValue ?? options.pageSize,
|
|
1439
|
+
nextPage: this.nextPageValue ?? options.nextPage,
|
|
1440
|
+
partition: this.partitionValue
|
|
1441
|
+
};
|
|
1442
|
+
return this.db._queryPage(table, this.toSelectQuery(), final);
|
|
1443
|
+
}
|
|
1444
|
+
list(options = {}) {
|
|
1445
|
+
const size = this.pageSizeValue ?? options.pageSize;
|
|
1446
|
+
const pgPromise = this.page(options).then((pg) => {
|
|
1447
|
+
const fetcher = (token) => this.nextPage(token).list({ pageSize: size });
|
|
1448
|
+
return new QueryResults(Array.isArray(pg.records) ? pg.records : [], pg.nextPage ?? null, fetcher);
|
|
1449
|
+
});
|
|
1450
|
+
for (const m of Object.getOwnPropertyNames(QueryResults.prototype)) {
|
|
1451
|
+
if (m === "constructor") continue;
|
|
1452
|
+
pgPromise[m] = (...args) => pgPromise.then((res) => res[m](...args));
|
|
1453
|
+
}
|
|
1454
|
+
return pgPromise;
|
|
1455
|
+
}
|
|
1456
|
+
async firstOrNull() {
|
|
1457
|
+
if (this.mode !== "select") throw new Error("Cannot call firstOrNull() in update mode.");
|
|
1458
|
+
if (!this.conditions) throw new OnyxError("firstOrNull() requires a where() clause.");
|
|
1459
|
+
this.limitValue = 1;
|
|
1460
|
+
const pg = await this.page();
|
|
1461
|
+
return Array.isArray(pg.records) && pg.records.length > 0 ? pg.records[0] : null;
|
|
1462
|
+
}
|
|
1463
|
+
async one() {
|
|
1464
|
+
return this.firstOrNull();
|
|
1465
|
+
}
|
|
1466
|
+
async delete() {
|
|
1467
|
+
if (this.mode !== "select") throw new Error("delete() is only applicable in select mode.");
|
|
1468
|
+
const table = this.ensureTable();
|
|
1469
|
+
return this.db._deleteByQuery(table, this.toSelectQuery(), this.partitionValue);
|
|
1470
|
+
}
|
|
1471
|
+
async update() {
|
|
1472
|
+
if (this.mode !== "update") throw new Error("Call setUpdates(...) before update().");
|
|
1473
|
+
const table = this.ensureTable();
|
|
1474
|
+
const update = {
|
|
1475
|
+
type: "UpdateQuery",
|
|
1476
|
+
conditions: this.conditions,
|
|
1477
|
+
updates: this.updates ?? {},
|
|
1478
|
+
sort: this.sort,
|
|
1479
|
+
limit: this.limitValue,
|
|
1480
|
+
partition: this.partitionValue ?? null
|
|
1481
|
+
};
|
|
1482
|
+
return this.db._update(table, update, this.partitionValue);
|
|
1483
|
+
}
|
|
1484
|
+
onItemAdded(listener) {
|
|
1485
|
+
this.onItemAddedListener = listener;
|
|
1486
|
+
return this;
|
|
1487
|
+
}
|
|
1488
|
+
onItemUpdated(listener) {
|
|
1489
|
+
this.onItemUpdatedListener = listener;
|
|
1490
|
+
return this;
|
|
1491
|
+
}
|
|
1492
|
+
onItemDeleted(listener) {
|
|
1493
|
+
this.onItemDeletedListener = listener;
|
|
1494
|
+
return this;
|
|
1495
|
+
}
|
|
1496
|
+
onItem(listener) {
|
|
1497
|
+
this.onItemListener = listener;
|
|
1498
|
+
return this;
|
|
1499
|
+
}
|
|
1500
|
+
async streamEventsOnly(keepAlive = true) {
|
|
1501
|
+
return this.stream(false, keepAlive);
|
|
1502
|
+
}
|
|
1503
|
+
async streamWithQueryResults(keepAlive = false) {
|
|
1504
|
+
return this.stream(true, keepAlive);
|
|
1505
|
+
}
|
|
1506
|
+
async stream(includeQueryResults = true, keepAlive = false) {
|
|
1507
|
+
if (this.mode !== "select") throw new Error("Streaming is only applicable in select mode.");
|
|
1508
|
+
const table = this.ensureTable();
|
|
1509
|
+
return this.db._stream(table, this.toSelectQuery(), includeQueryResults, keepAlive, {
|
|
1510
|
+
onItemAdded: this.onItemAddedListener ?? void 0,
|
|
1511
|
+
onItemUpdated: this.onItemUpdatedListener ?? void 0,
|
|
1512
|
+
onItemDeleted: this.onItemDeletedListener ?? void 0,
|
|
1513
|
+
onItem: this.onItemListener ?? void 0
|
|
1514
|
+
});
|
|
1515
|
+
}
|
|
1516
|
+
};
|
|
1517
|
+
var SaveBuilderImpl = class {
|
|
1518
|
+
db;
|
|
1519
|
+
table;
|
|
1520
|
+
relationships = null;
|
|
1521
|
+
constructor(db, table) {
|
|
1522
|
+
this.db = db;
|
|
1523
|
+
this.table = table;
|
|
1524
|
+
}
|
|
1525
|
+
cascade(...relationships) {
|
|
1526
|
+
this.relationships = relationships.flat();
|
|
1527
|
+
return this;
|
|
1528
|
+
}
|
|
1529
|
+
one(entity) {
|
|
1530
|
+
const opts = this.relationships ? { relationships: this.relationships } : void 0;
|
|
1531
|
+
return this.db._saveInternal(this.table, entity, opts);
|
|
1532
|
+
}
|
|
1533
|
+
many(entities) {
|
|
1534
|
+
const opts = this.relationships ? { relationships: this.relationships } : void 0;
|
|
1535
|
+
return this.db._saveInternal(this.table, entities, opts);
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
var CascadeBuilderImpl = class {
|
|
1539
|
+
db;
|
|
1540
|
+
rels = null;
|
|
1541
|
+
constructor(db) {
|
|
1542
|
+
this.db = db;
|
|
1543
|
+
}
|
|
1544
|
+
cascade(...relationships) {
|
|
1545
|
+
this.rels = relationships.flat();
|
|
1546
|
+
return this;
|
|
1547
|
+
}
|
|
1548
|
+
save(table, entityOrEntities) {
|
|
1549
|
+
const opts = this.rels ? { relationships: this.rels } : void 0;
|
|
1550
|
+
return this.db._saveInternal(String(table), entityOrEntities, opts);
|
|
1551
|
+
}
|
|
1552
|
+
delete(table, primaryKey) {
|
|
1553
|
+
const opts = this.rels ? { relationships: this.rels } : void 0;
|
|
1554
|
+
return this.db.delete(table, primaryKey, opts);
|
|
1555
|
+
}
|
|
1556
|
+
};
|
|
1557
|
+
var onyx = {
|
|
1558
|
+
init(config) {
|
|
1559
|
+
return new OnyxDatabaseImpl(config);
|
|
1560
|
+
},
|
|
1561
|
+
clearCacheConfig
|
|
1562
|
+
};
|
|
1563
|
+
|
|
1564
|
+
// schema/cli/schema.ts
|
|
1565
|
+
var DEFAULT_SCHEMA_PATH = "./onyx.schema.json";
|
|
1566
|
+
function printHelp() {
|
|
1567
|
+
process__default.default.stdout.write(`onyx-schema \u2014 Manage Onyx database schemas via API
|
|
1568
|
+
|
|
1569
|
+
Usage:
|
|
1570
|
+
onyx-schema publish [file]
|
|
1571
|
+
onyx-schema get [file] [--tables tableA,tableB]
|
|
1572
|
+
onyx-schema validate [file]
|
|
1573
|
+
|
|
1574
|
+
Options:
|
|
1575
|
+
[file] Path to schema JSON (default: ./onyx.schema.json)
|
|
1576
|
+
--tables <list> Comma-separated list of tables to fetch (for get)
|
|
1577
|
+
-h, --help Show this help message
|
|
1578
|
+
`);
|
|
1579
|
+
}
|
|
1580
|
+
function parseTables(value) {
|
|
1581
|
+
if (!value) return void 0;
|
|
1582
|
+
const items = value.split(",").map((t) => t.trim()).filter(Boolean);
|
|
1583
|
+
return items.length ? items : void 0;
|
|
1584
|
+
}
|
|
1585
|
+
function parseArgs(argv) {
|
|
1586
|
+
const cmd = (argv[2] ?? "").toLowerCase();
|
|
1587
|
+
let command = "help";
|
|
1588
|
+
if (cmd === "publish" || cmd === "get" || cmd === "validate") {
|
|
1589
|
+
command = cmd;
|
|
1590
|
+
}
|
|
1591
|
+
let idx = 3;
|
|
1592
|
+
let filePath = DEFAULT_SCHEMA_PATH;
|
|
1593
|
+
if (argv[idx] && !argv[idx].startsWith("-")) {
|
|
1594
|
+
filePath = argv[idx];
|
|
1595
|
+
idx++;
|
|
1596
|
+
}
|
|
1597
|
+
let tables;
|
|
1598
|
+
for (; idx < argv.length; idx++) {
|
|
1599
|
+
const arg = argv[idx];
|
|
1600
|
+
switch (arg) {
|
|
1601
|
+
case "--tables":
|
|
1602
|
+
tables = parseTables(argv[idx + 1]);
|
|
1603
|
+
idx++;
|
|
1604
|
+
break;
|
|
1605
|
+
case "-h":
|
|
1606
|
+
case "--help":
|
|
1607
|
+
command = "help";
|
|
1608
|
+
break;
|
|
1609
|
+
default: {
|
|
1610
|
+
if (arg.startsWith("--tables=")) {
|
|
1611
|
+
tables = parseTables(arg.slice("--tables=".length));
|
|
1612
|
+
break;
|
|
1613
|
+
}
|
|
1614
|
+
if (arg.startsWith("-")) throw new Error(`Unknown option: ${arg}`);
|
|
1615
|
+
break;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
return { command, filePath, tables };
|
|
1620
|
+
}
|
|
1621
|
+
async function readFileJson(filePath) {
|
|
1622
|
+
const fs = await import('fs/promises');
|
|
1623
|
+
const content = await fs.readFile(filePath, "utf8");
|
|
1624
|
+
return JSON.parse(content);
|
|
1625
|
+
}
|
|
1626
|
+
async function writeFileJson(filePath, data) {
|
|
1627
|
+
const fs = await import('fs/promises');
|
|
1628
|
+
const resolved = path__default.default.resolve(filePath);
|
|
1629
|
+
await fs.mkdir(path__default.default.dirname(resolved), { recursive: true });
|
|
1630
|
+
const serialized = `${JSON.stringify(data, null, 2)}
|
|
1631
|
+
`;
|
|
1632
|
+
await fs.writeFile(resolved, serialized, "utf8");
|
|
1633
|
+
}
|
|
1634
|
+
async function fetchSchema(filePath, tables) {
|
|
1635
|
+
const db = onyx.init();
|
|
1636
|
+
const schema = await db.getSchema({ tables });
|
|
1637
|
+
if (tables?.length) {
|
|
1638
|
+
process__default.default.stdout.write(`${JSON.stringify(schema, null, 2)}
|
|
1639
|
+
`);
|
|
1640
|
+
return;
|
|
1641
|
+
}
|
|
1642
|
+
await writeFileJson(filePath, schema);
|
|
1643
|
+
process__default.default.stdout.write(`Schema written to ${filePath}.
|
|
1644
|
+
`);
|
|
1645
|
+
}
|
|
1646
|
+
function formatSchemaErrors(errors) {
|
|
1647
|
+
if (!errors?.length) return "Unknown validation error";
|
|
1648
|
+
return errors.map((err) => `- ${err.message}`).join("\n");
|
|
1649
|
+
}
|
|
1650
|
+
async function validateSchema(filePath) {
|
|
1651
|
+
const db = onyx.init();
|
|
1652
|
+
const schema = await readFileJson(filePath);
|
|
1653
|
+
const result = await db.validateSchema(schema);
|
|
1654
|
+
if (!result.valid) {
|
|
1655
|
+
throw new Error(`Schema validation failed:
|
|
1656
|
+
${formatSchemaErrors(result.errors)}`);
|
|
1657
|
+
}
|
|
1658
|
+
process__default.default.stdout.write(`Schema at ${filePath} is valid.
|
|
1659
|
+
`);
|
|
1660
|
+
}
|
|
1661
|
+
async function publishSchema(filePath) {
|
|
1662
|
+
const db = onyx.init();
|
|
1663
|
+
const schema = await readFileJson(filePath);
|
|
1664
|
+
const result = await db.validateSchema(schema);
|
|
1665
|
+
if (!result.valid) {
|
|
1666
|
+
throw new Error(`Schema validation failed:
|
|
1667
|
+
${formatSchemaErrors(result.errors)}`);
|
|
1668
|
+
}
|
|
1669
|
+
const revision = await db.updateSchema(schema, { publish: true });
|
|
1670
|
+
process__default.default.stdout.write(`Schema published for database ${revision.databaseId} from ${filePath}.
|
|
1671
|
+
`);
|
|
1672
|
+
}
|
|
1673
|
+
(async () => {
|
|
1674
|
+
try {
|
|
1675
|
+
const parsed = parseArgs(process__default.default.argv);
|
|
1676
|
+
switch (parsed.command) {
|
|
1677
|
+
case "publish":
|
|
1678
|
+
await publishSchema(parsed.filePath);
|
|
1679
|
+
break;
|
|
1680
|
+
case "get":
|
|
1681
|
+
await fetchSchema(parsed.filePath, parsed.tables);
|
|
1682
|
+
break;
|
|
1683
|
+
case "validate":
|
|
1684
|
+
await validateSchema(parsed.filePath);
|
|
1685
|
+
break;
|
|
1686
|
+
default:
|
|
1687
|
+
printHelp();
|
|
1688
|
+
return;
|
|
1689
|
+
}
|
|
1690
|
+
} catch (err) {
|
|
1691
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1692
|
+
process__default.default.stderr.write(`onyx-schema: ${msg}
|
|
1693
|
+
`);
|
|
1694
|
+
process__default.default.exit(1);
|
|
1695
|
+
}
|
|
1696
|
+
})();
|
|
1697
|
+
//# sourceMappingURL=schema.cjs.map
|
|
1698
|
+
//# sourceMappingURL=schema.cjs.map
|