Repository navigation
Expand file tree
/
Copy pathprotocol-record-lock.ts
More file actions
46 lines (42 loc) · 2.19 KB
/
Copy pathprotocol-record-lock.ts
File metadata and controls
46 lines (42 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* [ADR-0162 D9] The data protocol's two doors onto the one lock read the data
* layer owns (`ObjectQL#getRecordLock` / `ObjectQL#getRecordLocks`,
* `@objectstack/objectql` `record-lock-read.ts`): `getRecordLock` for one
* record, `getRecordLocks` for a page.
*
* Thin by design. The object-existence gate every data entry point asks
* (#3770) runs first, so an unknown object fails here as it fails on a read;
* then the engine answers, as the caller. Readability, the producers and the
* answer's shape are the engine's, not this door's: a door that re-judged any
* of them would be a second read.
*
* An engine without the read (a test double, a metadata-only store) is
* answered `undefined`, which the REST door turns into `501 NOT_IMPLEMENTED`:
* a lock read that cannot be asked must not answer "unlocked".
*/
import type { RecordLock } from '@objectstack/spec/api';
import type { ProtocolHost } from './protocol-host.js';
/** One record's lock, as the caller; `null` unreadable or missing; `undefined` when the engine has no read. */
export async function readRecordLock(
host: Pick<ProtocolHost, 'engine' | 'assertObjectRegistered'>,
request: { object: string; id: string; context?: any },
): Promise<RecordLock | null | undefined> {
host.assertObjectRegistered(request.object);
const engine = host.engine;
if (typeof engine?.getRecordLock !== 'function') return undefined;
return engine.getRecordLock(request.object, request.id, { context: request.context });
}
/**
* A page of records' locks, as the caller: entry `i` answers `recordIds[i]`.
* `undefined` when the engine has no read. The bound is the engine's.
*/
export async function readRecordLocks(
host: Pick<ProtocolHost, 'engine' | 'assertObjectRegistered'>,
request: { object: string; recordIds: readonly string[]; context?: any },
): Promise<Array<RecordLock | null> | undefined> {
host.assertObjectRegistered(request.object);
const engine = host.engine;
if (typeof engine?.getRecordLocks !== 'function') return undefined;
return engine.getRecordLocks(request.object, request.recordIds, { context: request.context });
}