Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanups in the prisma adapter #5319

Merged
merged 2 commits into from
Mar 31, 2021
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
5 changes: 5 additions & 0 deletions .changeset/afraid-pumpkins-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/adapter-prisma-legacy': major
---

Removed legacy `PrismaAdapter.listAdapterClass`, `PrismaAdapter.postConnect()`, and `PrismaAdapter.checkDatabaseVersion()`.
61 changes: 9 additions & 52 deletions packages/adapter-prisma/src/adapter-prisma.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ class PrismaAdapter {
constructor(config = {}) {
this.config = { ...config };
this.listAdapters = {};
this.listAdapterClass = undefined;

this.listAdapterClass = PrismaListAdapter;
this.name = 'prisma';
this.provider = this.config.provider || 'postgresql';

this.enableLogging = this.config.enableLogging || false;
this.url = this.config.url || process.env.DATABASE_URL;
}

newListAdapter(key, adapterConfig) {
this.listAdapters[key] = new this.listAdapterClass(key, this, adapterConfig);
this.listAdapters[key] = new PrismaListAdapter(key, this, adapterConfig);
return this.listAdapters[key];
}

Expand All @@ -26,38 +22,6 @@ class PrismaAdapter {

async connect({ rels }) {
// Connect to the database
await this._connect({ rels }, this.config);

// Set up all list adapters
try {
// Validate the minimum database version requirements are met.
await this.checkDatabaseVersion();

const taskResults = await this.postConnect({ rels });
const errors = taskResults.filter(({ isRejected }) => isRejected).map(({ reason }) => reason);

if (errors.length) {
if (errors.length === 1) throw errors[0];
const error = new Error('Multiple errors in PrismaAdapter.postConnect():');
error.errors = errors;
throw error;
}
} catch (error) {
// close the database connection if it was opened
try {
await this.disconnect();
} catch (closeError) {
// Add the inability to close the database connection as an additional
// error
error.errors = error.errors || [];
error.errors.push(closeError);
}
// re-throw the error
throw error;
}
}

async _connect() {
// the adapter was already connected since we have a prisma client
// it may have been disconnected since it was connected though
// so connect but don't regenerate the prisma client
Expand All @@ -68,11 +32,16 @@ class PrismaAdapter {
if (!this.config.prismaClient) {
throw new Error('You must pass the prismaClient option to connect to a database');
}
const PrismaClient = this.config.prismaClient;
this.prisma = new PrismaClient({
this.prisma = new this.config.prismaClient({
log: this.enableLogging && ['query'],
datasources: { [this.provider]: { url: this.url } },
});

// Set up all list adapter models
Object.values(this.listAdapters).forEach(listAdapter => {
listAdapter._setupModel({ rels, prisma: this.prisma });
});

await this.prisma.$connect();
}

Expand Down Expand Up @@ -179,21 +148,9 @@ class PrismaAdapter {
return header + models.join('\n') + '\n' + enums.join('\n');
}

async postConnect({ rels }) {
Object.values(this.listAdapters).forEach(listAdapter => {
listAdapter._postConnect({ rels, prisma: this.prisma });
});

return [];
}

disconnect() {
return this.prisma.$disconnect();
}

async checkDatabaseVersion() {
// FIXME: Decide what/how we want to check things here
}
}

class PrismaListAdapter {
Expand Down Expand Up @@ -294,7 +251,7 @@ class PrismaListAdapter {
return this.fieldAdaptersByPath['id'];
}

_postConnect({ rels, prisma }) {
_setupModel({ rels, prisma }) {
// https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/models#queries-crud
// "By default the name of the property is the lowercase form of the model name,
// e.g. user for a User model or post for a Post model."
Expand Down