Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Jun 14, 2022
1 parent a6eb763 commit 562c888
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
4 changes: 3 additions & 1 deletion packages/auth/src/lib/createAuthToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export async function createAuthToken(
identityField: string,
identity: string,
dbItemAPI: KeystoneDbAPI<any>[string]
): Promise<{ success: false } | { success: true; itemId: string | number; token: string }> {
): Promise<
{ success: false } | { success: true; itemId: string | number | bigint; token: string }
> {
const item = await dbItemAPI.findOne({ where: { [identityField]: identity } });
if (item) {
return { success: true, itemId: item.id, token: generateToken(20) };
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type AuthGqlNames = {
};

export type SendTokenFn = (args: {
itemId: string | number;
itemId: string | number | bigint;
identity: string;
token: string;
context: KeystoneContext;
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/lib/config/applyIdFieldDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { idFieldType } from '../id-field';
export function applyIdFieldDefaults(config: KeystoneConfig): KeystoneConfig['lists'] {
const lists: KeystoneConfig['lists'] = {};
const defaultIdField = config.db.idField ?? { kind: 'cuid' };
if (
defaultIdField.kind === 'autoincrement' &&
defaultIdField.type === 'BigInt' &&
config.db.provider === 'sqlite'
) {
throw new Error(
'BigInt autoincrements are not supported on SQLite but they are configured as the global id field type at db.idField'
);
}
Object.keys(config.lists).forEach(key => {
const listConfig = config.lists[key];
if (listConfig.fields.id) {
Expand All @@ -14,6 +23,15 @@ export function applyIdFieldDefaults(config: KeystoneConfig): KeystoneConfig['li
)} list. This is not allowed, use the idField option instead.`
);
}
if (
listConfig.db?.idField?.kind === 'autoincrement' &&
listConfig.db.idField.type === 'BigInt' &&
config.db.provider === 'sqlite'
) {
throw new Error(
`BigInt autoincrements are not supported on SQLite but they are configured at db.idField on the ${key} list`
);
}
const idField = idFieldType(listConfig.db?.idField ?? defaultIdField);

const fields = { id: idField, ...listConfig.fields };
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/core/prisma-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function assertDbFieldIsValidForIdField(
// this may be loosened in the future
if (field.scalar !== 'String' && field.scalar !== 'Int' && field.scalar !== 'BigInt') {
throw new Error(
`id fields must be either String or Int Prisma scalars but the id field for the ${listKey} list is a ${field.scalar} scalar`
`id fields must be String, Int or BigInt Prisma scalars but the id field for the ${listKey} list is a ${field.scalar} scalar`
);
}
if (field.mode !== 'required') {
Expand Down
28 changes: 12 additions & 16 deletions packages/core/src/lib/id-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ const idParsers = {
},
autoincrementBigInt(val: string | null) {
if (val === null) {
throw userInputError('Only BigInt integer can be passed to id filters');
throw userInputError('Only a bigint can be passed to id filters');
}
const parsed = BigInt(val);
if (typeof parsed === 'bigint') {
return parsed;
}
throw userInputError('Only BigInt integer can be passed to id filters');
throw userInputError('Only a bigint can be passed to id filters');
},
cuid(val: string | null) {
// isCuid is just "it's a string and it starts with c"
Expand Down Expand Up @@ -82,17 +82,16 @@ const filterArg = graphql.arg({ type: IDFilter });

function resolveVal(
input: Exclude<graphql.InferValueFromArg<typeof filterArg>, undefined>,
kind: IdFieldConfig['kind']
parseId: (id: string | null) => unknown
): any {
if (input === null) {
throw userInputError('id filter cannot be null');
}
const idParser = idParsers[kind];
const obj: any = {};
for (const key of ['equals', 'gt', 'gte', 'lt', 'lte'] as const) {
const val = input[key];
if (val !== undefined) {
const parsed = idParser(val);
const parsed = parseId(val);
obj[key] = parsed;
}
}
Expand All @@ -102,30 +101,27 @@ function resolveVal(
if (val === null) {
throw userInputError(`${key} id filter cannot be null`);
}
obj[key] = val.map(x => idParser(x));
obj[key] = val.map(x => parseId(x));
}
}
if (input.not !== undefined) {
obj.not = resolveVal(input.not, kind);
obj.not = resolveVal(input.not, parseId);
}
return obj;
}

export const idFieldType =
(config: IdFieldConfig): FieldTypeFunc<BaseListTypeInfo> =>
meta => {
const parseVal = idParsers[config.kind];
const parseVal =
config.kind === 'autoincrement' && config.type === 'BigInt'
? idParsers.autoincrementBigInt
: idParsers[config.kind];
return fieldType({
kind: 'scalar',
mode: 'required',
scalar:
config.kind === 'autoincrement'
? // SQLite doesn't support autoincrement feature for BigInt fields, solution is using Int field
// because it is 64 bit integer always in SQLite, more info here https://github.com/linq2db/linq2db/issues/930
config.useBigInt && meta.provider !== 'sqlite'
? 'BigInt'
: 'Int'
: 'String',
config.kind === 'autoincrement' ? (config.type === 'BigInt' ? 'BigInt' : 'Int') : 'String',
nativeType: meta.provider === 'postgresql' && config.kind === 'uuid' ? 'Uuid' : undefined,
default: { kind: config.kind },
})({
Expand All @@ -137,7 +133,7 @@ export const idFieldType =
where: {
arg: filterArg,
resolve(val) {
return resolveVal(val, config.kind);
return resolveVal(val, parseVal);
},
},
uniqueWhere: { arg: graphql.arg({ type: graphql.ID }), resolve: parseVal },
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/types/config/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import type { BaseFields, FilterOrderArgs } from './fields';
export type ListSchemaConfig = Record<string, ListConfig<any, BaseFields<BaseListTypeInfo>>>;

export type IdFieldConfig =
| {
kind: 'cuid' | 'uuid';
}
| { kind: 'cuid' | 'uuid' }
| {
kind: 'autoincrement';
useBigInt?: boolean;
/**
* Configures the database type of the id field. Only `Int` is supported on SQLite.
* @default 'Int'
*/
type?: 'Int' | 'BigInt';
};

export type ListConfig<
Expand Down

0 comments on commit 562c888

Please sign in to comment.