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

feat!: Include versions in IDB name #741

Merged
merged 1 commit into from Dec 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/replicache.test.ts
Expand Up @@ -83,7 +83,6 @@ async function replicacheForTestingNoDefaultURLs<MD extends MutatorDefs = {}>(
...rest
}: ReplicacheOptions<MD> = {},
): Promise<ReplicacheTest<MD>> {
dbsToDrop.add(name);
const rep = new ReplicacheTest<MD>({
pullURL,
pushDelay,
Expand All @@ -92,6 +91,7 @@ async function replicacheForTestingNoDefaultURLs<MD extends MutatorDefs = {}>(
useMemstore,
...rest,
});
dbsToDrop.add(rep.idbName);
reps.add(rep);
// Wait for open to be done.
await rep.clientID;
Expand Down Expand Up @@ -955,8 +955,8 @@ testWithBothStores('name', async () => {
await repA.close();
await repB.close();

indexedDB.deleteDatabase('a');
indexedDB.deleteDatabase('b');
indexedDB.deleteDatabase(repA.idbName);
indexedDB.deleteDatabase(repB.idbName);
});

testWithBothStores('register with error', async () => {
Expand Down
13 changes: 12 additions & 1 deletion src/replicache.ts
Expand Up @@ -49,6 +49,8 @@ export type Poke = {

export const httpStatusUnauthorized = 401;

const REPLICACHE_FORMAT_VERSION = 3;

export type MaybePromise<T> = T | Promise<T>;

type ToPromise<P> = P extends Promise<unknown> ? P : Promise<P>;
Expand Down Expand Up @@ -138,6 +140,15 @@ export class Replicache<MD extends MutatorDefs = {}> {
/** The name of the Replicache database. */
readonly name: string;

/**
* This is the name Replicache uses for the IndexedDB database where data is
* stored.
*/
get idbName(): string {
const n = `${this.name}:${REPLICACHE_FORMAT_VERSION}`;
return this.schemaVersion ? `${n}:${this.schemaVersion}` : n;
}

private readonly _useMemstore: boolean;

/** The schema version of the data understood by this application. */
Expand Down Expand Up @@ -288,7 +299,7 @@ export class Replicache<MD extends MutatorDefs = {}> {
this.pusher = pusher;
this._kvStore =
experimentalKVStore ||
(this._useMemstore ? new MemStore() : new IDBStore(this.name));
(this._useMemstore ? new MemStore() : new IDBStore(this.idbName));
this._dagStore = new dag.Store(
this._kvStore,
dag.defaultChunkHasher,
Expand Down