@shivaedev/effect-prisma 0.0.0 → 0.1.0
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 +14 -0
- package/LICENSE +21 -0
- package/README.md +120 -2
- package/dist/database.d.ts +35 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +83 -0
- package/dist/database.js.map +1 -0
- package/dist/error.d.ts +52 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +48 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/client-lifecycle.d.ts +7 -0
- package/dist/internal/client-lifecycle.d.ts.map +1 -0
- package/dist/internal/client-lifecycle.js +19 -0
- package/dist/internal/client-lifecycle.js.map +1 -0
- package/dist/internal/executor.d.ts +16 -0
- package/dist/internal/executor.d.ts.map +1 -0
- package/dist/internal/executor.js +2 -0
- package/dist/internal/executor.js.map +1 -0
- package/dist/internal/promise.d.ts +4 -0
- package/dist/internal/promise.d.ts.map +1 -0
- package/dist/internal/promise.js +9 -0
- package/dist/internal/promise.js.map +1 -0
- package/dist/internal/recipe.d.ts +13 -0
- package/dist/internal/recipe.d.ts.map +1 -0
- package/dist/internal/recipe.js +37 -0
- package/dist/internal/recipe.js.map +1 -0
- package/dist/internal/relation-runtime.d.ts +5 -0
- package/dist/internal/relation-runtime.d.ts.map +1 -0
- package/dist/internal/relation-runtime.js +88 -0
- package/dist/internal/relation-runtime.js.map +1 -0
- package/dist/internal/testing.d.ts +11 -0
- package/dist/internal/testing.d.ts.map +1 -0
- package/dist/internal/testing.js +9 -0
- package/dist/internal/testing.js.map +1 -0
- package/dist/internal/transaction.d.ts +14 -0
- package/dist/internal/transaction.d.ts.map +1 -0
- package/dist/internal/transaction.js +79 -0
- package/dist/internal/transaction.js.map +1 -0
- package/dist/internal/vitest-database.d.ts +18 -0
- package/dist/internal/vitest-database.d.ts.map +1 -0
- package/dist/internal/vitest-database.js +73 -0
- package/dist/internal/vitest-database.js.map +1 -0
- package/dist/relation/prisma-methods.d.ts +53 -0
- package/dist/relation/prisma-methods.d.ts.map +1 -0
- package/dist/relation/prisma-methods.js +2 -0
- package/dist/relation/prisma-methods.js.map +1 -0
- package/dist/relation.d.ts +27 -0
- package/dist/relation.d.ts.map +1 -0
- package/dist/relation.js +2 -0
- package/dist/relation.js.map +1 -0
- package/dist/testing/transaction.d.ts +10 -0
- package/dist/testing/transaction.d.ts.map +1 -0
- package/dist/testing/transaction.js +6 -0
- package/dist/testing/transaction.js.map +1 -0
- package/dist/testing/types.d.ts +29 -0
- package/dist/testing/types.d.ts.map +1 -0
- package/dist/testing/types.js +2 -0
- package/dist/testing/types.js.map +1 -0
- package/dist/testing/vitest.d.ts +6 -0
- package/dist/testing/vitest.d.ts.map +1 -0
- package/dist/testing/vitest.js +28 -0
- package/dist/testing/vitest.js.map +1 -0
- package/dist/testing.d.ts +4 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +3 -0
- package/dist/testing.js.map +1 -0
- package/package.json +69 -4
- package/src/database.ts +232 -0
- package/src/error.ts +89 -0
- package/src/index.ts +14 -0
- package/src/internal/client-lifecycle.ts +31 -0
- package/src/internal/executor.ts +28 -0
- package/src/internal/promise.ts +16 -0
- package/src/internal/recipe.ts +71 -0
- package/src/internal/relation-runtime.ts +217 -0
- package/src/internal/testing.ts +32 -0
- package/src/internal/transaction.ts +149 -0
- package/src/internal/vitest-database.ts +210 -0
- package/src/relation/prisma-methods.ts +192 -0
- package/src/relation.ts +104 -0
- package/src/testing/transaction.ts +28 -0
- package/src/testing/types.ts +67 -0
- package/src/testing/vitest.ts +67 -0
- package/src/testing.ts +8 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { Contract as PrismaContract } from "@prisma-next/contract/types";
|
|
2
|
+
import type { SqlStorage } from "@prisma-next/sql-contract/types";
|
|
3
|
+
import type {
|
|
4
|
+
AggregateBuilder,
|
|
5
|
+
AggregateResult,
|
|
6
|
+
AggregateSpec,
|
|
7
|
+
DefaultModelRow,
|
|
8
|
+
GroupedCollection,
|
|
9
|
+
Collection as PrismaCollection,
|
|
10
|
+
} from "@prisma-next/sql-orm-client";
|
|
11
|
+
import type { Effect, Stream } from "effect";
|
|
12
|
+
import type { PrismaError } from "../error.js";
|
|
13
|
+
import type { CollectionResult, Relation } from "../relation.js";
|
|
14
|
+
|
|
15
|
+
type AnyFunction = (...arguments_: ReadonlyArray<never>) => unknown;
|
|
16
|
+
type AnyPostgresContract = PrismaContract<SqlStorage>;
|
|
17
|
+
type FieldTuple<Row> = readonly [keyof Row & string, ...(keyof Row & string)[]];
|
|
18
|
+
type Simplify<Value> = { [Key in keyof Value]: Value[Key] };
|
|
19
|
+
|
|
20
|
+
type TerminalMethod<Method, Requirement> = Method extends (
|
|
21
|
+
...arguments_: infer Arguments
|
|
22
|
+
) => PromiseLike<infer Value>
|
|
23
|
+
? (
|
|
24
|
+
...arguments_: Arguments
|
|
25
|
+
) => Effect.Effect<Awaited<Value>, PrismaError, Requirement>
|
|
26
|
+
: never;
|
|
27
|
+
|
|
28
|
+
type SelectMethod<
|
|
29
|
+
Collection,
|
|
30
|
+
Requirement,
|
|
31
|
+
Contract,
|
|
32
|
+
Model extends string,
|
|
33
|
+
> = Contract extends AnyPostgresContract
|
|
34
|
+
? Collection extends PrismaCollection<
|
|
35
|
+
Contract,
|
|
36
|
+
Model,
|
|
37
|
+
infer _Row,
|
|
38
|
+
infer State
|
|
39
|
+
>
|
|
40
|
+
? {
|
|
41
|
+
select<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
|
|
42
|
+
...fields: Fields
|
|
43
|
+
): Relation<
|
|
44
|
+
PrismaCollection<
|
|
45
|
+
Contract,
|
|
46
|
+
Model,
|
|
47
|
+
Pick<DefaultModelRow<Contract, Model>, Fields[number]>,
|
|
48
|
+
State
|
|
49
|
+
>,
|
|
50
|
+
Requirement,
|
|
51
|
+
Contract,
|
|
52
|
+
Model
|
|
53
|
+
>;
|
|
54
|
+
}
|
|
55
|
+
: Record<never, never>
|
|
56
|
+
: Collection extends { select: infer Select extends AnyFunction }
|
|
57
|
+
? {
|
|
58
|
+
select: Select extends (...arguments_: infer Arguments) => infer Result
|
|
59
|
+
? (
|
|
60
|
+
...arguments_: Arguments
|
|
61
|
+
) => Result extends object
|
|
62
|
+
? Relation<Result, Requirement, Contract, Model>
|
|
63
|
+
: never
|
|
64
|
+
: never;
|
|
65
|
+
}
|
|
66
|
+
: Record<never, never>;
|
|
67
|
+
|
|
68
|
+
type AggregateConfigure<Collection> = Collection extends {
|
|
69
|
+
aggregate: infer Method extends AnyFunction;
|
|
70
|
+
}
|
|
71
|
+
? Exclude<Parameters<Method>[1], undefined>
|
|
72
|
+
: never;
|
|
73
|
+
|
|
74
|
+
type AggregateSuccess<
|
|
75
|
+
Collection,
|
|
76
|
+
Contract extends AnyPostgresContract,
|
|
77
|
+
Model extends string,
|
|
78
|
+
Spec extends AggregateSpec,
|
|
79
|
+
> =
|
|
80
|
+
Collection extends GroupedCollection<
|
|
81
|
+
Contract,
|
|
82
|
+
Model,
|
|
83
|
+
infer Fields extends FieldTuple<DefaultModelRow<Contract, Model>>
|
|
84
|
+
>
|
|
85
|
+
? Array<
|
|
86
|
+
Simplify<
|
|
87
|
+
Pick<DefaultModelRow<Contract, Model>, Fields[number]> &
|
|
88
|
+
AggregateResult<Spec>
|
|
89
|
+
>
|
|
90
|
+
>
|
|
91
|
+
: AggregateResult<Spec>;
|
|
92
|
+
|
|
93
|
+
type AggregateMethod<
|
|
94
|
+
Collection,
|
|
95
|
+
Requirement,
|
|
96
|
+
Contract,
|
|
97
|
+
Model extends string,
|
|
98
|
+
> = Contract extends AnyPostgresContract
|
|
99
|
+
? Collection extends { aggregate: AnyFunction }
|
|
100
|
+
? {
|
|
101
|
+
aggregate<Spec extends AggregateSpec>(
|
|
102
|
+
make: (aggregate: AggregateBuilder<Contract, Model>) => Spec,
|
|
103
|
+
configure?: AggregateConfigure<Collection>,
|
|
104
|
+
): Effect.Effect<
|
|
105
|
+
AggregateSuccess<Collection, Contract, Model, Spec>,
|
|
106
|
+
PrismaError,
|
|
107
|
+
Requirement
|
|
108
|
+
>;
|
|
109
|
+
}
|
|
110
|
+
: Record<never, never>
|
|
111
|
+
: Record<never, never>;
|
|
112
|
+
|
|
113
|
+
type CollectionMethods<
|
|
114
|
+
Collection,
|
|
115
|
+
Requirement,
|
|
116
|
+
Contract,
|
|
117
|
+
Model extends string,
|
|
118
|
+
> = Contract extends AnyPostgresContract
|
|
119
|
+
? Collection extends PrismaCollection<
|
|
120
|
+
Contract,
|
|
121
|
+
Model,
|
|
122
|
+
infer _Row,
|
|
123
|
+
infer State
|
|
124
|
+
>
|
|
125
|
+
? {
|
|
126
|
+
groupBy<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
|
|
127
|
+
...fields: Fields
|
|
128
|
+
): Relation<
|
|
129
|
+
GroupedCollection<Contract, Model, Fields>,
|
|
130
|
+
Requirement,
|
|
131
|
+
Contract,
|
|
132
|
+
Model
|
|
133
|
+
>;
|
|
134
|
+
cursor(
|
|
135
|
+
values: State extends { readonly hasOrderBy: true }
|
|
136
|
+
? Partial<
|
|
137
|
+
Record<keyof DefaultModelRow<Contract, Model> & string, unknown>
|
|
138
|
+
>
|
|
139
|
+
: never,
|
|
140
|
+
): Relation<Collection, Requirement, Contract, Model>;
|
|
141
|
+
distinct<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
|
|
142
|
+
...fields: Fields
|
|
143
|
+
): Relation<Collection, Requirement, Contract, Model>;
|
|
144
|
+
distinctOn<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
|
|
145
|
+
...fields: State extends { readonly hasOrderBy: true }
|
|
146
|
+
? Fields
|
|
147
|
+
: never
|
|
148
|
+
): Relation<Collection, Requirement, Contract, Model>;
|
|
149
|
+
} & (State extends { readonly hasWhere: true }
|
|
150
|
+
? {
|
|
151
|
+
readonly [Key in
|
|
152
|
+
| "delete"
|
|
153
|
+
| "deleteAll"
|
|
154
|
+
| "deleteCount"]: TerminalMethod<Collection[Key], Requirement>;
|
|
155
|
+
}
|
|
156
|
+
: Record<never, never>)
|
|
157
|
+
: Record<never, never>
|
|
158
|
+
: Record<never, never>;
|
|
159
|
+
|
|
160
|
+
type CollectionConveniences<
|
|
161
|
+
Collection,
|
|
162
|
+
Requirement,
|
|
163
|
+
Contract,
|
|
164
|
+
Model extends string,
|
|
165
|
+
> = Contract extends AnyPostgresContract
|
|
166
|
+
? Collection extends PrismaCollection<
|
|
167
|
+
Contract,
|
|
168
|
+
Model,
|
|
169
|
+
infer Row,
|
|
170
|
+
infer _State
|
|
171
|
+
>
|
|
172
|
+
? {
|
|
173
|
+
readonly stream: Stream.Stream<Row, PrismaError, Requirement>;
|
|
174
|
+
exists(): Effect.Effect<boolean, PrismaError, Requirement>;
|
|
175
|
+
}
|
|
176
|
+
: Record<never, never>
|
|
177
|
+
: CollectionResult<Collection> extends ReadonlyArray<infer Row>
|
|
178
|
+
? {
|
|
179
|
+
readonly stream: Stream.Stream<Row, PrismaError, Requirement>;
|
|
180
|
+
exists(): Effect.Effect<boolean, PrismaError, Requirement>;
|
|
181
|
+
}
|
|
182
|
+
: Record<never, never>;
|
|
183
|
+
|
|
184
|
+
export type PrismaRelationMethods<
|
|
185
|
+
Collection,
|
|
186
|
+
Requirement,
|
|
187
|
+
Contract,
|
|
188
|
+
Model extends string,
|
|
189
|
+
> = SelectMethod<Collection, Requirement, Contract, Model> &
|
|
190
|
+
AggregateMethod<Collection, Requirement, Contract, Model> &
|
|
191
|
+
CollectionMethods<Collection, Requirement, Contract, Model> &
|
|
192
|
+
CollectionConveniences<Collection, Requirement, Contract, Model>;
|
package/src/relation.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { PrismaError } from "./error.js";
|
|
3
|
+
import type { PrismaRelationMethods } from "./relation/prisma-methods.js";
|
|
4
|
+
|
|
5
|
+
type AnyFunction = (...arguments_: ReadonlyArray<never>) => unknown;
|
|
6
|
+
|
|
7
|
+
export type CollectionResult<Collection> = Collection extends {
|
|
8
|
+
all(): infer Result;
|
|
9
|
+
}
|
|
10
|
+
? Awaited<Result>
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
type NormalizeTerminal<Name, Result> = Name extends "first"
|
|
14
|
+
? import("effect").Option.Option<Exclude<Result, null>>
|
|
15
|
+
: Result;
|
|
16
|
+
|
|
17
|
+
type WrapReturn<Name, Result, Requirement, Contract, Model extends string> =
|
|
18
|
+
Result extends PromiseLike<infer Value>
|
|
19
|
+
? Effect.Effect<
|
|
20
|
+
NormalizeTerminal<Name, Awaited<Value>>,
|
|
21
|
+
PrismaError,
|
|
22
|
+
Requirement
|
|
23
|
+
>
|
|
24
|
+
: Result extends object
|
|
25
|
+
? Relation<Result, Requirement, Contract, Model>
|
|
26
|
+
: never;
|
|
27
|
+
|
|
28
|
+
type WrapFunction<
|
|
29
|
+
Name,
|
|
30
|
+
Function_,
|
|
31
|
+
Requirement,
|
|
32
|
+
Contract,
|
|
33
|
+
Model extends string,
|
|
34
|
+
> = Function_ extends {
|
|
35
|
+
(...arguments_: infer Arguments1): infer Result1;
|
|
36
|
+
(...arguments_: infer Arguments2): infer Result2;
|
|
37
|
+
(...arguments_: infer Arguments3): infer Result3;
|
|
38
|
+
(...arguments_: infer Arguments4): infer Result4;
|
|
39
|
+
(...arguments_: infer Arguments5): infer Result5;
|
|
40
|
+
(...arguments_: infer Arguments6): infer Result6;
|
|
41
|
+
}
|
|
42
|
+
? ((
|
|
43
|
+
...arguments_: Arguments1
|
|
44
|
+
) => WrapReturn<Name, Result1, Requirement, Contract, Model>) &
|
|
45
|
+
((
|
|
46
|
+
...arguments_: Arguments2
|
|
47
|
+
) => WrapReturn<Name, Result2, Requirement, Contract, Model>) &
|
|
48
|
+
((
|
|
49
|
+
...arguments_: Arguments3
|
|
50
|
+
) => WrapReturn<Name, Result3, Requirement, Contract, Model>) &
|
|
51
|
+
((
|
|
52
|
+
...arguments_: Arguments4
|
|
53
|
+
) => WrapReturn<Name, Result4, Requirement, Contract, Model>) &
|
|
54
|
+
((
|
|
55
|
+
...arguments_: Arguments5
|
|
56
|
+
) => WrapReturn<Name, Result5, Requirement, Contract, Model>) &
|
|
57
|
+
((
|
|
58
|
+
...arguments_: Arguments6
|
|
59
|
+
) => WrapReturn<Name, Result6, Requirement, Contract, Model>)
|
|
60
|
+
: never;
|
|
61
|
+
|
|
62
|
+
type FunctionKeys<Value> = {
|
|
63
|
+
[Key in keyof Value]-?: Value[Key] extends AnyFunction ? Key : never;
|
|
64
|
+
}[keyof Value];
|
|
65
|
+
|
|
66
|
+
type ExplicitPrismaMethod =
|
|
67
|
+
| "aggregate"
|
|
68
|
+
| "avg"
|
|
69
|
+
| "combine"
|
|
70
|
+
| "count"
|
|
71
|
+
| "cursor"
|
|
72
|
+
| "delete"
|
|
73
|
+
| "deleteAll"
|
|
74
|
+
| "deleteCount"
|
|
75
|
+
| "distinct"
|
|
76
|
+
| "distinctOn"
|
|
77
|
+
| "groupBy"
|
|
78
|
+
| "include"
|
|
79
|
+
| "max"
|
|
80
|
+
| "min"
|
|
81
|
+
| "select"
|
|
82
|
+
| "sum"
|
|
83
|
+
| "variant";
|
|
84
|
+
|
|
85
|
+
type RelationMethods<
|
|
86
|
+
Collection,
|
|
87
|
+
Requirement,
|
|
88
|
+
Contract,
|
|
89
|
+
Model extends string,
|
|
90
|
+
> = {
|
|
91
|
+
readonly [Key in Exclude<
|
|
92
|
+
FunctionKeys<Collection>,
|
|
93
|
+
ExplicitPrismaMethod
|
|
94
|
+
>]: WrapFunction<Key, Collection[Key], Requirement, Contract, Model>;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type Relation<
|
|
98
|
+
Collection,
|
|
99
|
+
Requirement,
|
|
100
|
+
Contract = undefined,
|
|
101
|
+
Model extends string = string,
|
|
102
|
+
> = Effect.Effect<CollectionResult<Collection>, PrismaError, Requirement> &
|
|
103
|
+
RelationMethods<Collection, Requirement, Contract, Model> &
|
|
104
|
+
PrismaRelationMethods<Collection, Requirement, Contract, Model>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { PrismaError } from "../error.js";
|
|
3
|
+
import { getDatabaseTesting } from "../internal/testing.js";
|
|
4
|
+
import type { AnyDatabase, DatabaseRequirement } from "./types.js";
|
|
5
|
+
|
|
6
|
+
type WithTestTransaction = {
|
|
7
|
+
<Database extends AnyDatabase>(
|
|
8
|
+
database: Database,
|
|
9
|
+
): <A, E, R>(
|
|
10
|
+
program: Effect.Effect<A, E, R>,
|
|
11
|
+
) => Effect.Effect<A, E | PrismaError, R | DatabaseRequirement<Database>>;
|
|
12
|
+
<Database extends AnyDatabase, A, E, R>(
|
|
13
|
+
database: Database,
|
|
14
|
+
program: Effect.Effect<A, E, R>,
|
|
15
|
+
): Effect.Effect<A, E | PrismaError, R | DatabaseRequirement<Database>>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const withTestTransaction: WithTestTransaction = ((
|
|
19
|
+
database: AnyDatabase,
|
|
20
|
+
program?: Effect.Effect<unknown, unknown, unknown>,
|
|
21
|
+
) => {
|
|
22
|
+
const run = (effect: Effect.Effect<unknown, unknown, unknown>) =>
|
|
23
|
+
getDatabaseTesting<DatabaseRequirement<typeof database>>(
|
|
24
|
+
database,
|
|
25
|
+
).withTestTransaction(effect);
|
|
26
|
+
|
|
27
|
+
return program === undefined ? run : run(program);
|
|
28
|
+
}) as WithTestTransaction;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { TestContext, TestOptions, Vitest } from "@effect/vitest";
|
|
2
|
+
import type { Contract as PrismaContract } from "@prisma-next/contract/types";
|
|
3
|
+
import type { SqlStorage } from "@prisma-next/sql-contract/types";
|
|
4
|
+
import type { Effect, Layer } from "effect";
|
|
5
|
+
import type { DatabaseDefinition } from "../database.js";
|
|
6
|
+
|
|
7
|
+
type AnyPostgresContract = PrismaContract<SqlStorage>;
|
|
8
|
+
|
|
9
|
+
export type AnyDatabase = Effect.Effect<unknown, never, unknown> & {
|
|
10
|
+
readonly layer: (...arguments_: ReadonlyArray<never>) => Layer.Any;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type DatabaseRequirement<Database> =
|
|
14
|
+
Database extends DatabaseDefinition<AnyPostgresContract, infer Requirement>
|
|
15
|
+
? Requirement
|
|
16
|
+
: never;
|
|
17
|
+
|
|
18
|
+
export type DatabaseService<Database extends AnyDatabase> =
|
|
19
|
+
Effect.Success<Database>;
|
|
20
|
+
|
|
21
|
+
export type DatabaseTest<Database extends AnyDatabase, Provided> = <
|
|
22
|
+
A,
|
|
23
|
+
Eff extends Effect.Effect<unknown, unknown, Provided>,
|
|
24
|
+
>(
|
|
25
|
+
name: string,
|
|
26
|
+
body: (
|
|
27
|
+
database: DatabaseService<Database>,
|
|
28
|
+
context: TestContext,
|
|
29
|
+
) => Generator<Eff, A, never>,
|
|
30
|
+
options?: number | TestOptions,
|
|
31
|
+
) => void;
|
|
32
|
+
|
|
33
|
+
export interface DatabaseTester<Database extends AnyDatabase, Provided>
|
|
34
|
+
extends DatabaseTest<Database, Provided> {
|
|
35
|
+
readonly skip: DatabaseTest<Database, Provided>;
|
|
36
|
+
readonly skipIf: (condition: unknown) => DatabaseTest<Database, Provided>;
|
|
37
|
+
readonly runIf: (condition: unknown) => DatabaseTest<Database, Provided>;
|
|
38
|
+
readonly only: DatabaseTest<Database, Provided>;
|
|
39
|
+
readonly each: <Item>(
|
|
40
|
+
cases: ReadonlyArray<Item>,
|
|
41
|
+
) => <A, Eff extends Effect.Effect<unknown, unknown, Provided>>(
|
|
42
|
+
name: string,
|
|
43
|
+
body: (
|
|
44
|
+
item: Item,
|
|
45
|
+
database: DatabaseService<Database>,
|
|
46
|
+
context: TestContext,
|
|
47
|
+
) => Generator<Eff, A, never>,
|
|
48
|
+
options?: number | TestOptions,
|
|
49
|
+
) => void;
|
|
50
|
+
readonly fails: DatabaseTest<Database, Provided>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type DatabaseIt<
|
|
54
|
+
Database extends AnyDatabase,
|
|
55
|
+
Provided,
|
|
56
|
+
> = Vitest.Methods & {
|
|
57
|
+
readonly effectDB: DatabaseTester<Database, Provided>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export interface MakeDatabaseItOptions<
|
|
61
|
+
Database extends AnyDatabase,
|
|
62
|
+
Provided,
|
|
63
|
+
LayerError,
|
|
64
|
+
> {
|
|
65
|
+
readonly database: Database;
|
|
66
|
+
readonly layer: Layer.Layer<Provided, LayerError>;
|
|
67
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { it as effectIt } from "@effect/vitest";
|
|
2
|
+
import { Effect, Exit, Layer, Scope } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
type FixtureTestApi,
|
|
5
|
+
fixtureName,
|
|
6
|
+
makeDatabaseTester,
|
|
7
|
+
} from "../internal/vitest-database.js";
|
|
8
|
+
import type {
|
|
9
|
+
AnyDatabase,
|
|
10
|
+
DatabaseIt,
|
|
11
|
+
DatabaseRequirement,
|
|
12
|
+
MakeDatabaseItOptions,
|
|
13
|
+
} from "./types.js";
|
|
14
|
+
|
|
15
|
+
export const makeDatabaseIt = <
|
|
16
|
+
Database extends AnyDatabase,
|
|
17
|
+
Provided,
|
|
18
|
+
LayerError,
|
|
19
|
+
>(
|
|
20
|
+
options: MakeDatabaseItOptions<Database, Provided, LayerError> & {
|
|
21
|
+
readonly layer: Layer.Layer<
|
|
22
|
+
Provided | DatabaseRequirement<Database> | Effect.Services<Database>,
|
|
23
|
+
LayerError
|
|
24
|
+
>;
|
|
25
|
+
},
|
|
26
|
+
): DatabaseIt<
|
|
27
|
+
Database,
|
|
28
|
+
Provided | DatabaseRequirement<Database> | Effect.Services<Database>
|
|
29
|
+
> => {
|
|
30
|
+
type Services =
|
|
31
|
+
| Provided
|
|
32
|
+
| DatabaseRequirement<Database>
|
|
33
|
+
| Effect.Services<Database>;
|
|
34
|
+
|
|
35
|
+
const fixtureIt = effectIt.extend(
|
|
36
|
+
fixtureName,
|
|
37
|
+
{ scope: "worker" },
|
|
38
|
+
// biome-ignore lint/correctness/noEmptyPattern: Vitest fixtures require a destructured context parameter.
|
|
39
|
+
async ({}, { onCleanup }) => {
|
|
40
|
+
const scope = Effect.runSync(Scope.make());
|
|
41
|
+
onCleanup(() => Effect.runPromise(Scope.close(scope, Exit.void)));
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
return await Effect.runPromise(
|
|
45
|
+
Layer.buildWithScope(options.layer, scope),
|
|
46
|
+
);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
await Effect.runPromise(Scope.close(scope, Exit.void));
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const effectDB = makeDatabaseTester(
|
|
55
|
+
fixtureIt as unknown as FixtureTestApi<Services>,
|
|
56
|
+
options.database,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return new Proxy(effectIt, {
|
|
60
|
+
get(target, property, receiver) {
|
|
61
|
+
if (property === "effectDB") {
|
|
62
|
+
return effectDB;
|
|
63
|
+
}
|
|
64
|
+
return Reflect.get(target, property, receiver);
|
|
65
|
+
},
|
|
66
|
+
}) as unknown as DatabaseIt<Database, Services>;
|
|
67
|
+
};
|
package/src/testing.ts
ADDED