forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorex.ts
More file actions
58 lines (50 loc) · 1.46 KB
/
storex.ts
File metadata and controls
58 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Storex, {
CollectionDefinitionMap,
StorageBackendPlugin,
} from '@worldbrain/storex'
import {
DexieStorageBackend,
IndexedDbImplementation,
StemmerSelector,
} from '@worldbrain/storex-backend-dexie'
import schemaPatcherFn from './storage/dexie-schema'
export interface CustomField {
key: string
field: any // TODO: type properly after storex exports
}
export default <T extends Storex = Storex>({
dbName,
stemmerSelector,
schemaPatcher,
idbImplementation,
collections,
backendPlugins = [],
customFields = [],
modifyInstance = f => f as any,
}: {
stemmerSelector: StemmerSelector
schemaPatcher: typeof schemaPatcherFn
dbName: string
idbImplementation: IndexedDbImplementation
collections: CollectionDefinitionMap
backendPlugins?: StorageBackendPlugin<DexieStorageBackend>[]
customFields?: CustomField[]
modifyInstance?: (instance: Storex) => T
}): T => {
const backend = new DexieStorageBackend({
stemmerSelector,
schemaPatcher,
dbName,
idbImplementation,
})
for (const plugin of backendPlugins) {
backend.use(plugin)
}
const storex = new Storex({ backend })
// Override default storex fields with Memex-specific ones
for (const { key, field } of customFields) {
storex.registry.fieldTypes.registerType(key, field)
}
storex.registry.registerCollections(collections)
return modifyInstance(storex)
}