Skip to content

Commit

Permalink
SQLite support for the document field (#5173)
Browse files Browse the repository at this point in the history
* SQLite support for the document field

* Remove from unsupported adapter list
  • Loading branch information
emmatown authored Mar 22, 2021
1 parent bc71c1a commit 7debecb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-ants-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/fields-document': minor
---

Added support for SQLite with Prisma
30 changes: 23 additions & 7 deletions packages-next/fields-document/src/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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',
}),
];
}
}
1 change: 0 additions & 1 deletion packages-next/fields-document/src/test-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down

1 comment on commit 7debecb

@vercel
Copy link

@vercel vercel bot commented on 7debecb Mar 22, 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.