@mistriwork/engine 1.0.0-rc.41 → 1.0.0-rc.42

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.
@@ -27,6 +27,10 @@ export interface ColumnGrant {
27
27
  writable: ReadonlySet<string>;
28
28
  /** Of `writable`, where a generic insert may make a row (`roles.<role>.create`). Absent: all of it. */
29
29
  creatable?: ReadonlySet<string>;
30
+ /** Of `writable`, what the principal only appends to: a row is made there and never edited or removed
31
+ (update, delete, lock and a file's replacement are refused). Absent: none. Carried wherever
32
+ `creatable` is, so no copy of a grant can leave such a table editable. */
33
+ appendOnly?: ReadonlySet<string>;
30
34
  /** Missing entries retain unrestricted columns within the resource grants. */
31
35
  fields?: ReadonlyMap<string, {
32
36
  read: ReadonlySet<string>;
@@ -39,6 +39,7 @@ export function captureGrant(grant) {
39
39
  readable: new Set(grant.readable),
40
40
  writable: new Set(grant.writable),
41
41
  ...(grant.creatable ? { creatable: new Set(grant.creatable) } : {}),
42
+ ...(grant.appendOnly ? { appendOnly: new Set(grant.appendOnly) } : {}),
42
43
  ...(grant.fields
43
44
  ? {
44
45
  fields: new Map([...grant.fields].map(([table, fields]) => [table, { read: new Set(fields.read), write: new Set(fields.write) }])),
@@ -67,7 +68,7 @@ const prunedCatalogs = new WeakMap();
67
68
  /* A tenant request resolves its grant afresh each time, so its sets are never
68
69
  the same objects twice, and every read in it pruned the catalog again
69
70
  (G118). A captured catalog is frozen, and pruning reads nothing of the grant
70
- but the five things in this key, so the answer for a catalog and a key is
71
+ but the six things in this key, so the answer for a catalog and a key is
71
72
  kept — bounded, oldest out first — and frozen, so no caller can change the
72
73
  copy another one is handed. */
73
74
  const prunedByContent = new WeakMap();
@@ -78,6 +79,7 @@ function grantKey(access) {
78
79
  sorted(access.readable),
79
80
  sorted(access.writable),
80
81
  access.creatable ? sorted(access.creatable) : null,
82
+ access.appendOnly ? sorted(access.appendOnly) : null,
81
83
  access.fields
82
84
  ? [...access.fields].map(([table, { read, write }]) => [table, sorted(read), sorted(write)]).sort((a, b) => (a[0] < b[0] ? -1 : 1))
83
85
  : null,
@@ -115,6 +117,7 @@ export function accessibleSchema(schema, access) {
115
117
  if (kept &&
116
118
  kept.writable === access.writable &&
117
119
  kept.creatable === access.creatable &&
120
+ kept.appendOnly === access.appendOnly &&
118
121
  kept.fields === access.fields &&
119
122
  kept.internal === access.internal &&
120
123
  kept.scoped === scoped)
@@ -122,8 +125,8 @@ export function accessibleSchema(schema, access) {
122
125
  // Frozen over a captured catalog, as the content path's is: what reads it later (resourceChecks) may then keep by its identity.
123
126
  const made = pruneCatalog(schema, access);
124
127
  const pruned = isCapturedScopeCatalog(schema) ? freezeDerivedCatalog(made) : made;
125
- const { writable, creatable, fields, internal } = access;
126
- byGrant.set(access.readable, { writable, creatable, fields, internal, scoped, pruned });
128
+ const { writable, creatable, appendOnly, fields, internal } = access;
129
+ byGrant.set(access.readable, { writable, creatable, appendOnly, fields, internal, scoped, pruned });
127
130
  return pruned;
128
131
  }
129
132
  function pruneCatalog(schema, access) {
@@ -210,6 +213,10 @@ function pruneCatalog(schema, access) {
210
213
  table.columns.every((column) => readable(table.id, column.name) && (access.fields?.get(table.id)?.write.has(column.name) ?? true)),
211
214
  }
212
215
  : {}),
216
+ /* Append-only: the reader adds rows here and never edits or removes them. Said last so it outranks the
217
+ deletable above; the columns stay writable, for the New form. Capability only, never the authority
218
+ (authorizeColumnWrites is). */
219
+ ...(access.appendOnly?.has(table.id) ? { appendOnly: true, deletable: false } : {}),
213
220
  ...(table.indexed ? { indexed: table.indexed.filter((column) => listed(table.id, column)) } : {}),
214
221
  // Like `indexed`: a hidden column is not named, even as unique.
215
222
  ...(table.unique ? { unique: table.unique.filter((column) => listed(table.id, column)) } : {}),
@@ -688,6 +695,14 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
688
695
  const updateVia = (input, write) => {
689
696
  const request = jsonWritten(full, requestData(input));
690
697
  const operation = capture(grant);
698
+ /* An append-only table is in `writable` so its rows can be made, and edited by nothing generic: what answers an
699
+ update to a table the principal does not write (a granted fixed action) answers it, else it is refused. */
700
+ if (operation.appendOnly?.has(request.table)) {
701
+ const answered = product?.unwritable?.(request, operation, write);
702
+ if (answered)
703
+ return answered;
704
+ throw new AppAccessDenied();
705
+ }
691
706
  if (!operation.writable.has(request.table)) {
692
707
  const answered = product?.unwritable?.(request, operation, write);
693
708
  if (answered)
@@ -709,7 +724,11 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
709
724
  ? {
710
725
  lockRecord: async (input) => {
711
726
  const request = requestData(input);
712
- if (grant.writable.has(request.table) || !product?.locksUnwritable?.(request.table, grant))
727
+ // A lock is the first step of an edit or a removal: an append-only table is locked only as a table not written is.
728
+ if (grant.appendOnly?.has(request.table) && !product?.locksUnwritable?.(request.table, grant))
729
+ throw new AppAccessDenied();
730
+ if ((grant.writable.has(request.table) && !grant.appendOnly?.has(request.table)) ||
731
+ !product?.locksUnwritable?.(request.table, grant))
713
732
  assertWrite(request.table);
714
733
  else
715
734
  assertRead(request.table);
@@ -744,6 +763,8 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
744
763
  remove: (input) => {
745
764
  const request = requestData(input);
746
765
  assertWrite(request.table);
766
+ if (grant.appendOnly?.has(request.table))
767
+ throw new AppAccessDenied();
747
768
  keyFields(request.table, request.key);
748
769
  // Delete/Undo requires a complete readable before-image and permission
749
770
  // to remove all fields, including those protected from ordinary updates.
@@ -145,6 +145,8 @@ export interface TableMeta {
145
145
  deletable?: boolean;
146
146
  /** Server capability projection; omitted retains ordinary insert behavior. */
147
147
  insertable?: boolean;
148
+ /** Server capability projection, never authority: the reader adds rows here and never edits or removes them. */
149
+ appendOnly?: boolean;
148
150
  /** Schema/namespace the table lives in. Empty when the database has none. */
149
151
  schema: string;
150
152
  /** Qualified name used everywhere as the table's identity. */
@@ -323,6 +325,8 @@ export declare function columnOrRollup(table: TableMeta, name: string): boolean;
323
325
  export declare function referencesTo(schema: DatabaseSchema, tableId: string): ForeignKey[];
324
326
  /** Columns on this table that point elsewhere. */
325
327
  export declare function referencesFrom(schema: DatabaseSchema, tableId: string): ForeignKey[];
328
+ /** Whether the reader only adds rows to this table (`appendOnly`): every editor and every Delete is withheld, New stays. */
329
+ export declare function isAppendOnly(table: Pick<TableMeta, 'appendOnly'> | null | undefined): boolean;
326
330
  export declare function findTable(schema: DatabaseSchema, tableId: string): TableMeta | undefined;
327
331
  /**
328
332
  * A table by its id, or by a bare name when only one table has it.
@@ -123,6 +123,10 @@ export function referencesFrom(schema, tableId) {
123
123
  is never indexed, even once frozen later. The first of an id, as the scan finds it. */
124
124
  const tableIndexes = new WeakMap();
125
125
  const fixed = (table) => Object.isFrozen(table) && 'value' in (Object.getOwnPropertyDescriptor(table, 'id') ?? {});
126
+ /** Whether the reader only adds rows to this table (`appendOnly`): every editor and every Delete is withheld, New stays. */
127
+ export function isAppendOnly(table) {
128
+ return table?.appendOnly === true;
129
+ }
126
130
  export function findTable(schema, tableId) {
127
131
  const tables = schema.tables;
128
132
  let index = tableIndexes.get(tables);
@@ -172,6 +172,7 @@ export function captureScopeCatalog(schema) {
172
172
  'trigram',
173
173
  'deletable',
174
174
  'insertable',
175
+ 'appendOnly',
175
176
  'sourceDialect',
176
177
  'filterable',
177
178
  'filterIn',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/engine",
3
- "version": "1.0.0-rc.41",
3
+ "version": "1.0.0-rc.42",
4
4
  "description": "The engine both products stand on: the adapters (SQLite, Postgres), the catalog, the query language, keyset pages, the HTTP kit and the shared rules. No React.",
5
5
  "homepage": "https://mistriwork.dev",
6
6
  "bugs": {