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

Add intent to recover prisma #1636

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,10 @@ export class PrismaSessionStorage<T extends PrismaClient>
if (this.getSessionTable() === undefined) {
throw new Error(`PrismaClient does not have a ${this.tableName} table`);
}

this.ready = this.pollForTable().catch((cause) => {
throw new MissingSessionTableError(
`Prisma ${this.tableName} table does not exist. This could happen for a few reasons, see https://github.com/Shopify/shopify-app-js/tree/main/packages/apps/session-storage/shopify-app-session-storage-prisma#troubleshooting for more information`,
cause,
);
});
}

public async storeSession(session: Session): Promise<boolean> {
await this.ready;
await this.isReady();

const data = this.sessionToRow(session);

Expand Down Expand Up @@ -84,7 +77,7 @@ export class PrismaSessionStorage<T extends PrismaClient>
}

public async loadSession(id: string): Promise<Session | undefined> {
await this.ready;
await this.isReady();

const row = await this.getSessionTable().findUnique({
where: {id},
Expand All @@ -98,7 +91,7 @@ export class PrismaSessionStorage<T extends PrismaClient>
}

public async deleteSession(id: string): Promise<boolean> {
await this.ready;
await this.isReady();

try {
await this.getSessionTable().delete({where: {id}});
Expand All @@ -110,15 +103,15 @@ export class PrismaSessionStorage<T extends PrismaClient>
}

public async deleteSessions(ids: string[]): Promise<boolean> {
await this.ready;
await this.isReady();

await this.getSessionTable().deleteMany({where: {id: {in: ids}}});

return true;
}

public async findSessionsByShop(shop: string): Promise<Session[]> {
await this.ready;
await this.isReady();

const sessions = await this.getSessionTable().findMany({
where: {shop},
Expand All @@ -129,6 +122,22 @@ export class PrismaSessionStorage<T extends PrismaClient>
return sessions.map((session) => this.rowToSession(session));
}

private async isReady() {
if (this.ready) {
return this.ready;
}
return this.pollForTable()
.then(() => {
this.ready = Promise.resolve();
})
.catch((cause) => {
throw new MissingSessionTableError(
`Prisma ${this.tableName} table does not exist. This could happen for a few reasons, see https://github.com/Shopify/shopify-app-js/tree/main/packages/apps/session-storage/shopify-app-session-storage-prisma#troubleshooting for more information`,
cause,
);
});
}

private async pollForTable(): Promise<void> {
const promise = new Promise<void>((resolve, reject) => {
let retries = 0;
Expand Down
Loading