@buley/dash 0.0.30 → 2.1.4
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/README.md +82 -65
- package/dist/src/engine/ai.d.ts +10 -0
- package/dist/src/engine/ai.js +47 -0
- package/dist/src/engine/sqlite.d.ts +31 -0
- package/dist/src/engine/sqlite.js +239 -0
- package/dist/src/engine/vec_extension.d.ts +5 -0
- package/dist/src/engine/vec_extension.js +10 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +3 -0
- package/dist/src/mcp/server.d.ts +8 -0
- package/dist/src/mcp/server.js +87 -0
- package/dist/src/reactivity/signal.d.ts +3 -0
- package/dist/src/reactivity/signal.js +31 -0
- package/dist/src/sync/backup.d.ts +12 -0
- package/dist/src/sync/backup.js +44 -0
- package/dist/src/sync/connection.d.ts +16 -0
- package/dist/src/sync/connection.js +29 -0
- package/dist/src/sync/provider.d.ts +11 -0
- package/dist/src/sync/provider.js +67 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +33 -31
- package/.coveralls.yml +0 -1
- package/.github/workflows/opencommit.yml +0 -33
- package/.github/workflows/testing.yml +0 -20
- package/.gitmodules +0 -0
- package/behaviors/cache.dev.js +0 -282
- package/behaviors/changes.dev.js +0 -337
- package/behaviors/collect.dev.js +0 -40
- package/behaviors/examples/async.dev.js +0 -17
- package/behaviors/firebase.dev.js +0 -283
- package/behaviors/live.dev.js +0 -67
- package/behaviors/map.dev.js +0 -54
- package/behaviors/mapreduce.dev.js +0 -68
- package/behaviors/match.dev.js +0 -66
- package/behaviors/patch.dev.js +0 -69
- package/behaviors/rest.dev.js +0 -340
- package/behaviors/shorthand.dev.js +0 -59
- package/behaviors/stats.dev.js +0 -672
- package/dist/behaviors/index.js +0 -142
- package/dist/database/index.js +0 -76
- package/dist/databases/index.js +0 -121
- package/dist/entry/index.js +0 -166
- package/dist/index.js +0 -93
- package/dist/indexes/index.js +0 -153
- package/dist/store/index.js +0 -97
- package/dist/stores/index.js +0 -90
- package/dist/utilities/index.js +0 -174
- package/documentation/database/closing.md +0 -3
- package/documentation/database/getting.md +0 -1
- package/documentation/database/opening.md +0 -5
- package/documentation/database/removing.md +0 -3
- package/documentation/databases.md +0 -21
- package/documentation/entries.md +0 -13
- package/documentation/entry/adding.md +0 -1
- package/documentation/entry/getting.md +0 -4
- package/documentation/entry/putting.md +0 -0
- package/documentation/entry/removing.md +0 -3
- package/documentation/entry/updating.md +0 -0
- package/documentation/general/security.md +0 -10
- package/documentation/general/transaction/requests.md +0 -3
- package/documentation/general/transactions.md +0 -5
- package/documentation/index/creating.md +0 -1
- package/documentation/index/getting.md +0 -1
- package/documentation/index/iterating.md +0 -1
- package/documentation/index/removing.md +0 -1
- package/documentation/indexes.md +0 -3
- package/documentation/key/cursors.md +0 -5
- package/documentation/key/range/bounds.md +0 -11
- package/documentation/key/range/direction.md +0 -1
- package/documentation/key/ranges.md +0 -3
- package/documentation/keys.md +0 -12
- package/documentation/objectstore/clearing.md +0 -1
- package/documentation/objectstore/creating.md +0 -1
- package/documentation/objectstore/getting.md +0 -1
- package/documentation/objectstore/iteration.md +0 -1
- package/documentation/objectstore/removing.md +0 -1
- package/documentation/overview.md +0 -5
- package/documentation/stores.md +0 -13
- package/jest.config.js +0 -12
- package/src/behaviors/index.ts +0 -140
- package/src/database/index.ts +0 -81
- package/src/databases/index.ts +0 -127
- package/src/entry/index.ts +0 -183
- package/src/index/index.ts +0 -61
- package/src/index.ts +0 -96
- package/src/indexes/index.ts +0 -151
- package/src/store/index.ts +0 -102
- package/src/stores/index.ts +0 -90
- package/src/utilities/index.ts +0 -349
- package/tests/behaviors/behaviors.spec.ts +0 -123
- package/tests/database/database.spec.ts +0 -177
- package/tests/databases/databases.spec.ts +0 -199
- package/tests/entry/entry.spec.ts +0 -252
- package/tests/index/index.spec.ts +0 -94
- package/tests/indexes/indexes.spec.ts +0 -203
- package/tests/store/store.spec.ts +0 -164
- package/tests/stores/stores.spec.ts +0 -148
- package/tests/utilities/clone.spec.ts +0 -48
- package/tests/utilities/cloneError.spec.ts +0 -33
- package/tests/utilities/contains.spec.ts +0 -28
- package/tests/utilities/exists.spec.ts +0 -21
- package/tests/utilities/is.spec.ts +0 -37
- package/tests/utilities/isArray.spec.ts +0 -21
- package/tests/utilities/isBoolean.spec.ts +0 -23
- package/tests/utilities/isEmpty.spec.ts +0 -45
- package/tests/utilities/isFunction.spec.ts +0 -30
- package/tests/utilities/isNumber.spec.ts +0 -29
- package/tests/utilities/isObject.spec.ts +0 -42
- package/tests/utilities/isRegEx.spec.ts +0 -33
- package/tests/utilities/isString.spec.ts +0 -25
- package/tests/utilities/isnt.spec.ts +0 -50
- package/tests/utilities/randomId.spec.ts +0 -39
- package/tests/utilities/safeApply.spec.ts +0 -49
- package/tests/utilities/safeEach.spec.ts +0 -38
- package/tests/utilities/safeIterate.spec.ts +0 -47
- package/tsconfig.json +0 -16
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
Say for example there's an index with 26 records containing the alphabet "A" to "Z". Here are some ways we could query it:
|
|
2
|
-
* "only" "Z" would return just one object.
|
|
3
|
-
* lower "A", upper "Z" would return 24 objects
|
|
4
|
-
* lower "A", upper "Z", with "lowerOpen" and "upperOpen" set to `true` would return 26 objects
|
|
5
|
-
* lower "A", with "lowerOpen" set to `true` and the direction `next` would return 26 objects
|
|
6
|
-
* lower "A", with "lowerOpen" set to `true` and the direction `previous` would return 1 object
|
|
7
|
-
* lower "Z", with "lowerOpen" set to `true` would return one object
|
|
8
|
-
* lower "Z", with "lowerOpen" set to `false` would return no objects
|
|
9
|
-
* lower "Z", with "upperOpen" set to `false` would return no objects
|
|
10
|
-
* lower "A", upper "Z", with "lowerOpen" and "upperOpen" set to `false` would return 24 objects
|
|
11
|
-
* lower "A", upper "Z", with "lowerOpen" set to `true` and and "upperOpen" set to `false` would return 25 objects
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[`Direction`](http://www.w3.org/TR/IndexedDB/#dfn-direction) is the way in which you query the index: "next" (forward) or "previous" (backward). The default value is next. The programmer can dictate whether or not to include duplicate keys in the [request](http://www.w3.org/TR/IndexedDB/#idl-def-IDBCursor) using two other directions: "next no duplicates" and "previous no duplicates". The programmer represents these directions using a number: 0 for "next", 1 for "next no duplicates", 2 for "previous" and 3 for "previous no duplicates".
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
Each cursor request needs an index against which to search and a range of keys to match, called a "[`key range`](http://www.w3.org/TR/IndexedDB/#dfn-key-range)". Key ranges are instances of [`IDBKeyRange`](https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange) and the [contents](http://www.w3.org/TR/IndexedDB/#dfn-in-a-key-range) of a key range are characterized by their "bounds" and "direction".
|
|
2
|
-
|
|
3
|
-
In addition to use on indexes, key ranges can also be used as a key when getting (but not adding, putting or deleting) single values with object stores or indexes, in which case the programmer will recieve the first matching value for in that key range.
|
package/documentation/keys.md
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
A [`key`](http://www.w3.org/TR/IndexedDB/#dfn-cursor-key) identifies a record and the value is what's being stored. The list of records associated is represented by a JavaScript object, so there can be only one value per key. A [`valid key`](http://www.w3.org/TR/IndexedDB/#dfn-valid-key-path) can be any of these types: String, Dates, floats and Arrays. The key that identifies a record is found using something called a "[`key path`](http://www.w3.org/TR/IndexedDB/#dfn-key-path)."
|
|
2
|
-
|
|
3
|
-
To specify a key path, a "plain string" will create a key matching a shallow attribute on a stored record. To specify the key among nested attributes in JavaScript objects, [`valid key path`](http://www.w3.org/TR/IndexedDB/#dfn-valid-key-path) syntax uses "dot notation". For example, "first.second.third" would be the key path for an index on the "third" key in the following JavaScript object:
|
|
4
|
-
|
|
5
|
-
{ "first": {
|
|
6
|
-
"second": {
|
|
7
|
-
"third": [ "string value 1", "string value 2" ]
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
As shown above, it's possible to put indexes on array objects in addition to primitive values such as numbers. In such cases, the "[`multirow`](http://www.w3.org/TR/IndexedDB/#dfn-multirow)" allow the programmer to specify when creating a database whether an index record will be created on each object in a particular array (multirow set to `true`) or whether a record is created for only the first (multirow set to `false`). When an empty array is passed as the value of a multirow index, no record will be added.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[`Clear`ing](http://www.w3.org/TR/IndexedDB/#widl-IDBObjectStore-clear) an object store requires a "readwrite" type transaction.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[Creating an object store](http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-createObjectStore) requires a "versionchange" type transaction.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
With an open database it's possible to get a reference to an object store. [`Get`](http://www.w3.org/TR/IndexedDB/#widl-IDBObjectStore-get)ting an object store accepts both "readonly" and "readwrite" typed transactions.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
With an open database it's possible to get a list of all object store names. This list is an `Array`-like collection of string object store names.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[`Delete`ing](http://www.w3.org/TR/IndexedDB/#widl-IDBObjectStore-delete) an object store removes the object store, any associated indexes and its content. A `delete` requires a "versionchange" type transaction.
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
[`IndexedDB`](https://developer.mozilla.org/en-US/docs/IndexedDB) (abbreviated "IDB") is a way to store data in the browser, exposed to JavaScript programmers as a programmatic interface or "API" in most [`HTML5`](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5)-enabled browsers. [1]
|
|
2
|
-
|
|
3
|
-
Using IDB, programmers can organize JavaScript data and make it findable inside "databases" and "object stores", two types of containers in IndexedDB terminology. Databases are the outermost container of data in IndexedDB, and contain object stores. They are unique to each domain or "host". Object stores contain ["`lists`" of "`records`"](http://www.w3.org/TR/IndexedDB/#dfn-index-record-list), where each list is a JavaScript object and each record is breaks down into a "key" and "value".
|
|
4
|
-
|
|
5
|
-
IDB is, in essence, an indexed key/value store. For data that doesn't need to be accessed via index, a lighter weight client-side technology such as localStorage, sessionStorage or cookies may be more appropriate.
|
package/documentation/stores.md
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
[Object stores](http://www.w3.org/TR/IndexedDB/#dfn-object-store), like their name implies, store JavaScript objects as entries. We store entries in an object store and store object stores in a database. Only after opening a database can we list all the object stores in that database or do something with a store contained therein.
|
|
2
|
-
|
|
3
|
-
Via an object store we can access these entries by looking up objects using key paths on those objects, optionally enlisting the help of "indexes" to enable for various combinations of key lookups.
|
|
4
|
-
|
|
5
|
-
Object stores are instances of [`IDBObjectStore`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore). After creation, object stores can be either `delete`ed, or `clear`ed of their values. Deleting an object store removes any indexes associated with it, whereas clearing an object store maintains any indexes.
|
|
6
|
-
|
|
7
|
-
##### Key Paths
|
|
8
|
-
|
|
9
|
-
Every store record must have a key by which it can be identified. This is called a "[`key path`](http://www.w3.org/TR/IndexedDB/#idl-def-IDBCursorWithValueSync#widl-IDBObjectStoreSync-keyPath)", and the regular key syntax rules apply. It's OK to omit a key path when creating an object store, in which case the keys are said to be "[`out-of-line`](http://www.w3.org/TR/IndexedDB/#dfn-out-of-line)" (vs. [`in-line`](http://www.w3.org/TR/IndexedDB/#dfn-in-line-keys)). In that case, IDB knows not from where to source it's key, and so the programmer must supply a key parameter with each object addition.
|
|
10
|
-
|
|
11
|
-
The key path provided with an object store on creation works, in effect, like an "`index`"; however, unlike an index it's possible to delegate key generation to IndexedDB. The key is optional when putting an object into an object store if the "autoIncrement" parameter is set to true on object store creation. Otherwise the programmer will have to include a non-null key on each object added or put into the object store.
|
|
12
|
-
|
|
13
|
-
The "autoIncrement" parameter creates a IDB ["key generator"](http://www.w3.org/TR/IndexedDB/#dfn-key-generator) that will create a monotonically increasing integer key automatically for each new object added to the store. For example, first object added to a database with the key "`foo`" and the `autoIncrement` set to `true`, will have the value `1` for its key path, the second value `2`, and so on for the object attribute `foo`.
|
package/jest.config.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: 'ts-jest',
|
|
3
|
-
testEnvironment: 'jsdom',
|
|
4
|
-
moduleFileExtensions: ['ts', 'js'],
|
|
5
|
-
testMatch: ['**/*.test.ts', '**/*.spec.ts'],
|
|
6
|
-
coverageDirectory: 'coverage',
|
|
7
|
-
collectCoverage: true,
|
|
8
|
-
transform: {
|
|
9
|
-
'^.+\\.ts$': 'ts-jest',
|
|
10
|
-
},
|
|
11
|
-
};
|
|
12
|
-
|
package/src/behaviors/index.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Behavior methods
|
|
3
|
-
* @module behaviors
|
|
4
|
-
*/
|
|
5
|
-
import { cloneError, clone, contains, exists, isEmpty, is, isnt, isArray, isBoolean, isRegEx, isFunction, isObject, isNumber, isString, safeApply, safeEach, safeIterate, randomId, DashContext, PublicAPI, DashBehaviorContext } from './../utilities';
|
|
6
|
-
|
|
7
|
-
const behaviorFilters: Array<(ctx: any) => any> = [];
|
|
8
|
-
const behaviorActions: Array<(ctx: any) => any> = [];
|
|
9
|
-
const behaviorContext: DashBehaviorContext = {
|
|
10
|
-
clone,
|
|
11
|
-
contains,
|
|
12
|
-
exists,
|
|
13
|
-
is,
|
|
14
|
-
isEmpty,
|
|
15
|
-
isnt,
|
|
16
|
-
isArray,
|
|
17
|
-
isBoolean,
|
|
18
|
-
isRegEx,
|
|
19
|
-
isFunction,
|
|
20
|
-
isObject,
|
|
21
|
-
isNumber,
|
|
22
|
-
isString,
|
|
23
|
-
apply: safeApply,
|
|
24
|
-
each: safeEach,
|
|
25
|
-
iterate: safeIterate,
|
|
26
|
-
random: randomId,
|
|
27
|
-
api: {} as any
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// Methods for managing streams of deferred objects
|
|
32
|
-
export const behaviorMethods = {
|
|
33
|
-
/**
|
|
34
|
-
* Retrieves all behaviors.
|
|
35
|
-
* @param get_ctx The context object.
|
|
36
|
-
* @returns Promise<DashContext>
|
|
37
|
-
*/
|
|
38
|
-
get: function (get_ctx: DashContext): Promise<DashContext> {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
try {
|
|
41
|
-
get_ctx.behaviors = get_ctx.objectstore?.indexNames;
|
|
42
|
-
safeApply(get_ctx.on_success, [get_ctx]);
|
|
43
|
-
resolve(get_ctx);
|
|
44
|
-
} catch (error) {
|
|
45
|
-
get_ctx.error = cloneError(error as Error);
|
|
46
|
-
safeApply(get_ctx.on_error, [get_ctx]);
|
|
47
|
-
reject(get_ctx);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
/**
|
|
52
|
-
* Adds a new behavior.
|
|
53
|
-
* @param add_ctx The context object.
|
|
54
|
-
* @returns Promise<DashContext>
|
|
55
|
-
*/
|
|
56
|
-
add: function (influence: ((ctx: any) => any) | [((ctx: any) => any), ((ctx: any) => any)]) {
|
|
57
|
-
if (Array.isArray(influence)) {
|
|
58
|
-
if (typeof influence[0] === 'function') {
|
|
59
|
-
behaviorFilters.push(influence[0]);
|
|
60
|
-
}
|
|
61
|
-
if (typeof influence[1] === 'function') {
|
|
62
|
-
behaviorActions.push(influence[1]);
|
|
63
|
-
}
|
|
64
|
-
} else if (typeof influence === 'function') {
|
|
65
|
-
behaviorFilters.push(influence);
|
|
66
|
-
behaviorActions.push(influence);
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
/**
|
|
70
|
-
* Removes a behavior.
|
|
71
|
-
* @param remove_ctx The context object.
|
|
72
|
-
* @returns Promise<DashContext>
|
|
73
|
-
*/
|
|
74
|
-
process: function (ctx: DashContext, slug: string, method: string): Promise<DashContext> {
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
let count = 0;
|
|
77
|
-
let response: any;
|
|
78
|
-
let promisesChain = Promise.resolve(ctx); // Initial promise chain starts with the context
|
|
79
|
-
|
|
80
|
-
// Iterate over the behavior filters and chain them
|
|
81
|
-
safeEach(behaviorFilters, (filter) => {
|
|
82
|
-
promisesChain = promisesChain.then((context) => {
|
|
83
|
-
response = safeApply(filter, [{ type: slug, method, context, priority: ++count }], behaviorContext);
|
|
84
|
-
return response.context || context; // Chain the next context
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// After all filters have been processed, resolve or reject based on the final state
|
|
89
|
-
promisesChain
|
|
90
|
-
.then((finalContext) => {
|
|
91
|
-
resolve(finalContext); // Resolve the final context
|
|
92
|
-
})
|
|
93
|
-
.catch((errorContext) => {
|
|
94
|
-
reject(errorContext); // Reject with the error context if any filter failed
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
},
|
|
98
|
-
/**
|
|
99
|
-
* Applies user-defined behaviors to a context.
|
|
100
|
-
* @param ctx The context object.
|
|
101
|
-
* @param slug The slug.
|
|
102
|
-
* @param method The method.
|
|
103
|
-
* @param initialPromise The initial promise.
|
|
104
|
-
* @returns Promise<DashContext>
|
|
105
|
-
*/
|
|
106
|
-
applyBehaviors: function (ctx: DashContext, slug: string, method: string, initialPromise: Promise<DashContext>): Promise<DashContext> {
|
|
107
|
-
let count = 0;
|
|
108
|
-
|
|
109
|
-
return new Promise((resolve, reject) => {
|
|
110
|
-
let promisesChain = initialPromise || Promise.resolve(ctx); // Start the promise chain with the initial promise or the context
|
|
111
|
-
|
|
112
|
-
// Iterate over the behavior actions and chain them using Promises
|
|
113
|
-
safeEach(behaviorActions, (action) => {
|
|
114
|
-
promisesChain = promisesChain.then((context) => {
|
|
115
|
-
const response = safeApply(action, [{ type: slug, method, context, priority: ++count }], behaviorContext);
|
|
116
|
-
|
|
117
|
-
ctx = response.context || ctx; // Update the context if it's modified
|
|
118
|
-
slug = response.type || slug; // Update the slug if modified
|
|
119
|
-
return ctx;
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// After chaining all actions, resolve or reject based on the final context
|
|
124
|
-
promisesChain
|
|
125
|
-
.then((finalContext) => {
|
|
126
|
-
resolve(finalContext); // Resolve with the final context
|
|
127
|
-
})
|
|
128
|
-
.catch((errorContext) => {
|
|
129
|
-
reject(errorContext); // Reject with the error context if any action failed
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export default {
|
|
137
|
-
...behaviorMethods,
|
|
138
|
-
behaviorFilters,
|
|
139
|
-
behaviorActions
|
|
140
|
-
};
|
package/src/database/index.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { cloneError, DashContext, exists } from "../utilities";
|
|
2
|
-
|
|
3
|
-
export const databaseMethods = {
|
|
4
|
-
get: (get_ctx: DashContext): Promise<DashContext> => {
|
|
5
|
-
return new Promise((resolve, reject) => {
|
|
6
|
-
const request = get_ctx.objectstore?.get(get_ctx.key);
|
|
7
|
-
if (!request) {
|
|
8
|
-
get_ctx.error = { message: 'Invalid object store', name: 'DashError' };
|
|
9
|
-
reject(get_ctx);
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
request.onsuccess = (event: Event) => {
|
|
14
|
-
const result = (event.target as IDBRequest).result;
|
|
15
|
-
if (!result) {
|
|
16
|
-
get_ctx.error = { message: 'missing', name: 'DashNoEntry' };
|
|
17
|
-
reject(get_ctx);
|
|
18
|
-
} else {
|
|
19
|
-
get_ctx.entry = result;
|
|
20
|
-
resolve(get_ctx);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
request.onerror = (event: Event) => {
|
|
25
|
-
const error = (event.target as IDBRequest).error;
|
|
26
|
-
get_ctx.error = error ? cloneError(error) : { message: 'Unknown error', name: 'DashError' };
|
|
27
|
-
reject(get_ctx);
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
put: (put_ctx: DashContext): Promise<DashContext> => {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
const request = exists(put_ctx.key)
|
|
35
|
-
? put_ctx.objectstore?.put(put_ctx.data, put_ctx.key)
|
|
36
|
-
: put_ctx.objectstore?.put(put_ctx.data);
|
|
37
|
-
|
|
38
|
-
if (!request) {
|
|
39
|
-
put_ctx.error = { message: 'Invalid object store', name: 'DashError' };
|
|
40
|
-
reject(put_ctx);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
request.onsuccess = (event: Event) => {
|
|
45
|
-
put_ctx.key = (event.target as IDBRequest).result;
|
|
46
|
-
put_ctx.entry = put_ctx.data;
|
|
47
|
-
resolve(put_ctx);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
request.onerror = (event: Event) => {
|
|
51
|
-
const error = (event.target as IDBRequest).error;
|
|
52
|
-
put_ctx.error = error ? cloneError(error) : { message: 'Unknown error', name: 'DashError' };
|
|
53
|
-
reject(put_ctx);
|
|
54
|
-
};
|
|
55
|
-
});
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
remove: (remove_ctx: DashContext): Promise<DashContext> => {
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
const request = remove_ctx.objectstore?.delete(remove_ctx.key);
|
|
61
|
-
|
|
62
|
-
if (!request) {
|
|
63
|
-
remove_ctx.error = { message: 'Invalid object store', name: 'DashError' };
|
|
64
|
-
reject(remove_ctx);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
request.onsuccess = () => {
|
|
69
|
-
resolve(remove_ctx);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
request.onerror = (event: Event) => {
|
|
73
|
-
const error = (event.target as IDBRequest).error;
|
|
74
|
-
remove_ctx.error = error ? cloneError(error) : { message: 'Unknown error', name: 'DashError' };
|
|
75
|
-
reject(remove_ctx);
|
|
76
|
-
};
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export default databaseMethods;
|
package/src/databases/index.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Databases module.
|
|
3
|
-
* @module databases
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { cloneError, DashContext, safeApply } from "../utilities";
|
|
7
|
-
|
|
8
|
-
// Database methods
|
|
9
|
-
export const databasesMethods = {
|
|
10
|
-
/**
|
|
11
|
-
* Gets all databases.
|
|
12
|
-
* @param get_ctx The context object.
|
|
13
|
-
* @returns Promise<DashContext>
|
|
14
|
-
*/
|
|
15
|
-
get: function (get_ctx: DashContext): Promise<DashContext> {
|
|
16
|
-
return new Promise((resolve, reject) => {
|
|
17
|
-
try {
|
|
18
|
-
get_ctx.databases = indexedDB.databases();
|
|
19
|
-
safeApply(get_ctx.on_success, [get_ctx]);
|
|
20
|
-
resolve(get_ctx);
|
|
21
|
-
} catch (error) {
|
|
22
|
-
// Capture the error in the context object
|
|
23
|
-
get_ctx.error = error;
|
|
24
|
-
safeApply(get_ctx.on_error, [get_ctx]);
|
|
25
|
-
reject(get_ctx);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Opens a database.
|
|
32
|
-
* @param open_ctx The context object.
|
|
33
|
-
* @returns Promise<DashContext>
|
|
34
|
-
*/
|
|
35
|
-
open: function (open_ctx: DashContext): Promise<DashContext> {
|
|
36
|
-
return new Promise((resolve, reject) => {
|
|
37
|
-
const request = indexedDB.open(open_ctx.database, open_ctx.version);
|
|
38
|
-
|
|
39
|
-
// Handle success
|
|
40
|
-
request.addEventListener('success', (event) => {
|
|
41
|
-
open_ctx.db = (event.target as IDBOpenDBRequest).result;
|
|
42
|
-
resolve(open_ctx);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Handle upgradeneeded event (used for setting up new object stores or changing version)
|
|
46
|
-
request.addEventListener('upgradeneeded', (event) => {
|
|
47
|
-
open_ctx.db = (event.target as IDBOpenDBRequest).result;
|
|
48
|
-
if (open_ctx.on_upgrade_needed) {
|
|
49
|
-
safeApply(open_ctx.on_upgrade_needed, [open_ctx]);
|
|
50
|
-
}
|
|
51
|
-
resolve(open_ctx);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Handle errors
|
|
55
|
-
request.addEventListener('error', (event) => {
|
|
56
|
-
open_ctx.error = cloneError((event as any).target.error);
|
|
57
|
-
reject(open_ctx);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// Handle blocked (when another connection keeps the database from opening)
|
|
61
|
-
request.addEventListener('blocked', (event) => {
|
|
62
|
-
if (open_ctx.on_blocked) {
|
|
63
|
-
safeApply(open_ctx.on_blocked, [open_ctx]);
|
|
64
|
-
}
|
|
65
|
-
reject(open_ctx);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
},
|
|
69
|
-
/**
|
|
70
|
-
* Deletes a database.
|
|
71
|
-
* @param delete_ctx The context object.
|
|
72
|
-
* @returns Promise<DashContext>
|
|
73
|
-
*/
|
|
74
|
-
delete: function (delete_ctx: DashContext): Promise<DashContext> {
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
const request = indexedDB.deleteDatabase(delete_ctx.database);
|
|
77
|
-
|
|
78
|
-
request.addEventListener('success', () => {
|
|
79
|
-
resolve(delete_ctx);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
request.addEventListener('error', (event) => {
|
|
83
|
-
delete_ctx.error = cloneError((event as any).target.error);
|
|
84
|
-
reject(delete_ctx);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
request.addEventListener('blocked', (event) => {
|
|
88
|
-
if (delete_ctx.on_blocked) {
|
|
89
|
-
safeApply(delete_ctx.on_blocked, [delete_ctx]);
|
|
90
|
-
}
|
|
91
|
-
reject(delete_ctx);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
/**
|
|
96
|
-
* Closes a database.
|
|
97
|
-
* @param close_ctx The context object.
|
|
98
|
-
* @returns Promise<DashContext>
|
|
99
|
-
*/
|
|
100
|
-
close: function (close_ctx: DashContext): Promise<DashContext> {
|
|
101
|
-
return new Promise((resolve) => {
|
|
102
|
-
if (close_ctx.db) {
|
|
103
|
-
close_ctx.db.close();
|
|
104
|
-
}
|
|
105
|
-
resolve(close_ctx);
|
|
106
|
-
});
|
|
107
|
-
},
|
|
108
|
-
/**
|
|
109
|
-
* Lists all databases.
|
|
110
|
-
* @returns Promise<IDBDatabaseInfo[]>
|
|
111
|
-
*/
|
|
112
|
-
listAll: function (): Promise<IDBDatabaseInfo[]> {
|
|
113
|
-
return new Promise((resolve, reject) => {
|
|
114
|
-
if ('databases' in indexedDB && typeof (indexedDB as any).databases === 'function') {
|
|
115
|
-
(indexedDB as any).databases().then((databases: IDBDatabaseInfo[]) => {
|
|
116
|
-
resolve(databases);
|
|
117
|
-
}).catch((error: any) => {
|
|
118
|
-
reject(error);
|
|
119
|
-
});
|
|
120
|
-
} else {
|
|
121
|
-
reject(new Error("The `databases` method is not supported in this browser."));
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
export default databasesMethods;
|
package/src/entry/index.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Entry methods
|
|
4
|
-
* @module entry
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { cloneError, DashContext } from "../utilities";
|
|
8
|
-
|
|
9
|
-
// Entry methods
|
|
10
|
-
export const entryMethods = {
|
|
11
|
-
/**
|
|
12
|
-
* Add an entry.
|
|
13
|
-
* @param add_ctx
|
|
14
|
-
* @returns Promise<DashContext>
|
|
15
|
-
*/
|
|
16
|
-
add: (add_ctx: DashContext) => {
|
|
17
|
-
return new Promise((resolve, reject) => {
|
|
18
|
-
const request = add_ctx.objectstore?.add(add_ctx.data, add_ctx.key);
|
|
19
|
-
|
|
20
|
-
if (!request) {
|
|
21
|
-
add_ctx.error = { message: "Request object is null", name: "DashRequestError" };
|
|
22
|
-
return reject(add_ctx);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
request.addEventListener('success', () => {
|
|
26
|
-
add_ctx.key = request.result;
|
|
27
|
-
add_ctx.entry = add_ctx.data;
|
|
28
|
-
resolve(add_ctx);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
request.addEventListener('error', (event) => {
|
|
32
|
-
add_ctx.error = cloneError((event as any).target.error);
|
|
33
|
-
reject(add_ctx);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Get an entry.
|
|
40
|
-
* @param get_ctx
|
|
41
|
-
* @returns Promise<DashContext>
|
|
42
|
-
*/
|
|
43
|
-
get: (get_ctx: DashContext) => {
|
|
44
|
-
return new Promise((resolve, reject) => {
|
|
45
|
-
let request;
|
|
46
|
-
|
|
47
|
-
if (get_ctx.index && get_ctx.objectstore && typeof get_ctx.objectstore.index === 'function') {
|
|
48
|
-
const index = get_ctx.objectstore.index(get_ctx.index);
|
|
49
|
-
if (index && typeof index.get === 'function') {
|
|
50
|
-
request = index.get(get_ctx.key);
|
|
51
|
-
} else {
|
|
52
|
-
get_ctx.error = { message: 'Invalid index or get method', name: 'DashInvalidIndex' };
|
|
53
|
-
return reject(get_ctx);
|
|
54
|
-
}
|
|
55
|
-
} else if (get_ctx.objectstore && typeof get_ctx.objectstore.get === 'function') {
|
|
56
|
-
request = get_ctx.objectstore.get(get_ctx.key);
|
|
57
|
-
} else {
|
|
58
|
-
get_ctx.error = { message: 'Invalid objectstore or get method', name: 'DashInvalidObjectStore' };
|
|
59
|
-
return reject(get_ctx);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (!request) {
|
|
63
|
-
get_ctx.error = { message: "Request object is null", name: "DashRequestError" };
|
|
64
|
-
return reject(get_ctx);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
request.addEventListener('success', () => {
|
|
68
|
-
get_ctx.entry = request.result;
|
|
69
|
-
resolve(get_ctx);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
request.addEventListener('error', (event) => {
|
|
73
|
-
get_ctx.error = cloneError((event as any).target.error);
|
|
74
|
-
reject(get_ctx);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Put an entry.
|
|
81
|
-
* @param put_ctx
|
|
82
|
-
* @returns Promise<DashContext>
|
|
83
|
-
*/
|
|
84
|
-
put: (put_ctx: DashContext) => {
|
|
85
|
-
return new Promise((resolve, reject) => {
|
|
86
|
-
const request = put_ctx.objectstore?.put(put_ctx.data, put_ctx.key);
|
|
87
|
-
|
|
88
|
-
if (!request) {
|
|
89
|
-
put_ctx.error = { message: "Request object is null", name: "DashRequestError" };
|
|
90
|
-
return reject(put_ctx);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
request.addEventListener('success', () => {
|
|
94
|
-
put_ctx.entry = put_ctx.data;
|
|
95
|
-
resolve(put_ctx);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
request.addEventListener('error', (event) => {
|
|
99
|
-
put_ctx.error = cloneError((event as any).target.error);
|
|
100
|
-
reject(put_ctx);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Remove an entry.
|
|
107
|
-
* @param remove_ctx
|
|
108
|
-
* @returns Promise<DashContext>
|
|
109
|
-
*/
|
|
110
|
-
remove: (remove_ctx: DashContext) => {
|
|
111
|
-
return new Promise((resolve, reject) => {
|
|
112
|
-
const request = remove_ctx.objectstore?.delete(remove_ctx.key);
|
|
113
|
-
|
|
114
|
-
if (!request) {
|
|
115
|
-
remove_ctx.error = { message: "Request object is null", name: "DashRequestError" };
|
|
116
|
-
return reject(remove_ctx);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
request.addEventListener('success', () => {
|
|
120
|
-
resolve(remove_ctx);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
request.addEventListener('error', (event) => {
|
|
124
|
-
remove_ctx.error = cloneError((event as any).target.error);
|
|
125
|
-
reject(remove_ctx);
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Update an entry.
|
|
132
|
-
* @param update_ctx
|
|
133
|
-
* @returns Promise<DashContext>
|
|
134
|
-
*/
|
|
135
|
-
update: (update_ctx: DashContext) => {
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
const request = update_ctx.objectstore?.put(update_ctx.data, update_ctx.key);
|
|
138
|
-
|
|
139
|
-
if (!request) {
|
|
140
|
-
update_ctx.error = { message: "Request object is null", name: "DashRequestError" };
|
|
141
|
-
return reject(update_ctx);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
request.addEventListener('success', () => {
|
|
145
|
-
update_ctx.entry = update_ctx.data;
|
|
146
|
-
resolve(update_ctx);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
request.addEventListener('error', (event) => {
|
|
150
|
-
update_ctx.error = cloneError((event as any).target.error);
|
|
151
|
-
reject(update_ctx);
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Count entries.
|
|
158
|
-
* @param count_ctx
|
|
159
|
-
* @returns Promise<DashContext>
|
|
160
|
-
*/
|
|
161
|
-
count: (count_ctx: DashContext) => {
|
|
162
|
-
return new Promise((resolve, reject) => {
|
|
163
|
-
const request = count_ctx.objectstore?.count(count_ctx.range);
|
|
164
|
-
|
|
165
|
-
if (!request) {
|
|
166
|
-
count_ctx.error = { message: "Request object is null", name: "DashRequestError" };
|
|
167
|
-
return reject(count_ctx);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
request.addEventListener('success', () => {
|
|
171
|
-
count_ctx.amount = request.result;
|
|
172
|
-
resolve(count_ctx);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
request.addEventListener('error', (event) => {
|
|
176
|
-
count_ctx.error = cloneError((event as any).target.error);
|
|
177
|
-
reject(count_ctx);
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
export default entryMethods;
|