@incoqnito.io/bajadab 1.1.0
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 +1 -0
- package/README.md +101 -0
- package/dist/bajadab.d.ts +3 -0
- package/dist/bajadab.js +4 -0
- package/dist/bajadab.js.map +1 -0
- package/dist/base.d.ts +110 -0
- package/dist/base.js +54 -0
- package/dist/base.js.map +1 -0
- package/dist/basics.d.ts +50 -0
- package/dist/basics.js +434 -0
- package/dist/basics.js.map +1 -0
- package/dist/fileio.d.ts +24 -0
- package/dist/fileio.js +125 -0
- package/dist/fileio.js.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +1 -0
- package/package.json +52 -0
- package/src/main/bajadab.ts +3 -0
- package/src/main/base.ts +125 -0
- package/src/main/basics.ts +482 -0
- package/src/main/fileio.ts +154 -0
- package/src/main/utils.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(c) Copyright 2026 incoqnito GmbH, all rights reserved.
|
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# bajadab
|
|
2
|
+
|
|
3
|
+
A lightweight JSON-file-backed database for small, single-machine setups. No server, no native bindings — just collections of values persisted as JSON under a directory you choose.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Collections stored as plain JSON files, one directory per database.
|
|
8
|
+
- `MAP` (keyed by id) or `ARRAY` (ordered list) in-memory storage per collection.
|
|
9
|
+
- Configurable behavior for duplicate ids, missing ids, array deletion, auto-flush, and unloading dirty data.
|
|
10
|
+
- Custom id extraction/assignment via a pluggable function registry.
|
|
11
|
+
- Atomic writes (write-to-temp, then rename) and, on POSIX systems, owner-only file permissions (`0600` files, `0700` directories).
|
|
12
|
+
- In-process concurrency safety per collection and per database.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm install @incoqnito.io/bajadab
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Requires Node.js `>= 24`.
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { getDatabase, EBMemStorageStrategy } from "@incoqnito.io/bajadab";
|
|
26
|
+
|
|
27
|
+
const db = await getDatabase("./data");
|
|
28
|
+
|
|
29
|
+
const notes = await db.getCollection<{ id?: string; text: string }>("notes", {
|
|
30
|
+
...db.config.defaultCollectionConfig,
|
|
31
|
+
memStorageStrategy: EBMemStorageStrategy.ARRAY,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
await notes.add({ text: "hello" });
|
|
35
|
+
const all = await notes.get();
|
|
36
|
+
const some = await notes.get(n => n.text.startsWith("h"));
|
|
37
|
+
|
|
38
|
+
await notes.delete(n => n.text === "hello");
|
|
39
|
+
await db.dropCollection("notes");
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`getDatabase(basePath)` opens the database at `basePath`, creating it if it doesn't exist. `db.getCollection(name, config?)` returns the named collection, creating it with `config` (or the database's default) if it doesn't exist yet; `db.createCollection(name, config?)` instead rejects if one already exists. Collection names are matched case- and whitespace-insensitively (`"Notes"` and `" notes "` refer to the same collection).
|
|
43
|
+
|
|
44
|
+
## Configuring a collection
|
|
45
|
+
|
|
46
|
+
Every collection has an `IBCollectionConfig`. `db.config.defaultCollectionConfig` holds the database's defaults — spread it and override what you need, as above.
|
|
47
|
+
|
|
48
|
+
| Option | Values | Meaning |
|
|
49
|
+
| --- | --- | --- |
|
|
50
|
+
| `memStorageStrategy` | `MAP` (default) / `ARRAY` | Keyed lookup by id, or an ordered list. |
|
|
51
|
+
| `autoIDStrategy` | `AUTO_ID_UUID` (default) / `NO_AUTO_ID` | Generate a `uuidv7` for values with no id, or reject them. |
|
|
52
|
+
| `duplicateIDStrategy` | `OVERWRITE` (default) / `ERROR` / `IGNORE` | What `add()` does when a value's id already exists. |
|
|
53
|
+
| `arrayDeletionStrategy` | `IN_PLACE` (default) / `NEW_ARRAY` | How `delete()` removes matches from an `ARRAY` collection. |
|
|
54
|
+
| `autoFlushStrategy` | `ALWAYS_AUTO_FLUSH` (default) / `NO_AUTO_FLUSH` | Whether every mutation is written to disk immediately, or only on an explicit `flush()`. |
|
|
55
|
+
| `dirtyUnloadStrategy` | `FLUSH` (default) / `ERROR` / `IGNORE` | What `unload()` does with unsaved changes. |
|
|
56
|
+
| `getID` / `setID` | registry key (optional) | Custom id accessors — see below. Omit to use the default, which reads/writes a plain `.id` property. |
|
|
57
|
+
|
|
58
|
+
## Custom ids
|
|
59
|
+
|
|
60
|
+
By default, a value's id is its `.id` property. To use something else, implement `IBFunctionRegistry` and register `getID`/`setID` functions under whatever keys you configure:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { getDatabase, IBFunctionRegistry } from "@incoqnito.io/bajadab";
|
|
64
|
+
|
|
65
|
+
class Registry implements IBFunctionRegistry {
|
|
66
|
+
private fns = new Map<string, Function>([
|
|
67
|
+
["userGetID", async (v: any) => v.userId],
|
|
68
|
+
["userSetID", async (v: any, id: string) => { v.userId = id; }],
|
|
69
|
+
]);
|
|
70
|
+
fetch(key?: string) {
|
|
71
|
+
return key ? this.fns.get(key) : undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const db = await getDatabase("./data", new Registry());
|
|
76
|
+
const users = await db.createCollection("users", {
|
|
77
|
+
...db.config.defaultCollectionConfig,
|
|
78
|
+
getID: "userGetID",
|
|
79
|
+
setID: "userSetID",
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
A `getID`/`setID` key that isn't found in the registry causes `add()` to reject, rather than silently falling back to the default.
|
|
84
|
+
|
|
85
|
+
## Persistence and file layout
|
|
86
|
+
|
|
87
|
+
Each database directory contains:
|
|
88
|
+
|
|
89
|
+
- `dbmeta.json` — the database config and a name → id map for its collections.
|
|
90
|
+
- `<id>.json` — per-collection metadata (name, config, id).
|
|
91
|
+
- `<id>_data/data.json` — the collection's actual values.
|
|
92
|
+
|
|
93
|
+
Collections are addressed by a stable id, not by name, so a handle to a since-renamed-or-recreated collection can't end up reading or writing the wrong data. Every write goes through a temp-file-then-rename step to avoid partial writes. On POSIX systems, created files get mode `0600` and directories `0700`; this has no effect on Windows, which has no equivalent permission bits.
|
|
94
|
+
|
|
95
|
+
## Concurrency
|
|
96
|
+
|
|
97
|
+
Operations on the same collection, and database-level operations (`create`/`get`/`has`/`dropCollection`), are serialized within one process. There is no cross-process locking — multiple processes pointed at the same directory can still race on the underlying files.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
See [LICENSE](./LICENSE).
|
package/dist/bajadab.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bajadab.js","sourceRoot":"","sources":["../src/main/bajadab.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,yBAAyB,EAAE,uBAAuB,EAAE,cAAc,EAAE,cAAc,GAAG,MAAM,aAAa,CAAC"}
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/** How a missing id is handled on {@link IBCollection.add}. */
|
|
2
|
+
export declare enum EBAutoIDStrategy {
|
|
3
|
+
/** Reject values without an id. */
|
|
4
|
+
NO_AUTO_ID = "NO_AUTO_ID",
|
|
5
|
+
/** Generate a `uuidv7` and assign it via the configured setter. */
|
|
6
|
+
AUTO_ID_UUID = "AUTO_ID_UUID"
|
|
7
|
+
}
|
|
8
|
+
/** In-memory representation of a collection's data. */
|
|
9
|
+
export declare enum EBMemStorageStrategy {
|
|
10
|
+
/** Keyed by id; `add()` looks up existing entries by id in O(1). */
|
|
11
|
+
MAP = "MAP",
|
|
12
|
+
/** Plain list; insertion order is preserved. */
|
|
13
|
+
ARRAY = "ARRAY"
|
|
14
|
+
}
|
|
15
|
+
/** What happens on {@link IBCollection.add} when a value's id already exists. */
|
|
16
|
+
export declare enum EBDuplicateIDStrategy {
|
|
17
|
+
/** Replace the existing value. */
|
|
18
|
+
OVERWRITE = "OVERWRITE",
|
|
19
|
+
/** Reject the whole call. */
|
|
20
|
+
ERROR = "ERROR",
|
|
21
|
+
/** Keep the existing value, drop the new one. */
|
|
22
|
+
IGNORE = "IGNORE"
|
|
23
|
+
}
|
|
24
|
+
/** How {@link IBCollection.delete} removes matched items from an `ARRAY` collection. */
|
|
25
|
+
export declare enum EBArrayDeletionStrategy {
|
|
26
|
+
/** Build a new array via `filter()`. */
|
|
27
|
+
NEW_ARRAY = "NEW_ARRAY",
|
|
28
|
+
/** Compact the existing array in place. */
|
|
29
|
+
IN_PLACE = "IN_PLACE"
|
|
30
|
+
}
|
|
31
|
+
/** When a collection is written to disk after a mutation. */
|
|
32
|
+
export declare enum EBAutoFlushStrategy {
|
|
33
|
+
/** Only on an explicit {@link IBCollection.flush} call. */
|
|
34
|
+
NO_AUTO_FLUSH = "NO_AUTO_FLUSH",
|
|
35
|
+
/** After every mutating call (`add`/`update`/`delete`). */
|
|
36
|
+
ALWAYS_AUTO_FLUSH = "ALWAYS_AUTO_FLUSH"
|
|
37
|
+
}
|
|
38
|
+
/** What {@link IBCollection.unload} does with unsaved changes. */
|
|
39
|
+
export declare enum EBDirtyUnloadStrategy {
|
|
40
|
+
/** Reject the unload. */
|
|
41
|
+
ERROR = "ERROR",
|
|
42
|
+
/** Flush first, then unload. */
|
|
43
|
+
FLUSH = "FLUSH",
|
|
44
|
+
/** Discard the unsaved changes silently. */
|
|
45
|
+
IGNORE = "IGNORE"
|
|
46
|
+
}
|
|
47
|
+
/** Resolves the id of a value; `undefined` means "no id yet". */
|
|
48
|
+
export type TGetIDFunction = (v: any) => Promise<string | undefined>;
|
|
49
|
+
/** Assigns an id onto a value. */
|
|
50
|
+
export type TSetIDFunction = (v: any, id: string) => Promise<void>;
|
|
51
|
+
/** Per-collection behavior. Passed to {@link IBDatabase.createCollection}/{@link IBDatabase.getCollection}. */
|
|
52
|
+
export interface IBCollectionConfig {
|
|
53
|
+
autoIDStrategy: EBAutoIDStrategy;
|
|
54
|
+
duplicateIDStrategy: EBDuplicateIDStrategy;
|
|
55
|
+
memStorageStrategy: EBMemStorageStrategy;
|
|
56
|
+
arrayDeletionStrategy: EBArrayDeletionStrategy;
|
|
57
|
+
dirtyUnloadStrategy: EBDirtyUnloadStrategy;
|
|
58
|
+
autoFlushStrategy: EBAutoFlushStrategy;
|
|
59
|
+
/** Registry key for a custom {@link TGetIDFunction}. Omit to use the default (reads `.id`). */
|
|
60
|
+
getID?: string;
|
|
61
|
+
/** Registry key for a custom {@link TSetIDFunction}. Omit to use the default (sets `.id`). */
|
|
62
|
+
setID?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Database-wide configuration. */
|
|
65
|
+
export interface IBDatabaseConfig {
|
|
66
|
+
/** Used for any collection created without an explicit config. */
|
|
67
|
+
readonly defaultCollectionConfig: IBCollectionConfig;
|
|
68
|
+
}
|
|
69
|
+
/** A JSON-file-backed database. Obtain one via `getDatabase()`. */
|
|
70
|
+
export interface IBDatabase {
|
|
71
|
+
readonly config: IBDatabaseConfig;
|
|
72
|
+
/** Creates a new collection. Rejects if one with the same (canonicalized) name already exists. */
|
|
73
|
+
createCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
74
|
+
/** Returns the named collection, creating it with `collectionConfig` (or the database default) if it doesn't exist yet. */
|
|
75
|
+
getCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
76
|
+
/** Whether a collection with this (canonicalized) name exists. */
|
|
77
|
+
hasCollection(collectionName: string): Promise<boolean>;
|
|
78
|
+
/** Deletes a collection and invalidates any handle still held on it. Rejects if it doesn't exist. */
|
|
79
|
+
dropCollection(collectionName: string): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
/** A single collection of values of type `T`. Obtain one via {@link IBDatabase}. */
|
|
82
|
+
export interface IBCollection<T = any> {
|
|
83
|
+
readonly database: IBDatabase;
|
|
84
|
+
readonly collectionConfig: IBCollectionConfig;
|
|
85
|
+
/** Canonicalized (trimmed, lowercased) name. */
|
|
86
|
+
readonly collectionName: string;
|
|
87
|
+
/** Stable identity used to address this collection's storage, independent of its name. */
|
|
88
|
+
readonly collectionID: string;
|
|
89
|
+
/** All items, or only those matching `predicate`. Always a defensive copy, never the live storage. */
|
|
90
|
+
get(predicate?: (v: T) => boolean): Promise<T[]>;
|
|
91
|
+
/** Deletes matching items. Returns the number removed. */
|
|
92
|
+
delete(predicate: (v: T) => boolean): Promise<number>;
|
|
93
|
+
/** Whether any item matches `predicate`. */
|
|
94
|
+
has(predicate: (v: T) => boolean): Promise<boolean>;
|
|
95
|
+
/** Adds values, resolving/assigning ids per the collection's id strategies. Returns the number added. */
|
|
96
|
+
add(...v: T[]): Promise<number>;
|
|
97
|
+
/** Replaces each item matching `predicate` with the (defined) result of `updater`. Returns the number updated. */
|
|
98
|
+
update(predicate: (v: T) => boolean, updater: (v: T) => Promise<T>): Promise<number>;
|
|
99
|
+
/** Writes the current in-memory state to disk. */
|
|
100
|
+
flush(): Promise<void>;
|
|
101
|
+
/** Discards in-memory state and re-reads it from disk. */
|
|
102
|
+
reload(): Promise<void>;
|
|
103
|
+
/** Evicts the in-memory state (per the dirty-unload strategy if there are unsaved changes). */
|
|
104
|
+
unload(): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
/** Resolves the custom id functions referenced by {@link IBCollectionConfig.getID}/`setID`. */
|
|
107
|
+
export interface IBFunctionRegistry {
|
|
108
|
+
/** Returns the function registered under `key`, or `undefined` if there is none. */
|
|
109
|
+
fetch(key?: string): Function | undefined;
|
|
110
|
+
}
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** How a missing id is handled on {@link IBCollection.add}. */
|
|
2
|
+
export var EBAutoIDStrategy;
|
|
3
|
+
(function (EBAutoIDStrategy) {
|
|
4
|
+
/** Reject values without an id. */
|
|
5
|
+
EBAutoIDStrategy["NO_AUTO_ID"] = "NO_AUTO_ID";
|
|
6
|
+
/** Generate a `uuidv7` and assign it via the configured setter. */
|
|
7
|
+
EBAutoIDStrategy["AUTO_ID_UUID"] = "AUTO_ID_UUID";
|
|
8
|
+
})(EBAutoIDStrategy || (EBAutoIDStrategy = {}));
|
|
9
|
+
/** In-memory representation of a collection's data. */
|
|
10
|
+
export var EBMemStorageStrategy;
|
|
11
|
+
(function (EBMemStorageStrategy) {
|
|
12
|
+
/** Keyed by id; `add()` looks up existing entries by id in O(1). */
|
|
13
|
+
EBMemStorageStrategy["MAP"] = "MAP";
|
|
14
|
+
// LINKED_LIST = "LINKED_LIST",
|
|
15
|
+
/** Plain list; insertion order is preserved. */
|
|
16
|
+
EBMemStorageStrategy["ARRAY"] = "ARRAY";
|
|
17
|
+
})(EBMemStorageStrategy || (EBMemStorageStrategy = {}));
|
|
18
|
+
/** What happens on {@link IBCollection.add} when a value's id already exists. */
|
|
19
|
+
export var EBDuplicateIDStrategy;
|
|
20
|
+
(function (EBDuplicateIDStrategy) {
|
|
21
|
+
/** Replace the existing value. */
|
|
22
|
+
EBDuplicateIDStrategy["OVERWRITE"] = "OVERWRITE";
|
|
23
|
+
/** Reject the whole call. */
|
|
24
|
+
EBDuplicateIDStrategy["ERROR"] = "ERROR";
|
|
25
|
+
/** Keep the existing value, drop the new one. */
|
|
26
|
+
EBDuplicateIDStrategy["IGNORE"] = "IGNORE";
|
|
27
|
+
})(EBDuplicateIDStrategy || (EBDuplicateIDStrategy = {}));
|
|
28
|
+
/** How {@link IBCollection.delete} removes matched items from an `ARRAY` collection. */
|
|
29
|
+
export var EBArrayDeletionStrategy;
|
|
30
|
+
(function (EBArrayDeletionStrategy) {
|
|
31
|
+
/** Build a new array via `filter()`. */
|
|
32
|
+
EBArrayDeletionStrategy["NEW_ARRAY"] = "NEW_ARRAY";
|
|
33
|
+
/** Compact the existing array in place. */
|
|
34
|
+
EBArrayDeletionStrategy["IN_PLACE"] = "IN_PLACE";
|
|
35
|
+
})(EBArrayDeletionStrategy || (EBArrayDeletionStrategy = {}));
|
|
36
|
+
/** When a collection is written to disk after a mutation. */
|
|
37
|
+
export var EBAutoFlushStrategy;
|
|
38
|
+
(function (EBAutoFlushStrategy) {
|
|
39
|
+
/** Only on an explicit {@link IBCollection.flush} call. */
|
|
40
|
+
EBAutoFlushStrategy["NO_AUTO_FLUSH"] = "NO_AUTO_FLUSH";
|
|
41
|
+
/** After every mutating call (`add`/`update`/`delete`). */
|
|
42
|
+
EBAutoFlushStrategy["ALWAYS_AUTO_FLUSH"] = "ALWAYS_AUTO_FLUSH";
|
|
43
|
+
})(EBAutoFlushStrategy || (EBAutoFlushStrategy = {}));
|
|
44
|
+
/** What {@link IBCollection.unload} does with unsaved changes. */
|
|
45
|
+
export var EBDirtyUnloadStrategy;
|
|
46
|
+
(function (EBDirtyUnloadStrategy) {
|
|
47
|
+
/** Reject the unload. */
|
|
48
|
+
EBDirtyUnloadStrategy["ERROR"] = "ERROR";
|
|
49
|
+
/** Flush first, then unload. */
|
|
50
|
+
EBDirtyUnloadStrategy["FLUSH"] = "FLUSH";
|
|
51
|
+
/** Discard the unsaved changes silently. */
|
|
52
|
+
EBDirtyUnloadStrategy["IGNORE"] = "IGNORE";
|
|
53
|
+
})(EBDirtyUnloadStrategy || (EBDirtyUnloadStrategy = {}));
|
|
54
|
+
//# sourceMappingURL=base.js.map
|
package/dist/base.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/main/base.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,MAAM,CAAN,IAAY,gBAKX;AALD,WAAY,gBAAgB;IACxB,mCAAmC;IACnC,6CAAyB,CAAA;IACzB,mEAAmE;IACnE,iDAA6B,CAAA;AACjC,CAAC,EALW,gBAAgB,KAAhB,gBAAgB,QAK3B;AAED,uDAAuD;AACvD,MAAM,CAAN,IAAY,oBAMX;AAND,WAAY,oBAAoB;IAC5B,oEAAoE;IACpE,mCAAW,CAAA;IACX,+BAA+B;IAC/B,gDAAgD;IAChD,uCAAe,CAAA;AACnB,CAAC,EANW,oBAAoB,KAApB,oBAAoB,QAM/B;AAED,iFAAiF;AACjF,MAAM,CAAN,IAAY,qBAOX;AAPD,WAAY,qBAAqB;IAC7B,kCAAkC;IAClC,gDAAuB,CAAA;IACvB,6BAA6B;IAC7B,wCAAe,CAAA;IACf,iDAAiD;IACjD,0CAAiB,CAAA;AACrB,CAAC,EAPW,qBAAqB,KAArB,qBAAqB,QAOhC;AAED,wFAAwF;AACxF,MAAM,CAAN,IAAY,uBAKX;AALD,WAAY,uBAAuB;IAC/B,wCAAwC;IACxC,kDAAuB,CAAA;IACvB,2CAA2C;IAC3C,gDAAqB,CAAA;AACzB,CAAC,EALW,uBAAuB,KAAvB,uBAAuB,QAKlC;AAED,6DAA6D;AAC7D,MAAM,CAAN,IAAY,mBAKX;AALD,WAAY,mBAAmB;IAC3B,2DAA2D;IAC3D,sDAA+B,CAAA;IAC/B,2DAA2D;IAC3D,8DAAuC,CAAA;AAC3C,CAAC,EALW,mBAAmB,KAAnB,mBAAmB,QAK9B;AAED,kEAAkE;AAClE,MAAM,CAAN,IAAY,qBAOX;AAPD,WAAY,qBAAqB;IAC7B,yBAAyB;IACzB,wCAAe,CAAA;IACf,gCAAgC;IAChC,wCAAe,CAAA;IACf,4CAA4C;IAC5C,0CAAiB,CAAA;AACrB,CAAC,EAPW,qBAAqB,KAArB,qBAAqB,QAOhC"}
|
package/dist/basics.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { IODatabase } from "./fileio.js";
|
|
2
|
+
import { IBCollection, IBCollectionConfig, IBDatabase, IBDatabaseConfig, IBFunctionRegistry, TGetIDFunction, TSetIDFunction } from "./base.js";
|
|
3
|
+
/** Default {@link TGetIDFunction}: reads `v.id`, `undefined` if absent. */
|
|
4
|
+
export declare const DEFAULT_GET_ID: TGetIDFunction;
|
|
5
|
+
/** Default {@link TSetIDFunction}: assigns to `v.id`. */
|
|
6
|
+
export declare const DEFAULT_SET_ID: TSetIDFunction;
|
|
7
|
+
/** {@link IBCollectionConfig} used when a collection is created without an explicit one. */
|
|
8
|
+
export declare const DEFAULT_COLLECTION_CONFIG: IBCollectionConfig;
|
|
9
|
+
/** {@link IBDatabaseConfig} used when `getDatabase()` creates a fresh database. */
|
|
10
|
+
export declare const DEFAULT_DATABASE_CONFIG: IBDatabaseConfig;
|
|
11
|
+
export declare class BDatabase implements IBDatabase {
|
|
12
|
+
readonly config: IBDatabaseConfig;
|
|
13
|
+
private collections;
|
|
14
|
+
readonly io: IODatabase;
|
|
15
|
+
readonly functionRegistry?: IBFunctionRegistry;
|
|
16
|
+
constructor(io: IODatabase, config?: IBDatabaseConfig, functionRegistry?: IBFunctionRegistry);
|
|
17
|
+
addCollection(collection: BCollection): void;
|
|
18
|
+
getCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
19
|
+
createCollection<T = any>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<BCollection<T>>;
|
|
20
|
+
hasCollection(collectionName: string): Promise<boolean>;
|
|
21
|
+
dropCollection(collectionName: string): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export declare class BCollection<T = any> implements IBCollection<T> {
|
|
24
|
+
readonly database: BDatabase;
|
|
25
|
+
readonly collectionConfig: IBCollectionConfig;
|
|
26
|
+
readonly collectionName: string;
|
|
27
|
+
readonly collectionID: string;
|
|
28
|
+
private mapData;
|
|
29
|
+
private arrayData;
|
|
30
|
+
private loaded;
|
|
31
|
+
private dirty;
|
|
32
|
+
private dropped;
|
|
33
|
+
constructor(database: BDatabase, collectionID: string, collectionName: string, collectionConfig: IBCollectionConfig);
|
|
34
|
+
private checkLoaded;
|
|
35
|
+
private runProtected;
|
|
36
|
+
/**
|
|
37
|
+
* call this only within the proteing mutex
|
|
38
|
+
*/
|
|
39
|
+
markDropped(): void;
|
|
40
|
+
get(predicate?: (v: T) => boolean): Promise<T[]>;
|
|
41
|
+
delete(predicate: (v: T) => boolean): Promise<number>;
|
|
42
|
+
has(predicate: (v: T) => boolean): Promise<boolean>;
|
|
43
|
+
private getID;
|
|
44
|
+
add(...vs: T[]): Promise<number>;
|
|
45
|
+
update(predicate: (v: T) => boolean, updater: (v: T) => Promise<T>): Promise<number>;
|
|
46
|
+
private doOnDirty;
|
|
47
|
+
flush(): Promise<void>;
|
|
48
|
+
reload(): Promise<void>;
|
|
49
|
+
unload(): Promise<void>;
|
|
50
|
+
}
|