Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, test} from 'vitest';
import {hasMemStore} from '../../../replicache/src/kv/mem-store.ts';
import {h64} from '../../../shared/src/hash.ts';
import {createSchema} from '../../../zero-schema/src/builder/schema-builder.ts';
import {string, table} from '../../../zero-schema/src/builder/table-builder.ts';
Expand Down Expand Up @@ -95,3 +96,35 @@ test('idbName generation with URL configuration', async () => {
await zero.close();
}
});

test('dropAllDatabases drops all known mem stores', async () => {
const z1 = new Zero({
userID: 'drop-all-user-1',
storageKey: 'drop-all-storage-1',
schema,
kvStore: 'mem',
});
const z2 = new Zero({
userID: 'drop-all-user-2',
storageKey: 'drop-all-storage-2',
schema,
kvStore: 'mem',
});

const dbNames = [z1.idbName, z2.idbName];

for (const dbName of dbNames) {
expect(hasMemStore(dbName)).toBe(true);
}

await z1.close();
await z2.close();

const result = await z1.dropAllDatabases();

for (const dbName of dbNames) {
expect(result.dropped).toContain(dbName);
expect(hasMemStore(dbName)).toBe(false);
}
expect(result.errors).toHaveLength(0);
});
16 changes: 15 additions & 1 deletion packages/zero-client/src/client/zero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
ReplicacheImpl,
type ReplicacheImplOptions,
} from '../../../replicache/src/impl.ts';
import {dropDatabase} from '../../../replicache/src/persist/collect-idb-databases.ts';
import {
dropAllDatabases,
dropDatabase,
} from '../../../replicache/src/persist/collect-idb-databases.ts';
import type {Puller, PullerResult} from '../../../replicache/src/puller.ts';
import type {Pusher, PusherResult} from '../../../replicache/src/pusher.ts';
import type {ReplicacheOptions} from '../../../replicache/src/replicache-options.ts';
Expand Down Expand Up @@ -2387,6 +2390,17 @@ export class Zero<
}
}

dropAllDatabases(): Promise<{
dropped: string[];
errors: unknown[];
}> {
return dropAllDatabases({
kvStore: this.#options.kvStore,
logLevel: this.#logOptions.logLevel,
logSinks: [this.#logOptions.logSink],
});
}

#addMetric: <K extends keyof MetricMap>(
metric: K,
value: number,
Expand Down
Loading