@jnode/db 1.0.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 +201 -0
- package/README.md +386 -0
- package/package.json +35 -0
- package/src/dble.js +1039 -0
- package/src/index.js +14 -0
- package/src/jdb.js +81 -0
- package/src/ljson.js +165 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
# `@jnode/db`
|
|
2
|
+
|
|
3
|
+
Simple database package for Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i @jnode/db
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
### Import
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
// Import from the main entry
|
|
17
|
+
const { jdb, dble, ljson } = require('@jnode/db');
|
|
18
|
+
|
|
19
|
+
// Or import from subpaths
|
|
20
|
+
const { JDBFile } = require('@jnode/db/jdb');
|
|
21
|
+
const { DBLEFile, DBLEUInt32Field, DBLEStringField } = require('@jnode/db/dble');
|
|
22
|
+
const { LJSONFile } = require('@jnode/db/ljson');
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Start with a simple LJSON database
|
|
26
|
+
|
|
27
|
+
LJSON is perfect for small, append-only or simple text-based data.
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { LJSONFile } = require('@jnode/db/ljson');
|
|
31
|
+
|
|
32
|
+
(async () => {
|
|
33
|
+
// Load or create a database
|
|
34
|
+
const db = await LJSONFile.load('./data.ljson');
|
|
35
|
+
|
|
36
|
+
// Push new data
|
|
37
|
+
await db.push({ name: 'Alice', score: 10 });
|
|
38
|
+
await db.push({ name: 'Bob', score: 15 });
|
|
39
|
+
|
|
40
|
+
// Update data (adds to numbers/strings, merges objects, etc.)
|
|
41
|
+
await db.update(0, { score: 5 }); // Alice's score becomes 15
|
|
42
|
+
|
|
43
|
+
// Overwrite or delete
|
|
44
|
+
await db.overwrite(1, { name: 'Bob', score: 20 });
|
|
45
|
+
await db.delete(0); // Deletes Alice
|
|
46
|
+
})();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Start a complex DBLE database
|
|
50
|
+
|
|
51
|
+
DBLE is a fast, structured, binary-based database with schema definitions.
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const { DBLEFile, DBLEUInt32Field, DBLEStringField } = require('@jnode/db/dble');
|
|
55
|
+
|
|
56
|
+
(async () => {
|
|
57
|
+
// Create a new DBLE database with a defined schema
|
|
58
|
+
const db = await DBLEFile.create('./users.dble', {
|
|
59
|
+
fields:[
|
|
60
|
+
new DBLEUInt32Field('id', true), // 'id' is a key (indexed)
|
|
61
|
+
new DBLEStringField(255, 'username')
|
|
62
|
+
]
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Append lines (rows)
|
|
66
|
+
await db.appendLine({ id: 1, username: 'justapple' });
|
|
67
|
+
await db.appendLine({ id: 2, username: 'nick' });
|
|
68
|
+
|
|
69
|
+
// Fast query using indexed keys
|
|
70
|
+
const nickData = await db.readLineByField('id', 2);
|
|
71
|
+
console.log(nickData.fields.username); // 'nick'
|
|
72
|
+
|
|
73
|
+
// Safely close the database
|
|
74
|
+
await db.close();
|
|
75
|
+
})();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## How it works?
|
|
79
|
+
|
|
80
|
+
The `@jnode/db` package provides three core components to handle different data persistence needs seamlessly:
|
|
81
|
+
|
|
82
|
+
1. **JDB** (`.jdb`): The base binary file format manager. It handles basic file magic headers, versions, and provides raw file access wrappers.
|
|
83
|
+
2. **DBLE** (`.dble`): Stands for **JDB Lite Extended**. It is a simple, fast, schema-defined binary database built on top of JDB. It supports fixed-length fields, typed records, automatic indexing, deleted space reuse, and dynamic extended fields.
|
|
84
|
+
3. **LJSON** (`.ljson`): Stands for **LineJSON**. It follows the `JLD` (JustLineData) text-based format. It stores an array of JSON objects line-by-line and processes append-only mutations like updates, deletions, and overwrites, making it extremely durable for logging or small configs.
|
|
85
|
+
|
|
86
|
+
**Advanced Usage (DBLE):** Because DBLE queues its async file operations internally to ensure data integrity, executing multiple interdependent queries could sometimes face race conditions if another script block appends data concurrently. If you want to run complex logic that involves multiple operations, you can use the internal task queue method `._doTask(async () => { ... })` and pass `true` to the `skipQueue` argument of any method called inside. This bypasses the queue check internally and ensures your complex block runs uninterruptedly and safely!
|
|
87
|
+
|
|
88
|
+
------
|
|
89
|
+
|
|
90
|
+
# Reference
|
|
91
|
+
|
|
92
|
+
## Class: `jdb.JDBFile`
|
|
93
|
+
|
|
94
|
+
The base JDB file manager for handling binary file headers.
|
|
95
|
+
|
|
96
|
+
### Static method: `JDBFile.load(path)`
|
|
97
|
+
|
|
98
|
+
- `path` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Path to the file.
|
|
99
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Fulfills with a[\<jdb.JDBFile\>](#class-jdbjdbfile).
|
|
100
|
+
|
|
101
|
+
Opens an existing JDB file and verifies its format and magic string.
|
|
102
|
+
|
|
103
|
+
### Static method: `JDBFile.create(path[, options])`
|
|
104
|
+
|
|
105
|
+
- `path` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Path to the file.
|
|
106
|
+
- `options`[\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
107
|
+
- `dbType` [\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) A 4-byte identifier for the database type (E.g., `0x44424C45` for DBLE). **Default:** `0`.
|
|
108
|
+
- `dbVersion` [\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) The version of the specific database format. **Default:** `0`.
|
|
109
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Fulfills with a [\<jdb.JDBFile\>](#class-jdbjdbfile).
|
|
110
|
+
|
|
111
|
+
Creates a new JDB file (fails if the file already exists).
|
|
112
|
+
|
|
113
|
+
### Static method: `JDBFile.forceCreate(path[, options])`
|
|
114
|
+
|
|
115
|
+
- `path` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Path to the file.
|
|
116
|
+
- `options` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) Same as[`JDBFile.create()`](#staticmethod-jdbfilecreatepath-options).
|
|
117
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Fulfills with a [\<jdb.JDBFile\>](#class-jdbjdbfile).
|
|
118
|
+
|
|
119
|
+
Forces creation of a new JDB file, overwriting if it already exists.
|
|
120
|
+
|
|
121
|
+
### `jdb.close()`
|
|
122
|
+
|
|
123
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
|
124
|
+
|
|
125
|
+
Closes the internal file handle safely.
|
|
126
|
+
|
|
127
|
+
### `jdb.sync()`
|
|
128
|
+
|
|
129
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
|
130
|
+
|
|
131
|
+
Synchronizes the file's data and metadata to the storage device.
|
|
132
|
+
|
|
133
|
+
### `jdb.datasync()`
|
|
134
|
+
|
|
135
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
|
136
|
+
|
|
137
|
+
Synchronizes only the file's data to the storage device.
|
|
138
|
+
|
|
139
|
+
### Inner method: `jdb._init([options])`
|
|
140
|
+
|
|
141
|
+
### Inner method: `jdb._loadHead()`
|
|
142
|
+
|
|
143
|
+
Internal methods to initialize and load file headers.
|
|
144
|
+
|
|
145
|
+
## Class: `dble.DBLEFile`
|
|
146
|
+
|
|
147
|
+
The main manager for DBLE files, which extends `JDBFile`.
|
|
148
|
+
|
|
149
|
+
### Static method: `DBLEFile.load(path[, options])`
|
|
150
|
+
|
|
151
|
+
- `path`[\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Path to the file.
|
|
152
|
+
- `options` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
153
|
+
- `types` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) Custom field types mappings.
|
|
154
|
+
- `maxLineCache` / `maxLineCacheSize` [\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) Controls the LRU cache limit for read lines.
|
|
155
|
+
- `scanBufferLines` / `scanBufferSize` [\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) Controls buffer block sizes when iterating sequentially.
|
|
156
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Fulfills with a[\<dble.DBLEFile\>](#class-dbledblefile).
|
|
157
|
+
|
|
158
|
+
### Static method: `DBLEFile.create(path, options)`
|
|
159
|
+
|
|
160
|
+
### Static method: `DBLEFile.forceCreate(path, options)`
|
|
161
|
+
|
|
162
|
+
- `path` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Path to the file.
|
|
163
|
+
- `options`[\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
164
|
+
- `fields` [\<DBLEField[]\>](#class-dbledblefield) **Required.** An array of field instances defining the table structure.
|
|
165
|
+
- `extBaseLength`[\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) Optional override to calculate extension field base length manually.
|
|
166
|
+
- Plus `DBLEFile.load` options.
|
|
167
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Fulfills with a[\<dble.DBLEFile\>](#class-dbledblefile).
|
|
168
|
+
|
|
169
|
+
Creates (or forcefully overwrites) a DBLE database file.
|
|
170
|
+
|
|
171
|
+
### `dble.getField(line, field[, skipQueue])`
|
|
172
|
+
|
|
173
|
+
- `line`[\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) The index of the line/row.
|
|
174
|
+
- `field`[\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Field name.
|
|
175
|
+
- `skipQueue` [\<boolean\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type) Skip the internal task queue execution lock.
|
|
176
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Resolves with the parsed field value.
|
|
177
|
+
|
|
178
|
+
Retrieves a specific field from a specific line.
|
|
179
|
+
|
|
180
|
+
### `dble.getExt(line[, skipQueue])`
|
|
181
|
+
|
|
182
|
+
Retrieves the concatenated binary extended data for a line. Returns a [\<Buffer\>](https://nodejs.org/docs/latest/api/buffer.html#class-buffer).
|
|
183
|
+
|
|
184
|
+
### `dble.getLineByField(field, query[, skipQueue])`
|
|
185
|
+
|
|
186
|
+
Finds the numeric line index where the specified `field` matches the `query`. If the field was declared with `isKey: true`, this operation is `O(1)` via index map. Otherwise, it executes a sequential scan.
|
|
187
|
+
|
|
188
|
+
### `dble.readLine(line[, skipQueue])`
|
|
189
|
+
|
|
190
|
+
Reads a full line structure at the target index. The resolved object looks like: `{ type, nextLine, fields: { ... } }`.
|
|
191
|
+
|
|
192
|
+
### `dble.readLineByField(field, query[, skipQueue])`
|
|
193
|
+
|
|
194
|
+
Combination of `getLineByField` and `readLine`. Returns the parsed line object.
|
|
195
|
+
|
|
196
|
+
### `dble.findLine(fn[, skipQueue])`
|
|
197
|
+
|
|
198
|
+
Iterates over all lines and returns the line index when the async callback `fn(parsedLine)` returns `true`.
|
|
199
|
+
|
|
200
|
+
### `dble.forEachLine(fn, start, to[, skipQueue])`
|
|
201
|
+
|
|
202
|
+
Executes the callback `fn(parsedLine, lineIndex)` on every valid database line starting from `start` up to `to`.
|
|
203
|
+
|
|
204
|
+
### `dble.setLine(line, fields[, skipQueue])`
|
|
205
|
+
|
|
206
|
+
Updates specific fields on an existing line. Pass `{}` to fields to just touch it, or pass an object with partial properties. Also updates indexes seamlessly. If `line` is not a number, it behaves like `appendLine`.
|
|
207
|
+
|
|
208
|
+
### `dble.setLineByField(field, query, fields[, skipQueue])`
|
|
209
|
+
|
|
210
|
+
Finds the line via field/query matching, and updates it.
|
|
211
|
+
|
|
212
|
+
### `dble.appendLine(fields[, skipQueue])`
|
|
213
|
+
|
|
214
|
+
Appends a new record to the database, or overwrites an empty (deleted) line dynamically for disk space reuse. Returns the new line index.
|
|
215
|
+
|
|
216
|
+
### `dble.deleteLine(line, releaseSpace[, skipQueue])`
|
|
217
|
+
|
|
218
|
+
Flags a line as deleted. If `releaseSpace` is `true`, it dynamically links the block to the empty line manager to allow future `appendLine` calls to overwrite this spot.
|
|
219
|
+
|
|
220
|
+
### `dble.deleteLineByField(field, query, releaseSpace[, skipQueue])`
|
|
221
|
+
|
|
222
|
+
Finds a line by a query and deletes it.
|
|
223
|
+
|
|
224
|
+
### `dble.cleanUp([skipQueue])`
|
|
225
|
+
|
|
226
|
+
Loops over the DB sequentially, completely clearing up lines marked as deleted and moving them to the empty reusable line linked list.
|
|
227
|
+
|
|
228
|
+
### `dble.setExt(line, ext[, skipQueue])`
|
|
229
|
+
|
|
230
|
+
Saves an arbitrary binary[\<Buffer\>](https://nodejs.org/docs/latest/api/buffer.html#class-buffer) to a line's extension block. If the buffer is large, it automatically chunks it across multiple linked empty lines.
|
|
231
|
+
|
|
232
|
+
### `dble.setExtByField(field, query, ext[, skipQueue])`
|
|
233
|
+
|
|
234
|
+
Finds a line by field query and sets its extended buffer data.
|
|
235
|
+
|
|
236
|
+
### `dble.close()`
|
|
237
|
+
|
|
238
|
+
### `dble.sync([skipQueue])`
|
|
239
|
+
|
|
240
|
+
### `dble.datasync([skipQueue])`
|
|
241
|
+
|
|
242
|
+
File handle operations natively routed to the base JDB class.
|
|
243
|
+
|
|
244
|
+
### Inner method: `dble._doTask(func, skipQueue)`
|
|
245
|
+
|
|
246
|
+
Advanced tool. Runs your `func` exclusively inside the DB task queue, locking other async modifications from messing with your state.
|
|
247
|
+
|
|
248
|
+
### Inner methods: `_init`, `_loadHead`, `_loadIndices`, `_getLineBuffer`, `_writeLineBuffer`, `_appendLineBuffer`, `_getLastEmptyLine`, `_clearLinesFrom`, `_readlines`, `_parseLine`, etc
|
|
249
|
+
|
|
250
|
+
Internal lifecycle and binary chunking methods.
|
|
251
|
+
|
|
252
|
+
## Class: `dble.DBLEField`
|
|
253
|
+
|
|
254
|
+
The base interface for field typings in DBLE. You instantiate these classes in the `fields` array when creating a new database.
|
|
255
|
+
|
|
256
|
+
### Built-in Field Types
|
|
257
|
+
|
|
258
|
+
Access these via `require('@jnode/db/dble')` or `dble.defaultDBLETypes`:
|
|
259
|
+
|
|
260
|
+
- `DBLEInt8Field(name, isKey, isRelative)` / `DBLEUInt8Field`
|
|
261
|
+
- `DBLEInt16Field(name, isKey, isRelative)` / `DBLEUInt16Field`
|
|
262
|
+
- `DBLEInt32Field(name, isKey, isRelative)` / `DBLEUInt32Field`
|
|
263
|
+
- `DBLEBigInt64Field(name, isKey, isRelative)` / `DBLEBigUInt64Field`
|
|
264
|
+
- `DBLEFloatField(name, isKey, isRelative)` / `DBLEDoubleField`
|
|
265
|
+
- `DBLEStringField(maxLength, name, isKey, isRelative)`
|
|
266
|
+
- `DBLEBufferField(maxLength, name, isKey, isRelative)`
|
|
267
|
+
- `DBLEAnyField(length, name, isKey, isRelative)`
|
|
268
|
+
|
|
269
|
+
*Note: The `maxLength` argument is required for String and Buffer types to enforce DBLE's fixed-length binary schema standard.*
|
|
270
|
+
|
|
271
|
+
## Class: `ljson.LJSONFile`
|
|
272
|
+
|
|
273
|
+
Manages text-based LJSON files. This works by keeping a stateful memory representation of the JSON array, and appending modification operations (like `+`, `=`, `-`) to the file stream iteratively.
|
|
274
|
+
|
|
275
|
+
### Static method: `LJSONFile.load(file[, options])`
|
|
276
|
+
|
|
277
|
+
- `file` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Target LJSON file path.
|
|
278
|
+
- `options` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
279
|
+
- `disableCleanupOnStart` [\<boolean\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type) Stop LJSON from compacting its mutation logs on load.
|
|
280
|
+
- Returns: [\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Fulfills with a [\<ljson.LJSONFile\>](#class-ljsonljsonfile).
|
|
281
|
+
|
|
282
|
+
Loads the JLD formatted LJSON file. If it doesn't exist, an empty file will be created.
|
|
283
|
+
|
|
284
|
+
### Static method: `LJSONFile.forceCreate(file[, options])`
|
|
285
|
+
|
|
286
|
+
Overwrites a file with a fresh, empty LJSON database.
|
|
287
|
+
|
|
288
|
+
### `ljson.cleanUp(force)`
|
|
289
|
+
|
|
290
|
+
Rewrites the entire file on disk, collapsing all mutation lines (updates, overwrites, deletions) into a clean, flat list of final JSON representations.
|
|
291
|
+
|
|
292
|
+
### `ljson.push(json)`
|
|
293
|
+
|
|
294
|
+
- `json` [\<any\>] Valid JSON.
|
|
295
|
+
- Returns:[\<Promise\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
|
296
|
+
|
|
297
|
+
Appends a new line with the object directly to the database.
|
|
298
|
+
|
|
299
|
+
### `ljson.update(index, json)`
|
|
300
|
+
|
|
301
|
+
- `index` [\<number\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type) The array index to modify.
|
|
302
|
+
- `json` [\<any\>] The mutation payload.
|
|
303
|
+
|
|
304
|
+
Applies a mutation diff based on the original data type:
|
|
305
|
+
|
|
306
|
+
- **String / Number**: `original += update`
|
|
307
|
+
- **Boolean**: `original = update !== original`
|
|
308
|
+
- **Object**: `Object.assign(original, update)`
|
|
309
|
+
- **Array**: `original.push(...update)`
|
|
310
|
+
|
|
311
|
+
### `ljson.overwrite(index, json)`
|
|
312
|
+
|
|
313
|
+
Overwrites the target index entirely with the new `json` payload.
|
|
314
|
+
|
|
315
|
+
### `ljson.delete(index)`
|
|
316
|
+
|
|
317
|
+
Removes the index element from the database. Note that this shifts subsequent indexes.
|
|
318
|
+
|
|
319
|
+
### Inner method: `ljson._loadData(data)`
|
|
320
|
+
|
|
321
|
+
Parses textual JLD lines and applies all operators into the memory array.
|
|
322
|
+
|
|
323
|
+
------
|
|
324
|
+
|
|
325
|
+
# Format Specifications
|
|
326
|
+
|
|
327
|
+
## DBLE Format Specification
|
|
328
|
+
|
|
329
|
+
```
|
|
330
|
+
---- File Head
|
|
331
|
+
[4 Byte] "JDBD"
|
|
332
|
+
[UInt8 ] Version, 1
|
|
333
|
+
[3 Byte] RSV
|
|
334
|
+
[4 Byte] DB Type, "DBLE"
|
|
335
|
+
[UInt8 ] DB Version, 1
|
|
336
|
+
[3 Byte] RSV
|
|
337
|
+
---- DB Head
|
|
338
|
+
[Array ] Definition[16 Bit] Flag (isLast, isKey, isRelative RSV...)
|
|
339
|
+
[UInt16] Field Length
|
|
340
|
+
[UInt8 ] Type Name Length
|
|
341
|
+
[String] Type Name
|
|
342
|
+
[UInt8 ] Field Name Length
|
|
343
|
+
[String] Field Name
|
|
344
|
+
[n Byte] (OPTIONAL) Field Relative Data[UInt16] Extend Field Base Length (making each line 2 ** N bytes)
|
|
345
|
+
[UInt32] Last Empty Line (0xFFFFFFFF for null)
|
|
346
|
+
[n Byte] Padding (making 'File Head' + 'DB Head' N * Line Length)
|
|
347
|
+
---- DB Body
|
|
348
|
+
[Array ] Lines
|
|
349
|
+
[2 Bit ] Type (Normal Start, Extended, Empty, Deleted Start)
|
|
350
|
+
[14 Bit] Flag[UInt32] Extended Line At/Last Empty Line (if type is 'Empty', 0xFFFFFFFF for null)
|
|
351
|
+
[Array ] Line Data
|
|
352
|
+
[n Byte] Field Data
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## LJSON Format Specification
|
|
356
|
+
|
|
357
|
+
This is a simple text-based, line-based data format.
|
|
358
|
+
|
|
359
|
+
### JLD v1 File head line
|
|
360
|
+
|
|
361
|
+
> `JLD` stands for **JustLineData**, a subtype of JDB but is text-and-line-based while a `.jdb` file is binary based.
|
|
362
|
+
|
|
363
|
+
```
|
|
364
|
+
@ JLD 01 <DATA-TYPE (4 ASCII Character)> <DATA-VERSION (2 HEX)>
|
|
365
|
+
[@ <OTHER-HEADERS...>]...
|
|
366
|
+
<ACTUAL-DATA>...
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
For LJSON, we use:
|
|
370
|
+
|
|
371
|
+
```
|
|
372
|
+
@ JLD 01 LJSO 01
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### Line types
|
|
376
|
+
|
|
377
|
+
- `<JSON>` JSON data line. E.G. `{"name":"nick"}`.
|
|
378
|
+
- `/ <CONTENT>` Comment. Only for temporary use, we won't keep the comment lines.
|
|
379
|
+
- `+ <INDEX> <JSON>` Update line. Update JSON's data type must be same as original JSON's data type. Works in different ways according to the data type:
|
|
380
|
+
- **String**: `original += update`.
|
|
381
|
+
- **Number**: `original += update`.
|
|
382
|
+
- **Boolean**: `original = update !== original`.
|
|
383
|
+
- **Object**: `Object.assign(original, update)`
|
|
384
|
+
- **Array**: `original.push(...update)`
|
|
385
|
+
- `= <INDEX> <JSON>` Overwrite line.
|
|
386
|
+
- `- <INDEX>` Delete line.
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jnode/db",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple database package for Node.js.",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.js",
|
|
8
|
+
"./jdb": "./src/jdb.js",
|
|
9
|
+
"./dble": "./src/dble.js",
|
|
10
|
+
"./ljson": "./src/ljson.js"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "echo \"Hello, database.\""
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/japple-jnode/db.git"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"JustNode",
|
|
21
|
+
"JustApple",
|
|
22
|
+
"Database",
|
|
23
|
+
"JSON",
|
|
24
|
+
"DB",
|
|
25
|
+
"JDB",
|
|
26
|
+
"DBLE",
|
|
27
|
+
"LJSON"
|
|
28
|
+
],
|
|
29
|
+
"author": "JustApple <apple@justapple.tw>",
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/japple-jnode/db/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/japple-jnode/db#readme"
|
|
35
|
+
}
|