@rebasepro/studio 0.30.0 → 0.31.0-canary.g902a0ce
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ApiKeysView-Bqf6XYR1.js → ApiKeysView-9ZvwMPyh.js} +16 -6
- package/dist/ApiKeysView-9ZvwMPyh.js.map +1 -0
- package/dist/{JSEditor-DCnPXsx6.js → JSEditor-ojAVSkaU.js} +158 -100
- package/dist/JSEditor-ojAVSkaU.js.map +1 -0
- package/dist/{RLSEditor-zAat58U0.js → RLSEditor-BAwkENpj.js} +394 -206
- package/dist/RLSEditor-BAwkENpj.js.map +1 -0
- package/dist/{SQLEditor-DV_VEEpL.js → SQLEditor-DX9LfbU7.js} +10 -8
- package/dist/SQLEditor-DX9LfbU7.js.map +1 -0
- package/dist/{SchemaVisualizer-pCYQ_Ds6.js → SchemaVisualizer-C75TENPm.js} +196 -189
- package/dist/SchemaVisualizer-C75TENPm.js.map +1 -0
- package/dist/components/JSEditor/JSEditorSidebar.d.ts +3 -6
- package/dist/components/JSEditor/JSMonacoEditor.d.ts +10 -10
- package/dist/components/JSEditor/table_access.d.ts +39 -0
- package/dist/components/RLSEditor/policy-presets.d.ts +1 -1
- package/dist/components/RLSEditor/policyListing.d.ts +32 -12
- package/dist/components/RLSEditor/policyRules.d.ts +32 -14
- package/dist/components/RLSEditor/saveRules.d.ts +48 -18
- package/dist/components/RLSEditor/saveSecurityRules.d.ts +5 -4
- package/dist/components/SchemaVisualizer/useSchemaGraph.d.ts +13 -8
- package/dist/components/schema-source.d.ts +37 -0
- package/dist/index.es.js +9 -9
- package/dist/index.es.js.map +1 -1
- package/dist/schema-source-C4ekB7WU.js +93 -0
- package/dist/schema-source-C4ekB7WU.js.map +1 -0
- package/dist/{sql_utils-DXvmxiCq.js → sql_utils-CYcgpgam.js} +14 -12
- package/dist/sql_utils-CYcgpgam.js.map +1 -0
- package/dist/utils/sql_utils.d.ts +14 -10
- package/package.json +9 -9
- package/dist/ApiKeysView-Bqf6XYR1.js.map +0 -1
- package/dist/JSEditor-DCnPXsx6.js.map +0 -1
- package/dist/RLSEditor-zAat58U0.js.map +0 -1
- package/dist/SQLEditor-DV_VEEpL.js.map +0 -1
- package/dist/SchemaVisualizer-pCYQ_Ds6.js.map +0 -1
- package/dist/sql_utils-DXvmxiCq.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { n as AuthSimulationSelector, t as useRunAsUsers } from "./useRunAsUsers-CLaAtXt6.js";
|
|
2
|
-
import { p as toCsv } from "./sql_utils-
|
|
3
|
-
import { ErrorView, IconForView, useApiConfig, useModeController, useRebaseClient, useRebaseContext, useSnackbarController, useStudioCollectionRegistry, useStudioSidePanelController, useTranslation } from "@rebasepro/app";
|
|
2
|
+
import { p as toCsv } from "./sql_utils-CYcgpgam.js";
|
|
3
|
+
import { ErrorView, IconForView, useApiConfig, useModeController, useRebaseClient, useRebaseContext, useSnackbarController, useStudioCollectionRegistry, useStudioSidePanelController, useTableDescriptors, useTranslation } from "@rebasepro/app";
|
|
4
4
|
import React, { useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
5
5
|
import { Button, Chip, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, DownloadIcon, IconButton, Menu, MenuItem, MoreVerticalIcon, PencilIcon, PlayIcon, PlusIcon, ResizablePanels, SaveIcon, Tab, Tabs, TerminalIcon, TextField, Tooltip, Trash2Icon, Typography, VirtualTable, XIcon, cls, defaultBorderMixin, iconSize } from "@rebasepro/ui";
|
|
6
6
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -10,6 +10,23 @@ import { Highlight, themes } from "prism-react-renderer";
|
|
|
10
10
|
import { createMemoryStorage, createRebaseClient } from "@rebasepro/client";
|
|
11
11
|
import Editor from "@monaco-editor/react";
|
|
12
12
|
//#region src/components/JSEditor/JSMonacoEditor.tsx
|
|
13
|
+
var NO_TABLES = [];
|
|
14
|
+
/**
|
|
15
|
+
* Ambient typings for the served tables: a row type per table from its column
|
|
16
|
+
* keys, and `client.data.<accessor>` for each table one property reaches
|
|
17
|
+
* (the rest are reached with `client.data.table("<slug>")`).
|
|
18
|
+
*/
|
|
19
|
+
function tableTypings(tables) {
|
|
20
|
+
const lines = [];
|
|
21
|
+
for (const table of tables) {
|
|
22
|
+
const rowName = `${table.slug.replace(/[^a-zA-Z0-9_]/g, "_")}_Row`;
|
|
23
|
+
const rowType = table.columns.length > 0 ? `{ ${table.columns.map((key) => `${JSON.stringify(key)}: any`).join("; ")} }` : "Record<string, any>";
|
|
24
|
+
lines.push(`/** Table: ${table.name} (${table.slug}) */`);
|
|
25
|
+
lines.push(`interface ${rowName} ${rowType}`);
|
|
26
|
+
if (table.accessor) lines.push(`declare namespace client.data { const ${table.accessor}: TableClient<${rowName}>; }`);
|
|
27
|
+
}
|
|
28
|
+
return lines.join("\n");
|
|
29
|
+
}
|
|
13
30
|
/**
|
|
14
31
|
* Ambient type definitions for the Rebase client SDK injected into Monaco.
|
|
15
32
|
*
|
|
@@ -71,7 +88,7 @@ interface QueryBuilder<M extends Record<string, any> = any> {
|
|
|
71
88
|
count(): Promise<number>;
|
|
72
89
|
}
|
|
73
90
|
|
|
74
|
-
interface
|
|
91
|
+
interface TableClient<M extends Record<string, any> = any> {
|
|
75
92
|
find(params?: FindParams): Promise<FindResult<M>>;
|
|
76
93
|
findById(id: string | number): Promise<M | undefined>;
|
|
77
94
|
create(data: Partial<M>, id?: string | number): Promise<M>;
|
|
@@ -196,11 +213,11 @@ interface StorageSource {
|
|
|
196
213
|
}
|
|
197
214
|
|
|
198
215
|
type RebaseData = {
|
|
199
|
-
/** Look up a
|
|
200
|
-
|
|
216
|
+
/** Look up a table by slug. */
|
|
217
|
+
table(slug: string): TableClient;
|
|
201
218
|
} & {
|
|
202
|
-
/** Dynamic
|
|
203
|
-
[
|
|
219
|
+
/** Dynamic table access — e.g. client.data.authors */
|
|
220
|
+
[tableSlug: string]: TableClient;
|
|
204
221
|
};
|
|
205
222
|
|
|
206
223
|
/**
|
|
@@ -208,7 +225,7 @@ type RebaseData = {
|
|
|
208
225
|
* \`client.storage\`, and \`client.call()\` to interact with your Rebase backend.
|
|
209
226
|
*
|
|
210
227
|
* @example
|
|
211
|
-
* // Query a
|
|
228
|
+
* // Query a table
|
|
212
229
|
* const result = await client.data.products.find({ limit: 10 });
|
|
213
230
|
*
|
|
214
231
|
* // Create a record
|
|
@@ -227,7 +244,7 @@ type RebaseData = {
|
|
|
227
244
|
* const result = await client.call("/my-endpoint", { myData: 123 });
|
|
228
245
|
*/
|
|
229
246
|
interface RebaseClient {
|
|
230
|
-
/** Data access — dynamic
|
|
247
|
+
/** Data access — dynamic table accessors */
|
|
231
248
|
data: RebaseData;
|
|
232
249
|
/** Authentication methods */
|
|
233
250
|
auth: RebaseAuth;
|
|
@@ -237,8 +254,8 @@ interface RebaseClient {
|
|
|
237
254
|
storage?: StorageSource;
|
|
238
255
|
/** Call a custom server-side endpoint */
|
|
239
256
|
call<T = unknown>(endpoint: string, payload?: unknown): Promise<T>;
|
|
240
|
-
/** Direct
|
|
241
|
-
[
|
|
257
|
+
/** Direct table access (shorthand) */
|
|
258
|
+
[tableSlug: string]: unknown;
|
|
242
259
|
}
|
|
243
260
|
|
|
244
261
|
/**
|
|
@@ -248,7 +265,7 @@ interface RebaseClient {
|
|
|
248
265
|
*/
|
|
249
266
|
declare const client: RebaseClient;
|
|
250
267
|
|
|
251
|
-
/** Execution context with user and
|
|
268
|
+
/** Execution context with user and table information. */
|
|
252
269
|
interface JSEditorContext {
|
|
253
270
|
/** The user the script runs as: the one picked in "Run as", or you. \`null\` under "No Auth". */
|
|
254
271
|
user: {
|
|
@@ -257,24 +274,29 @@ interface JSEditorContext {
|
|
|
257
274
|
email: string | null;
|
|
258
275
|
roles?: string[];
|
|
259
276
|
} | null;
|
|
260
|
-
/**
|
|
261
|
-
|
|
277
|
+
/** The tables the server serves, with their column keys. */
|
|
278
|
+
tables: Array<{
|
|
262
279
|
slug: string;
|
|
263
280
|
name: string;
|
|
264
|
-
|
|
281
|
+
/** The property that reaches it on \`client.data\`, when one does; else \`client.data.table(slug)\`. */
|
|
282
|
+
accessor?: string;
|
|
283
|
+
columns: string[];
|
|
284
|
+
primaryKey: string[];
|
|
265
285
|
}>;
|
|
266
286
|
}
|
|
267
287
|
|
|
268
|
-
/** Execution context — contains info about the selected user and
|
|
288
|
+
/** Execution context — contains info about the selected user and the tables. */
|
|
269
289
|
declare const context: JSEditorContext;
|
|
270
290
|
`;
|
|
271
|
-
var JSMonacoEditor = ({ ref, value, onChange, onRun, className, readOnly = false, autoFocus = true,
|
|
291
|
+
var JSMonacoEditor = ({ ref, value, onChange, onRun, className, readOnly = false, autoFocus = true, tables = NO_TABLES }) => {
|
|
272
292
|
const { mode } = useModeController();
|
|
273
293
|
const editorRef = useRef(null);
|
|
274
294
|
const monacoRef = useRef(null);
|
|
275
295
|
const onRunRef = useRef(onRun);
|
|
276
296
|
onRunRef.current = onRun;
|
|
277
297
|
const typesRegisteredRef = useRef(false);
|
|
298
|
+
/** Monaco, once the editor has mounted: the table typings wait for it. */
|
|
299
|
+
const [monacoApi, setMonacoApi] = useState(null);
|
|
278
300
|
useImperativeHandle(ref, () => ({
|
|
279
301
|
getSelectedText: () => {
|
|
280
302
|
const instance = editorRef.current;
|
|
@@ -316,23 +338,15 @@ var JSMonacoEditor = ({ ref, value, onChange, onRun, className, readOnly = false
|
|
|
316
338
|
});
|
|
317
339
|
ts.typescriptDefaults.setDiagnosticsOptions({ diagnosticCodesToIgnore: [1375, 1108] });
|
|
318
340
|
ts.typescriptDefaults.addExtraLib(REBASE_CLIENT_TYPES, "ts:rebase-client.d.ts");
|
|
319
|
-
|
|
320
|
-
const lines = [];
|
|
321
|
-
for (const col of collections) {
|
|
322
|
-
const ifaceName = col.slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
323
|
-
const propsType = col.properties.length > 0 ? `{ ${col.properties.map((p) => `${p}: any`).join("; ")} }` : "Record<string, any>";
|
|
324
|
-
lines.push(`/** Collection: ${col.name} (${col.slug}) */`);
|
|
325
|
-
lines.push(`interface ${ifaceName}_Row ${propsType}`);
|
|
326
|
-
lines.push(`declare namespace client.data { const ${col.slug}: CollectionClient<${ifaceName}_Row>; }`);
|
|
327
|
-
}
|
|
328
|
-
ts.typescriptDefaults.addExtraLib(lines.join("\n"), "ts:rebase-collections.d.ts");
|
|
329
|
-
} else if (collectionSlugs.length > 0) {
|
|
330
|
-
const collectionHints = collectionSlugs.map((slug) => `/** Collection: ${slug} */\ndeclare namespace client.data { const ${slug}: CollectionClient; }`).join("\n");
|
|
331
|
-
ts.typescriptDefaults.addExtraLib(collectionHints, "ts:rebase-collections.d.ts");
|
|
332
|
-
}
|
|
341
|
+
setMonacoApi(ts);
|
|
333
342
|
}
|
|
334
343
|
if (autoFocus) editorInstance.focus();
|
|
335
344
|
};
|
|
345
|
+
useEffect(() => {
|
|
346
|
+
if (!monacoApi || tables.length === 0) return;
|
|
347
|
+
const lib = monacoApi.typescriptDefaults.addExtraLib(tableTypings(tables), "ts:rebase-tables.d.ts");
|
|
348
|
+
return () => lib.dispose();
|
|
349
|
+
}, [monacoApi, tables]);
|
|
336
350
|
return /* @__PURE__ */ jsx("div", {
|
|
337
351
|
className: cls("relative w-full h-full overflow-hidden", className),
|
|
338
352
|
children: /* @__PURE__ */ jsx(Editor, {
|
|
@@ -360,36 +374,88 @@ var JSMonacoEditor = ({ ref, value, onChange, onRun, className, readOnly = false
|
|
|
360
374
|
});
|
|
361
375
|
};
|
|
362
376
|
//#endregion
|
|
377
|
+
//#region src/components/JSEditor/table_access.ts
|
|
378
|
+
/**
|
|
379
|
+
* Names `client.data` answers itself, or that every object has, rather than as
|
|
380
|
+
* a table: such a table is reached with `data.table("<slug>")`.
|
|
381
|
+
*/
|
|
382
|
+
var RESERVED = /* @__PURE__ */ new Set([
|
|
383
|
+
"table",
|
|
384
|
+
"batch",
|
|
385
|
+
"then",
|
|
386
|
+
"toJSON",
|
|
387
|
+
"$$typeof"
|
|
388
|
+
]);
|
|
389
|
+
/**
|
|
390
|
+
* The property that reaches `slug` on the editor's `client.data`, or
|
|
391
|
+
* `undefined` when no property does.
|
|
392
|
+
*
|
|
393
|
+
* The editor's clients carry no generated table dictionary, so a property is
|
|
394
|
+
* read the untyped way: its snake_case is the slug (`companyMembers` →
|
|
395
|
+
* `company_members`). A slug that is its own snake_case identifier (`posts`,
|
|
396
|
+
* `order_items`) is reached as itself; any other slug (`blog-posts`,
|
|
397
|
+
* `blogPosts`) by no property at all.
|
|
398
|
+
*/
|
|
399
|
+
function dotAccessorOf(slug) {
|
|
400
|
+
if (!/^[A-Za-z_$][\w$]*$/.test(slug)) return void 0;
|
|
401
|
+
if (RESERVED.has(slug) || slug in Object.prototype) return void 0;
|
|
402
|
+
return toSnakeCase(slug) === slug ? slug : void 0;
|
|
403
|
+
}
|
|
404
|
+
/** How a script reaches `table`: `client.data.posts`, or `client.data.table("blog-posts")`. */
|
|
405
|
+
function tableAccessExpression(table) {
|
|
406
|
+
return table.accessor ? `client.data.${table.accessor}` : `client.data.table(${JSON.stringify(table.slug)})`;
|
|
407
|
+
}
|
|
408
|
+
/** The {@link TableInfo} of every table the server serves (views included: they are read the same way). */
|
|
409
|
+
function tableInfosOf(tables) {
|
|
410
|
+
return tables.map((table) => {
|
|
411
|
+
const accessor = dotAccessorOf(table.slug);
|
|
412
|
+
return {
|
|
413
|
+
slug: table.slug,
|
|
414
|
+
name: table.name,
|
|
415
|
+
...accessor ? { accessor } : {},
|
|
416
|
+
columns: table.columns.map((column) => column.key),
|
|
417
|
+
primaryKey: [...table.primaryKey]
|
|
418
|
+
};
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* The slug a `client.data.<property>` names, as the client resolves it: a
|
|
423
|
+
* table's own accessor, or else the property's snake_case.
|
|
424
|
+
*/
|
|
425
|
+
function slugOfProperty(property, tables) {
|
|
426
|
+
return tables.find((table) => table.accessor === property)?.slug ?? toSnakeCase(property);
|
|
427
|
+
}
|
|
428
|
+
//#endregion
|
|
363
429
|
//#region src/components/JSEditor/JSEditorSidebar.tsx
|
|
364
430
|
var QUICK_REFERENCE = [
|
|
365
431
|
{
|
|
366
432
|
label: "Find all",
|
|
367
|
-
code: "const result = await client.data.
|
|
433
|
+
code: "const result = await client.data.table(\"TABLE\").find({ limit: 20 });\nreturn result;",
|
|
368
434
|
description: "Fetch records with pagination"
|
|
369
435
|
},
|
|
370
436
|
{
|
|
371
437
|
label: "Find by ID",
|
|
372
|
-
code: "const item = await client.data.
|
|
438
|
+
code: "const item = await client.data.table(\"TABLE\").findById(\"ID\");\nreturn item;",
|
|
373
439
|
description: "Fetch a single record"
|
|
374
440
|
},
|
|
375
441
|
{
|
|
376
442
|
label: "Create",
|
|
377
|
-
code: "const created = await client.data.
|
|
443
|
+
code: "const created = await client.data.table(\"TABLE\").create({\n // your fields here\n});\nreturn created;",
|
|
378
444
|
description: "Insert a new record"
|
|
379
445
|
},
|
|
380
446
|
{
|
|
381
447
|
label: "Update",
|
|
382
|
-
code: "const updated = await client.data.
|
|
448
|
+
code: "const updated = await client.data.table(\"TABLE\").update(\"ID\", {\n // fields to update\n});\nreturn updated;",
|
|
383
449
|
description: "Update an existing record"
|
|
384
450
|
},
|
|
385
451
|
{
|
|
386
452
|
label: "Delete",
|
|
387
|
-
code: "await client.data.
|
|
453
|
+
code: "await client.data.table(\"TABLE\").delete(\"ID\");\nreturn { success: true };",
|
|
388
454
|
description: "Delete a record"
|
|
389
455
|
},
|
|
390
456
|
{
|
|
391
457
|
label: "Fluent query",
|
|
392
|
-
code: "const result = await client.data.
|
|
458
|
+
code: "const result = await client.data.table(\"TABLE\")\n .where(\"field\", \">\", value)\n .orderBy(\"field\", \"desc\")\n .limit(10)\n .find();\nreturn result;",
|
|
393
459
|
description: "Chained query builder"
|
|
394
460
|
},
|
|
395
461
|
{
|
|
@@ -413,7 +479,7 @@ var QUICK_REFERENCE = [
|
|
|
413
479
|
description: "Call a custom API endpoint"
|
|
414
480
|
}
|
|
415
481
|
];
|
|
416
|
-
var JSEditorSidebar = ({
|
|
482
|
+
var JSEditorSidebar = ({ tables, snippets, history, onSelectSnippet, onDeleteSnippet, onInsertCode }) => {
|
|
417
483
|
const { t } = useTranslation();
|
|
418
484
|
const [activeTab, setActiveTab] = useState("collections");
|
|
419
485
|
return /* @__PURE__ */ jsxs("div", {
|
|
@@ -427,10 +493,10 @@ var JSEditorSidebar = ({ collections, snippets, history, onSelectSnippet, onDele
|
|
|
427
493
|
/* @__PURE__ */ jsx(Tab, {
|
|
428
494
|
value: "collections",
|
|
429
495
|
children: /* @__PURE__ */ jsx(Tooltip, {
|
|
430
|
-
title: "Browse
|
|
496
|
+
title: "Browse tables",
|
|
431
497
|
children: /* @__PURE__ */ jsx("span", {
|
|
432
498
|
className: "text-xs",
|
|
433
|
-
children: t("
|
|
499
|
+
children: t("studio_js_tables_tab")
|
|
434
500
|
})
|
|
435
501
|
})
|
|
436
502
|
}),
|
|
@@ -469,21 +535,21 @@ var JSEditorSidebar = ({ collections, snippets, history, onSelectSnippet, onDele
|
|
|
469
535
|
children: /* @__PURE__ */ jsx(Typography, {
|
|
470
536
|
variant: "caption",
|
|
471
537
|
className: "font-semibold uppercase tracking-wider text-text-disabled dark:text-text-disabled-dark",
|
|
472
|
-
children: t("
|
|
538
|
+
children: t("studio_js_tables_tab")
|
|
473
539
|
})
|
|
474
540
|
}), /* @__PURE__ */ jsx("div", {
|
|
475
541
|
className: "flex-grow overflow-y-auto p-1 space-y-1 no-scrollbar",
|
|
476
|
-
children:
|
|
542
|
+
children: tables.length === 0 ? /* @__PURE__ */ jsx("div", {
|
|
477
543
|
className: "p-4 text-center",
|
|
478
544
|
children: /* @__PURE__ */ jsx(Typography, {
|
|
479
545
|
variant: "caption",
|
|
480
546
|
className: "text-text-disabled dark:text-text-disabled-dark",
|
|
481
|
-
children: "
|
|
547
|
+
children: t("studio_schema_no_tables")
|
|
482
548
|
})
|
|
483
|
-
}) :
|
|
484
|
-
collection:
|
|
549
|
+
}) : tables.map((table) => /* @__PURE__ */ jsx(CollectionItem, {
|
|
550
|
+
collection: table,
|
|
485
551
|
onInsertCode
|
|
486
|
-
},
|
|
552
|
+
}, table.slug))
|
|
487
553
|
})]
|
|
488
554
|
}),
|
|
489
555
|
activeTab === "reference" && /* @__PURE__ */ jsxs("div", {
|
|
@@ -662,15 +728,15 @@ function CollectionItem({ collection, onInsertCode }) {
|
|
|
662
728
|
className: "opacity-0 group-hover:opacity-100 text-text-disabled hover:text-primary transition-all",
|
|
663
729
|
onClick: (e) => {
|
|
664
730
|
e.stopPropagation();
|
|
665
|
-
onInsertCode(`const result = await
|
|
731
|
+
onInsertCode(`const result = await ${tableAccessExpression(collection)}.find({ limit: 20 });\nreturn result;`);
|
|
666
732
|
},
|
|
667
733
|
children: /* @__PURE__ */ jsx(PlusIcon, { size: iconSize.smallest })
|
|
668
734
|
})
|
|
669
735
|
})
|
|
670
736
|
]
|
|
671
|
-
}), expanded && collection.
|
|
737
|
+
}), expanded && collection.columns.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
672
738
|
className: cls("ml-5 mt-1 space-y-0.5 border-l", defaultBorderMixin),
|
|
673
|
-
children: collection.
|
|
739
|
+
children: collection.columns.map((prop) => /* @__PURE__ */ jsxs("div", {
|
|
674
740
|
className: "flex items-center p-1 pl-2 hover:bg-surface-hover rounded-r cursor-pointer transition-colors group/prop relative min-h-[28px]",
|
|
675
741
|
onClick: () => onInsertCode(`"${prop}"`),
|
|
676
742
|
children: [/* @__PURE__ */ jsx("svg", {
|
|
@@ -739,11 +805,11 @@ var makeDefaultCode = (slug) => `// Available: client (RebaseClient), context
|
|
|
739
805
|
// const { users, total } = await client.admin.listUsers({ limit: 50, offset: 0 }); // one page of total
|
|
740
806
|
// const me = context.user; // who this script runs as ("Run as"), null under No Auth
|
|
741
807
|
${slug ? `
|
|
742
|
-
const result = await client.data.
|
|
808
|
+
const result = await client.data.table(${JSON.stringify(slug)}).find({ limit: 10 });
|
|
743
809
|
return result;
|
|
744
810
|
` : `
|
|
745
811
|
// Name one of your collections to query it:
|
|
746
|
-
// const result = await client.data.
|
|
812
|
+
// const result = await client.data.table("posts").find({ limit: 10 });
|
|
747
813
|
// return result;
|
|
748
814
|
|
|
749
815
|
return context.user;
|
|
@@ -784,52 +850,46 @@ function formatJSON(value) {
|
|
|
784
850
|
return String(value);
|
|
785
851
|
}
|
|
786
852
|
}
|
|
787
|
-
/** The script without its comments: a
|
|
853
|
+
/** The script without its comments: a table named in one is not where rows came from. */
|
|
788
854
|
function withoutComments(code) {
|
|
789
855
|
return code.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/[^\n]*/g, "");
|
|
790
856
|
}
|
|
791
857
|
/**
|
|
792
|
-
* Given the raw SDK result, try to detect which
|
|
793
|
-
* JS SDK results typically come back as `{ data: [
|
|
858
|
+
* Given the raw SDK result, try to detect which table its rows belong to.
|
|
859
|
+
* JS SDK results typically come back as `{ data: [...] }` or plain arrays.
|
|
794
860
|
* The heuristic: the executed code, outside its comments, names exactly one
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
861
|
+
* table — `table("<slug>")` or `client.data.<accessor>` — the server declares
|
|
862
|
+
* it with a single-column primary key, the result has rows carrying that key,
|
|
863
|
+
* and the CMS on the page can open it (`collectionFor`). A script naming two
|
|
864
|
+
* could have returned either's rows, and an action on a row would open the
|
|
865
|
+
* other table's row with the same key, so it gets none.
|
|
799
866
|
*/
|
|
800
|
-
function detectCollectionsInResult(executedCode, resultValue,
|
|
801
|
-
if (!resultValue ||
|
|
867
|
+
function detectCollectionsInResult(executedCode, resultValue, tables, collectionFor) {
|
|
868
|
+
if (!resultValue || tables.length === 0) return [];
|
|
802
869
|
const code = withoutComments(executedCode);
|
|
803
870
|
const mentionedSlugs = /* @__PURE__ */ new Set();
|
|
804
|
-
const
|
|
871
|
+
const tableCallRegex = /\.table\(["']([^"']+)["']\)/g;
|
|
805
872
|
let m;
|
|
806
|
-
while ((m =
|
|
873
|
+
while ((m = tableCallRegex.exec(code)) !== null) mentionedSlugs.add(m[1]);
|
|
807
874
|
const dotAccessRegex = /client\.data\.([a-zA-Z_][a-zA-Z0-9_]*)\./g;
|
|
808
|
-
while ((m = dotAccessRegex.exec(code)) !== null) if (![
|
|
809
|
-
"collection",
|
|
810
|
-
"find",
|
|
811
|
-
"findById",
|
|
812
|
-
"create",
|
|
813
|
-
"update",
|
|
814
|
-
"delete"
|
|
815
|
-
].includes(m[1])) mentionedSlugs.add(m[1]);
|
|
875
|
+
while ((m = dotAccessRegex.exec(code)) !== null) if (!["table", "batch"].includes(m[1])) mentionedSlugs.add(slugOfProperty(m[1], tables));
|
|
816
876
|
if (mentionedSlugs.size !== 1) return [];
|
|
817
877
|
let rows = [];
|
|
818
878
|
const rv = resultValue;
|
|
819
879
|
if (rv?.data && Array.isArray(rv.data)) rows = rv.data;
|
|
820
880
|
else if (Array.isArray(resultValue)) rows = resultValue;
|
|
821
|
-
if (!(rows.length > 0 && rows.some((r) => r?.id != null))) return [];
|
|
822
881
|
const matched = [];
|
|
823
882
|
for (const slug of mentionedSlugs) {
|
|
824
|
-
const
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
if (
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
883
|
+
const table = tables.find((t) => t.slug === slug);
|
|
884
|
+
if (!table || table.primaryKey.length !== 1) continue;
|
|
885
|
+
const [pkColumn] = table.primaryKey;
|
|
886
|
+
if (!rows.some((r) => r?.[pkColumn] != null)) continue;
|
|
887
|
+
const collection = collectionFor(slug);
|
|
888
|
+
if (!collection) continue;
|
|
889
|
+
matched.push({
|
|
890
|
+
collectionSlug: slug,
|
|
891
|
+
collection,
|
|
892
|
+
pkColumn
|
|
833
893
|
});
|
|
834
894
|
}
|
|
835
895
|
return matched;
|
|
@@ -839,7 +899,9 @@ function JSEditor() {
|
|
|
839
899
|
const rebaseClient = useRebaseClient();
|
|
840
900
|
const apiConfig = useApiConfig();
|
|
841
901
|
const snackbar = useSnackbarController();
|
|
902
|
+
const descriptors = useTableDescriptors();
|
|
842
903
|
const collectionRegistry = useStudioCollectionRegistry();
|
|
904
|
+
const collectionFor = useCallback((slug) => collectionRegistry.getCollection(slug), [collectionRegistry]);
|
|
843
905
|
const sidePanelController = useStudioSidePanelController();
|
|
844
906
|
const { t } = useTranslation();
|
|
845
907
|
const currentUser = rebaseContext.authController?.user;
|
|
@@ -878,24 +940,17 @@ function JSEditor() {
|
|
|
878
940
|
}
|
|
879
941
|
});
|
|
880
942
|
const activeTab = tabs.find((t) => t.id === activeTabId) ?? tabs[0];
|
|
881
|
-
const
|
|
882
|
-
return (collectionRegistry?.collections ?? []).map((col) => ({
|
|
883
|
-
slug: col.slug,
|
|
884
|
-
name: col.name,
|
|
885
|
-
properties: Object.keys(col.properties ?? {})
|
|
886
|
-
}));
|
|
887
|
-
}, [collectionRegistry?.collections]);
|
|
888
|
-
const collectionSlugs = useMemo(() => collectionInfos.map((c) => c.slug), [collectionInfos]);
|
|
943
|
+
const tableInfos = useMemo(() => tableInfosOf(descriptors.tables), [descriptors.tables]);
|
|
889
944
|
/**
|
|
890
|
-
* Name a real
|
|
945
|
+
* Name a real table in the starter script once the server has served one.
|
|
891
946
|
*
|
|
892
|
-
* The
|
|
893
|
-
*
|
|
947
|
+
* The tables are not there on first paint — they arrive over the network —
|
|
948
|
+
* so the placeholder-free default cannot be built in the
|
|
894
949
|
* `useState` initialiser. Rewriting only a tab whose content is still
|
|
895
950
|
* *exactly* the untouched default means this can never overwrite anything
|
|
896
951
|
* anyone typed, and never fires twice.
|
|
897
952
|
*/
|
|
898
|
-
const firstSlug =
|
|
953
|
+
const firstSlug = tableInfos[0]?.slug;
|
|
899
954
|
useEffect(() => {
|
|
900
955
|
if (!firstSlug) return;
|
|
901
956
|
setTabs((prev) => {
|
|
@@ -1049,7 +1104,7 @@ function JSEditor() {
|
|
|
1049
1104
|
email: currentUser.email,
|
|
1050
1105
|
roles: currentUser.roles
|
|
1051
1106
|
} : null),
|
|
1052
|
-
|
|
1107
|
+
tables: tableInfos
|
|
1053
1108
|
};
|
|
1054
1109
|
const AsyncFunction = Object.getPrototypeOf(async function() {}).constructor;
|
|
1055
1110
|
const value = await new AsyncFunction("client", "context", code)(client, context);
|
|
@@ -1092,7 +1147,7 @@ function JSEditor() {
|
|
|
1092
1147
|
authMode,
|
|
1093
1148
|
selectedUser,
|
|
1094
1149
|
currentUser,
|
|
1095
|
-
|
|
1150
|
+
tableInfos
|
|
1096
1151
|
]);
|
|
1097
1152
|
const saveSnippet = useCallback(() => {
|
|
1098
1153
|
if (!snippetName.trim() || !activeTab?.code.trim()) return;
|
|
@@ -1159,8 +1214,12 @@ function JSEditor() {
|
|
|
1159
1214
|
}, [result]);
|
|
1160
1215
|
const matchedCollections = useMemo(() => {
|
|
1161
1216
|
if (!result?.value || result.error || !result.ranAsSignedInUser) return [];
|
|
1162
|
-
return detectCollectionsInResult(result.code, result.value,
|
|
1163
|
-
}, [
|
|
1217
|
+
return detectCollectionsInResult(result.code, result.value, tableInfos, collectionFor);
|
|
1218
|
+
}, [
|
|
1219
|
+
result,
|
|
1220
|
+
tableInfos,
|
|
1221
|
+
collectionFor
|
|
1222
|
+
]);
|
|
1164
1223
|
const getRowEntityActions = useCallback((rowData) => {
|
|
1165
1224
|
if (!rowData || matchedCollections.length === 0) return [];
|
|
1166
1225
|
return matchedCollections.filter((mc) => rowData[mc.pkColumn] != null).map((mc) => ({
|
|
@@ -1306,7 +1365,7 @@ function JSEditor() {
|
|
|
1306
1365
|
onPanelSizeChange: setSidebarSize,
|
|
1307
1366
|
minPanelSizePx: 200,
|
|
1308
1367
|
firstPanel: /* @__PURE__ */ jsx(JSEditorSidebar, {
|
|
1309
|
-
|
|
1368
|
+
tables: tableInfos,
|
|
1310
1369
|
snippets,
|
|
1311
1370
|
history,
|
|
1312
1371
|
onSelectSnippet: (code) => updateActiveCode(code),
|
|
@@ -1429,8 +1488,7 @@ function JSEditor() {
|
|
|
1429
1488
|
value: activeTab?.code ?? "",
|
|
1430
1489
|
onChange: updateActiveCode,
|
|
1431
1490
|
onRun: (selectedText) => executeCode(selectedText),
|
|
1432
|
-
|
|
1433
|
-
collections: collectionInfos,
|
|
1491
|
+
tables: tableInfos,
|
|
1434
1492
|
autoFocus: true
|
|
1435
1493
|
})
|
|
1436
1494
|
})]
|
|
@@ -1717,4 +1775,4 @@ var FormattedJSON = React.memo(function FormattedJSON({ value }) {
|
|
|
1717
1775
|
//#endregion
|
|
1718
1776
|
export { JSEditor };
|
|
1719
1777
|
|
|
1720
|
-
//# sourceMappingURL=JSEditor-
|
|
1778
|
+
//# sourceMappingURL=JSEditor-ojAVSkaU.js.map
|