Skip to content

Commit 01580a7

Browse files
authored
feat: compound id fields support (#16)
1 parent b4321e7 commit 01580a7

File tree

12 files changed

+1013
-156
lines changed

12 files changed

+1013
-156
lines changed

TODO.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [x] Create many
1717
- [x] ID generation
1818
- [x] CreateManyAndReturn
19-
- [ ] Find
19+
- [x] Find
2020
- [x] Input validation
2121
- [x] Field selection
2222
- [x] Omit
@@ -56,15 +56,15 @@
5656
- [ ] Prisma client extension
5757
- [ ] Misc
5858
- [ ] Cache validation schemas
59-
- [ ] Compound ID
59+
- [x] Compound ID
6060
- [ ] Cross field comparison
6161
- [x] Many-to-many relation
6262
- [ ] Empty AND/OR/NOT behavior
6363
- [?] Logging
64-
- [?] Error system
64+
- [ ] Error system
6565
- [x] Custom table name
6666
- [x] Custom field name
67-
- [?] Strict undefined check
67+
- [ ] Strict undefined check
6868
- [ ] Access Policy
6969
- [ ] Short-circuit pre-create check for scalar-field only policies
7070
- [ ] Inject "replace into"
@@ -74,4 +74,4 @@
7474
- [ ] Databases
7575
- [x] SQLite
7676
- [x] PostgreSQL
77-
- [ ] Schema
77+
- [x] Multi-schema

packages/runtime/src/client/crud-types.ts

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export type OrderBy<
323323
})
324324
: {});
325325

326-
export type WhereUnique<
326+
export type WhereUniqueInput<
327327
Schema extends SchemaDef,
328328
Model extends GetModels<Schema>
329329
> = AtLeast<
@@ -336,7 +336,8 @@ export type WhereUnique<
336336
Schema,
337337
GetModel<Schema, Model>['uniqueFields'][Key]
338338
>
339-
: {
339+
: // multi-field unique
340+
{
340341
[Key1 in keyof GetModel<
341342
Schema,
342343
Model
@@ -373,7 +374,7 @@ type Distinct<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
373374
};
374375

375376
type Cursor<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
376-
cursor?: WhereUnique<Schema, Model>;
377+
cursor?: WhereUniqueInput<Schema, Model>;
377378
};
378379

379380
type Select<
@@ -591,7 +592,7 @@ export type FindUniqueArgs<
591592
Schema extends SchemaDef,
592593
Model extends GetModels<Schema>
593594
> = {
594-
where?: WhereUnique<Schema, Model>;
595+
where?: WhereUniqueInput<Schema, Model>;
595596
} & SelectIncludeOmit<Schema, Model, true>;
596597

597598
//#endregion
@@ -711,7 +712,7 @@ type ConnectOrCreatePayload<
711712
Schema extends SchemaDef,
712713
Model extends GetModels<Schema>
713714
> = {
714-
where: WhereUnique<Schema, Model>;
715+
where: WhereUniqueInput<Schema, Model>;
715716
create: CreateInput<Schema, Model>;
716717
};
717718

@@ -768,7 +769,7 @@ export type UpdateArgs<
768769
Model extends GetModels<Schema>
769770
> = {
770771
data: UpdateInput<Schema, Model>;
771-
where: WhereUnique<Schema, Model>;
772+
where: WhereUniqueInput<Schema, Model>;
772773
select?: Select<Schema, Model, true>;
773774
include?: Include<Schema, Model>;
774775
omit?: OmitFields<Schema, Model>;
@@ -789,7 +790,7 @@ export type UpsertArgs<
789790
> = {
790791
create: CreateInput<Schema, Model>;
791792
update: UpdateInput<Schema, Model>;
792-
where: WhereUnique<Schema, Model>;
793+
where: WhereUniqueInput<Schema, Model>;
793794
select?: Select<Schema, Model, true>;
794795
include?: Include<Schema, Model>;
795796
omit?: OmitFields<Schema, Model>;
@@ -858,25 +859,44 @@ type UpdateRelationFieldPayload<
858859
Schema extends SchemaDef,
859860
Model extends GetModels<Schema>,
860861
Field extends RelationFields<Schema, Model>
861-
> = Omit<
862-
{
863-
create?: NestedCreateInput<Schema, Model, Field>;
864-
createMany?: NestedCreateManyInput<Schema, Model, Field>;
865-
connect?: ConnectInput<Schema, Model, Field>;
866-
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
867-
disconnect?: DisconnectInput<Schema, Model, Field>;
868-
set?: SetInput<Schema, Model, Field>;
869-
update?: NestedUpdateInput<Schema, Model, Field>;
870-
upsert?: NestedUpsertInput<Schema, Model, Field>;
871-
updateMany?: NestedUpdateManyInput<Schema, Model, Field>;
872-
delete?: NestedDeleteInput<Schema, Model, Field>;
873-
deleteMany?: NestedDeleteManyInput<Schema, Model, Field>;
874-
},
875-
// no "createMany" for non-array fields
876-
FieldIsArray<Schema, Model, Field> extends true
877-
? never
878-
: 'createMany' | 'set'
879-
>;
862+
> = FieldIsArray<Schema, Model, Field> extends true
863+
? ToManyRelationUpdateInput<Schema, Model, Field>
864+
: ToOneRelationUpdateInput<Schema, Model, Field>;
865+
866+
type ToManyRelationUpdateInput<
867+
Schema extends SchemaDef,
868+
Model extends GetModels<Schema>,
869+
Field extends RelationFields<Schema, Model>
870+
> = {
871+
create?: NestedCreateInput<Schema, Model, Field>;
872+
createMany?: NestedCreateManyInput<Schema, Model, Field>;
873+
connect?: ConnectInput<Schema, Model, Field>;
874+
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
875+
disconnect?: DisconnectInput<Schema, Model, Field>;
876+
update?: NestedUpdateInput<Schema, Model, Field>;
877+
upsert?: NestedUpsertInput<Schema, Model, Field>;
878+
updateMany?: NestedUpdateManyInput<Schema, Model, Field>;
879+
delete?: NestedDeleteInput<Schema, Model, Field>;
880+
deleteMany?: NestedDeleteManyInput<Schema, Model, Field>;
881+
set?: SetRelationInput<Schema, Model, Field>;
882+
};
883+
884+
type ToOneRelationUpdateInput<
885+
Schema extends SchemaDef,
886+
Model extends GetModels<Schema>,
887+
Field extends RelationFields<Schema, Model>
888+
> = {
889+
create?: NestedCreateInput<Schema, Model, Field>;
890+
connect?: ConnectInput<Schema, Model, Field>;
891+
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
892+
update?: NestedUpdateInput<Schema, Model, Field>;
893+
upsert?: NestedUpsertInput<Schema, Model, Field>;
894+
} & (FieldIsOptional<Schema, Model, Field> extends true
895+
? {
896+
disconnect?: DisconnectInput<Schema, Model, Field>;
897+
delete?: NestedDeleteInput<Schema, Model, Field>;
898+
}
899+
: {});
880900

881901
// #endregion
882902

@@ -886,7 +906,7 @@ export type DeleteArgs<
886906
Schema extends SchemaDef,
887907
Model extends GetModels<Schema>
888908
> = {
889-
where: WhereUnique<Schema, Model>;
909+
where: WhereUniqueInput<Schema, Model>;
890910
select?: Select<Schema, Model, true>;
891911
include?: Include<Schema, Model>;
892912
omit?: OmitFields<Schema, Model>;
@@ -1099,8 +1119,8 @@ type ConnectInput<
10991119
Model extends GetModels<Schema>,
11001120
Field extends RelationFields<Schema, Model>
11011121
> = FieldIsArray<Schema, Model, Field> extends true
1102-
? OrArray<WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>>
1103-
: WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>;
1122+
? OrArray<WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>>
1123+
: WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>;
11041124

11051125
type ConnectOrCreateInput<
11061126
Schema extends SchemaDef,
@@ -1121,16 +1141,16 @@ type DisconnectInput<
11211141
Field extends RelationFields<Schema, Model>
11221142
> = FieldIsArray<Schema, Model, Field> extends true
11231143
? OrArray<
1124-
WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>,
1144+
WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>,
11251145
true
11261146
>
11271147
: boolean | WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;
11281148

1129-
type SetInput<
1149+
type SetRelationInput<
11301150
Schema extends SchemaDef,
11311151
Model extends GetModels<Schema>,
11321152
Field extends RelationFields<Schema, Model>
1133-
> = OrArray<WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>>;
1153+
> = OrArray<WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>>;
11341154

11351155
type NestedUpdateInput<
11361156
Schema extends SchemaDef,
@@ -1139,7 +1159,7 @@ type NestedUpdateInput<
11391159
> = FieldIsArray<Schema, Model, Field> extends true
11401160
? OrArray<
11411161
{
1142-
where: WhereUnique<
1162+
where: WhereUniqueInput<
11431163
Schema,
11441164
RelationFieldType<Schema, Model, Field>
11451165
>;
@@ -1153,7 +1173,7 @@ type NestedUpdateInput<
11531173
>
11541174
: XOR<
11551175
{
1156-
where: WhereUnique<
1176+
where: WhereUniqueInput<
11571177
Schema,
11581178
RelationFieldType<Schema, Model, Field>
11591179
>;
@@ -1176,7 +1196,7 @@ type NestedUpsertInput<
11761196
Field extends RelationFields<Schema, Model>
11771197
> = OrArray<
11781198
{
1179-
where: WhereUnique<Schema, Model>;
1199+
where: WhereUniqueInput<Schema, Model>;
11801200
create: CreateInput<
11811201
Schema,
11821202
RelationFieldType<Schema, Model, Field>,
@@ -1213,7 +1233,7 @@ type NestedDeleteInput<
12131233
Field extends RelationFields<Schema, Model>
12141234
> = FieldIsArray<Schema, Model, Field> extends true
12151235
? OrArray<
1216-
WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>,
1236+
WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>,
12171237
true
12181238
>
12191239
: boolean | WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;

packages/runtime/src/client/crud/dialects/base.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type { ClientOptions } from '../../options';
3131
import {
3232
buildFieldRef,
3333
buildJoinPairs,
34+
flattenCompoundUniqueFilters,
3435
getField,
3536
getIdFields,
3637
getManyToManyRelation,
@@ -81,8 +82,9 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
8182
}
8283

8384
let result = this.true(eb);
85+
let _where = flattenCompoundUniqueFilters(this.schema, model, where);
8486

85-
for (const [key, payload] of Object.entries(where)) {
87+
for (const [key, payload] of Object.entries(_where)) {
8688
if (payload === undefined) {
8789
continue;
8890
}
@@ -150,13 +152,8 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
150152
}
151153

152154
// call expression builder and combine the results
153-
if (
154-
typeof where === 'object' &&
155-
where !== null &&
156-
'$expr' in where &&
157-
typeof where['$expr'] === 'function'
158-
) {
159-
result = this.and(eb, result, where['$expr'](eb));
155+
if ('$expr' in _where && typeof _where['$expr'] === 'function') {
156+
result = this.and(eb, result, _where['$expr'](eb));
160157
}
161158

162159
return result;

0 commit comments

Comments
 (0)