@shivaedev/effect-prisma 0.1.0 → 0.3.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 +22 -0
- package/README.md +44 -5
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +1 -0
- package/dist/database.js.map +1 -1
- package/dist/internal/executor.d.ts +2 -1
- package/dist/internal/executor.d.ts.map +1 -1
- package/dist/internal/query-execution.d.ts +6 -0
- package/dist/internal/query-execution.d.ts.map +1 -0
- package/dist/internal/query-execution.js +5 -0
- package/dist/internal/query-execution.js.map +1 -0
- package/dist/internal/recipe.d.ts.map +1 -1
- package/dist/internal/recipe.js +66 -11
- package/dist/internal/recipe.js.map +1 -1
- package/dist/internal/relation-plan.d.ts +8 -0
- package/dist/internal/relation-plan.d.ts.map +1 -0
- package/dist/internal/relation-plan.js +10 -0
- package/dist/internal/relation-plan.js.map +1 -0
- package/dist/internal/relation-runtime.d.ts.map +1 -1
- package/dist/internal/relation-runtime.js +24 -18
- package/dist/internal/relation-runtime.js.map +1 -1
- package/dist/internal/relation-stream.d.ts +7 -0
- package/dist/internal/relation-stream.d.ts.map +1 -0
- package/dist/internal/relation-stream.js +28 -0
- package/dist/internal/relation-stream.js.map +1 -0
- package/dist/internal/transaction.d.ts.map +1 -1
- package/dist/internal/transaction.js +2 -1
- package/dist/internal/transaction.js.map +1 -1
- package/dist/relation/include-metadata.d.ts +34 -0
- package/dist/relation/include-metadata.d.ts.map +1 -0
- package/dist/relation/include-metadata.js +2 -0
- package/dist/relation/include-metadata.js.map +1 -0
- package/dist/relation/include.d.ts +25 -0
- package/dist/relation/include.d.ts.map +1 -0
- package/dist/relation/include.js +2 -0
- package/dist/relation/include.js.map +1 -0
- package/dist/relation/prisma-methods.d.ts +5 -2
- package/dist/relation/prisma-methods.d.ts.map +1 -1
- package/dist/relation.d.ts +8 -1
- package/dist/relation.d.ts.map +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js.map +1 -1
- package/package.json +5 -5
- package/src/database.ts +1 -0
- package/src/internal/executor.ts +2 -1
- package/src/internal/query-execution.ts +10 -0
- package/src/internal/recipe.ts +96 -19
- package/src/internal/relation-plan.ts +22 -0
- package/src/internal/relation-runtime.ts +50 -43
- package/src/internal/relation-stream.ts +66 -0
- package/src/internal/transaction.ts +2 -1
- package/src/relation/include-metadata.ts +102 -0
- package/src/relation/include.ts +169 -0
- package/src/relation/prisma-methods.ts +5 -1
- package/src/relation.ts +15 -1
- package/src/testing.ts +3 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Contract as PrismaContract } from "@prisma-next/contract/types";
|
|
2
|
+
import type { SqlStorage } from "@prisma-next/sql-contract/types";
|
|
3
|
+
import type { RelationsOf } from "@prisma-next/sql-orm-client";
|
|
4
|
+
|
|
5
|
+
export type AnyPostgresContract = PrismaContract<SqlStorage>;
|
|
6
|
+
|
|
7
|
+
type RelationDefinition<
|
|
8
|
+
Contract extends AnyPostgresContract,
|
|
9
|
+
Model extends string,
|
|
10
|
+
RelationName extends string,
|
|
11
|
+
> = RelationName extends keyof RelationsOf<Contract, Model>
|
|
12
|
+
? RelationsOf<Contract, Model>[RelationName]
|
|
13
|
+
: never;
|
|
14
|
+
|
|
15
|
+
type RelationCardinalityOf<
|
|
16
|
+
Contract extends AnyPostgresContract,
|
|
17
|
+
Model extends string,
|
|
18
|
+
RelationName extends string,
|
|
19
|
+
> =
|
|
20
|
+
RelationDefinition<Contract, Model, RelationName> extends {
|
|
21
|
+
readonly cardinality: infer Cardinality extends string;
|
|
22
|
+
}
|
|
23
|
+
? Cardinality
|
|
24
|
+
: never;
|
|
25
|
+
|
|
26
|
+
export type IsToManyRelation<
|
|
27
|
+
Contract extends AnyPostgresContract,
|
|
28
|
+
Model extends string,
|
|
29
|
+
RelationName extends string,
|
|
30
|
+
> =
|
|
31
|
+
RelationCardinalityOf<Contract, Model, RelationName> extends "1:N" | "N:M"
|
|
32
|
+
? true
|
|
33
|
+
: false;
|
|
34
|
+
|
|
35
|
+
export type RelatedModelNameOf<
|
|
36
|
+
Contract extends AnyPostgresContract,
|
|
37
|
+
Model extends string,
|
|
38
|
+
RelationName extends string,
|
|
39
|
+
> =
|
|
40
|
+
RelationDefinition<Contract, Model, RelationName> extends {
|
|
41
|
+
readonly to: { readonly model: infer Related extends string };
|
|
42
|
+
}
|
|
43
|
+
? Related
|
|
44
|
+
: never;
|
|
45
|
+
|
|
46
|
+
type RelationLocalFields<
|
|
47
|
+
Contract extends AnyPostgresContract,
|
|
48
|
+
Model extends string,
|
|
49
|
+
RelationName extends string,
|
|
50
|
+
> =
|
|
51
|
+
RelationDefinition<Contract, Model, RelationName> extends {
|
|
52
|
+
readonly on: {
|
|
53
|
+
readonly localFields: infer Fields extends readonly string[];
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
? Fields
|
|
57
|
+
: readonly [];
|
|
58
|
+
|
|
59
|
+
type DefaultNamespace<Contract extends AnyPostgresContract> =
|
|
60
|
+
Contract["domain"]["namespaces"][keyof Contract["domain"]["namespaces"]];
|
|
61
|
+
|
|
62
|
+
type ModelFields<Contract extends AnyPostgresContract, Model extends string> =
|
|
63
|
+
DefaultNamespace<Contract> extends {
|
|
64
|
+
readonly models: infer Models;
|
|
65
|
+
}
|
|
66
|
+
? Model extends keyof Models
|
|
67
|
+
? Models[Model] extends { readonly fields: infer Fields }
|
|
68
|
+
? Fields
|
|
69
|
+
: never
|
|
70
|
+
: never
|
|
71
|
+
: never;
|
|
72
|
+
|
|
73
|
+
type AnyNullableField<
|
|
74
|
+
Fields,
|
|
75
|
+
Names extends readonly string[],
|
|
76
|
+
> = Names extends readonly [
|
|
77
|
+
infer Head extends string,
|
|
78
|
+
...infer Tail extends readonly string[],
|
|
79
|
+
]
|
|
80
|
+
? Head extends keyof Fields
|
|
81
|
+
? Fields[Head] extends { readonly nullable: true }
|
|
82
|
+
? true
|
|
83
|
+
: AnyNullableField<Fields, Tail>
|
|
84
|
+
: true
|
|
85
|
+
: false;
|
|
86
|
+
|
|
87
|
+
export type IncludedRelationValue<
|
|
88
|
+
Contract extends AnyPostgresContract,
|
|
89
|
+
Model extends string,
|
|
90
|
+
RelationName extends string,
|
|
91
|
+
Value,
|
|
92
|
+
> =
|
|
93
|
+
RelationCardinalityOf<Contract, Model, RelationName> extends "1:N" | "N:M"
|
|
94
|
+
? Array<Value>
|
|
95
|
+
: RelationCardinalityOf<Contract, Model, RelationName> extends "N:1"
|
|
96
|
+
? AnyNullableField<
|
|
97
|
+
ModelFields<Contract, Model>,
|
|
98
|
+
RelationLocalFields<Contract, Model, RelationName>
|
|
99
|
+
> extends true
|
|
100
|
+
? Value | null
|
|
101
|
+
: Value
|
|
102
|
+
: Value | null;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DefaultModelRow,
|
|
3
|
+
Collection as PrismaCollection,
|
|
4
|
+
RelationNames,
|
|
5
|
+
} from "@prisma-next/sql-orm-client";
|
|
6
|
+
import type { Relation, RelationQuery } from "../relation.js";
|
|
7
|
+
import type {
|
|
8
|
+
AnyPostgresContract,
|
|
9
|
+
IncludedRelationValue,
|
|
10
|
+
IsToManyRelation,
|
|
11
|
+
RelatedModelNameOf,
|
|
12
|
+
} from "./include-metadata.js";
|
|
13
|
+
|
|
14
|
+
type Simplify<Value> = { [Key in keyof Value]: Value[Key] };
|
|
15
|
+
|
|
16
|
+
type QueryValue<Query> =
|
|
17
|
+
Query extends RelationQuery<
|
|
18
|
+
infer Value,
|
|
19
|
+
infer _Requirement,
|
|
20
|
+
infer _Contract,
|
|
21
|
+
infer _Model
|
|
22
|
+
>
|
|
23
|
+
? Value
|
|
24
|
+
: never;
|
|
25
|
+
|
|
26
|
+
type AcceptRelatedQuery<
|
|
27
|
+
Query,
|
|
28
|
+
Contract extends AnyPostgresContract,
|
|
29
|
+
Model extends string,
|
|
30
|
+
> =
|
|
31
|
+
Query extends RelationQuery<
|
|
32
|
+
infer _Value,
|
|
33
|
+
infer _Requirement,
|
|
34
|
+
infer QueryContract,
|
|
35
|
+
infer QueryModel
|
|
36
|
+
>
|
|
37
|
+
? [QueryContract] extends [Contract]
|
|
38
|
+
? [Contract] extends [QueryContract]
|
|
39
|
+
? QueryModel extends Model
|
|
40
|
+
? unknown
|
|
41
|
+
: never
|
|
42
|
+
: never
|
|
43
|
+
: never
|
|
44
|
+
: never;
|
|
45
|
+
|
|
46
|
+
type IncludedQueryValue<
|
|
47
|
+
Query,
|
|
48
|
+
Contract extends AnyPostgresContract,
|
|
49
|
+
Model extends string,
|
|
50
|
+
RelationName extends string,
|
|
51
|
+
> =
|
|
52
|
+
QueryValue<Query> extends ReadonlyArray<infer Row>
|
|
53
|
+
? IncludedRelationValue<Contract, Model, RelationName, Row>
|
|
54
|
+
: QueryValue<Query>;
|
|
55
|
+
|
|
56
|
+
type QueryShapeValue<Shape extends Readonly<Record<string, unknown>>> = {
|
|
57
|
+
readonly [Key in keyof Shape]: QueryValue<Shape[Key]>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type AcceptRelatedQueryShape<
|
|
61
|
+
Shape extends Readonly<Record<string, unknown>>,
|
|
62
|
+
Contract extends AnyPostgresContract,
|
|
63
|
+
Model extends string,
|
|
64
|
+
> = {
|
|
65
|
+
readonly [Key in keyof Shape]: Shape[Key] &
|
|
66
|
+
AcceptRelatedQuery<Shape[Key], Contract, Model>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
type WithIncludedRelation<
|
|
70
|
+
Collection,
|
|
71
|
+
Contract extends AnyPostgresContract,
|
|
72
|
+
Model extends string,
|
|
73
|
+
RelationName extends string,
|
|
74
|
+
Value,
|
|
75
|
+
> =
|
|
76
|
+
Collection extends PrismaCollection<Contract, Model, infer Row, infer State>
|
|
77
|
+
? PrismaCollection<
|
|
78
|
+
Contract,
|
|
79
|
+
Model,
|
|
80
|
+
Simplify<Row & { readonly [Key in RelationName]: Value }>,
|
|
81
|
+
State
|
|
82
|
+
>
|
|
83
|
+
: never;
|
|
84
|
+
|
|
85
|
+
export type IncludeMethod<
|
|
86
|
+
Collection,
|
|
87
|
+
Requirement,
|
|
88
|
+
Contract,
|
|
89
|
+
Model extends string,
|
|
90
|
+
> = Contract extends AnyPostgresContract
|
|
91
|
+
? Collection extends PrismaCollection<
|
|
92
|
+
Contract,
|
|
93
|
+
Model,
|
|
94
|
+
infer _Row,
|
|
95
|
+
infer _State
|
|
96
|
+
>
|
|
97
|
+
? {
|
|
98
|
+
include<RelationName extends RelationNames<Contract, Model>>(
|
|
99
|
+
relationName: RelationName,
|
|
100
|
+
): Relation<
|
|
101
|
+
WithIncludedRelation<
|
|
102
|
+
Collection,
|
|
103
|
+
Contract,
|
|
104
|
+
Model,
|
|
105
|
+
RelationName,
|
|
106
|
+
IncludedRelationValue<
|
|
107
|
+
Contract,
|
|
108
|
+
Model,
|
|
109
|
+
RelationName,
|
|
110
|
+
DefaultModelRow<
|
|
111
|
+
Contract,
|
|
112
|
+
RelatedModelNameOf<Contract, Model, RelationName>
|
|
113
|
+
>
|
|
114
|
+
>
|
|
115
|
+
>,
|
|
116
|
+
Requirement,
|
|
117
|
+
Contract,
|
|
118
|
+
Model
|
|
119
|
+
>;
|
|
120
|
+
include<RelationName extends RelationNames<Contract, Model>, Query>(
|
|
121
|
+
relationName: RelationName,
|
|
122
|
+
query: Query &
|
|
123
|
+
AcceptRelatedQuery<
|
|
124
|
+
Query,
|
|
125
|
+
Contract,
|
|
126
|
+
RelatedModelNameOf<Contract, Model, RelationName>
|
|
127
|
+
>,
|
|
128
|
+
): Relation<
|
|
129
|
+
WithIncludedRelation<
|
|
130
|
+
Collection,
|
|
131
|
+
Contract,
|
|
132
|
+
Model,
|
|
133
|
+
RelationName,
|
|
134
|
+
IncludedQueryValue<Query, Contract, Model, RelationName>
|
|
135
|
+
>,
|
|
136
|
+
Requirement,
|
|
137
|
+
Contract,
|
|
138
|
+
Model
|
|
139
|
+
>;
|
|
140
|
+
include<
|
|
141
|
+
RelationName extends RelationNames<Contract, Model>,
|
|
142
|
+
Shape extends Readonly<Record<string, unknown>>,
|
|
143
|
+
>(
|
|
144
|
+
relationName: RelationName,
|
|
145
|
+
shape: IsToManyRelation<Contract, Model, RelationName> extends true
|
|
146
|
+
? keyof Shape extends never
|
|
147
|
+
? never
|
|
148
|
+
: Shape &
|
|
149
|
+
AcceptRelatedQueryShape<
|
|
150
|
+
Shape,
|
|
151
|
+
Contract,
|
|
152
|
+
RelatedModelNameOf<Contract, Model, RelationName>
|
|
153
|
+
>
|
|
154
|
+
: never,
|
|
155
|
+
): Relation<
|
|
156
|
+
WithIncludedRelation<
|
|
157
|
+
Collection,
|
|
158
|
+
Contract,
|
|
159
|
+
Model,
|
|
160
|
+
RelationName,
|
|
161
|
+
QueryShapeValue<Shape>
|
|
162
|
+
>,
|
|
163
|
+
Requirement,
|
|
164
|
+
Contract,
|
|
165
|
+
Model
|
|
166
|
+
>;
|
|
167
|
+
}
|
|
168
|
+
: Record<never, never>
|
|
169
|
+
: Record<never, never>;
|
|
@@ -10,7 +10,8 @@ import type {
|
|
|
10
10
|
} from "@prisma-next/sql-orm-client";
|
|
11
11
|
import type { Effect, Stream } from "effect";
|
|
12
12
|
import type { PrismaError } from "../error.js";
|
|
13
|
-
import type { CollectionResult, Relation } from "../relation.js";
|
|
13
|
+
import type { CollectionResult, Relation, RelationQuery } from "../relation.js";
|
|
14
|
+
import type { IncludeMethod } from "./include.js";
|
|
14
15
|
|
|
15
16
|
type AnyFunction = (...arguments_: ReadonlyArray<never>) => unknown;
|
|
16
17
|
type AnyPostgresContract = PrismaContract<SqlStorage>;
|
|
@@ -171,12 +172,14 @@ type CollectionConveniences<
|
|
|
171
172
|
>
|
|
172
173
|
? {
|
|
173
174
|
readonly stream: Stream.Stream<Row, PrismaError, Requirement>;
|
|
175
|
+
count(): RelationQuery<number, Requirement, Contract, Model>;
|
|
174
176
|
exists(): Effect.Effect<boolean, PrismaError, Requirement>;
|
|
175
177
|
}
|
|
176
178
|
: Record<never, never>
|
|
177
179
|
: CollectionResult<Collection> extends ReadonlyArray<infer Row>
|
|
178
180
|
? {
|
|
179
181
|
readonly stream: Stream.Stream<Row, PrismaError, Requirement>;
|
|
182
|
+
count(): Effect.Effect<number, PrismaError, Requirement>;
|
|
180
183
|
exists(): Effect.Effect<boolean, PrismaError, Requirement>;
|
|
181
184
|
}
|
|
182
185
|
: Record<never, never>;
|
|
@@ -187,6 +190,7 @@ export type PrismaRelationMethods<
|
|
|
187
190
|
Contract,
|
|
188
191
|
Model extends string,
|
|
189
192
|
> = SelectMethod<Collection, Requirement, Contract, Model> &
|
|
193
|
+
IncludeMethod<Collection, Requirement, Contract, Model> &
|
|
190
194
|
AggregateMethod<Collection, Requirement, Contract, Model> &
|
|
191
195
|
CollectionMethods<Collection, Requirement, Contract, Model> &
|
|
192
196
|
CollectionConveniences<Collection, Requirement, Contract, Model>;
|
package/src/relation.ts
CHANGED
|
@@ -94,11 +94,25 @@ type RelationMethods<
|
|
|
94
94
|
>]: WrapFunction<Key, Collection[Key], Requirement, Contract, Model>;
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
+
declare const RelationQueryTypeId: unique symbol;
|
|
98
|
+
|
|
99
|
+
export type RelationQuery<
|
|
100
|
+
Value,
|
|
101
|
+
Requirement,
|
|
102
|
+
Contract,
|
|
103
|
+
Model extends string,
|
|
104
|
+
> = Effect.Effect<Value, PrismaError, Requirement> & {
|
|
105
|
+
readonly [RelationQueryTypeId]: {
|
|
106
|
+
readonly contract: Contract;
|
|
107
|
+
readonly model: Model;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
97
111
|
export type Relation<
|
|
98
112
|
Collection,
|
|
99
113
|
Requirement,
|
|
100
114
|
Contract = undefined,
|
|
101
115
|
Model extends string = string,
|
|
102
|
-
> =
|
|
116
|
+
> = RelationQuery<CollectionResult<Collection>, Requirement, Contract, Model> &
|
|
103
117
|
RelationMethods<Collection, Requirement, Contract, Model> &
|
|
104
118
|
PrismaRelationMethods<Collection, Requirement, Contract, Model>;
|