Skip to content

Commit f97f00a

Browse files
committed
Expose a NextDexie that translates an entire db instance to a NextDexie db instance (currently readonly and limited)
1 parent c45999b commit f97f00a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/next/NextDexie.mts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Dexie, PromiseExtended } from '../../dist/dexie';
2+
import { DexieCollection } from './DexieCollection.mjs';
3+
4+
type TableProps<D extends Dexie> = {
5+
[P in keyof D]: D[P] extends {get(key: any): PromiseExtended<any>, toArray: () => PromiseExtended<any[]>} ? P : never;
6+
}[keyof D];
7+
8+
9+
export type NextDexie<D extends Dexie> = {
10+
[P in TableProps<D>]: D[P] extends {
11+
get(key: infer K): PromiseExtended<any>;
12+
toArray: () => PromiseExtended<Array<infer T>>;
13+
}
14+
? DexieCollection<T, K>
15+
: D[P];
16+
} & {
17+
readonly name: string;
18+
readonly tables: DexieCollection<any, any>[];
19+
}
20+
21+
const wm = new WeakMap<Dexie, NextDexie<Dexie>>();
22+
23+
export function NextDexie<D extends Dexie>(db: D): NextDexie<D> {
24+
let nextDexie = wm.get(db);
25+
if (nextDexie) return nextDexie as NextDexie<D>;
26+
nextDexie = {
27+
name: db.name,
28+
tables: [] as DexieCollection<any, any>[],
29+
} as NextDexie<D>;
30+
for (const table of db.tables) {
31+
nextDexie.tables.push(new DexieCollection(table));
32+
}
33+
wm.set(db, nextDexie);
34+
return nextDexie as NextDexie<D>;
35+
}
36+

src/next/index.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export { createMangoFilter } from './createMangoFilter.mjs';
99
export { executeQuery, executeCount } from './executeMangoQuery.mjs';
1010
export type { MangoExpression } from './MangoExpression.mjs';
1111
export { toMapKey } from './toMapKey.mjs';
12+
export { NextDexie } from './NextDexie.mjs'
13+

0 commit comments

Comments
 (0)