Skip to content

Commit

Permalink
Merge pull request #80 from xmtp/rygine/fix-db-version
Browse files Browse the repository at this point in the history
Prevent DB versioning when DB is already open
  • Loading branch information
rygine authored Sep 5, 2023
2 parents ce66361 + 450a6b9 commit 3301cf7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-coins-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/react-sdk": patch
---

- Prevent DB versioning when DB is already open
75 changes: 39 additions & 36 deletions packages/react-sdk/src/helpers/caching/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,42 +75,45 @@ export type GetDBInstanceOptions = {
export const getDbInstance = (options?: GetDBInstanceOptions) => {
const db = options?.db ?? new Dexie("__XMTP__");

// note that duplicate keys will be overwritten
const customSchema = options?.contentTypeConfigs?.reduce(
(result, { schema }) => ({
...result,
...schema,
}),
{} as Record<string, string>,
);

const version = options?.version ?? 1;

db.version(version).stores({
...customSchema,
conversations: `
++id,
[topic+walletAddress],
createdAt,
peerAddress,
topic,
updatedAt,
walletAddress
`,
messages: `
++id,
[conversationTopic+walletAddress],
contentFallback,
contentType,
conversationTopic,
senderAddress,
sentAt,
status,
uuid,
walletAddress,
xmtpID
`,
});
// do not attempt to version the db if it is already open
if (!db.isOpen()) {
// note that duplicate keys will be overwritten
const customSchema = options?.contentTypeConfigs?.reduce(
(result, { schema }) => ({
...result,
...schema,
}),
{} as Record<string, string>,
);

const version = options?.version ?? 1;

db.version(version).stores({
...customSchema,
conversations: `
++id,
[topic+walletAddress],
createdAt,
peerAddress,
topic,
updatedAt,
walletAddress
`,
messages: `
++id,
[conversationTopic+walletAddress],
contentFallback,
contentType,
conversationTopic,
senderAddress,
sentAt,
status,
uuid,
walletAddress,
xmtpID
`,
});
}

return db;
};
Expand Down

0 comments on commit 3301cf7

Please sign in to comment.