@xylabs/indexed-db 4.4.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +165 -0
- package/README.md +69 -0
- package/dist/neutral/IndexDescription.d.ts +34 -0
- package/dist/neutral/IndexDescription.d.ts.map +1 -0
- package/dist/neutral/ObjectStore.d.ts +5 -0
- package/dist/neutral/ObjectStore.d.ts.map +1 -0
- package/dist/neutral/createStore.d.ts +7 -0
- package/dist/neutral/createStore.d.ts.map +1 -0
- package/dist/neutral/getExistingIndexes.d.ts +6 -0
- package/dist/neutral/getExistingIndexes.d.ts.map +1 -0
- package/dist/neutral/index.d.ts +8 -0
- package/dist/neutral/index.d.ts.map +1 -0
- package/dist/neutral/index.mjs +96 -0
- package/dist/neutral/index.mjs.map +1 -0
- package/dist/neutral/withDb.d.ts +5 -0
- package/dist/neutral/withDb.d.ts.map +1 -0
- package/dist/neutral/withReadOnlyStore.d.ts +5 -0
- package/dist/neutral/withReadOnlyStore.d.ts.map +1 -0
- package/dist/neutral/withReadWriteStore.d.ts +5 -0
- package/dist/neutral/withReadWriteStore.d.ts.map +1 -0
- package/package.json +59 -0
- package/src/IndexDescription.ts +41 -0
- package/src/ObjectStore.ts +5 -0
- package/src/createStore.ts +37 -0
- package/src/getExistingIndexes.ts +35 -0
- package/src/index.ts +7 -0
- package/src/withDb.ts +14 -0
- package/src/withReadOnlyStore.ts +20 -0
- package/src/withReadWriteStore.ts +20 -0
- package/xy.config.ts +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[![logo][]](https://xylabs.com)
|
|
2
|
+
|
|
3
|
+
# @xylabs/indexed-db
|
|
4
|
+
|
|
5
|
+
[![npm-badge][]][npm-link]
|
|
6
|
+
[![npm-downloads-badge][]][npm-link]
|
|
7
|
+
[![jsdelivr-badge][]][jsdelivr-link]
|
|
8
|
+
[![npm-license-badge][]](LICENSE)
|
|
9
|
+
[![socket-badge][]][socket-link]
|
|
10
|
+
|
|
11
|
+
> XY Labs Indexed DB Helpers
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
|
|
15
|
+
- [Description](#description)
|
|
16
|
+
- [Install](#install)
|
|
17
|
+
- [Maintainers](#maintainers)
|
|
18
|
+
- [License](#license)
|
|
19
|
+
- [Credits](#credits)
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
Various Indexed DB Helper Functions
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
Using npm:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm i --save @xylabs/indexed-db
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Using yarn:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
yarn add @xylabs/indexed-db
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Documentation
|
|
40
|
+
[Developer Reference](https://xylabs.github.io/sdk-js)
|
|
41
|
+
|
|
42
|
+
## Maintainers
|
|
43
|
+
|
|
44
|
+
- [Arie Trouw](https://github.com/arietrouw) ([arietrouw.com](https://arietrouw.com))
|
|
45
|
+
- [Joel Carter](https://github.com/JoelBCarter)
|
|
46
|
+
- [Matt Jones](https://github.com/jonesmac)
|
|
47
|
+
- [Jordan Trouw](https://github.com/jordantrouw)
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
See the [LICENSE](LICENSE) file for license details
|
|
52
|
+
|
|
53
|
+
## Credits
|
|
54
|
+
|
|
55
|
+
[Made with 🔥and ❄️ by XY Labs](https://xylabs.com)
|
|
56
|
+
|
|
57
|
+
[logo]: https://cdn.xy.company/img/brand/XYPersistentCompany_Logo_Icon_Colored.svg
|
|
58
|
+
|
|
59
|
+
[npm-badge]: https://img.shields.io/npm/v/@xylabs/indexed-db.svg
|
|
60
|
+
[npm-link]: https://www.npmjs.com/package/@xylabs/indexed-db
|
|
61
|
+
|
|
62
|
+
[npm-downloads-badge]: https://img.shields.io/npm/dw/@xylabs/indexed-db
|
|
63
|
+
[npm-license-badge]: https://img.shields.io/npm/l/@xylabs/indexed-db
|
|
64
|
+
|
|
65
|
+
[jsdelivr-badge]: https://data.jsdelivr.com/v1/package/npm/@xylabs/indexed-db/badge
|
|
66
|
+
[jsdelivr-link]: https://www.jsdelivr.com/package/npm/@xylabs/indexed-db
|
|
67
|
+
|
|
68
|
+
[socket-badge]: https://socket.dev/api/badge/npm/package/@xylabs/indexed-db
|
|
69
|
+
[socket-link]: https://socket.dev/npm/package/@xylabs/indexed-db
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The index direction (1 for ascending, -1 for descending)
|
|
3
|
+
*/
|
|
4
|
+
export type IndexDirection = -1 | 1;
|
|
5
|
+
/**
|
|
6
|
+
* Description of index(es) to be created on a store
|
|
7
|
+
*/
|
|
8
|
+
export type IndexDescription = {
|
|
9
|
+
/**
|
|
10
|
+
* The key(s) to index
|
|
11
|
+
*/
|
|
12
|
+
key: Record<string, IndexDirection>;
|
|
13
|
+
/**
|
|
14
|
+
* Is the indexed value an array
|
|
15
|
+
*/
|
|
16
|
+
multiEntry?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The name of the index
|
|
19
|
+
*/
|
|
20
|
+
name?: string;
|
|
21
|
+
/**
|
|
22
|
+
* If true, the index must enforce uniqueness on the key
|
|
23
|
+
*/
|
|
24
|
+
unique?: boolean;
|
|
25
|
+
};
|
|
26
|
+
export declare const IndexSeparator = "-";
|
|
27
|
+
/**
|
|
28
|
+
* Given an index description, this will build the index
|
|
29
|
+
* name in standard form
|
|
30
|
+
* @param index The index description
|
|
31
|
+
* @returns The index name in standard form
|
|
32
|
+
*/
|
|
33
|
+
export declare const buildStandardIndexName: (index: IndexDescription) => string;
|
|
34
|
+
//# sourceMappingURL=IndexDescription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IndexDescription.d.ts","sourceRoot":"","sources":["../../src/IndexDescription.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACnC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,eAAO,MAAM,cAAc,MAAM,CAAA;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,UAAW,gBAAgB,WAK7D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAC9D,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;CACf"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Logger } from '@xylabs/logger';
|
|
2
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
+
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
4
|
+
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
|
+
import type { ObjectStore } from './ObjectStore.ts';
|
|
6
|
+
export declare function createStore<T extends EmptyObject = EmptyObject>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>, indexes: IndexDescription[], logger?: Logger): void;
|
|
7
|
+
//# sourceMappingURL=createStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createStore.d.ts","sourceRoot":"","sources":["../../src/createStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EACV,YAAY,EAAc,UAAU,EACrC,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAgB,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC7D,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAChC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EACrC,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,QAoBhB"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
+
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
3
|
+
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
|
+
import type { ObjectStore } from './ObjectStore.ts';
|
|
5
|
+
export declare function getExistingIndexes<T extends EmptyObject = EmptyObject>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>): Promise<IndexDescription[]>;
|
|
6
|
+
//# sourceMappingURL=getExistingIndexes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getExistingIndexes.d.ts","sourceRoot":"","sources":["../../src/getExistingIndexes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AAEnD,OAAO,EACL,KAAK,gBAAgB,EAEtB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAGnD,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC1E,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAChC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GACpC,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAqB7B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './createStore.ts';
|
|
2
|
+
export * from './getExistingIndexes.ts';
|
|
3
|
+
export * from './IndexDescription.ts';
|
|
4
|
+
export * from './ObjectStore.ts';
|
|
5
|
+
export * from './withDb.ts';
|
|
6
|
+
export * from './withReadOnlyStore.ts';
|
|
7
|
+
export * from './withReadWriteStore.ts';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AACvC,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,wBAAwB,CAAA;AACtC,cAAc,yBAAyB,CAAA"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// src/IndexDescription.ts
|
|
2
|
+
var IndexSeparator = "-";
|
|
3
|
+
var buildStandardIndexName = (index) => {
|
|
4
|
+
const { key, unique } = index;
|
|
5
|
+
const prefix = unique ? "UX" : "IX";
|
|
6
|
+
const indexKeys = Object.keys(key);
|
|
7
|
+
return `${prefix}_${indexKeys.join(IndexSeparator)}`;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/createStore.ts
|
|
11
|
+
function createStore(db, storeName, indexes, logger) {
|
|
12
|
+
logger?.log(`Creating store ${storeName}`);
|
|
13
|
+
const store = db.createObjectStore(storeName, {
|
|
14
|
+
// If it isn't explicitly set, create a value by auto incrementing.
|
|
15
|
+
autoIncrement: true
|
|
16
|
+
});
|
|
17
|
+
store.name = storeName;
|
|
18
|
+
for (const {
|
|
19
|
+
key,
|
|
20
|
+
multiEntry,
|
|
21
|
+
unique
|
|
22
|
+
} of indexes) {
|
|
23
|
+
const indexKeys = Object.keys(key);
|
|
24
|
+
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys;
|
|
25
|
+
const indexName = buildStandardIndexName({ key, unique });
|
|
26
|
+
console.log("createIndex", indexName, keys, { multiEntry, unique });
|
|
27
|
+
store.createIndex(indexName, keys, { multiEntry, unique });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/withReadOnlyStore.ts
|
|
32
|
+
async function withReadOnlyStore(db, storeName, callback) {
|
|
33
|
+
const transaction = db.transaction(storeName, "readonly");
|
|
34
|
+
const store = transaction.objectStore(storeName);
|
|
35
|
+
try {
|
|
36
|
+
return await callback(store);
|
|
37
|
+
} finally {
|
|
38
|
+
await transaction.done;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/getExistingIndexes.ts
|
|
43
|
+
async function getExistingIndexes(db, storeName) {
|
|
44
|
+
return await withReadOnlyStore(db, storeName, (store) => {
|
|
45
|
+
return [...store.indexNames].map((indexName) => {
|
|
46
|
+
const index = store.index(indexName);
|
|
47
|
+
const key = {};
|
|
48
|
+
if (Array.isArray(index.keyPath)) {
|
|
49
|
+
for (const keyPath of index.keyPath) {
|
|
50
|
+
key[keyPath] = 1;
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
key[index.keyPath] = 1;
|
|
54
|
+
}
|
|
55
|
+
const desc = {
|
|
56
|
+
name: indexName,
|
|
57
|
+
key,
|
|
58
|
+
unique: index.unique,
|
|
59
|
+
multiEntry: index.multiEntry
|
|
60
|
+
};
|
|
61
|
+
return desc;
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/withDb.ts
|
|
67
|
+
import { openDB } from "idb";
|
|
68
|
+
async function withDb(dbName, callback) {
|
|
69
|
+
const db = await openDB(dbName);
|
|
70
|
+
try {
|
|
71
|
+
return await callback(db);
|
|
72
|
+
} finally {
|
|
73
|
+
db.close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/withReadWriteStore.ts
|
|
78
|
+
async function withReadWriteStore(db, storeName, callback) {
|
|
79
|
+
const transaction = db.transaction(storeName, "readwrite");
|
|
80
|
+
const store = transaction.objectStore(storeName);
|
|
81
|
+
try {
|
|
82
|
+
return await callback(store);
|
|
83
|
+
} finally {
|
|
84
|
+
await transaction.done;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
IndexSeparator,
|
|
89
|
+
buildStandardIndexName,
|
|
90
|
+
createStore,
|
|
91
|
+
getExistingIndexes,
|
|
92
|
+
withDb,
|
|
93
|
+
withReadOnlyStore,
|
|
94
|
+
withReadWriteStore
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/IndexDescription.ts","../../src/createStore.ts","../../src/withReadOnlyStore.ts","../../src/getExistingIndexes.ts","../../src/withDb.ts","../../src/withReadWriteStore.ts"],"sourcesContent":["/**\n * The index direction (1 for ascending, -1 for descending)\n */\nexport type IndexDirection = -1 | 1\n\n/**\n * Description of index(es) to be created on a store\n */\nexport type IndexDescription = {\n /**\n * The key(s) to index\n */\n key: Record<string, IndexDirection>\n /**\n * Is the indexed value an array\n */\n multiEntry?: boolean\n /**\n * The name of the index\n */\n name?: string\n /**\n * If true, the index must enforce uniqueness on the key\n */\n unique?: boolean\n}\n\nexport const IndexSeparator = '-'\n\n/**\n * Given an index description, this will build the index\n * name in standard form\n * @param index The index description\n * @returns The index name in standard form\n */\nexport const buildStandardIndexName = (index: IndexDescription) => {\n const { key, unique } = index\n const prefix = unique ? 'UX' : 'IX'\n const indexKeys = Object.keys(key)\n return `${prefix}_${indexKeys.join(IndexSeparator)}`\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IndexNames, StoreNames,\n} from 'idb'\n\nimport {\n buildStandardIndexName,\n type IndexDescription,\n} from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport function createStore<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${storeName}`)\n // Create the store\n const store = db.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\n // Name the store\n store.name = storeName\n // Create an index on the hash\n for (const {\n key, multiEntry, unique,\n } of indexes) {\n const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique }) as IndexNames<ObjectStore<T>, StoreNames<ObjectStore<T>>>\n console.log('createIndex', indexName, keys, { multiEntry, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,\n): Promise<R> {\n const transaction = db.transaction(storeName, 'readonly')\n const store = transaction.objectStore(storeName)\n try {\n return await callback(store)\n } finally {\n await transaction.done\n }\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type { IDBPDatabase, StoreNames } from 'idb'\n\nimport {\n type IndexDescription,\n type IndexDirection,\n} from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withReadOnlyStore } from './withReadOnlyStore.ts'\n\nexport async function getExistingIndexes<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n): Promise<IndexDescription[]> {\n return await withReadOnlyStore(db, storeName, (store) => {\n return [...store.indexNames].map((indexName) => {\n const index = store.index(indexName)\n const key: Record<string, IndexDirection> = {}\n if (Array.isArray(index.keyPath)) {\n for (const keyPath of index.keyPath) {\n key[keyPath] = 1\n }\n } else {\n key[index.keyPath] = 1\n }\n const desc: IndexDescription = {\n name: indexName as string,\n key,\n unique: index.unique,\n multiEntry: index.multiEntry,\n }\n return desc\n })\n })\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type { IDBPDatabase } from 'idb'\nimport { openDB } from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withDb<T extends EmptyObject = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<T> | T): Promise<T> {\n const db = await openDB<ObjectStore<T>>(dbName)\n try {\n return await callback(db)\n } finally {\n db.close()\n }\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,\n): Promise<R> {\n const transaction = db.transaction(storeName, 'readwrite')\n const store = transaction.objectStore(storeName)\n try {\n return await callback(store)\n } finally {\n await transaction.done\n }\n}\n"],"mappings":";AA2BO,IAAM,iBAAiB;AAQvB,IAAM,yBAAyB,CAAC,UAA4B;AACjE,QAAM,EAAE,KAAK,OAAO,IAAI;AACxB,QAAM,SAAS,SAAS,OAAO;AAC/B,QAAM,YAAY,OAAO,KAAK,GAAG;AACjC,SAAO,GAAG,MAAM,IAAI,UAAU,KAAK,cAAc,CAAC;AACpD;;;AC5BO,SAAS,YACd,IACA,WACA,SACA,QACA;AACA,UAAQ,IAAI,kBAAkB,SAAS,EAAE;AAEzC,QAAM,QAAQ,GAAG,kBAAkB,WAAW;AAAA;AAAA,IAE5C,eAAe;AAAA,EACjB,CAAC;AAED,QAAM,OAAO;AAEb,aAAW;AAAA,IACT;AAAA,IAAK;AAAA,IAAY;AAAA,EACnB,KAAK,SAAS;AACZ,UAAM,YAAY,OAAO,KAAK,GAAG;AACjC,UAAM,OAAO,UAAU,WAAW,IAAI,UAAU,CAAC,IAAI;AACrD,UAAM,YAAY,uBAAuB,EAAE,KAAK,OAAO,CAAC;AACxD,YAAQ,IAAI,eAAe,WAAW,MAAM,EAAE,YAAY,OAAO,CAAC;AAClE,UAAM,YAAY,WAAW,MAAM,EAAE,YAAY,OAAO,CAAC;AAAA,EAC3D;AACF;;;AC7BA,eAAsB,kBACpB,IACA,WACA,UACY;AACZ,QAAM,cAAc,GAAG,YAAY,WAAW,UAAU;AACxD,QAAM,QAAQ,YAAY,YAAY,SAAS;AAC/C,MAAI;AACF,WAAO,MAAM,SAAS,KAAK;AAAA,EAC7B,UAAE;AACA,UAAM,YAAY;AAAA,EACpB;AACF;;;ACTA,eAAsB,mBACpB,IACA,WAC6B;AAC7B,SAAO,MAAM,kBAAkB,IAAI,WAAW,CAAC,UAAU;AACvD,WAAO,CAAC,GAAG,MAAM,UAAU,EAAE,IAAI,CAAC,cAAc;AAC9C,YAAM,QAAQ,MAAM,MAAM,SAAS;AACnC,YAAM,MAAsC,CAAC;AAC7C,UAAI,MAAM,QAAQ,MAAM,OAAO,GAAG;AAChC,mBAAW,WAAW,MAAM,SAAS;AACnC,cAAI,OAAO,IAAI;AAAA,QACjB;AAAA,MACF,OAAO;AACL,YAAI,MAAM,OAAO,IAAI;AAAA,MACvB;AACA,YAAM,OAAyB;AAAA,QAC7B,MAAM;AAAA,QACN;AAAA,QACA,QAAQ,MAAM;AAAA,QACd,YAAY,MAAM;AAAA,MACpB;AACA,aAAO;AAAA,IACT,CAAC;AAAA,EACH,CAAC;AACH;;;AChCA,SAAS,cAAc;AAIvB,eAAsB,OAA4C,QAAgB,UAA4E;AAC5J,QAAM,KAAK,MAAM,OAAuB,MAAM;AAC9C,MAAI;AACF,WAAO,MAAM,SAAS,EAAE;AAAA,EAC1B,UAAE;AACA,OAAG,MAAM;AAAA,EACX;AACF;;;ACNA,eAAsB,mBACpB,IACA,WACA,UACY;AACZ,QAAM,cAAc,GAAG,YAAY,WAAW,WAAW;AACzD,QAAM,QAAQ,YAAY,YAAY,SAAS;AAC/C,MAAI;AACF,WAAO,MAAM,SAAS,KAAK;AAAA,EAC7B,UAAE;AACA,UAAM,YAAY;AAAA,EACpB;AACF;","names":[]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
+
import type { IDBPDatabase } from 'idb';
|
|
3
|
+
import type { ObjectStore } from './ObjectStore.ts';
|
|
4
|
+
export declare function withDb<T extends EmptyObject = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<T> | T): Promise<T>;
|
|
5
|
+
//# sourceMappingURL=withDb.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withDb.d.ts","sourceRoot":"","sources":["../../src/withDb.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,MAAM,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAO5J"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
+
import type { IDBPDatabase, IDBPObjectStore, StoreNames } from 'idb';
|
|
3
|
+
import type { ObjectStore } from './ObjectStore.ts';
|
|
4
|
+
export declare function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>, callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R): Promise<R>;
|
|
5
|
+
//# sourceMappingURL=withReadOnlyStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withReadOnlyStore.d.ts","sourceRoot":"","sources":["../../src/withReadOnlyStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EACV,YAAY,EAAE,eAAe,EAAE,UAAU,EAC1C,MAAM,KAAK,CAAA;AAEZ,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,iBAAiB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAAE,CAAC,GAAG,CAAC,EAChF,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAChC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EACrC,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GACrI,OAAO,CAAC,CAAC,CAAC,CAQZ"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
+
import type { IDBPDatabase, IDBPObjectStore, StoreNames } from 'idb';
|
|
3
|
+
import type { ObjectStore } from './ObjectStore.ts';
|
|
4
|
+
export declare function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>, callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R): Promise<R>;
|
|
5
|
+
//# sourceMappingURL=withReadWriteStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withReadWriteStore.d.ts","sourceRoot":"","sources":["../../src/withReadWriteStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EACV,YAAY,EAAE,eAAe,EAAE,UAAU,EAC1C,MAAM,KAAK,CAAA;AAEZ,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAAE,CAAC,GAAG,CAAC,EACjF,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAChC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EACrC,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GACrI,OAAO,CAAC,CAAC,CAAC,CAQZ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xylabs/indexed-db",
|
|
3
|
+
"version": "4.4.28",
|
|
4
|
+
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"hex",
|
|
7
|
+
"xylabs",
|
|
8
|
+
"utility",
|
|
9
|
+
"typescript",
|
|
10
|
+
"esm"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://xylabs.com",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "git+https://github.com/xylabs/sdk-js/issues",
|
|
15
|
+
"email": "support@xylabs.com"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/xylabs/sdk-js.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "LGPL-3.0-only",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "XY Labs Development Team",
|
|
24
|
+
"email": "support@xylabs.com",
|
|
25
|
+
"url": "https://xylabs.com"
|
|
26
|
+
},
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"type": "module",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/neutral/index.d.ts",
|
|
32
|
+
"default": "./dist/neutral/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"module": "./dist/neutral/index.mjs",
|
|
37
|
+
"types": "./dist/neutral/index.d.ts",
|
|
38
|
+
"workspaces": [
|
|
39
|
+
"packages/**/*"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@xylabs/logger": "^4.4.28",
|
|
43
|
+
"@xylabs/object": "^4.4.28",
|
|
44
|
+
"idb": "^8.0.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@xylabs/ts-scripts-yarn3": "^4.2.6",
|
|
48
|
+
"@xylabs/tsconfig": "^4.2.6",
|
|
49
|
+
"@xylabs/vitest-extended": "^4.4.28",
|
|
50
|
+
"typescript": "^5.7.2",
|
|
51
|
+
"vitest": "^2.1.8"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The index direction (1 for ascending, -1 for descending)
|
|
3
|
+
*/
|
|
4
|
+
export type IndexDirection = -1 | 1
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Description of index(es) to be created on a store
|
|
8
|
+
*/
|
|
9
|
+
export type IndexDescription = {
|
|
10
|
+
/**
|
|
11
|
+
* The key(s) to index
|
|
12
|
+
*/
|
|
13
|
+
key: Record<string, IndexDirection>
|
|
14
|
+
/**
|
|
15
|
+
* Is the indexed value an array
|
|
16
|
+
*/
|
|
17
|
+
multiEntry?: boolean
|
|
18
|
+
/**
|
|
19
|
+
* The name of the index
|
|
20
|
+
*/
|
|
21
|
+
name?: string
|
|
22
|
+
/**
|
|
23
|
+
* If true, the index must enforce uniqueness on the key
|
|
24
|
+
*/
|
|
25
|
+
unique?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const IndexSeparator = '-'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Given an index description, this will build the index
|
|
32
|
+
* name in standard form
|
|
33
|
+
* @param index The index description
|
|
34
|
+
* @returns The index name in standard form
|
|
35
|
+
*/
|
|
36
|
+
export const buildStandardIndexName = (index: IndexDescription) => {
|
|
37
|
+
const { key, unique } = index
|
|
38
|
+
const prefix = unique ? 'UX' : 'IX'
|
|
39
|
+
const indexKeys = Object.keys(key)
|
|
40
|
+
return `${prefix}_${indexKeys.join(IndexSeparator)}`
|
|
41
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Logger } from '@xylabs/logger'
|
|
2
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
3
|
+
import type {
|
|
4
|
+
IDBPDatabase, IndexNames, StoreNames,
|
|
5
|
+
} from 'idb'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
buildStandardIndexName,
|
|
9
|
+
type IndexDescription,
|
|
10
|
+
} from './IndexDescription.ts'
|
|
11
|
+
import type { ObjectStore } from './ObjectStore.ts'
|
|
12
|
+
|
|
13
|
+
export function createStore<T extends EmptyObject = EmptyObject>(
|
|
14
|
+
db: IDBPDatabase<ObjectStore<T>>,
|
|
15
|
+
storeName: StoreNames<ObjectStore<T>>,
|
|
16
|
+
indexes: IndexDescription[],
|
|
17
|
+
logger?: Logger,
|
|
18
|
+
) {
|
|
19
|
+
logger?.log(`Creating store ${storeName}`)
|
|
20
|
+
// Create the store
|
|
21
|
+
const store = db.createObjectStore(storeName, {
|
|
22
|
+
// If it isn't explicitly set, create a value by auto incrementing.
|
|
23
|
+
autoIncrement: true,
|
|
24
|
+
})
|
|
25
|
+
// Name the store
|
|
26
|
+
store.name = storeName
|
|
27
|
+
// Create an index on the hash
|
|
28
|
+
for (const {
|
|
29
|
+
key, multiEntry, unique,
|
|
30
|
+
} of indexes) {
|
|
31
|
+
const indexKeys = Object.keys(key)
|
|
32
|
+
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys
|
|
33
|
+
const indexName = buildStandardIndexName({ key, unique }) as IndexNames<ObjectStore<T>, StoreNames<ObjectStore<T>>>
|
|
34
|
+
console.log('createIndex', indexName, keys, { multiEntry, unique })
|
|
35
|
+
store.createIndex(indexName, keys, { multiEntry, unique })
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
2
|
+
import type { IDBPDatabase, StoreNames } from 'idb'
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type IndexDescription,
|
|
6
|
+
type IndexDirection,
|
|
7
|
+
} from './IndexDescription.ts'
|
|
8
|
+
import type { ObjectStore } from './ObjectStore.ts'
|
|
9
|
+
import { withReadOnlyStore } from './withReadOnlyStore.ts'
|
|
10
|
+
|
|
11
|
+
export async function getExistingIndexes<T extends EmptyObject = EmptyObject>(
|
|
12
|
+
db: IDBPDatabase<ObjectStore<T>>,
|
|
13
|
+
storeName: StoreNames<ObjectStore<T>>,
|
|
14
|
+
): Promise<IndexDescription[]> {
|
|
15
|
+
return await withReadOnlyStore(db, storeName, (store) => {
|
|
16
|
+
return [...store.indexNames].map((indexName) => {
|
|
17
|
+
const index = store.index(indexName)
|
|
18
|
+
const key: Record<string, IndexDirection> = {}
|
|
19
|
+
if (Array.isArray(index.keyPath)) {
|
|
20
|
+
for (const keyPath of index.keyPath) {
|
|
21
|
+
key[keyPath] = 1
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
key[index.keyPath] = 1
|
|
25
|
+
}
|
|
26
|
+
const desc: IndexDescription = {
|
|
27
|
+
name: indexName as string,
|
|
28
|
+
key,
|
|
29
|
+
unique: index.unique,
|
|
30
|
+
multiEntry: index.multiEntry,
|
|
31
|
+
}
|
|
32
|
+
return desc
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
}
|
package/src/index.ts
ADDED
package/src/withDb.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
2
|
+
import type { IDBPDatabase } from 'idb'
|
|
3
|
+
import { openDB } from 'idb'
|
|
4
|
+
|
|
5
|
+
import type { ObjectStore } from './ObjectStore.ts'
|
|
6
|
+
|
|
7
|
+
export async function withDb<T extends EmptyObject = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<T> | T): Promise<T> {
|
|
8
|
+
const db = await openDB<ObjectStore<T>>(dbName)
|
|
9
|
+
try {
|
|
10
|
+
return await callback(db)
|
|
11
|
+
} finally {
|
|
12
|
+
db.close()
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
2
|
+
import type {
|
|
3
|
+
IDBPDatabase, IDBPObjectStore, StoreNames,
|
|
4
|
+
} from 'idb'
|
|
5
|
+
|
|
6
|
+
import type { ObjectStore } from './ObjectStore.ts'
|
|
7
|
+
|
|
8
|
+
export async function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(
|
|
9
|
+
db: IDBPDatabase<ObjectStore<T>>,
|
|
10
|
+
storeName: StoreNames<ObjectStore<T>>,
|
|
11
|
+
callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,
|
|
12
|
+
): Promise<R> {
|
|
13
|
+
const transaction = db.transaction(storeName, 'readonly')
|
|
14
|
+
const store = transaction.objectStore(storeName)
|
|
15
|
+
try {
|
|
16
|
+
return await callback(store)
|
|
17
|
+
} finally {
|
|
18
|
+
await transaction.done
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
2
|
+
import type {
|
|
3
|
+
IDBPDatabase, IDBPObjectStore, StoreNames,
|
|
4
|
+
} from 'idb'
|
|
5
|
+
|
|
6
|
+
import type { ObjectStore } from './ObjectStore.ts'
|
|
7
|
+
|
|
8
|
+
export async function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(
|
|
9
|
+
db: IDBPDatabase<ObjectStore<T>>,
|
|
10
|
+
storeName: StoreNames<ObjectStore<T>>,
|
|
11
|
+
callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,
|
|
12
|
+
): Promise<R> {
|
|
13
|
+
const transaction = db.transaction(storeName, 'readwrite')
|
|
14
|
+
const store = transaction.objectStore(storeName)
|
|
15
|
+
try {
|
|
16
|
+
return await callback(store)
|
|
17
|
+
} finally {
|
|
18
|
+
await transaction.done
|
|
19
|
+
}
|
|
20
|
+
}
|