@teable/app 6.0.0 → 6.1.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.
@@ -47,7 +47,10 @@ function schemaOf(target) {
47
47
  }
48
48
  function valuesOf(column, reference) {
49
49
  const { kind, currency, references } = describeColumn(column.name, column, reference, false);
50
- const keywords = { "x-teable-kind": kind };
50
+ const keywords = {
51
+ "x-teable-kind": kind,
52
+ "x-teable-column": { table: getTableName(column.table), column: column.name }
53
+ };
51
54
  if (currency !== void 0) keywords["x-teable-currency"] = currency;
52
55
  if (references !== void 0) keywords["x-teable-references"] = references;
53
56
  const meaning = currency === void 0 ? {} : { description: moneyMeaning(currency) };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  describeTables
3
- } from "./N7LBNIRZ.js";
3
+ } from "./FBIHOTDB.js";
4
4
  import {
5
5
  CAPABILITIES,
6
6
  PROTOCOL_VERSION,
@@ -807,6 +807,7 @@ function createApp(options) {
807
807
  output: def.output,
808
808
  access: def.access ?? "editor",
809
809
  description: def.description,
810
+ edits: def.edits,
810
811
  handler: def.handler
811
812
  };
812
813
  },
@@ -873,9 +874,11 @@ function defineApp(schema, migrations, methods, { capabilities, pages }) {
873
874
  throw new Error(`Table "${table.name}" uses the reserved prefix ${RESERVED_PREFIX}`);
874
875
  }
875
876
  }
877
+ const exported = new Map(Object.entries(schema).map(([key, value]) => [value, key]));
878
+ const tableOf = (table) => tables.find((t) => t.key === exported.get(table));
876
879
  const manifest = {
877
880
  protocol: PROTOCOL_VERSION,
878
- methods: Object.entries(methods).map(([name, m]) => describeMethod(name, m)),
881
+ methods: Object.entries(methods).map(([name, m]) => describeMethod(name, m, tableOf)),
879
882
  tables,
880
883
  capabilities: [...new Set(capabilities)],
881
884
  pages
@@ -1020,7 +1023,7 @@ function capabilitiesFrom(env) {
1020
1023
  CAPABILITIES.map((name) => [name, each(name)])
1021
1024
  );
1022
1025
  }
1023
- function describeMethod(name, method) {
1026
+ function describeMethod(name, method, tableOf) {
1024
1027
  const input = z2.toJSONSchema(method.input, {
1025
1028
  io: "input",
1026
1029
  unrepresentable: "any"
@@ -1028,6 +1031,13 @@ function describeMethod(name, method) {
1028
1031
  if (input.type !== "object") {
1029
1032
  throw new Error(`Method "${name}" input must be a z.object(...)`);
1030
1033
  }
1034
+ if (method.kind === "write" && method.edits !== void 0) {
1035
+ const table = tableOf(method.edits);
1036
+ if (!table) throw new Error(`Method "${name}" edits a table the app's schema does not export`);
1037
+ checkEdits(name, input, table);
1038
+ const edits = { "x-teable-edits": table.name };
1039
+ Object.assign(input, edits);
1040
+ }
1031
1041
  const info = { name, kind: method.kind, access: method.access, input };
1032
1042
  if (method.description !== void 0) info.description = method.description;
1033
1043
  if (method.kind !== "task" && method.output !== void 0) {
@@ -1041,6 +1051,36 @@ function describeMethod(name, method) {
1041
1051
  }
1042
1052
  return info;
1043
1053
  }
1054
+ var isSchema = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
1055
+ function checkEdits(name, input, table) {
1056
+ const refuse = (why) => new Error(`Method "${name}" edits ${table.name}: ${why}`);
1057
+ const properties = isSchema(input.properties) ? input.properties : {};
1058
+ const required = new Set(Array.isArray(input.required) ? input.required : []);
1059
+ const key = table.columns.filter((column) => column.primaryKey);
1060
+ if (key.length === 0) throw refuse("it has no primary key to say which row an edit is of");
1061
+ for (const column of key) {
1062
+ if (!required.has(column.key)) {
1063
+ throw refuse(`its input must require "${column.key}", the row's primary key`);
1064
+ }
1065
+ }
1066
+ let columns = 0;
1067
+ for (const [property, schema] of Object.entries(properties)) {
1068
+ const column = table.columns.find((c) => c.key === property);
1069
+ if (!column) {
1070
+ throw refuse(`"${property}" is not one of its columns, and an edit sends only columns`);
1071
+ }
1072
+ if (column.primaryKey) continue;
1073
+ if (column.generated) throw refuse(`"${property}" is computed by SQLite, so nothing sets it`);
1074
+ if (required.has(property)) {
1075
+ throw refuse(`"${property}" must be optional, since an edit sends only what changed`);
1076
+ }
1077
+ if (isSchema(schema) && "default" in schema) {
1078
+ throw refuse(`"${property}" must have no default, or an edit without it would set it`);
1079
+ }
1080
+ columns++;
1081
+ }
1082
+ if (columns === 0) throw refuse("its input takes none of its columns but the primary key");
1083
+ }
1044
1084
  function outputSchema(output) {
1045
1085
  return z2.toJSONSchema(output, {
1046
1086
  io: "output",
@@ -84,7 +84,9 @@ function createClient(options) {
84
84
  return () => {
85
85
  closed = true;
86
86
  if (timer) clearTimeout(timer);
87
- socket?.close();
87
+ const last = socket;
88
+ if (last?.readyState === 0) last.onopen = () => last.close();
89
+ else last?.close();
88
90
  };
89
91
  }
90
92
  return {
package/dist/client.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  MethodError,
3
3
  createClient
4
- } from "./chunks/L6RROFUY.js";
4
+ } from "./chunks/YGOAZOUE.js";
5
5
  import "./chunks/VMIIUPHE.js";
6
6
  export {
7
7
  MethodError,
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createApp,
3
3
  fail
4
- } from "./chunks/33OZ5YAY.js";
5
- import "./chunks/N7LBNIRZ.js";
4
+ } from "./chunks/SQSAV7KS.js";
5
+ import "./chunks/FBIHOTDB.js";
6
6
  import "./chunks/VMIIUPHE.js";
7
7
 
8
8
  // src/index.ts
@@ -314,6 +314,30 @@ export type Result<T> = {
314
314
  ok: false;
315
315
  error: AppErrorShape;
316
316
  };
317
+ /**
318
+ * What methods' JSON Schemas tell the builder beside `KindKeywords`, so its
319
+ * Data view edits rows in place: the column a value is read from, and the
320
+ * table a write edits. The host passes schemas through and reads neither,
321
+ * so they are not part of the host ↔ app contract above: an app built
322
+ * without them shows its rows read-only.
323
+ */
324
+ export interface EditKeywords {
325
+ /**
326
+ * On a value read from a column (`schemaOf`): that column, by its SQLite
327
+ * names, as `x-teable-references` names the column a reference points at.
328
+ */
329
+ "x-teable-column": {
330
+ table: string;
331
+ column: string;
332
+ };
333
+ /**
334
+ * On the input of a write that edits one row of a table (`edits` in
335
+ * `app.write`): the table's SQLite name. The input holds the row's primary
336
+ * key, and any of its other columns as optional properties with no
337
+ * default, each under its column's key; the write sets those it is given.
338
+ */
339
+ "x-teable-edits": string;
340
+ }
317
341
  /** The currency of a `money` column that names none. */
318
342
  export declare const DEFAULT_CURRENCY = "USD";
319
343
  /**
package/dist/schema.d.ts CHANGED
@@ -33,7 +33,9 @@ type RowShape<T extends SQLiteTable> = {
33
33
  * The zod schema of a column's values, for method outputs: what Drizzle reads
34
34
  * from the column (null too, unless it is NOT NULL). It carries the column's
35
35
  * kind into the JSON Schema callers see (`KindKeywords`), so they tell money
36
- * from a plain number, and know its currency. Given a table, its rows.
36
+ * from a plain number, and know its currency, and names the column
37
+ * (`EditKeywords`), so the builder can edit a row it shows with the write
38
+ * that edits its table. Given a table, its rows.
37
39
  *
38
40
  * output: z.array(schemaOf(skus)),
39
41
  * output: z.object({ orders: z.number().int(), revenue: schemaOf(orders.amount) }),
package/dist/schema.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  col,
4
4
  describeTables,
5
5
  schemaOf
6
- } from "./chunks/N7LBNIRZ.js";
6
+ } from "./chunks/FBIHOTDB.js";
7
7
  import "./chunks/VMIIUPHE.js";
8
8
  export {
9
9
  attachment,
@@ -1,9 +1,12 @@
1
1
  import { DurableObject } from "cloudflare:workers";
2
2
  import { type DrizzleSqliteDODatabase } from "drizzle-orm/durable-sqlite";
3
+ import type { SQLiteTable } from "drizzle-orm/sqlite-core";
3
4
  import { z } from "zod";
4
5
  import { type Access, type AppFacet, type AppManifest, type Caller, type Capabilities, type Capability, type PageAccess } from "../protocol.js";
5
6
  import { type Migrations } from "./migrations.js";
6
7
  type Schema = Record<string, unknown>;
8
+ /** The tables a schema module exports. */
9
+ type TableOf<S extends Schema> = Extract<S[keyof S], SQLiteTable>;
7
10
  /** The Drizzle database handed to read and write handlers. */
8
11
  export type Database<S extends Schema> = DrizzleSqliteDODatabase<S>;
9
12
  export type InputSchema = z.ZodType<unknown, Record<string, unknown>>;
@@ -63,6 +66,8 @@ export interface WriteMethod<In extends InputSchema, R, A extends Access = Acces
63
66
  output: OutputSchema | undefined;
64
67
  access: A;
65
68
  description: string | undefined;
69
+ /** The table one of whose rows it edits, as the builder's grid edits rows (`edits`). */
70
+ edits: SQLiteTable | undefined;
66
71
  handler(ctx: WriteContext<Schema, z.output<In>>): R;
67
72
  }
68
73
  export interface TaskMethod<In extends InputSchema> {
@@ -111,7 +116,17 @@ export declare function createApp<S extends Schema>(options: {
111
116
  /** Read data. Synchronous and read-only: a read that writes is rolled back and fails. */
112
117
  read<R, In extends InputSchema = typeof emptyInput, A extends Access = "viewer", Out extends OutputSchema | undefined = undefined>(def: Common<In> & MethodOptions<ReadContext<S, z.output<In>>, R, A, Out> & MustBeSync<R>): ReadMethod<In, Given<Out, R>, A>;
113
118
  /** Change data. Synchronous and atomic: if it throws, nothing it wrote is kept. */
114
- write<R, In extends InputSchema = typeof emptyInput, A extends Access = "editor", Out extends OutputSchema | undefined = undefined>(def: Common<In> & MethodOptions<WriteContext<S, z.output<In>>, R, A, Out> & MustBeSync<R>): WriteMethod<In, Given<Out, R>, A>;
119
+ write<R, In extends InputSchema = typeof emptyInput, A extends Access = "editor", Out extends OutputSchema | undefined = undefined>(def: Common<In> & MethodOptions<WriteContext<S, z.output<In>>, R, A, Out> & MustBeSync<R> & {
120
+ /**
121
+ * The table one of whose rows this write edits, for a write that
122
+ * sets the columns it is given: then the builder's Data view edits
123
+ * that table's rows in place with it, as a page wires it to the
124
+ * grid. Its input holds the row's primary key, and any of its other
125
+ * columns as optional properties with no default, each under its
126
+ * column's key; `define()` refuses anything else.
127
+ */
128
+ edits?: TableOf<S>;
129
+ }): WriteMethod<In, Given<Out, R>, A>;
115
130
  /** Async work outside transactions: external services, multi-step flows. Retried on failure. */
116
131
  task<In extends InputSchema = typeof emptyInput>(def: Common<In> & {
117
132
  maxAttempts?: number;
package/dist/testing.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  methodName
3
- } from "./chunks/33OZ5YAY.js";
4
- import "./chunks/N7LBNIRZ.js";
3
+ } from "./chunks/SQSAV7KS.js";
4
+ import "./chunks/FBIHOTDB.js";
5
5
  import {
6
6
  MethodError
7
- } from "./chunks/L6RROFUY.js";
7
+ } from "./chunks/YGOAZOUE.js";
8
8
  import {
9
9
  ERROR_STATUS
10
10
  } from "./chunks/VMIIUPHE.js";
package/dist/worker.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MethodError
3
- } from "./chunks/L6RROFUY.js";
3
+ } from "./chunks/YGOAZOUE.js";
4
4
  import {
5
5
  ERROR_STATUS
6
6
  } from "./chunks/VMIIUPHE.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teable/app",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "Write a Teable app as a Drizzle schema plus methods",
5
5
  "license": "MIT",
6
6
  "repository": {