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/dist/client.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { DefussProvider, DefussRecord, PrimaryKeyValue, RecordValue } from './index.js';
|
|
2
|
+
export { DefussTable } from './index.js';
|
|
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 };
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
export { DefussTable } from './index.mjs';
|
|
2
|
+
|
|
3
|
+
class DexieProvider {
|
|
4
|
+
db;
|
|
5
|
+
tables = /* @__PURE__ */ new Map();
|
|
6
|
+
databaseName;
|
|
7
|
+
schema = /* @__PURE__ */ new Map();
|
|
8
|
+
schemaVersion = 1;
|
|
9
|
+
isOpen = false;
|
|
10
|
+
constructor(databaseName) {
|
|
11
|
+
if (!databaseName) {
|
|
12
|
+
databaseName = "DefussDB";
|
|
13
|
+
}
|
|
14
|
+
this.databaseName = databaseName;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Connects to IndexedDB. Dexie.js manages connections internally.
|
|
18
|
+
* @param options - Dexie-specific configuration options.
|
|
19
|
+
*/
|
|
20
|
+
async connect(options = {}) {
|
|
21
|
+
const isNode = typeof process !== "undefined" && typeof process.versions === "object" && typeof process.versions.node !== "undefined";
|
|
22
|
+
if (isNode) {
|
|
23
|
+
await import('fake-indexeddb/auto');
|
|
24
|
+
}
|
|
25
|
+
const DexieImpl = (await import('dexie')).default;
|
|
26
|
+
this.db = new DexieImpl(this.databaseName, options);
|
|
27
|
+
this.isOpen = true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Disconnects from IndexedDB.
|
|
31
|
+
*/
|
|
32
|
+
async disconnect() {
|
|
33
|
+
if (this.db) {
|
|
34
|
+
this.db.close();
|
|
35
|
+
this.isOpen = false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a table if it doesn't exist with dynamic schema support.
|
|
40
|
+
* @param table - The name of the table.
|
|
41
|
+
*/
|
|
42
|
+
async createTable(table) {
|
|
43
|
+
if (this.tables.has(table)) return;
|
|
44
|
+
if (!this.schema.has(table)) {
|
|
45
|
+
this.schema.set(table, { pk: "++" });
|
|
46
|
+
}
|
|
47
|
+
this.schemaVersion++;
|
|
48
|
+
if (this.isOpen) {
|
|
49
|
+
this.db.close();
|
|
50
|
+
}
|
|
51
|
+
const tableSchema = {};
|
|
52
|
+
for (const [tableName, columns] of this.schema.entries()) {
|
|
53
|
+
const schemaString = Object.entries(columns).map(([column, indexType]) => {
|
|
54
|
+
if (column === "pk") return "++pk";
|
|
55
|
+
return indexType ? `${indexType}${column}` : column;
|
|
56
|
+
}).join(", ");
|
|
57
|
+
tableSchema[tableName] = schemaString;
|
|
58
|
+
}
|
|
59
|
+
this.db.version(this.schemaVersion).stores(tableSchema);
|
|
60
|
+
await this.db.open();
|
|
61
|
+
this.isOpen = true;
|
|
62
|
+
const tableDexie = this.db.table(table);
|
|
63
|
+
this.tables.set(table, tableDexie);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Adds columns to the schema if they don't exist, with proper indexing.
|
|
67
|
+
* @param table - The name of the table.
|
|
68
|
+
* @param record - The record with properties to consider adding.
|
|
69
|
+
*/
|
|
70
|
+
async ensureColumnsForRecord(table, record) {
|
|
71
|
+
const tableSchema = this.schema.get(table) || { pk: "++" };
|
|
72
|
+
let schemaUpdated = false;
|
|
73
|
+
for (const [prop, value] of Object.entries(record)) {
|
|
74
|
+
if (prop === "pk") continue;
|
|
75
|
+
if (prop in tableSchema) continue;
|
|
76
|
+
if (prop.endsWith("_index")) {
|
|
77
|
+
const baseField = prop.slice(0, -6);
|
|
78
|
+
if (!(baseField in tableSchema)) {
|
|
79
|
+
if (Array.isArray(value)) {
|
|
80
|
+
tableSchema[prop] = "*";
|
|
81
|
+
} else {
|
|
82
|
+
tableSchema[prop] = "";
|
|
83
|
+
}
|
|
84
|
+
schemaUpdated = true;
|
|
85
|
+
}
|
|
86
|
+
} else if (!(prop in tableSchema)) {
|
|
87
|
+
tableSchema[prop] = "";
|
|
88
|
+
schemaUpdated = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (schemaUpdated) {
|
|
92
|
+
this.schema.set(table, tableSchema);
|
|
93
|
+
await this.createTable(table);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Inserts a record into a table.
|
|
98
|
+
* @param table - The name of the table.
|
|
99
|
+
* @param record - The record to insert.
|
|
100
|
+
* @returns The primary key of the inserted record.
|
|
101
|
+
*/
|
|
102
|
+
async insert(table, record) {
|
|
103
|
+
await this.createTable(table);
|
|
104
|
+
const insertData = {};
|
|
105
|
+
for (const [key, value] of Object.entries(record)) {
|
|
106
|
+
if (key === "pk") {
|
|
107
|
+
if (value !== void 0) {
|
|
108
|
+
insertData.pk = value;
|
|
109
|
+
}
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
insertData[key] = value;
|
|
113
|
+
}
|
|
114
|
+
await this.ensureColumnsForRecord(table, record);
|
|
115
|
+
const tableDexie = this.tables.get(table);
|
|
116
|
+
const id = await tableDexie.add(insertData);
|
|
117
|
+
if (typeof id === "number") {
|
|
118
|
+
return id;
|
|
119
|
+
} else if (typeof id === "string") {
|
|
120
|
+
return id;
|
|
121
|
+
} else {
|
|
122
|
+
return String(id);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Finds records in the table based on query.
|
|
127
|
+
* @param table - The name of the table.
|
|
128
|
+
* @param query - Query criteria.
|
|
129
|
+
* @returns An array of matching records.
|
|
130
|
+
*/
|
|
131
|
+
async find(table, query) {
|
|
132
|
+
await this.createTable(table);
|
|
133
|
+
const tableDexie = this.tables.get(table);
|
|
134
|
+
await this.ensureColumnsForRecord(table, query);
|
|
135
|
+
if (Object.keys(query).length === 0) {
|
|
136
|
+
const results2 = await tableDexie.toArray();
|
|
137
|
+
return results2.map((result) => this.normalizeRecord(result));
|
|
138
|
+
}
|
|
139
|
+
let collection = tableDexie.toCollection();
|
|
140
|
+
if ("pk" in query) {
|
|
141
|
+
try {
|
|
142
|
+
const record = await tableDexie.get(query.pk);
|
|
143
|
+
return record ? [this.normalizeRecord(record)] : [];
|
|
144
|
+
} catch (err) {
|
|
145
|
+
collection = collection.and((item) => item.pk === query.pk);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
for (const [key, value] of Object.entries(query)) {
|
|
149
|
+
if (key.endsWith("_index")) {
|
|
150
|
+
collection = collection.and((item) => item[key] === value);
|
|
151
|
+
} else {
|
|
152
|
+
collection = collection.and((item) => item[key] === value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const results = await collection.toArray();
|
|
157
|
+
return results.map((result) => this.normalizeRecord(result));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Finds a single record in the table based on query.
|
|
161
|
+
* @param table - The name of the table.
|
|
162
|
+
* @param query - Query criteria.
|
|
163
|
+
* @returns The first matching record or null.
|
|
164
|
+
*/
|
|
165
|
+
async findOne(table, query) {
|
|
166
|
+
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
167
|
+
await this.createTable(table);
|
|
168
|
+
const tableDexie = this.tables.get(table);
|
|
169
|
+
try {
|
|
170
|
+
const result = await tableDexie.get(query.pk);
|
|
171
|
+
return result ? this.normalizeRecord(result) : null;
|
|
172
|
+
} catch (err) {
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const results = await this.find(table, query);
|
|
176
|
+
return results.length > 0 ? results[0] : null;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Updates records in the table based on query.
|
|
180
|
+
* @param table - The name of the table.
|
|
181
|
+
* @param query - Query criteria.
|
|
182
|
+
* @param update - Fields to update.
|
|
183
|
+
*/
|
|
184
|
+
async update(table, query, update) {
|
|
185
|
+
await this.createTable(table);
|
|
186
|
+
const tableDexie = this.tables.get(table);
|
|
187
|
+
await this.ensureColumnsForRecord(table, query);
|
|
188
|
+
await this.ensureColumnsForRecord(table, update);
|
|
189
|
+
const updateData = {};
|
|
190
|
+
for (const [key, value] of Object.entries(update)) {
|
|
191
|
+
if (key === "pk") continue;
|
|
192
|
+
updateData[key] = value;
|
|
193
|
+
}
|
|
194
|
+
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
195
|
+
await tableDexie.update(query.pk, updateData);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const collection = tableDexie.toCollection().and((record) => {
|
|
199
|
+
for (const [key, value] of Object.entries(query)) {
|
|
200
|
+
if (record[key] !== value) return false;
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
});
|
|
204
|
+
const keys = await collection.primaryKeys();
|
|
205
|
+
for (const key of keys) {
|
|
206
|
+
await tableDexie.update(key, updateData);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Deletes records from the table based on query.
|
|
211
|
+
* @param table - The name of the table.
|
|
212
|
+
* @param query - Query criteria.
|
|
213
|
+
*/
|
|
214
|
+
async delete(table, query) {
|
|
215
|
+
await this.createTable(table);
|
|
216
|
+
const tableDexie = this.tables.get(table);
|
|
217
|
+
await this.ensureColumnsForRecord(table, query);
|
|
218
|
+
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
219
|
+
await tableDexie.delete(query.pk);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const collection = tableDexie.toCollection().and((record) => {
|
|
223
|
+
for (const [key, value] of Object.entries(query)) {
|
|
224
|
+
if (record[key] !== value) return false;
|
|
225
|
+
}
|
|
226
|
+
return true;
|
|
227
|
+
});
|
|
228
|
+
const keys = await collection.primaryKeys();
|
|
229
|
+
await tableDexie.bulkDelete(keys);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Upserts a record into the table based on query.
|
|
233
|
+
* @param table - The name of the table.
|
|
234
|
+
* @param record - The record to upsert.
|
|
235
|
+
* @param query - Query criteria.
|
|
236
|
+
* @returns The primary key of the upserted record.
|
|
237
|
+
*/
|
|
238
|
+
async upsert(table, record, query) {
|
|
239
|
+
await this.createTable(table);
|
|
240
|
+
const existingRecord = await this.findOne(table, query);
|
|
241
|
+
if (existingRecord && existingRecord.pk !== void 0) {
|
|
242
|
+
const mergedRecord = { ...record };
|
|
243
|
+
await this.update(
|
|
244
|
+
table,
|
|
245
|
+
{ pk: existingRecord.pk },
|
|
246
|
+
mergedRecord
|
|
247
|
+
);
|
|
248
|
+
return existingRecord.pk;
|
|
249
|
+
} else {
|
|
250
|
+
const mergedRecord = { ...record };
|
|
251
|
+
for (const [key, value] of Object.entries(query)) {
|
|
252
|
+
if (!(key in mergedRecord)) {
|
|
253
|
+
mergedRecord[key] = value;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return await this.insert(table, mergedRecord);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Normalizes a database record to match DefussRecord structure.
|
|
261
|
+
* @param record - The record from the database.
|
|
262
|
+
* @returns A normalized DefussRecord.
|
|
263
|
+
*/
|
|
264
|
+
normalizeRecord(record) {
|
|
265
|
+
const result = {};
|
|
266
|
+
for (const [key, value] of Object.entries(record)) {
|
|
267
|
+
if (key === "pk") {
|
|
268
|
+
result.pk = value;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (key.startsWith("_")) continue;
|
|
272
|
+
result[key] = value;
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export { DexieProvider };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class DefussTable {
|
|
4
|
+
provider;
|
|
5
|
+
tableName;
|
|
6
|
+
/**
|
|
7
|
+
* Initializes the DefussTable with the specified table name and provider.
|
|
8
|
+
* @param provider - The DefussProvider instance.
|
|
9
|
+
* @param tableName - The name of the table.
|
|
10
|
+
*/
|
|
11
|
+
constructor(provider, tableName) {
|
|
12
|
+
this.provider = provider;
|
|
13
|
+
this.tableName = tableName;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates the table if it doesn't exist.
|
|
17
|
+
*/
|
|
18
|
+
async init() {
|
|
19
|
+
await this.provider.createTable(this.tableName);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Inserts an item into the table with dynamic indices.
|
|
23
|
+
* @param value - The item to insert.
|
|
24
|
+
* @param indexData - A JSON object representing dynamic indices.
|
|
25
|
+
* @returns The primary key of the inserted row.
|
|
26
|
+
*/
|
|
27
|
+
async insert(value, indexData) {
|
|
28
|
+
return this.provider.insert(this.tableName, value, indexData);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Finds items based on provided index data.
|
|
32
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
33
|
+
* @returns An array of matching items.
|
|
34
|
+
*/
|
|
35
|
+
async find(indexData = {}) {
|
|
36
|
+
return this.provider.find(this.tableName, indexData);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Finds a single item based on provided index data.
|
|
40
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
41
|
+
* @returns The first matching item or null if not found.
|
|
42
|
+
*/
|
|
43
|
+
async findOne(indexData = {}) {
|
|
44
|
+
return this.provider.findOne(this.tableName, indexData);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Updates items based on provided index data.
|
|
48
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
49
|
+
* @param dataToUpdate - A JSON object representing fields to update.
|
|
50
|
+
*/
|
|
51
|
+
async update(indexData, dataToUpdate) {
|
|
52
|
+
await this.provider.update(this.tableName, indexData, dataToUpdate);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Deletes items based on provided index data.
|
|
56
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
57
|
+
*/
|
|
58
|
+
async delete(indexData) {
|
|
59
|
+
await this.provider.delete(this.tableName, indexData);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Upserts an item into the table based on index data.
|
|
63
|
+
* @param value - The item to insert or update.
|
|
64
|
+
* @param indexData - A JSON object representing dynamic indices used to identify the record.
|
|
65
|
+
* @returns The primary key of the upserted row.
|
|
66
|
+
*/
|
|
67
|
+
async upsert(value, indexData) {
|
|
68
|
+
return this.provider.upsert(this.tableName, value, indexData);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
exports.DefussTable = DefussTable;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
type RecordValue = string | bigint | number | boolean | null | ArrayBuffer | Blob;
|
|
2
|
+
type PrimaryKeyValue = bigint | number | string;
|
|
3
|
+
interface DefussRecord {
|
|
4
|
+
pk?: PrimaryKeyValue;
|
|
5
|
+
[key: string]: RecordValue | undefined;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Generic provider interface for Defuss microdb.
|
|
9
|
+
*/
|
|
10
|
+
interface DefussProvider<O> {
|
|
11
|
+
/**
|
|
12
|
+
* Connects to the database using the provided connection string.
|
|
13
|
+
* @param connectionString - The database connection string.
|
|
14
|
+
*/
|
|
15
|
+
connect(options: O): void;
|
|
16
|
+
/**
|
|
17
|
+
* Disconnects from the database.
|
|
18
|
+
*/
|
|
19
|
+
disconnect(): void;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a table if it doesn't exist.
|
|
22
|
+
* @param table - The name of the table.
|
|
23
|
+
*/
|
|
24
|
+
createTable(table: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Inserts an item into the specified table with dynamic indices.
|
|
27
|
+
* @param table - The name of the table.
|
|
28
|
+
* @param value - The item to insert.
|
|
29
|
+
* @param indexData - A JSON object representing dynamic indices.
|
|
30
|
+
* @returns The primary key of the inserted row.
|
|
31
|
+
*/
|
|
32
|
+
insert<T extends DefussRecord>(table: string, value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
33
|
+
/**
|
|
34
|
+
* Finds items based on provided index data.
|
|
35
|
+
* @param table - The name of the table.
|
|
36
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
37
|
+
* @returns An array of matching items.
|
|
38
|
+
*/
|
|
39
|
+
find<T extends DefussRecord>(table: string, indexData: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Finds a single item based on provided index data.
|
|
42
|
+
* @param table - The name of the table.
|
|
43
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
44
|
+
* @returns The first matching item or null if not found.
|
|
45
|
+
*/
|
|
46
|
+
findOne<T extends DefussRecord>(table: string, indexData: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Updates items based on provided index data.
|
|
49
|
+
* @param table - The name of the table.
|
|
50
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
51
|
+
* @param dataToUpdate - A JSON object representing fields to update.
|
|
52
|
+
*/
|
|
53
|
+
update<T extends DefussRecord>(table: string, indexData: Partial<Record<string, RecordValue>>, dataToUpdate: Partial<T>): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Deletes items based on provided index data.
|
|
56
|
+
* @param table - The name of the table.
|
|
57
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
58
|
+
*/
|
|
59
|
+
delete(table: string, indexData: Partial<Record<string, RecordValue>>): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Upserts an item into the specified table based on index data.
|
|
62
|
+
* @param table - The name of the table.
|
|
63
|
+
* @param value - The item to insert or update.
|
|
64
|
+
* @param indexData - A JSON object representing dynamic indices used to identify the record.
|
|
65
|
+
* @returns The primary key of the upserted row.
|
|
66
|
+
*/
|
|
67
|
+
upsert<T extends DefussRecord>(table: string, value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
declare class DefussTable<T extends DefussRecord, O> {
|
|
71
|
+
provider: DefussProvider<O>;
|
|
72
|
+
tableName: string;
|
|
73
|
+
/**
|
|
74
|
+
* Initializes the DefussTable with the specified table name and provider.
|
|
75
|
+
* @param provider - The DefussProvider instance.
|
|
76
|
+
* @param tableName - The name of the table.
|
|
77
|
+
*/
|
|
78
|
+
constructor(provider: DefussProvider<O>, tableName: string);
|
|
79
|
+
/**
|
|
80
|
+
* Creates the table if it doesn't exist.
|
|
81
|
+
*/
|
|
82
|
+
init(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Inserts an item into the table with dynamic indices.
|
|
85
|
+
* @param value - The item to insert.
|
|
86
|
+
* @param indexData - A JSON object representing dynamic indices.
|
|
87
|
+
* @returns The primary key of the inserted row.
|
|
88
|
+
*/
|
|
89
|
+
insert(value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
90
|
+
/**
|
|
91
|
+
* Finds items based on provided index data.
|
|
92
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
93
|
+
* @returns An array of matching items.
|
|
94
|
+
*/
|
|
95
|
+
find(indexData?: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
96
|
+
/**
|
|
97
|
+
* Finds a single item based on provided index data.
|
|
98
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
99
|
+
* @returns The first matching item or null if not found.
|
|
100
|
+
*/
|
|
101
|
+
findOne(indexData?: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
102
|
+
/**
|
|
103
|
+
* Updates items based on provided index data.
|
|
104
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
105
|
+
* @param dataToUpdate - A JSON object representing fields to update.
|
|
106
|
+
*/
|
|
107
|
+
update(indexData: Partial<Record<string, RecordValue>>, dataToUpdate: Partial<T>): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Deletes items based on provided index data.
|
|
110
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
111
|
+
*/
|
|
112
|
+
delete(indexData: Partial<Record<string, RecordValue>>): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Upserts an item into the table based on index data.
|
|
115
|
+
* @param value - The item to insert or update.
|
|
116
|
+
* @param indexData - A JSON object representing dynamic indices used to identify the record.
|
|
117
|
+
* @returns The primary key of the upserted row.
|
|
118
|
+
*/
|
|
119
|
+
upsert(value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { type DefussProvider, type DefussRecord, DefussTable, type PrimaryKeyValue, type RecordValue };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
type RecordValue = string | bigint | number | boolean | null | ArrayBuffer | Blob;
|
|
2
|
+
type PrimaryKeyValue = bigint | number | string;
|
|
3
|
+
interface DefussRecord {
|
|
4
|
+
pk?: PrimaryKeyValue;
|
|
5
|
+
[key: string]: RecordValue | undefined;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Generic provider interface for Defuss microdb.
|
|
9
|
+
*/
|
|
10
|
+
interface DefussProvider<O> {
|
|
11
|
+
/**
|
|
12
|
+
* Connects to the database using the provided connection string.
|
|
13
|
+
* @param connectionString - The database connection string.
|
|
14
|
+
*/
|
|
15
|
+
connect(options: O): void;
|
|
16
|
+
/**
|
|
17
|
+
* Disconnects from the database.
|
|
18
|
+
*/
|
|
19
|
+
disconnect(): void;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a table if it doesn't exist.
|
|
22
|
+
* @param table - The name of the table.
|
|
23
|
+
*/
|
|
24
|
+
createTable(table: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Inserts an item into the specified table with dynamic indices.
|
|
27
|
+
* @param table - The name of the table.
|
|
28
|
+
* @param value - The item to insert.
|
|
29
|
+
* @param indexData - A JSON object representing dynamic indices.
|
|
30
|
+
* @returns The primary key of the inserted row.
|
|
31
|
+
*/
|
|
32
|
+
insert<T extends DefussRecord>(table: string, value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
33
|
+
/**
|
|
34
|
+
* Finds items based on provided index data.
|
|
35
|
+
* @param table - The name of the table.
|
|
36
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
37
|
+
* @returns An array of matching items.
|
|
38
|
+
*/
|
|
39
|
+
find<T extends DefussRecord>(table: string, indexData: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Finds a single item based on provided index data.
|
|
42
|
+
* @param table - The name of the table.
|
|
43
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
44
|
+
* @returns The first matching item or null if not found.
|
|
45
|
+
*/
|
|
46
|
+
findOne<T extends DefussRecord>(table: string, indexData: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Updates items based on provided index data.
|
|
49
|
+
* @param table - The name of the table.
|
|
50
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
51
|
+
* @param dataToUpdate - A JSON object representing fields to update.
|
|
52
|
+
*/
|
|
53
|
+
update<T extends DefussRecord>(table: string, indexData: Partial<Record<string, RecordValue>>, dataToUpdate: Partial<T>): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Deletes items based on provided index data.
|
|
56
|
+
* @param table - The name of the table.
|
|
57
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
58
|
+
*/
|
|
59
|
+
delete(table: string, indexData: Partial<Record<string, RecordValue>>): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Upserts an item into the specified table based on index data.
|
|
62
|
+
* @param table - The name of the table.
|
|
63
|
+
* @param value - The item to insert or update.
|
|
64
|
+
* @param indexData - A JSON object representing dynamic indices used to identify the record.
|
|
65
|
+
* @returns The primary key of the upserted row.
|
|
66
|
+
*/
|
|
67
|
+
upsert<T extends DefussRecord>(table: string, value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
declare class DefussTable<T extends DefussRecord, O> {
|
|
71
|
+
provider: DefussProvider<O>;
|
|
72
|
+
tableName: string;
|
|
73
|
+
/**
|
|
74
|
+
* Initializes the DefussTable with the specified table name and provider.
|
|
75
|
+
* @param provider - The DefussProvider instance.
|
|
76
|
+
* @param tableName - The name of the table.
|
|
77
|
+
*/
|
|
78
|
+
constructor(provider: DefussProvider<O>, tableName: string);
|
|
79
|
+
/**
|
|
80
|
+
* Creates the table if it doesn't exist.
|
|
81
|
+
*/
|
|
82
|
+
init(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Inserts an item into the table with dynamic indices.
|
|
85
|
+
* @param value - The item to insert.
|
|
86
|
+
* @param indexData - A JSON object representing dynamic indices.
|
|
87
|
+
* @returns The primary key of the inserted row.
|
|
88
|
+
*/
|
|
89
|
+
insert(value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
90
|
+
/**
|
|
91
|
+
* Finds items based on provided index data.
|
|
92
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
93
|
+
* @returns An array of matching items.
|
|
94
|
+
*/
|
|
95
|
+
find(indexData?: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
96
|
+
/**
|
|
97
|
+
* Finds a single item based on provided index data.
|
|
98
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
99
|
+
* @returns The first matching item or null if not found.
|
|
100
|
+
*/
|
|
101
|
+
findOne(indexData?: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
102
|
+
/**
|
|
103
|
+
* Updates items based on provided index data.
|
|
104
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
105
|
+
* @param dataToUpdate - A JSON object representing fields to update.
|
|
106
|
+
*/
|
|
107
|
+
update(indexData: Partial<Record<string, RecordValue>>, dataToUpdate: Partial<T>): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Deletes items based on provided index data.
|
|
110
|
+
* @param indexData - A JSON object representing dynamic indices. All fields are optional.
|
|
111
|
+
*/
|
|
112
|
+
delete(indexData: Partial<Record<string, RecordValue>>): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Upserts an item into the table based on index data.
|
|
115
|
+
* @param value - The item to insert or update.
|
|
116
|
+
* @param indexData - A JSON object representing dynamic indices used to identify the record.
|
|
117
|
+
* @returns The primary key of the upserted row.
|
|
118
|
+
*/
|
|
119
|
+
upsert(value: T, indexData: Record<string, RecordValue>): Promise<PrimaryKeyValue>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { type DefussProvider, type DefussRecord, DefussTable, type PrimaryKeyValue, type RecordValue };
|