defuss-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 +21 -0
- package/README.md +152 -0
- package/dist/client.cjs +281 -0
- package/dist/client.d.cts +87 -0
- package/dist/client.d.mts +87 -0
- package/dist/client.d.ts +87 -0
- package/dist/client.mjs +278 -0
- package/dist/index.cjs +72 -0
- package/dist/index.d.cts +122 -0
- package/dist/index.d.mts +122 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.mjs +70 -0
- package/dist/server.cjs +777 -0
- package/dist/server.d.cts +243 -0
- package/dist/server.d.mts +243 -0
- package/dist/server.d.ts +243 -0
- package/dist/server.mjs +764 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 - 2025 Aron Homberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
|
|
3
|
+
<img src="assets/defuss_mascott.png" width="100px" />
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<code>defuss</code>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<sup align="center">
|
|
10
|
+
|
|
11
|
+
Database Abstraction
|
|
12
|
+
|
|
13
|
+
</sup>
|
|
14
|
+
|
|
15
|
+
</h1>
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
> `defuss-db` abstracts interactions with databases, allowing you to focus on your application logic without worrying about the underlying database implementation, schema, or queries. It provides a consistent API for various databases, making it easier to switch between them or use multiple databases in a single application.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
<h3 align="center">
|
|
22
|
+
|
|
23
|
+
Supported Databases
|
|
24
|
+
|
|
25
|
+
</h3>
|
|
26
|
+
|
|
27
|
+
- *Dexie*: A wrapper for IndexedDB that provides a simple and powerful API for working with client-side databases in web applications. It is designed to be easy to use and provides a consistent API across different browsers.
|
|
28
|
+
|
|
29
|
+
- *MongoDB*: A NoSQL database that uses a flexible, JSON-like format for data storage. It is designed for scalability and performance, making it a popular choice for modern web applications.
|
|
30
|
+
|
|
31
|
+
- *LibSQL*: A SQL database that is designed to be lightweight and easy to use. It is built on top of SQLite and provides a simple API for interacting with SQL databases.
|
|
32
|
+
|
|
33
|
+
Providing an isomorphic API for all three databases, `defuss-db` allows you to use the same codebase for both client-side and server-side database interactions. This means you can write your database queries once and use them in both environments without worrying about compatibility issues.
|
|
34
|
+
|
|
35
|
+
<h3 align="center">
|
|
36
|
+
|
|
37
|
+
Integrating `defuss-db` in an existing defuss project
|
|
38
|
+
|
|
39
|
+
</h3>
|
|
40
|
+
|
|
41
|
+
**🚀 Looking for a template to start from?** `examples/notebooks` is an Astro project pre-configured to work with `defuss-db` out-of-the-box.
|
|
42
|
+
|
|
43
|
+
#### 2. Integrate `defuss-db`:
|
|
44
|
+
|
|
45
|
+
Just import `Table` from `defuss-db` and use it in your project:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { Table } from 'defuss-db';
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
A table needs a database storage provider:
|
|
52
|
+
|
|
53
|
+
In-browser (client-side/frontend) use-case:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { DexieProvider } from "defuss-db";
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Using the database is as easy as wiring them up:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// define a model
|
|
63
|
+
interface User {
|
|
64
|
+
name: string;
|
|
65
|
+
age: number;
|
|
66
|
+
email: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const dexieProvider = new DexieProvider(TEST_DB_NAME);
|
|
70
|
+
await dexieProvider.connect(); // connects to the database
|
|
71
|
+
|
|
72
|
+
const myTable = new DefussTable<User>(provider, "my-table");
|
|
73
|
+
await myTable.init(); // runs a schema update
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
#### 3. Use the API of `defuss-db`:
|
|
78
|
+
|
|
79
|
+
To interact with the table, create, read, update, and delete records:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const user: User = {
|
|
83
|
+
name: "Alice",
|
|
84
|
+
age: 30,
|
|
85
|
+
email: "alice@example.com",
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// in defuss-db, indices are added explicitly, this allows for indices to diverge from the underlying from the data stored. This separation solves a common issue with performance in speed and size dimensions.
|
|
89
|
+
const indexData = { email: user.email }; // data to find the user by later-on
|
|
90
|
+
|
|
91
|
+
// returns the primary key of the inserted user
|
|
92
|
+
const pk = await table.insert(/* data */ user, /* indices */ indexData);
|
|
93
|
+
|
|
94
|
+
// find a specific user
|
|
95
|
+
const user = await table.findOne(/* indices */ { email: "alice@example.com" });
|
|
96
|
+
|
|
97
|
+
// find many users of a specific TLD
|
|
98
|
+
const users = await table.find(/* indices */ { email: "*@example.com" });
|
|
99
|
+
|
|
100
|
+
// updates the user
|
|
101
|
+
await table.update(/* update indices */ { email: user.email }, /* paertial data */ { age: 29 });
|
|
102
|
+
|
|
103
|
+
// deletes the user
|
|
104
|
+
await table.delete(/* indices */ { email: user.email });
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Due to the nature of `defuss-db`, bridging the gap beween different database architectures and even between frontend and backend, the API cannot feature all the specific functionality of each underlaying provider. It is designed to simply provide the intersection feature-set. There are no foreign keys, no transactions, no joins, and no complex queries. You are supposed to design your data model in a semi-normalized way and use smart indices and data access patterns to get the most out of this.
|
|
108
|
+
|
|
109
|
+
<h3 align="center">
|
|
110
|
+
|
|
111
|
+
🚀 How does `defuss-db` work?
|
|
112
|
+
|
|
113
|
+
</h3>
|
|
114
|
+
|
|
115
|
+
Inside this package, you'll find the following relevant folders and files:
|
|
116
|
+
|
|
117
|
+
```text
|
|
118
|
+
/
|
|
119
|
+
├── provider/dexie.ts
|
|
120
|
+
├── provider/libsql.ts
|
|
121
|
+
├── provider/mongodb.ts
|
|
122
|
+
├── src/index.ts
|
|
123
|
+
├── src/types.ts
|
|
124
|
+
├── src/env.ts
|
|
125
|
+
├── src/table.ts
|
|
126
|
+
├── src/client.ts
|
|
127
|
+
├── src/server.ts
|
|
128
|
+
├── tsconfig.json
|
|
129
|
+
├── LICENSE
|
|
130
|
+
├── package.json
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The `src/index.ts` file is the "entry point" for general and type imports, while `server.ts` and `client.ts` are the respective imports to use whenever a specific runtime environment implement is desired to be used. The `defuss-db/client` and `defuss-db/server` imports will resolve to these files automatically.
|
|
134
|
+
|
|
135
|
+
You'll find each specific provider implementation in the `provider` folder.
|
|
136
|
+
|
|
137
|
+
## 🧞 Commands
|
|
138
|
+
|
|
139
|
+
All commands are run from the root of the project, from a terminal:
|
|
140
|
+
|
|
141
|
+
| Command | Action |
|
|
142
|
+
| :------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
143
|
+
| `npm build` | Build a new version of the integration. |
|
|
144
|
+
| `npm mongodb:start` | Start the MongoDB server (requires Docker). |
|
|
145
|
+
| `npm mongodb:stop` | Stop the MongoDB server (requires Docker). |
|
|
146
|
+
| `npm test:integration` | Run the integration tests for the `defuss-db` package. |
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
<img src="assets/defuss_comic.png" />
|
|
151
|
+
|
|
152
|
+
<caption><i><b>Come visit us on defuss island!</b></i></caption>
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./index.cjs');
|
|
4
|
+
|
|
5
|
+
class DexieProvider {
|
|
6
|
+
db;
|
|
7
|
+
tables = /* @__PURE__ */ new Map();
|
|
8
|
+
databaseName;
|
|
9
|
+
schema = /* @__PURE__ */ new Map();
|
|
10
|
+
schemaVersion = 1;
|
|
11
|
+
isOpen = false;
|
|
12
|
+
constructor(databaseName) {
|
|
13
|
+
if (!databaseName) {
|
|
14
|
+
databaseName = "DefussDB";
|
|
15
|
+
}
|
|
16
|
+
this.databaseName = databaseName;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Connects to IndexedDB. Dexie.js manages connections internally.
|
|
20
|
+
* @param options - Dexie-specific configuration options.
|
|
21
|
+
*/
|
|
22
|
+
async connect(options = {}) {
|
|
23
|
+
const isNode = typeof process !== "undefined" && typeof process.versions === "object" && typeof process.versions.node !== "undefined";
|
|
24
|
+
if (isNode) {
|
|
25
|
+
await import('fake-indexeddb/auto');
|
|
26
|
+
}
|
|
27
|
+
const DexieImpl = (await import('dexie')).default;
|
|
28
|
+
this.db = new DexieImpl(this.databaseName, options);
|
|
29
|
+
this.isOpen = true;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Disconnects from IndexedDB.
|
|
33
|
+
*/
|
|
34
|
+
async disconnect() {
|
|
35
|
+
if (this.db) {
|
|
36
|
+
this.db.close();
|
|
37
|
+
this.isOpen = false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a table if it doesn't exist with dynamic schema support.
|
|
42
|
+
* @param table - The name of the table.
|
|
43
|
+
*/
|
|
44
|
+
async createTable(table) {
|
|
45
|
+
if (this.tables.has(table)) return;
|
|
46
|
+
if (!this.schema.has(table)) {
|
|
47
|
+
this.schema.set(table, { pk: "++" });
|
|
48
|
+
}
|
|
49
|
+
this.schemaVersion++;
|
|
50
|
+
if (this.isOpen) {
|
|
51
|
+
this.db.close();
|
|
52
|
+
}
|
|
53
|
+
const tableSchema = {};
|
|
54
|
+
for (const [tableName, columns] of this.schema.entries()) {
|
|
55
|
+
const schemaString = Object.entries(columns).map(([column, indexType]) => {
|
|
56
|
+
if (column === "pk") return "++pk";
|
|
57
|
+
return indexType ? `${indexType}${column}` : column;
|
|
58
|
+
}).join(", ");
|
|
59
|
+
tableSchema[tableName] = schemaString;
|
|
60
|
+
}
|
|
61
|
+
this.db.version(this.schemaVersion).stores(tableSchema);
|
|
62
|
+
await this.db.open();
|
|
63
|
+
this.isOpen = true;
|
|
64
|
+
const tableDexie = this.db.table(table);
|
|
65
|
+
this.tables.set(table, tableDexie);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Adds columns to the schema if they don't exist, with proper indexing.
|
|
69
|
+
* @param table - The name of the table.
|
|
70
|
+
* @param record - The record with properties to consider adding.
|
|
71
|
+
*/
|
|
72
|
+
async ensureColumnsForRecord(table, record) {
|
|
73
|
+
const tableSchema = this.schema.get(table) || { pk: "++" };
|
|
74
|
+
let schemaUpdated = false;
|
|
75
|
+
for (const [prop, value] of Object.entries(record)) {
|
|
76
|
+
if (prop === "pk") continue;
|
|
77
|
+
if (prop in tableSchema) continue;
|
|
78
|
+
if (prop.endsWith("_index")) {
|
|
79
|
+
const baseField = prop.slice(0, -6);
|
|
80
|
+
if (!(baseField in tableSchema)) {
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
tableSchema[prop] = "*";
|
|
83
|
+
} else {
|
|
84
|
+
tableSchema[prop] = "";
|
|
85
|
+
}
|
|
86
|
+
schemaUpdated = true;
|
|
87
|
+
}
|
|
88
|
+
} else if (!(prop in tableSchema)) {
|
|
89
|
+
tableSchema[prop] = "";
|
|
90
|
+
schemaUpdated = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (schemaUpdated) {
|
|
94
|
+
this.schema.set(table, tableSchema);
|
|
95
|
+
await this.createTable(table);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Inserts a record into a table.
|
|
100
|
+
* @param table - The name of the table.
|
|
101
|
+
* @param record - The record to insert.
|
|
102
|
+
* @returns The primary key of the inserted record.
|
|
103
|
+
*/
|
|
104
|
+
async insert(table, record) {
|
|
105
|
+
await this.createTable(table);
|
|
106
|
+
const insertData = {};
|
|
107
|
+
for (const [key, value] of Object.entries(record)) {
|
|
108
|
+
if (key === "pk") {
|
|
109
|
+
if (value !== void 0) {
|
|
110
|
+
insertData.pk = value;
|
|
111
|
+
}
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
insertData[key] = value;
|
|
115
|
+
}
|
|
116
|
+
await this.ensureColumnsForRecord(table, record);
|
|
117
|
+
const tableDexie = this.tables.get(table);
|
|
118
|
+
const id = await tableDexie.add(insertData);
|
|
119
|
+
if (typeof id === "number") {
|
|
120
|
+
return id;
|
|
121
|
+
} else if (typeof id === "string") {
|
|
122
|
+
return id;
|
|
123
|
+
} else {
|
|
124
|
+
return String(id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Finds records in the table based on query.
|
|
129
|
+
* @param table - The name of the table.
|
|
130
|
+
* @param query - Query criteria.
|
|
131
|
+
* @returns An array of matching records.
|
|
132
|
+
*/
|
|
133
|
+
async find(table, query) {
|
|
134
|
+
await this.createTable(table);
|
|
135
|
+
const tableDexie = this.tables.get(table);
|
|
136
|
+
await this.ensureColumnsForRecord(table, query);
|
|
137
|
+
if (Object.keys(query).length === 0) {
|
|
138
|
+
const results2 = await tableDexie.toArray();
|
|
139
|
+
return results2.map((result) => this.normalizeRecord(result));
|
|
140
|
+
}
|
|
141
|
+
let collection = tableDexie.toCollection();
|
|
142
|
+
if ("pk" in query) {
|
|
143
|
+
try {
|
|
144
|
+
const record = await tableDexie.get(query.pk);
|
|
145
|
+
return record ? [this.normalizeRecord(record)] : [];
|
|
146
|
+
} catch (err) {
|
|
147
|
+
collection = collection.and((item) => item.pk === query.pk);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
for (const [key, value] of Object.entries(query)) {
|
|
151
|
+
if (key.endsWith("_index")) {
|
|
152
|
+
collection = collection.and((item) => item[key] === value);
|
|
153
|
+
} else {
|
|
154
|
+
collection = collection.and((item) => item[key] === value);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const results = await collection.toArray();
|
|
159
|
+
return results.map((result) => this.normalizeRecord(result));
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Finds a single record in the table based on query.
|
|
163
|
+
* @param table - The name of the table.
|
|
164
|
+
* @param query - Query criteria.
|
|
165
|
+
* @returns The first matching record or null.
|
|
166
|
+
*/
|
|
167
|
+
async findOne(table, query) {
|
|
168
|
+
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
169
|
+
await this.createTable(table);
|
|
170
|
+
const tableDexie = this.tables.get(table);
|
|
171
|
+
try {
|
|
172
|
+
const result = await tableDexie.get(query.pk);
|
|
173
|
+
return result ? this.normalizeRecord(result) : null;
|
|
174
|
+
} catch (err) {
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const results = await this.find(table, query);
|
|
178
|
+
return results.length > 0 ? results[0] : null;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Updates records in the table based on query.
|
|
182
|
+
* @param table - The name of the table.
|
|
183
|
+
* @param query - Query criteria.
|
|
184
|
+
* @param update - Fields to update.
|
|
185
|
+
*/
|
|
186
|
+
async update(table, query, update) {
|
|
187
|
+
await this.createTable(table);
|
|
188
|
+
const tableDexie = this.tables.get(table);
|
|
189
|
+
await this.ensureColumnsForRecord(table, query);
|
|
190
|
+
await this.ensureColumnsForRecord(table, update);
|
|
191
|
+
const updateData = {};
|
|
192
|
+
for (const [key, value] of Object.entries(update)) {
|
|
193
|
+
if (key === "pk") continue;
|
|
194
|
+
updateData[key] = value;
|
|
195
|
+
}
|
|
196
|
+
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
197
|
+
await tableDexie.update(query.pk, updateData);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const collection = tableDexie.toCollection().and((record) => {
|
|
201
|
+
for (const [key, value] of Object.entries(query)) {
|
|
202
|
+
if (record[key] !== value) return false;
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
});
|
|
206
|
+
const keys = await collection.primaryKeys();
|
|
207
|
+
for (const key of keys) {
|
|
208
|
+
await tableDexie.update(key, updateData);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Deletes records from the table based on query.
|
|
213
|
+
* @param table - The name of the table.
|
|
214
|
+
* @param query - Query criteria.
|
|
215
|
+
*/
|
|
216
|
+
async delete(table, query) {
|
|
217
|
+
await this.createTable(table);
|
|
218
|
+
const tableDexie = this.tables.get(table);
|
|
219
|
+
await this.ensureColumnsForRecord(table, query);
|
|
220
|
+
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
221
|
+
await tableDexie.delete(query.pk);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const collection = tableDexie.toCollection().and((record) => {
|
|
225
|
+
for (const [key, value] of Object.entries(query)) {
|
|
226
|
+
if (record[key] !== value) return false;
|
|
227
|
+
}
|
|
228
|
+
return true;
|
|
229
|
+
});
|
|
230
|
+
const keys = await collection.primaryKeys();
|
|
231
|
+
await tableDexie.bulkDelete(keys);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Upserts a record into the table based on query.
|
|
235
|
+
* @param table - The name of the table.
|
|
236
|
+
* @param record - The record to upsert.
|
|
237
|
+
* @param query - Query criteria.
|
|
238
|
+
* @returns The primary key of the upserted record.
|
|
239
|
+
*/
|
|
240
|
+
async upsert(table, record, query) {
|
|
241
|
+
await this.createTable(table);
|
|
242
|
+
const existingRecord = await this.findOne(table, query);
|
|
243
|
+
if (existingRecord && existingRecord.pk !== void 0) {
|
|
244
|
+
const mergedRecord = { ...record };
|
|
245
|
+
await this.update(
|
|
246
|
+
table,
|
|
247
|
+
{ pk: existingRecord.pk },
|
|
248
|
+
mergedRecord
|
|
249
|
+
);
|
|
250
|
+
return existingRecord.pk;
|
|
251
|
+
} else {
|
|
252
|
+
const mergedRecord = { ...record };
|
|
253
|
+
for (const [key, value] of Object.entries(query)) {
|
|
254
|
+
if (!(key in mergedRecord)) {
|
|
255
|
+
mergedRecord[key] = value;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return await this.insert(table, mergedRecord);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Normalizes a database record to match DefussRecord structure.
|
|
263
|
+
* @param record - The record from the database.
|
|
264
|
+
* @returns A normalized DefussRecord.
|
|
265
|
+
*/
|
|
266
|
+
normalizeRecord(record) {
|
|
267
|
+
const result = {};
|
|
268
|
+
for (const [key, value] of Object.entries(record)) {
|
|
269
|
+
if (key === "pk") {
|
|
270
|
+
result.pk = value;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (key.startsWith("_")) continue;
|
|
274
|
+
result[key] = value;
|
|
275
|
+
}
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
exports.DefussTable = index.DefussTable;
|
|
281
|
+
exports.DexieProvider = DexieProvider;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { DefussProvider, DefussRecord, PrimaryKeyValue, RecordValue } from './index.cjs';
|
|
2
|
+
export { DefussTable } from './index.cjs';
|
|
3
|
+
import { DexieOptions, Dexie, Table, IndexableType } from 'dexie';
|
|
4
|
+
|
|
5
|
+
type DexieProviderOptions = DexieOptions;
|
|
6
|
+
/**
|
|
7
|
+
* DexieProvider implements DefussProvider for IndexedDB using Dexie.js.
|
|
8
|
+
*/
|
|
9
|
+
declare class DexieProvider implements DefussProvider<DexieOptions> {
|
|
10
|
+
db: Dexie;
|
|
11
|
+
tables: Map<string, Table<any, IndexableType>>;
|
|
12
|
+
databaseName: string;
|
|
13
|
+
schema: Map<string, Record<string, string>>;
|
|
14
|
+
schemaVersion: number;
|
|
15
|
+
isOpen: boolean;
|
|
16
|
+
constructor(databaseName: string);
|
|
17
|
+
/**
|
|
18
|
+
* Connects to IndexedDB. Dexie.js manages connections internally.
|
|
19
|
+
* @param options - Dexie-specific configuration options.
|
|
20
|
+
*/
|
|
21
|
+
connect(options?: DexieOptions): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Disconnects from IndexedDB.
|
|
24
|
+
*/
|
|
25
|
+
disconnect(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a table if it doesn't exist with dynamic schema support.
|
|
28
|
+
* @param table - The name of the table.
|
|
29
|
+
*/
|
|
30
|
+
createTable(table: string): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Adds columns to the schema if they don't exist, with proper indexing.
|
|
33
|
+
* @param table - The name of the table.
|
|
34
|
+
* @param record - The record with properties to consider adding.
|
|
35
|
+
*/
|
|
36
|
+
private ensureColumnsForRecord;
|
|
37
|
+
/**
|
|
38
|
+
* Inserts a record into a table.
|
|
39
|
+
* @param table - The name of the table.
|
|
40
|
+
* @param record - The record to insert.
|
|
41
|
+
* @returns The primary key of the inserted record.
|
|
42
|
+
*/
|
|
43
|
+
insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
|
|
44
|
+
/**
|
|
45
|
+
* Finds records in the table based on query.
|
|
46
|
+
* @param table - The name of the table.
|
|
47
|
+
* @param query - Query criteria.
|
|
48
|
+
* @returns An array of matching records.
|
|
49
|
+
*/
|
|
50
|
+
find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Finds a single record in the table based on query.
|
|
53
|
+
* @param table - The name of the table.
|
|
54
|
+
* @param query - Query criteria.
|
|
55
|
+
* @returns The first matching record or null.
|
|
56
|
+
*/
|
|
57
|
+
findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Updates records in the table based on query.
|
|
60
|
+
* @param table - The name of the table.
|
|
61
|
+
* @param query - Query criteria.
|
|
62
|
+
* @param update - Fields to update.
|
|
63
|
+
*/
|
|
64
|
+
update<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>, update: Partial<T>): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Deletes records from the table based on query.
|
|
67
|
+
* @param table - The name of the table.
|
|
68
|
+
* @param query - Query criteria.
|
|
69
|
+
*/
|
|
70
|
+
delete(table: string, query: Partial<DefussRecord>): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Upserts a record into the table based on query.
|
|
73
|
+
* @param table - The name of the table.
|
|
74
|
+
* @param record - The record to upsert.
|
|
75
|
+
* @param query - Query criteria.
|
|
76
|
+
* @returns The primary key of the upserted record.
|
|
77
|
+
*/
|
|
78
|
+
upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
|
|
79
|
+
/**
|
|
80
|
+
* Normalizes a database record to match DefussRecord structure.
|
|
81
|
+
* @param record - The record from the database.
|
|
82
|
+
* @returns A normalized DefussRecord.
|
|
83
|
+
*/
|
|
84
|
+
private normalizeRecord;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { DefussProvider, DefussRecord, DexieProvider, type DexieProviderOptions, PrimaryKeyValue, RecordValue };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { DefussProvider, DefussRecord, PrimaryKeyValue, RecordValue } from './index.mjs';
|
|
2
|
+
export { DefussTable } from './index.mjs';
|
|
3
|
+
import { DexieOptions, Dexie, Table, IndexableType } from 'dexie';
|
|
4
|
+
|
|
5
|
+
type DexieProviderOptions = DexieOptions;
|
|
6
|
+
/**
|
|
7
|
+
* DexieProvider implements DefussProvider for IndexedDB using Dexie.js.
|
|
8
|
+
*/
|
|
9
|
+
declare class DexieProvider implements DefussProvider<DexieOptions> {
|
|
10
|
+
db: Dexie;
|
|
11
|
+
tables: Map<string, Table<any, IndexableType>>;
|
|
12
|
+
databaseName: string;
|
|
13
|
+
schema: Map<string, Record<string, string>>;
|
|
14
|
+
schemaVersion: number;
|
|
15
|
+
isOpen: boolean;
|
|
16
|
+
constructor(databaseName: string);
|
|
17
|
+
/**
|
|
18
|
+
* Connects to IndexedDB. Dexie.js manages connections internally.
|
|
19
|
+
* @param options - Dexie-specific configuration options.
|
|
20
|
+
*/
|
|
21
|
+
connect(options?: DexieOptions): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Disconnects from IndexedDB.
|
|
24
|
+
*/
|
|
25
|
+
disconnect(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a table if it doesn't exist with dynamic schema support.
|
|
28
|
+
* @param table - The name of the table.
|
|
29
|
+
*/
|
|
30
|
+
createTable(table: string): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Adds columns to the schema if they don't exist, with proper indexing.
|
|
33
|
+
* @param table - The name of the table.
|
|
34
|
+
* @param record - The record with properties to consider adding.
|
|
35
|
+
*/
|
|
36
|
+
private ensureColumnsForRecord;
|
|
37
|
+
/**
|
|
38
|
+
* Inserts a record into a table.
|
|
39
|
+
* @param table - The name of the table.
|
|
40
|
+
* @param record - The record to insert.
|
|
41
|
+
* @returns The primary key of the inserted record.
|
|
42
|
+
*/
|
|
43
|
+
insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
|
|
44
|
+
/**
|
|
45
|
+
* Finds records in the table based on query.
|
|
46
|
+
* @param table - The name of the table.
|
|
47
|
+
* @param query - Query criteria.
|
|
48
|
+
* @returns An array of matching records.
|
|
49
|
+
*/
|
|
50
|
+
find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Finds a single record in the table based on query.
|
|
53
|
+
* @param table - The name of the table.
|
|
54
|
+
* @param query - Query criteria.
|
|
55
|
+
* @returns The first matching record or null.
|
|
56
|
+
*/
|
|
57
|
+
findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Updates records in the table based on query.
|
|
60
|
+
* @param table - The name of the table.
|
|
61
|
+
* @param query - Query criteria.
|
|
62
|
+
* @param update - Fields to update.
|
|
63
|
+
*/
|
|
64
|
+
update<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>, update: Partial<T>): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Deletes records from the table based on query.
|
|
67
|
+
* @param table - The name of the table.
|
|
68
|
+
* @param query - Query criteria.
|
|
69
|
+
*/
|
|
70
|
+
delete(table: string, query: Partial<DefussRecord>): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Upserts a record into the table based on query.
|
|
73
|
+
* @param table - The name of the table.
|
|
74
|
+
* @param record - The record to upsert.
|
|
75
|
+
* @param query - Query criteria.
|
|
76
|
+
* @returns The primary key of the upserted record.
|
|
77
|
+
*/
|
|
78
|
+
upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
|
|
79
|
+
/**
|
|
80
|
+
* Normalizes a database record to match DefussRecord structure.
|
|
81
|
+
* @param record - The record from the database.
|
|
82
|
+
* @returns A normalized DefussRecord.
|
|
83
|
+
*/
|
|
84
|
+
private normalizeRecord;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { DefussProvider, DefussRecord, DexieProvider, type DexieProviderOptions, PrimaryKeyValue, RecordValue };
|