Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [x] Create many
- [x] ID generation
- [x] CreateManyAndReturn
- [ ] Find
- [x] Find
- [x] Input validation
- [x] Field selection
- [x] Omit
Expand Down Expand Up @@ -56,15 +56,15 @@
- [ ] Prisma client extension
- [ ] Misc
- [ ] Cache validation schemas
- [ ] Compound ID
- [x] Compound ID
- [ ] Cross field comparison
- [x] Many-to-many relation
- [ ] Empty AND/OR/NOT behavior
- [?] Logging
- [?] Error system
- [ ] Error system
- [x] Custom table name
- [x] Custom field name
- [?] Strict undefined check
- [ ] Strict undefined check
- [ ] Access Policy
- [ ] Short-circuit pre-create check for scalar-field only policies
- [ ] Inject "replace into"
Expand All @@ -74,4 +74,4 @@
- [ ] Databases
- [x] SQLite
- [x] PostgreSQL
- [ ] Schema
- [x] Multi-schema
92 changes: 56 additions & 36 deletions packages/runtime/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export type OrderBy<
})
: {});

export type WhereUnique<
export type WhereUniqueInput<
Schema extends SchemaDef,
Model extends GetModels<Schema>
> = AtLeast<
Expand All @@ -336,7 +336,8 @@ export type WhereUnique<
Schema,
GetModel<Schema, Model>['uniqueFields'][Key]
>
: {
: // multi-field unique
{
[Key1 in keyof GetModel<
Schema,
Model
Expand Down Expand Up @@ -373,7 +374,7 @@ type Distinct<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
};

type Cursor<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
cursor?: WhereUnique<Schema, Model>;
cursor?: WhereUniqueInput<Schema, Model>;
};

type Select<
Expand Down Expand Up @@ -591,7 +592,7 @@ export type FindUniqueArgs<
Schema extends SchemaDef,
Model extends GetModels<Schema>
> = {
where?: WhereUnique<Schema, Model>;
where?: WhereUniqueInput<Schema, Model>;
} & SelectIncludeOmit<Schema, Model, true>;

//#endregion
Expand Down Expand Up @@ -711,7 +712,7 @@ type ConnectOrCreatePayload<
Schema extends SchemaDef,
Model extends GetModels<Schema>
> = {
where: WhereUnique<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
create: CreateInput<Schema, Model>;
};

Expand Down Expand Up @@ -768,7 +769,7 @@ export type UpdateArgs<
Model extends GetModels<Schema>
> = {
data: UpdateInput<Schema, Model>;
where: WhereUnique<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
select?: Select<Schema, Model, true>;
include?: Include<Schema, Model>;
omit?: OmitFields<Schema, Model>;
Expand All @@ -789,7 +790,7 @@ export type UpsertArgs<
> = {
create: CreateInput<Schema, Model>;
update: UpdateInput<Schema, Model>;
where: WhereUnique<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
select?: Select<Schema, Model, true>;
include?: Include<Schema, Model>;
omit?: OmitFields<Schema, Model>;
Expand Down Expand Up @@ -858,25 +859,44 @@ type UpdateRelationFieldPayload<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends RelationFields<Schema, Model>
> = Omit<
{
create?: NestedCreateInput<Schema, Model, Field>;
createMany?: NestedCreateManyInput<Schema, Model, Field>;
connect?: ConnectInput<Schema, Model, Field>;
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
disconnect?: DisconnectInput<Schema, Model, Field>;
set?: SetInput<Schema, Model, Field>;
update?: NestedUpdateInput<Schema, Model, Field>;
upsert?: NestedUpsertInput<Schema, Model, Field>;
updateMany?: NestedUpdateManyInput<Schema, Model, Field>;
delete?: NestedDeleteInput<Schema, Model, Field>;
deleteMany?: NestedDeleteManyInput<Schema, Model, Field>;
},
// no "createMany" for non-array fields
FieldIsArray<Schema, Model, Field> extends true
? never
: 'createMany' | 'set'
>;
> = FieldIsArray<Schema, Model, Field> extends true
? ToManyRelationUpdateInput<Schema, Model, Field>
: ToOneRelationUpdateInput<Schema, Model, Field>;

type ToManyRelationUpdateInput<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends RelationFields<Schema, Model>
> = {
create?: NestedCreateInput<Schema, Model, Field>;
createMany?: NestedCreateManyInput<Schema, Model, Field>;
connect?: ConnectInput<Schema, Model, Field>;
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
disconnect?: DisconnectInput<Schema, Model, Field>;
update?: NestedUpdateInput<Schema, Model, Field>;
upsert?: NestedUpsertInput<Schema, Model, Field>;
updateMany?: NestedUpdateManyInput<Schema, Model, Field>;
delete?: NestedDeleteInput<Schema, Model, Field>;
deleteMany?: NestedDeleteManyInput<Schema, Model, Field>;
set?: SetRelationInput<Schema, Model, Field>;
};

type ToOneRelationUpdateInput<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends RelationFields<Schema, Model>
> = {
create?: NestedCreateInput<Schema, Model, Field>;
connect?: ConnectInput<Schema, Model, Field>;
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
update?: NestedUpdateInput<Schema, Model, Field>;
upsert?: NestedUpsertInput<Schema, Model, Field>;
} & (FieldIsOptional<Schema, Model, Field> extends true
? {
disconnect?: DisconnectInput<Schema, Model, Field>;
delete?: NestedDeleteInput<Schema, Model, Field>;
}
: {});

// #endregion

Expand All @@ -886,7 +906,7 @@ export type DeleteArgs<
Schema extends SchemaDef,
Model extends GetModels<Schema>
> = {
where: WhereUnique<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
select?: Select<Schema, Model, true>;
include?: Include<Schema, Model>;
omit?: OmitFields<Schema, Model>;
Expand Down Expand Up @@ -1099,8 +1119,8 @@ type ConnectInput<
Model extends GetModels<Schema>,
Field extends RelationFields<Schema, Model>
> = FieldIsArray<Schema, Model, Field> extends true
? OrArray<WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>>
: WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>;
? OrArray<WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>>
: WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>;

type ConnectOrCreateInput<
Schema extends SchemaDef,
Expand All @@ -1121,16 +1141,16 @@ type DisconnectInput<
Field extends RelationFields<Schema, Model>
> = FieldIsArray<Schema, Model, Field> extends true
? OrArray<
WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>,
WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>,
true
>
: boolean | WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;

type SetInput<
type SetRelationInput<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends RelationFields<Schema, Model>
> = OrArray<WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>>;
> = OrArray<WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>>;

type NestedUpdateInput<
Schema extends SchemaDef,
Expand All @@ -1139,7 +1159,7 @@ type NestedUpdateInput<
> = FieldIsArray<Schema, Model, Field> extends true
? OrArray<
{
where: WhereUnique<
where: WhereUniqueInput<
Schema,
RelationFieldType<Schema, Model, Field>
>;
Expand All @@ -1153,7 +1173,7 @@ type NestedUpdateInput<
>
: XOR<
{
where: WhereUnique<
where: WhereUniqueInput<
Schema,
RelationFieldType<Schema, Model, Field>
>;
Expand All @@ -1176,7 +1196,7 @@ type NestedUpsertInput<
Field extends RelationFields<Schema, Model>
> = OrArray<
{
where: WhereUnique<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
create: CreateInput<
Schema,
RelationFieldType<Schema, Model, Field>,
Expand Down Expand Up @@ -1213,7 +1233,7 @@ type NestedDeleteInput<
Field extends RelationFields<Schema, Model>
> = FieldIsArray<Schema, Model, Field> extends true
? OrArray<
WhereUnique<Schema, RelationFieldType<Schema, Model, Field>>,
WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>,
true
>
: boolean | WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;
Expand Down
13 changes: 5 additions & 8 deletions packages/runtime/src/client/crud/dialects/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { ClientOptions } from '../../options';
import {
buildFieldRef,
buildJoinPairs,
flattenCompoundUniqueFilters,
getField,
getIdFields,
getManyToManyRelation,
Expand Down Expand Up @@ -81,8 +82,9 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
}

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

for (const [key, payload] of Object.entries(where)) {
for (const [key, payload] of Object.entries(_where)) {
if (payload === undefined) {
continue;
}
Expand Down Expand Up @@ -150,13 +152,8 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
}

// call expression builder and combine the results
if (
typeof where === 'object' &&
where !== null &&
'$expr' in where &&
typeof where['$expr'] === 'function'
) {
result = this.and(eb, result, where['$expr'](eb));
if ('$expr' in _where && typeof _where['$expr'] === 'function') {
result = this.and(eb, result, _where['$expr'](eb));
}

return result;
Expand Down
Loading