11import { promiseVoid } from '../../../shared/src/resolved-promises.js' ;
22import type { MaybePromise } from '../../../shared/src/types.js' ;
3- import { type PrimaryKeyValueRecord } from '../../../zero-protocol/src/primary-key.js' ;
3+ import {
4+ type PrimaryKeyValue ,
5+ type PrimaryKeyValueRecord ,
6+ } from '../../../zero-protocol/src/primary-key.js' ;
47import {
58 CRUD_MUTATION_NAME ,
69 type CreateOp ,
@@ -13,9 +16,10 @@ import {
1316} from '../../../zero-protocol/src/push.js' ;
1417import type { Row } from '../../../zql/src/zql/ivm/data.js' ;
1518import type { PrimaryKey } from '../../../zql/src/zql/ivm/schema.js' ;
19+ import type { NormalizedPrimaryKey } from '../../../zql/src/zql/query/normalize-table-schema.js' ;
1620import type { SchemaToRow } from '../../../zql/src/zql/query/query.js' ;
1721import { toPrimaryKeyString } from './keys.js' ;
18- import { makeIDFromPrimaryKey } from './make-id-from-primary-key .js' ;
22+ import type { NormalizedSchema } from './normalized-schema .js' ;
1923import type { MutatorDefs , WriteTransaction } from './replicache-types.js' ;
2024import type { Schema } from './zero.js' ;
2125
@@ -76,7 +80,7 @@ type ZeroCRUDMutate = {
7680 * `label` properties.
7781 */
7882export function makeCRUDMutate < const S extends Schema > (
79- schema : S ,
83+ schema : NormalizedSchema ,
8084 repMutate : ZeroCRUDMutate ,
8185) : MakeCRUDMutate < S > {
8286 const { [ CRUD_MUTATION_NAME ] : zeroCRUD } = repMutate ;
@@ -122,40 +126,10 @@ export function makeCRUDMutate<const S extends Schema>(
122126 return mutate as MakeCRUDMutate < S > ;
123127}
124128
125- // type NiceIntersection<S, T> = {[K in keyof (S & T)]: (S & T)[K]};
126-
127- // type MyRow = {
128- // id: string;
129- // n: number;
130- // b: boolean;
131- // null: null;
132- // };
133-
134- // type XXX = NiceIntersection<
135- // Pick<MyRow, 'id' | 'n'>,
136- // Partial<Omit<MyRow, 'id' | 'n'>>
137- // >
138-
139- // AsPrimaryKeyValueRecord<{
140- // [K in PK[number]]: R[K] extends PrimaryKeyValue ? R[K] : never;
141- // }>;
142- //Pick<R, PK[number]>;
143-
144- // const row = {
145- // id: 'abc',
146- // n: null,
147- // b: true,
148- // null: null,
149- // };
150- // const pk = ['id', 'n'] as const;
151- // export type Test = DeleteID<typeof row, typeof pk>;
152-
153- // export const id: Test = {id: 'a', n: 456};
154-
155129/**
156130 * Creates the `{create, set, update, delete}` object for use outside a batch.
157131 */
158- function makeEntityCRUDMutate < R extends Row , PK extends PrimaryKey > (
132+ function makeEntityCRUDMutate < R extends Row , PK extends NormalizedPrimaryKey > (
159133 entityType : string ,
160134 primaryKey : PK ,
161135 zeroCRUD : CRUDMutate ,
@@ -207,9 +181,12 @@ function makeEntityCRUDMutate<R extends Row, PK extends PrimaryKey>(
207181/**
208182 * Creates the `{create, set, update, delete}` object for use inside a batch.
209183 */
210- export function makeBatchCRUDMutate < R extends Row , PK extends PrimaryKey > (
184+ export function makeBatchCRUDMutate <
185+ R extends Row ,
186+ PK extends NormalizedPrimaryKey ,
187+ > (
211188 tableName : string ,
212- schema : Schema ,
189+ schema : NormalizedSchema ,
213190 ops : CRUDOp [ ] ,
214191) : RowCRUDMutate < R , PK > {
215192 const { primaryKey} = schema . tables [ tableName ] ;
@@ -267,7 +244,7 @@ export type CRUDMutator = (
267244 crudArg : CRUDMutationArg ,
268245) => Promise < void > ;
269246
270- export function makeCRUDMutator < S extends Schema > ( schema : S ) : CRUDMutator {
247+ export function makeCRUDMutator ( schema : NormalizedSchema ) : CRUDMutator {
271248 return async function zeroCRUDMutator (
272249 tx : WriteTransaction ,
273250 crudArg : CRUDMutationArg ,
@@ -294,7 +271,7 @@ export function makeCRUDMutator<S extends Schema>(schema: S): CRUDMutator {
294271async function createImpl (
295272 tx : WriteTransaction ,
296273 arg : CreateOp ,
297- schema : Schema ,
274+ schema : NormalizedSchema ,
298275) : Promise < void > {
299276 const key = toPrimaryKeyString (
300277 arg . entityType ,
@@ -306,10 +283,10 @@ async function createImpl(
306283 }
307284}
308285
309- export async function setImpl (
286+ async function setImpl (
310287 tx : WriteTransaction ,
311288 arg : CreateOp | SetOp ,
312- schema : Schema ,
289+ schema : NormalizedSchema ,
313290) : Promise < void > {
314291 const key = toPrimaryKeyString (
315292 arg . entityType ,
@@ -319,10 +296,10 @@ export async function setImpl(
319296 await tx . set ( key , arg . value ) ;
320297}
321298
322- export async function updateImpl (
299+ async function updateImpl (
323300 tx : WriteTransaction ,
324301 arg : UpdateOp ,
325- schema : Schema ,
302+ schema : NormalizedSchema ,
326303) : Promise < void > {
327304 const key = toPrimaryKeyString (
328305 arg . entityType ,
@@ -338,10 +315,10 @@ export async function updateImpl(
338315 await tx . set ( key , next ) ;
339316}
340317
341- export async function deleteImpl (
318+ async function deleteImpl (
342319 tx : WriteTransaction ,
343320 arg : DeleteOp ,
344- schema : Schema ,
321+ schema : NormalizedSchema ,
345322) : Promise < void > {
346323 const key = toPrimaryKeyString (
347324 arg . entityType ,
@@ -350,3 +327,14 @@ export async function deleteImpl(
350327 ) ;
351328 await tx . del ( key ) ;
352329}
330+
331+ function makeIDFromPrimaryKey (
332+ primaryKey : NormalizedPrimaryKey ,
333+ id : PrimaryKeyValueRecord ,
334+ ) : PrimaryKeyValueRecord {
335+ const rv : Record < string , PrimaryKeyValue > = { } ;
336+ for ( const key of primaryKey ) {
337+ rv [ key ] = id [ key ] ;
338+ }
339+ return rv ;
340+ }
0 commit comments