This repository was archived by the owner on Aug 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcollection-fixtures.ts
More file actions
83 lines (74 loc) · 2.61 KB
/
Copy pathcollection-fixtures.ts
File metadata and controls
83 lines (74 loc) · 2.61 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { Contract } from '@prisma-next/contract/types';
import type { SqlStorage } from '@prisma-next/sql-contract/types';
import type { ExecutionContext } from '@prisma-next/sql-relational-core/query-lane-context';
import { Collection } from '../src/collection';
import type { MockRuntime, TestContract } from './helpers';
import { createMockRuntime, getTestContext, getTestContract } from './helpers';
export type TestModelName = Extract<keyof TestContract['models'], string>;
export const baseContract = getTestContract();
function contextForContract(contract: Contract<SqlStorage>): ExecutionContext<TestContract> {
const base = getTestContext();
if (contract === baseContract) return base;
return { ...base, contract } as ExecutionContext<TestContract>;
}
export function createCollectionFor<ModelName extends TestModelName>(
modelName: ModelName,
contract: Contract<SqlStorage> = baseContract,
): {
collection: Collection<TestContract, ModelName>;
runtime: MockRuntime;
} {
const runtime = createMockRuntime();
const context = contextForContract(contract);
const collection = new Collection({ runtime, context }, modelName);
return {
collection,
runtime,
};
}
export function createCollection() {
return createCollectionFor('User');
}
export function withReturningCapability(contract: TestContract = baseContract): TestContract {
return {
...contract,
capabilities: {
...contract.capabilities,
returning: {
enabled: true,
},
},
} as TestContract;
}
export function withoutDefaultInInsert(contract: TestContract = baseContract): TestContract {
const clone = structuredClone(contract);
if (clone.capabilities?.['sql']) {
delete (clone.capabilities['sql'] as Record<string, unknown>)['defaultInInsert'];
}
return clone;
}
export function createReturningCollectionWithoutDefaultInInsert<ModelName extends TestModelName>(
modelName: ModelName,
): {
collection: Collection<TestContract, ModelName>;
runtime: MockRuntime;
} {
const runtime = createMockRuntime();
const context = contextForContract(withReturningCapability(withoutDefaultInInsert()));
const collection = new Collection({ runtime, context }, modelName);
return { collection, runtime };
}
export function createReturningCollectionFor<ModelName extends TestModelName>(
modelName: ModelName,
): {
collection: Collection<TestContract, ModelName>;
runtime: MockRuntime;
} {
const runtime = createMockRuntime();
const context = contextForContract(withReturningCapability());
const collection = new Collection({ runtime, context }, modelName);
return {
collection,
runtime,
};
}