@ontrails/store 1.0.0-beta.14 → 1.0.0-beta.16
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/CHANGELOG.md +43 -0
- package/README.md +144 -63
- package/package.json +14 -9
- package/src/adapter-support.ts +163 -0
- package/src/crud-doctrine.ts +43 -0
- package/src/index.ts +17 -8
- package/src/jsonfile/index.ts +6 -0
- package/src/jsonfile/runtime.ts +700 -0
- package/src/jsonfile/types.ts +50 -0
- package/src/store.ts +298 -137
- package/src/testing.ts +175 -0
- package/src/trails/crud.ts +356 -0
- package/src/trails/index.ts +15 -0
- package/src/trails/reconcile.ts +281 -0
- package/src/trails/sync.ts +251 -0
- package/src/trails/utils.ts +96 -0
- package/src/types.ts +361 -68
- package/.agents/notes/2026-04-04/handoff-202604032309-9e85a104.md +0 -38
- package/.turbo/turbo-build.log +0 -1
- package/.turbo/turbo-lint.log +0 -3
- package/.turbo/turbo-typecheck.log +0 -1
- package/dist/drizzle/index.d.ts +0 -3
- package/dist/drizzle/index.d.ts.map +0 -1
- package/dist/drizzle/index.js +0 -2
- package/dist/drizzle/index.js.map +0 -1
- package/dist/drizzle/runtime.d.ts +0 -21
- package/dist/drizzle/runtime.d.ts.map +0 -1
- package/dist/drizzle/runtime.js +0 -458
- package/dist/drizzle/runtime.js.map +0 -1
- package/dist/drizzle/schema.d.ts +0 -15
- package/dist/drizzle/schema.d.ts.map +0 -1
- package/dist/drizzle/schema.js +0 -322
- package/dist/drizzle/schema.js.map +0 -1
- package/dist/drizzle/types.d.ts +0 -40
- package/dist/drizzle/types.d.ts.map +0 -1
- package/dist/drizzle/types.js +0 -2
- package/dist/drizzle/types.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/dist/store.d.ts +0 -26
- package/dist/store.d.ts.map +0 -1
- package/dist/store.js +0 -192
- package/dist/store.js.map +0 -1
- package/dist/types.d.ts +0 -224
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/src/__tests__/store.test.ts +0 -333
- package/src/drizzle/__tests__/drizzle.test.ts +0 -469
- package/src/drizzle/index.ts +0 -17
- package/src/drizzle/runtime.ts +0 -853
- package/src/drizzle/schema.ts +0 -577
- package/src/drizzle/types.ts +0 -70
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
package/src/testing.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { expect } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AnyStoreTable,
|
|
5
|
+
EntityOf,
|
|
6
|
+
FiltersOf,
|
|
7
|
+
StoreAccessor,
|
|
8
|
+
StoreIdentifierOf,
|
|
9
|
+
UpsertOf,
|
|
10
|
+
} from './types.js';
|
|
11
|
+
|
|
12
|
+
export interface StoreAccessorContractSubject<TTable extends AnyStoreTable> {
|
|
13
|
+
readonly accessor: StoreAccessor<TTable>;
|
|
14
|
+
readonly dispose?: () => Promise<void> | void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface StoreAccessorContractOptions<TTable extends AnyStoreTable> {
|
|
18
|
+
readonly createInput: () => UpsertOf<TTable>;
|
|
19
|
+
readonly createSubject:
|
|
20
|
+
| (() => StoreAccessorContractSubject<TTable>)
|
|
21
|
+
| (() => Promise<StoreAccessorContractSubject<TTable>>);
|
|
22
|
+
readonly expectCreated:
|
|
23
|
+
| ((entity: EntityOf<TTable>, input: UpsertOf<TTable>) => void)
|
|
24
|
+
| ((entity: EntityOf<TTable>, input: UpsertOf<TTable>) => Promise<void>);
|
|
25
|
+
readonly expectUpdated:
|
|
26
|
+
| ((
|
|
27
|
+
entity: EntityOf<TTable>,
|
|
28
|
+
previous: EntityOf<TTable>,
|
|
29
|
+
input: UpsertOf<TTable>
|
|
30
|
+
) => void)
|
|
31
|
+
| ((
|
|
32
|
+
entity: EntityOf<TTable>,
|
|
33
|
+
previous: EntityOf<TTable>,
|
|
34
|
+
input: UpsertOf<TTable>
|
|
35
|
+
) => Promise<void>);
|
|
36
|
+
readonly missingId: StoreIdentifierOf<TTable>;
|
|
37
|
+
readonly seedExisting?:
|
|
38
|
+
| ((accessor: StoreAccessor<TTable>) => Promise<EntityOf<TTable>>)
|
|
39
|
+
| ((accessor: StoreAccessor<TTable>) => EntityOf<TTable>);
|
|
40
|
+
readonly table: TTable;
|
|
41
|
+
readonly updateInput: (existing: EntityOf<TTable>) => UpsertOf<TTable>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface StoreAccessorContractCase {
|
|
45
|
+
readonly name: string;
|
|
46
|
+
readonly run: () => Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const listByIdentity = async <TTable extends AnyStoreTable>(
|
|
50
|
+
table: TTable,
|
|
51
|
+
accessor: StoreAccessor<TTable>,
|
|
52
|
+
entity: EntityOf<TTable>
|
|
53
|
+
): Promise<readonly EntityOf<TTable>[]> => {
|
|
54
|
+
const identity = table.identity as keyof EntityOf<TTable> & string;
|
|
55
|
+
const filters = {
|
|
56
|
+
[identity]: entity[identity],
|
|
57
|
+
} as FiltersOf<TTable>;
|
|
58
|
+
|
|
59
|
+
return await accessor.list(filters);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const withSubject = async <TTable extends AnyStoreTable, TResult>(
|
|
63
|
+
createSubject: StoreAccessorContractOptions<TTable>['createSubject'],
|
|
64
|
+
run: (subject: StoreAccessorContractSubject<TTable>) => Promise<TResult>
|
|
65
|
+
): Promise<TResult> => {
|
|
66
|
+
const subject = await createSubject();
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
return await run(subject);
|
|
70
|
+
} finally {
|
|
71
|
+
await subject.dispose?.();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const seedExistingEntity = async <TTable extends AnyStoreTable>(
|
|
76
|
+
options: StoreAccessorContractOptions<TTable>,
|
|
77
|
+
accessor: StoreAccessor<TTable>
|
|
78
|
+
): Promise<EntityOf<TTable>> => {
|
|
79
|
+
if (options.seedExisting !== undefined) {
|
|
80
|
+
return await options.seedExisting(accessor);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return await accessor.upsert(options.createInput());
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Shared contract cases for backend-agnostic writable store accessors.
|
|
88
|
+
*
|
|
89
|
+
* Adapters can register these with their own `test(...)` wrappers so the
|
|
90
|
+
* baseline `get/list/upsert/remove` contract stays aligned across runtimes
|
|
91
|
+
* without fighting repository-specific test-lint rules.
|
|
92
|
+
*/
|
|
93
|
+
export const createStoreAccessorContractCases = <TTable extends AnyStoreTable>(
|
|
94
|
+
options: StoreAccessorContractOptions<TTable>
|
|
95
|
+
): readonly StoreAccessorContractCase[] =>
|
|
96
|
+
[
|
|
97
|
+
{
|
|
98
|
+
name: 'upsert creates an entity and exposes it through get/list',
|
|
99
|
+
run: async () => {
|
|
100
|
+
await withSubject(options.createSubject, async ({ accessor }) => {
|
|
101
|
+
const input = options.createInput();
|
|
102
|
+
const created = await accessor.upsert(input);
|
|
103
|
+
|
|
104
|
+
await options.expectCreated(created, input);
|
|
105
|
+
expect(
|
|
106
|
+
await accessor.get(
|
|
107
|
+
created[
|
|
108
|
+
options.table.identity as keyof EntityOf<TTable> & string
|
|
109
|
+
] as StoreIdentifierOf<TTable>
|
|
110
|
+
)
|
|
111
|
+
).toEqual(created);
|
|
112
|
+
expect(
|
|
113
|
+
await listByIdentity(options.table, accessor, created)
|
|
114
|
+
).toEqual([created]);
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: 'upsert updates an existing entity in place when the identity matches',
|
|
120
|
+
run: async () => {
|
|
121
|
+
await withSubject(options.createSubject, async ({ accessor }) => {
|
|
122
|
+
const existing = await seedExistingEntity(options, accessor);
|
|
123
|
+
const input = options.updateInput(existing);
|
|
124
|
+
const updated = await accessor.upsert(input);
|
|
125
|
+
const identity = options.table.identity as keyof EntityOf<TTable> &
|
|
126
|
+
string;
|
|
127
|
+
|
|
128
|
+
expect(updated[identity]).toBe(existing[identity]);
|
|
129
|
+
await options.expectUpdated(updated, existing, input);
|
|
130
|
+
expect(
|
|
131
|
+
await accessor.get(updated[identity] as StoreIdentifierOf<TTable>)
|
|
132
|
+
).toEqual(updated);
|
|
133
|
+
expect(
|
|
134
|
+
await listByIdentity(options.table, accessor, updated)
|
|
135
|
+
).toEqual([updated]);
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: 'remove deletes an existing entity',
|
|
141
|
+
run: async () => {
|
|
142
|
+
await withSubject(options.createSubject, async ({ accessor }) => {
|
|
143
|
+
const existing = await seedExistingEntity(options, accessor);
|
|
144
|
+
const identity = options.table.identity as keyof EntityOf<TTable> &
|
|
145
|
+
string;
|
|
146
|
+
const removed = await accessor.remove(
|
|
147
|
+
existing[identity] as StoreIdentifierOf<TTable>
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect(removed).toEqual({ deleted: true });
|
|
151
|
+
expect(
|
|
152
|
+
await accessor.get(existing[identity] as StoreIdentifierOf<TTable>)
|
|
153
|
+
).toBeNull();
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: 'get returns null for a missing identity',
|
|
159
|
+
run: async () => {
|
|
160
|
+
await withSubject(options.createSubject, async ({ accessor }) => {
|
|
161
|
+
expect(await accessor.get(options.missingId)).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: 'remove reports false when the identity is missing',
|
|
167
|
+
run: async () => {
|
|
168
|
+
await withSubject(options.createSubject, async ({ accessor }) => {
|
|
169
|
+
expect(await accessor.remove(options.missingId)).toEqual({
|
|
170
|
+
deleted: false,
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
] as const;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import type { Implementation, Resource, Trail } from '@ontrails/core';
|
|
2
|
+
import { deriveTrail } from '@ontrails/core/trails';
|
|
3
|
+
import type {
|
|
4
|
+
DeriveTrailInput,
|
|
5
|
+
DeriveTrailOutput,
|
|
6
|
+
} from '@ontrails/core/trails';
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
AnyStoreTable,
|
|
11
|
+
EntityOf,
|
|
12
|
+
FiltersOf,
|
|
13
|
+
InsertOf,
|
|
14
|
+
StoreAccessor,
|
|
15
|
+
StoreIdentifierOf,
|
|
16
|
+
UpdateOf,
|
|
17
|
+
} from '../types.js';
|
|
18
|
+
import type { CrudOperation } from '../crud-doctrine.js';
|
|
19
|
+
import { createTableContour } from './utils.js';
|
|
20
|
+
import type { TableContour } from './utils.js';
|
|
21
|
+
|
|
22
|
+
type IdentityInputOf<TTable extends AnyStoreTable> = Readonly<
|
|
23
|
+
Record<Extract<TTable['identity'], string>, StoreIdentifierOf<TTable>>
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
type CrudConnection<TTable extends AnyStoreTable> = Readonly<
|
|
27
|
+
Record<TTable['name'], StoreAccessor<TTable>>
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
type TableContourFieldKey<TTable extends AnyStoreTable> = Extract<
|
|
31
|
+
keyof z.output<TableContour<TTable>>,
|
|
32
|
+
string
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
type GeneratedFieldsOf<TTable extends AnyStoreTable> =
|
|
36
|
+
TTable['generated'] extends readonly TableContourFieldKey<TTable>[]
|
|
37
|
+
? TTable['generated']
|
|
38
|
+
: readonly [];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Input type `deriveTrail` projects for a given CRUD operation against a
|
|
42
|
+
* store table. Routes through `TableContour<TTable>` so the projected input
|
|
43
|
+
* structurally matches the contour-backed derivation path in
|
|
44
|
+
* `@ontrails/core`'s `deriveTrail`.
|
|
45
|
+
*/
|
|
46
|
+
type DerivedInput<
|
|
47
|
+
TTable extends AnyStoreTable,
|
|
48
|
+
TOperation extends CrudOperation,
|
|
49
|
+
> = DeriveTrailInput<
|
|
50
|
+
TableContour<TTable>,
|
|
51
|
+
TOperation,
|
|
52
|
+
GeneratedFieldsOf<TTable>
|
|
53
|
+
>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Output type `deriveTrail` projects for a given CRUD operation against a
|
|
57
|
+
* store table.
|
|
58
|
+
*/
|
|
59
|
+
type DerivedOutput<
|
|
60
|
+
TTable extends AnyStoreTable,
|
|
61
|
+
TOperation extends CrudOperation,
|
|
62
|
+
> = DeriveTrailOutput<TableContour<TTable>, TOperation>;
|
|
63
|
+
|
|
64
|
+
type InternalCreateTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
65
|
+
DerivedInput<TTable, 'create'>,
|
|
66
|
+
DerivedOutput<TTable, 'create'>
|
|
67
|
+
>;
|
|
68
|
+
|
|
69
|
+
type InternalReadTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
70
|
+
DerivedInput<TTable, 'read'>,
|
|
71
|
+
DerivedOutput<TTable, 'read'>
|
|
72
|
+
>;
|
|
73
|
+
|
|
74
|
+
type InternalUpdateTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
75
|
+
DerivedInput<TTable, 'update'>,
|
|
76
|
+
DerivedOutput<TTable, 'update'>
|
|
77
|
+
>;
|
|
78
|
+
|
|
79
|
+
type InternalDeleteTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
80
|
+
DerivedInput<TTable, 'delete'>,
|
|
81
|
+
DerivedOutput<TTable, 'delete'>
|
|
82
|
+
>;
|
|
83
|
+
|
|
84
|
+
type InternalListTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
85
|
+
DerivedInput<TTable, 'list'>,
|
|
86
|
+
DerivedOutput<TTable, 'list'>
|
|
87
|
+
>;
|
|
88
|
+
|
|
89
|
+
type InternalCrudBaseTrails<TTable extends AnyStoreTable> = Readonly<{
|
|
90
|
+
createBase: InternalCreateTrailOf<TTable>;
|
|
91
|
+
readBase: InternalReadTrailOf<TTable>;
|
|
92
|
+
updateBase: InternalUpdateTrailOf<TTable>;
|
|
93
|
+
deleteBase: InternalDeleteTrailOf<TTable>;
|
|
94
|
+
listBase: InternalListTrailOf<TTable>;
|
|
95
|
+
}>;
|
|
96
|
+
|
|
97
|
+
type TrailExampleOf<TInput, TOutput> = NonNullable<
|
|
98
|
+
Trail<TInput, TOutput>['examples']
|
|
99
|
+
>[number];
|
|
100
|
+
|
|
101
|
+
type CreateTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
102
|
+
InsertOf<TTable>,
|
|
103
|
+
EntityOf<TTable>
|
|
104
|
+
>;
|
|
105
|
+
|
|
106
|
+
type ReadTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
107
|
+
IdentityInputOf<TTable>,
|
|
108
|
+
EntityOf<TTable>
|
|
109
|
+
>;
|
|
110
|
+
|
|
111
|
+
type UpdateTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
112
|
+
IdentityInputOf<TTable> & UpdateOf<TTable>,
|
|
113
|
+
EntityOf<TTable>
|
|
114
|
+
>;
|
|
115
|
+
|
|
116
|
+
type DeleteTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
117
|
+
IdentityInputOf<TTable>,
|
|
118
|
+
undefined
|
|
119
|
+
>;
|
|
120
|
+
|
|
121
|
+
type ListTrailOf<TTable extends AnyStoreTable> = Trail<
|
|
122
|
+
FiltersOf<TTable>,
|
|
123
|
+
EntityOf<TTable>[]
|
|
124
|
+
>;
|
|
125
|
+
|
|
126
|
+
type InternalCrudTrails<TTable extends AnyStoreTable> = readonly [
|
|
127
|
+
create: InternalCreateTrailOf<TTable>,
|
|
128
|
+
read: InternalReadTrailOf<TTable>,
|
|
129
|
+
update: InternalUpdateTrailOf<TTable>,
|
|
130
|
+
remove: InternalDeleteTrailOf<TTable>,
|
|
131
|
+
list: InternalListTrailOf<TTable>,
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
export type CrudTrails<TTable extends AnyStoreTable> = readonly [
|
|
135
|
+
create: CreateTrailOf<TTable>,
|
|
136
|
+
read: ReadTrailOf<TTable>,
|
|
137
|
+
update: UpdateTrailOf<TTable>,
|
|
138
|
+
remove: DeleteTrailOf<TTable>,
|
|
139
|
+
list: ListTrailOf<TTable>,
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
export interface CrudBlazeOverrides<TTable extends AnyStoreTable> {
|
|
143
|
+
readonly create?: Implementation<InsertOf<TTable>, EntityOf<TTable>>;
|
|
144
|
+
readonly read?: Implementation<IdentityInputOf<TTable>, EntityOf<TTable>>;
|
|
145
|
+
readonly update?: Implementation<
|
|
146
|
+
IdentityInputOf<TTable> & UpdateOf<TTable>,
|
|
147
|
+
EntityOf<TTable>
|
|
148
|
+
>;
|
|
149
|
+
readonly delete?: Implementation<IdentityInputOf<TTable>, undefined>;
|
|
150
|
+
readonly list?: Implementation<FiltersOf<TTable>, EntityOf<TTable>[]>;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface CrudOptions<TTable extends AnyStoreTable> {
|
|
154
|
+
readonly blaze?: CrudBlazeOverrides<TTable>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface InternalCrudBlazeOverrides<TTable extends AnyStoreTable> {
|
|
158
|
+
readonly create?: Implementation<
|
|
159
|
+
DerivedInput<TTable, 'create'>,
|
|
160
|
+
DerivedOutput<TTable, 'create'>
|
|
161
|
+
>;
|
|
162
|
+
readonly read?: Implementation<
|
|
163
|
+
DerivedInput<TTable, 'read'>,
|
|
164
|
+
DerivedOutput<TTable, 'read'>
|
|
165
|
+
>;
|
|
166
|
+
readonly update?: Implementation<
|
|
167
|
+
DerivedInput<TTable, 'update'>,
|
|
168
|
+
DerivedOutput<TTable, 'update'>
|
|
169
|
+
>;
|
|
170
|
+
readonly delete?: Implementation<
|
|
171
|
+
DerivedInput<TTable, 'delete'>,
|
|
172
|
+
DerivedOutput<TTable, 'delete'>
|
|
173
|
+
>;
|
|
174
|
+
readonly list?: Implementation<
|
|
175
|
+
DerivedInput<TTable, 'list'>,
|
|
176
|
+
DerivedOutput<TTable, 'list'>
|
|
177
|
+
>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
interface InternalCrudOptions<TTable extends AnyStoreTable> {
|
|
181
|
+
readonly blaze?: InternalCrudBlazeOverrides<TTable>;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const normalizeExampleForOutput = <TInput, TOutput>(
|
|
185
|
+
example: TrailExampleOf<TInput, TOutput>,
|
|
186
|
+
output: z.ZodType<TOutput>
|
|
187
|
+
): TrailExampleOf<TInput, TOutput> | undefined => {
|
|
188
|
+
if (example.expected === undefined) {
|
|
189
|
+
return example;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const parsed = output.safeParse(example.expected);
|
|
193
|
+
return parsed.success
|
|
194
|
+
? {
|
|
195
|
+
...example,
|
|
196
|
+
expected: parsed.data,
|
|
197
|
+
}
|
|
198
|
+
: undefined;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const normalizeExamplesForOutput = <TInput, TOutput>(
|
|
202
|
+
base: Trail<TInput, TOutput>,
|
|
203
|
+
output: z.ZodType<TOutput>
|
|
204
|
+
): Trail<TInput, TOutput>['examples'] => {
|
|
205
|
+
const { examples } = base;
|
|
206
|
+
if (examples === undefined || examples.length === 0) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const next = examples
|
|
211
|
+
.map((example) => normalizeExampleForOutput(example, output))
|
|
212
|
+
.filter(
|
|
213
|
+
(example): example is TrailExampleOf<TInput, TOutput> =>
|
|
214
|
+
example !== undefined
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
return next.length === 0
|
|
218
|
+
? undefined
|
|
219
|
+
: (Object.freeze(next) as Trail<TInput, TOutput>['examples']);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const finalizeTrail = <TInput, TOutput>(
|
|
223
|
+
base: Trail<TInput, TOutput>,
|
|
224
|
+
options: {
|
|
225
|
+
readonly blaze?: Implementation<TInput, TOutput> | undefined;
|
|
226
|
+
readonly output?: z.ZodType<TOutput> | undefined;
|
|
227
|
+
readonly pattern?: string | undefined;
|
|
228
|
+
} = {}
|
|
229
|
+
): Trail<TInput, TOutput> =>
|
|
230
|
+
Object.freeze({
|
|
231
|
+
...base,
|
|
232
|
+
...(options.blaze === undefined ? {} : { blaze: options.blaze }),
|
|
233
|
+
...(options.output === undefined
|
|
234
|
+
? {}
|
|
235
|
+
: {
|
|
236
|
+
examples: normalizeExamplesForOutput(base, options.output),
|
|
237
|
+
output: options.output,
|
|
238
|
+
}),
|
|
239
|
+
...(options.pattern === undefined ? {} : { pattern: options.pattern }),
|
|
240
|
+
}) as Trail<TInput, TOutput>;
|
|
241
|
+
|
|
242
|
+
const deriveCrudBaseTrails = <
|
|
243
|
+
TTable extends AnyStoreTable,
|
|
244
|
+
TConnection extends CrudConnection<TTable>,
|
|
245
|
+
>(
|
|
246
|
+
table: TTable,
|
|
247
|
+
resource: Resource<TConnection>
|
|
248
|
+
): InternalCrudBaseTrails<TTable> => {
|
|
249
|
+
const entityContour = createTableContour(table);
|
|
250
|
+
// Narrow the store's `readonly string[]` to the contour's typed field-key
|
|
251
|
+
// array so `deriveTrail`'s `TGenerated` generic picks up the precise
|
|
252
|
+
// key-of shape that `CreateInputOf<Contour, TGenerated>` expects. The
|
|
253
|
+
// runtime value is unchanged — the names in `table.generated` are already
|
|
254
|
+
// keys of `table.schema.shape` by construction in `store()`.
|
|
255
|
+
const generated = table.generated as GeneratedFieldsOf<TTable>;
|
|
256
|
+
|
|
257
|
+
return {
|
|
258
|
+
createBase: deriveTrail(entityContour, 'create', {
|
|
259
|
+
generated,
|
|
260
|
+
resource,
|
|
261
|
+
}),
|
|
262
|
+
deleteBase: deriveTrail(entityContour, 'delete', {
|
|
263
|
+
resource,
|
|
264
|
+
}),
|
|
265
|
+
listBase: deriveTrail(entityContour, 'list', {
|
|
266
|
+
resource,
|
|
267
|
+
}),
|
|
268
|
+
readBase: deriveTrail(entityContour, 'read', {
|
|
269
|
+
resource,
|
|
270
|
+
}),
|
|
271
|
+
// The `update` blaze synthesized by `deriveTrail` handles the partial-patch
|
|
272
|
+
// concern: when the accessor lacks a native `update`, the fallback path in
|
|
273
|
+
// `derive-trail.ts` (`updateViaReadAndUpsert`) reads the current entity,
|
|
274
|
+
// merges the patch, strips the `version` field, then calls `upsert` with
|
|
275
|
+
// the full merged payload — so no fields are silently lost.
|
|
276
|
+
updateBase: deriveTrail(entityContour, 'update', {
|
|
277
|
+
generated,
|
|
278
|
+
resource,
|
|
279
|
+
}),
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const buildCrudTrails = <TTable extends AnyStoreTable>(
|
|
284
|
+
baseTrails: InternalCrudBaseTrails<TTable>,
|
|
285
|
+
overrides: InternalCrudBlazeOverrides<TTable>,
|
|
286
|
+
entityOutput: z.ZodType<DerivedOutput<TTable, 'create'>>,
|
|
287
|
+
listOutput: z.ZodType<DerivedOutput<TTable, 'list'>>
|
|
288
|
+
): InternalCrudTrails<TTable> =>
|
|
289
|
+
Object.freeze([
|
|
290
|
+
finalizeTrail(baseTrails.createBase, {
|
|
291
|
+
...(overrides.create === undefined ? {} : { blaze: overrides.create }),
|
|
292
|
+
output: entityOutput,
|
|
293
|
+
pattern: 'crud',
|
|
294
|
+
}),
|
|
295
|
+
finalizeTrail(baseTrails.readBase, {
|
|
296
|
+
...(overrides.read === undefined ? {} : { blaze: overrides.read }),
|
|
297
|
+
output: entityOutput,
|
|
298
|
+
pattern: 'crud',
|
|
299
|
+
}),
|
|
300
|
+
finalizeTrail(baseTrails.updateBase, {
|
|
301
|
+
...(overrides.update === undefined ? {} : { blaze: overrides.update }),
|
|
302
|
+
output: entityOutput,
|
|
303
|
+
pattern: 'crud',
|
|
304
|
+
}),
|
|
305
|
+
overrides.delete === undefined
|
|
306
|
+
? finalizeTrail(baseTrails.deleteBase, { pattern: 'crud' })
|
|
307
|
+
: finalizeTrail(baseTrails.deleteBase, {
|
|
308
|
+
blaze: overrides.delete,
|
|
309
|
+
pattern: 'crud',
|
|
310
|
+
}),
|
|
311
|
+
finalizeTrail(baseTrails.listBase, {
|
|
312
|
+
...(overrides.list === undefined ? {} : { blaze: overrides.list }),
|
|
313
|
+
output: listOutput,
|
|
314
|
+
pattern: 'crud',
|
|
315
|
+
}),
|
|
316
|
+
]) as InternalCrudTrails<TTable>;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Produce the standard CRUD trail tuple for one normalized store table.
|
|
320
|
+
*
|
|
321
|
+
* The factory derives schemas, examples, resources, and contour linkage from
|
|
322
|
+
* the table metadata. Blazes default to the backend-agnostic store accessor
|
|
323
|
+
* contract via `deriveTrail()`'s single-resource synthesis path. Per-operation
|
|
324
|
+
* blaze overrides stay available for callers that need custom persistence
|
|
325
|
+
* behavior and are layered onto the derived trails in a single pass.
|
|
326
|
+
*/
|
|
327
|
+
export function crud<
|
|
328
|
+
TTable extends AnyStoreTable,
|
|
329
|
+
TConnection extends CrudConnection<TTable>,
|
|
330
|
+
>(
|
|
331
|
+
table: TTable,
|
|
332
|
+
resource: Resource<TConnection>,
|
|
333
|
+
options?: CrudOptions<TTable>
|
|
334
|
+
): CrudTrails<TTable>;
|
|
335
|
+
export function crud<
|
|
336
|
+
TTable extends AnyStoreTable,
|
|
337
|
+
TConnection extends CrudConnection<TTable>,
|
|
338
|
+
>(
|
|
339
|
+
table: TTable,
|
|
340
|
+
resource: Resource<TConnection>,
|
|
341
|
+
options: InternalCrudOptions<TTable> = {}
|
|
342
|
+
) {
|
|
343
|
+
const overrides = options.blaze ?? {};
|
|
344
|
+
const baseTrails = deriveCrudBaseTrails(table, resource);
|
|
345
|
+
// Narrow `table.schema` (typed `StoreObjectSchema`, which is
|
|
346
|
+
// `z.ZodObject<Record<string, z.ZodType>>`) to a ZodObject keyed by the
|
|
347
|
+
// concrete shape so its `z.output` unifies with the contour-derived
|
|
348
|
+
// output. Structurally `table.schema` already has `shape:
|
|
349
|
+
// TTable['schema']['shape']` — this only refines the generic parameter.
|
|
350
|
+
const entitySchema = table.schema as z.ZodObject<TTable['schema']['shape']>;
|
|
351
|
+
const entityOutput: z.ZodType<DerivedOutput<TTable, 'create'>> = entitySchema;
|
|
352
|
+
const listOutput: z.ZodType<DerivedOutput<TTable, 'list'>> =
|
|
353
|
+
entitySchema.array();
|
|
354
|
+
|
|
355
|
+
return buildCrudTrails(baseTrails, overrides, entityOutput, listOutput);
|
|
356
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { crudAccessorExpectations, crudOperations } from '../crud-doctrine.js';
|
|
2
|
+
export type {
|
|
3
|
+
CrudAccessorExpectation,
|
|
4
|
+
CrudOperation,
|
|
5
|
+
} from '../crud-doctrine.js';
|
|
6
|
+
export { crud } from './crud.js';
|
|
7
|
+
export type { CrudBlazeOverrides, CrudOptions, CrudTrails } from './crud.js';
|
|
8
|
+
export { reconcile } from './reconcile.js';
|
|
9
|
+
export type {
|
|
10
|
+
ReconcileConflict,
|
|
11
|
+
ReconcileOptions,
|
|
12
|
+
ReconcileStrategy,
|
|
13
|
+
} from './reconcile.js';
|
|
14
|
+
export { sync } from './sync.js';
|
|
15
|
+
export type { SyncEndpoint, SyncOptions, SyncTransform } from './sync.js';
|