Skip to content

Commit

Permalink
Add initial support for isIndexed (#5616)
Browse files Browse the repository at this point in the history
* Add initial support for isIndexed

* Add more types

* Add changeset

* Reset example

* Fix incorrect changeset

* Remove indexing from password, push isIndexed onto adapter config object
  • Loading branch information
timleslie committed May 5, 2021
1 parent 1c02651 commit 3d38946
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .changeset/few-walls-ring.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@keystone-next/keystone': major
'@keystone-next/keystone': minor
---

Upgraded Primsa dependency to `2.22.0`.
6 changes: 6 additions & 0 deletions .changeset/grumpy-spiders-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-next/fields': minor
'@keystone-next/adapter-prisma-legacy': minor
---

Added an `isIndexed` config option to the `text`, `integer`, `float`, `select`, and `timestamp` field types.
1 change: 1 addition & 0 deletions packages-next/fields/src/types/float/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type FloatFieldConfig<
> = FieldConfig<TGeneratedListTypes> & {
isRequired?: boolean;
isUnique?: boolean;
isIndexed?: boolean;
defaultValue?: FieldDefaultValue<number>;
};

Expand Down
1 change: 1 addition & 0 deletions packages-next/fields/src/types/integer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type IntegerFieldConfig<
> = FieldConfig<TGeneratedListTypes> & {
isRequired?: boolean;
isUnique?: boolean;
isIndexed?: boolean;
defaultValue?: FieldDefaultValue<number>;
};

Expand Down
1 change: 1 addition & 0 deletions packages-next/fields/src/types/password/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type PasswordFieldConfig<
> = FieldConfig<TGeneratedListTypes> & {
minLength?: number;
isRequired?: boolean;
isIndexed?: boolean;
bcrypt?: Pick<typeof import('bcryptjs'), 'compare' | 'compareSync' | 'hash' | 'hashSync'>;
};

Expand Down
1 change: 1 addition & 0 deletions packages-next/fields/src/types/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SelectFieldConfig<
displayMode?: 'select' | 'segmented-control';
};
isRequired?: boolean;
isIndexed?: boolean;
isUnique?: boolean;
};

Expand Down
1 change: 1 addition & 0 deletions packages-next/fields/src/types/text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TextFieldConfig<
> = FieldConfig<TGeneratedListTypes> & {
defaultValue?: FieldDefaultValue<string>;
isRequired?: boolean;
isIndexed?: boolean;
isUnique?: boolean;
ui?: {
displayMode?: 'input' | 'textarea';
Expand Down
1 change: 1 addition & 0 deletions packages-next/fields/src/types/timestamp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TimestampFieldConfig<
> = FieldConfig<TGeneratedListTypes> & {
defaultValue?: FieldDefaultValue<string>;
isRequired?: boolean;
isIndexed?: boolean;
isUnique?: boolean;
};

Expand Down
43 changes: 26 additions & 17 deletions packages/adapter-prisma/src/adapter-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,32 @@ class PrismaAdapter {
),
];

const indexes = flatten(
listAdapter.fieldAdapters
.map(({ field }) => field)
.filter(f => f.isRelationship)
.map(f => {
const r = rels.find(r => r.left === f || r.right === f) as Rel;
const isLeft = r.left === f;
if (
(r.cardinality === 'N:1' && isLeft) ||
(r.cardinality === '1:N' && !isLeft) ||
(r.cardinality === '1:1' && isLeft)
) {
return [`@@index([${f.path}Id])`];
}
return [];
})
);
const indexes = [
...flatten(
listAdapter.fieldAdapters
.map(({ field }) => field)
.filter(f => f.isRelationship)
.map(f => {
const r = rels.find(r => r.left === f || r.right === f) as Rel;
const isLeft = r.left === f;
if (
(r.cardinality === 'N:1' && isLeft) ||
(r.cardinality === '1:N' && !isLeft) ||
(r.cardinality === '1:1' && isLeft)
) {
return [`@@index([${f.path}Id])`];
}
return [];
})
),
...flatten(
listAdapter.fieldAdapters
.filter(f => f.config.isIndexed)
.map(f => {
return [`@@index([${f.path}])`];
})
),
];

return `
model ${listAdapter.key} {
Expand Down

1 comment on commit 3d38946

@vercel
Copy link

@vercel vercel bot commented on 3d38946 May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.