diff --git a/.changeset/metal-ants-marry.md b/.changeset/metal-ants-marry.md new file mode 100644 index 00000000000..e6d0cbdf1ef --- /dev/null +++ b/.changeset/metal-ants-marry.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/fields-document': minor +--- + +Added support for SQLite with Prisma diff --git a/packages-next/fields-document/src/Implementation.js b/packages-next/fields-document/src/Implementation.js index 72673aa7459..67abe0786a2 100644 --- a/packages-next/fields-document/src/Implementation.js +++ b/packages-next/fields-document/src/Implementation.js @@ -38,7 +38,14 @@ export class DocumentImplementation extends Implementation { gqlOutputFieldResolvers() { return { [this.path]: (item, _args, context) => { - const document = item[this.path]; + let document = item[this.path]; + if (this.adapter.listAdapter.parentAdapter.provider === 'sqlite') { + // we store document data as a string on sqlite because Prisma doesn't support Json on sqlite + // https://github.com/prisma/prisma/issues/3786 + try { + document = JSON.parse(document); + } catch (err) {} + } if (!Array.isArray(document)) return null; return { document: hydrateRelationships => @@ -69,6 +76,11 @@ export class DocumentImplementation extends Implementation { // knex expects the json to be stringified return JSON.stringify(nodes); } + if (this.adapter.listAdapter.parentAdapter.provider === 'sqlite') { + // we store document data as a string on sqlite because Prisma doesn't support Json on sqlite + // https://github.com/prisma/prisma/issues/3786 + return JSON.stringify(nodes); + } return nodes; } @@ -121,11 +133,6 @@ export class KnexDocumentInterface extends CommonDocumentInterface(KnexFieldAdap export class PrismaDocumentInterface extends CommonDocumentInterface(PrismaFieldAdapter) { constructor() { super(...arguments); - if (this.listAdapter.parentAdapter.provider === 'sqlite') { - throw new Error( - `PrismaAdapter provider "sqlite" does not support field type "${this.field.constructor.name}"` - ); - } // Error rather than ignoring invalid config // We totally can index these values, it's just not trivial. See issue #1297 if (this.config.isIndexed) { @@ -137,6 +144,15 @@ export class PrismaDocumentInterface extends CommonDocumentInterface(PrismaField } getPrismaSchema() { - return [this._schemaField({ type: 'Json' })]; + return [ + this._schemaField({ + type: + this.listAdapter.parentAdapter.provider === 'sqlite' + ? // we store document data as a string on sqlite because Prisma doesn't support Json on sqlite + // https://github.com/prisma/prisma/issues/3786 + 'String' + : 'Json', + }), + ]; } } diff --git a/packages-next/fields-document/src/test-fixtures.js b/packages-next/fields-document/src/test-fixtures.js index 9ca3a2222c1..7ce37e182a8 100644 --- a/packages-next/fields-document/src/test-fixtures.js +++ b/packages-next/fields-document/src/test-fixtures.js @@ -11,7 +11,6 @@ export const exampleValue2 = () => [ export const supportsUnique = false; export const fieldName = 'content'; export const subfieldName = 'document'; -export const unSupportedAdapterList = ['prisma_sqlite']; export const fieldConfig = () => ({ ___validateAndNormalize: x => x });