@semics-tech/mongolite 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 +179 -0
- package/dist/adapters/browser.d.ts +97 -0
- package/dist/adapters/browser.js +103 -0
- package/dist/adapters/browser.js.map +1 -0
- package/dist/adapters/cloudflare.d.ts +106 -0
- package/dist/adapters/cloudflare.js +94 -0
- package/dist/adapters/cloudflare.js.map +1 -0
- package/dist/bin/mongolite-debug.d.ts +2 -0
- package/dist/bin/mongolite-debug.js +115 -0
- package/dist/bin/mongolite-debug.js.map +1 -0
- package/dist/changeStream.d.ts +114 -0
- package/dist/changeStream.js +338 -0
- package/dist/changeStream.js.map +1 -0
- package/dist/cloudflare.d.ts +39 -0
- package/dist/cloudflare.js +39 -0
- package/dist/cloudflare.js.map +1 -0
- package/dist/collection.d.ts +274 -0
- package/dist/collection.js +1706 -0
- package/dist/collection.js.map +1 -0
- package/dist/cursors/findCursor.d.ts +101 -0
- package/dist/cursors/findCursor.js +757 -0
- package/dist/cursors/findCursor.js.map +1 -0
- package/dist/db.d.ts +116 -0
- package/dist/db.js +254 -0
- package/dist/db.js.map +1 -0
- package/dist/debugger/query-debugger.d.ts +22 -0
- package/dist/debugger/query-debugger.js +258 -0
- package/dist/debugger/query-debugger.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/mongo-client.d.ts +56 -0
- package/dist/mongo-client.js +55 -0
- package/dist/mongo-client.js.map +1 -0
- package/dist/types.d.ts +318 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/indexing.d.ts +4 -0
- package/dist/utils/indexing.js +43 -0
- package/dist/utils/indexing.js.map +1 -0
- package/dist/utils/projection.d.ts +16 -0
- package/dist/utils/projection.js +98 -0
- package/dist/utils/projection.js.map +1 -0
- package/dist/utils/queryOperators.d.ts +16 -0
- package/dist/utils/queryOperators.js +138 -0
- package/dist/utils/queryOperators.js.map +1 -0
- package/package.json +92 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { MongoLite } from '../index.js';
|
|
2
|
+
import { FindCursor } from '../cursors/findCursor.js';
|
|
3
|
+
import * as readline from 'readline';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
export class QueryDebugger {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.currentCollection = null;
|
|
8
|
+
this.lastFilter = null;
|
|
9
|
+
this.lastSqlQuery = null;
|
|
10
|
+
const dbPath = options.database || path.join(process.cwd(), 'mongolite.db');
|
|
11
|
+
this.client = new MongoLite(dbPath, { verbose: options.verbose });
|
|
12
|
+
this.rl = readline.createInterface({
|
|
13
|
+
input: process.stdin,
|
|
14
|
+
output: process.stdout,
|
|
15
|
+
prompt: 'mongolite-debug> ',
|
|
16
|
+
});
|
|
17
|
+
if (options.collection) {
|
|
18
|
+
this.currentCollection = options.collection;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async start() {
|
|
22
|
+
console.log('Commands:');
|
|
23
|
+
console.log(' .help - Show this help');
|
|
24
|
+
console.log(' .collections - List all collections');
|
|
25
|
+
console.log(' .use <collection> - Select a collection to work with');
|
|
26
|
+
console.log(' .find <filter> - Convert find filter to SQL and execute');
|
|
27
|
+
console.log(' .sql <query> - Execute raw SQL query');
|
|
28
|
+
console.log(' .last - Show last generated SQL');
|
|
29
|
+
console.log(' .sample [count] - Show sample documents from current collection');
|
|
30
|
+
console.log(' .exit - Exit the debugger');
|
|
31
|
+
console.log('');
|
|
32
|
+
try {
|
|
33
|
+
await this.client.connect();
|
|
34
|
+
console.log('✅ Connected to database.');
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
console.error('❌ Failed to connect to database:', error instanceof Error ? error.message : error);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
if (this.currentCollection) {
|
|
41
|
+
console.log(`Current collection: ${this.currentCollection}`);
|
|
42
|
+
}
|
|
43
|
+
this.rl.prompt();
|
|
44
|
+
this.rl.on('line', async (input) => {
|
|
45
|
+
const trimmed = input.trim();
|
|
46
|
+
if (trimmed === '.exit') {
|
|
47
|
+
await this.cleanup();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
await this.handleCommand(trimmed);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('Error:', error instanceof Error ? error.message : error);
|
|
55
|
+
}
|
|
56
|
+
this.rl.prompt();
|
|
57
|
+
});
|
|
58
|
+
this.rl.on('close', async () => {
|
|
59
|
+
await this.cleanup();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async handleCommand(input) {
|
|
63
|
+
if (input === '.help') {
|
|
64
|
+
this.showHelp();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (input === '.collections') {
|
|
68
|
+
await this.listCollections();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (input.startsWith('.use ')) {
|
|
72
|
+
const collection = input.slice(5).trim();
|
|
73
|
+
this.currentCollection = collection;
|
|
74
|
+
console.log(`Switched to collection: ${collection}`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (input.startsWith('.find ')) {
|
|
78
|
+
await this.handleFindCommand(input.slice(6).trim());
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (input.startsWith('.sql ')) {
|
|
82
|
+
await this.handleSqlCommand(input.slice(5).trim());
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (input === '.last') {
|
|
86
|
+
this.showLastQuery();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (input.startsWith('.sample')) {
|
|
90
|
+
const parts = input.split(' ');
|
|
91
|
+
const count = parts.length > 1 ? parseInt(parts[1]) || 5 : 5;
|
|
92
|
+
await this.showSample(count);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
console.log('Unknown command. Type .help for available commands.');
|
|
96
|
+
}
|
|
97
|
+
showHelp() {
|
|
98
|
+
console.log('');
|
|
99
|
+
console.log('Available commands:');
|
|
100
|
+
console.log(' .help - Show this help');
|
|
101
|
+
console.log(' .collections - List all collections');
|
|
102
|
+
console.log(' .use <collection> - Select a collection to work with');
|
|
103
|
+
console.log(' .find <filter> - Convert find filter to SQL and execute');
|
|
104
|
+
console.log(' .sql <query> - Execute raw SQL query');
|
|
105
|
+
console.log(' .last - Show last generated SQL');
|
|
106
|
+
console.log(' .sample [count] - Show sample documents from current collection');
|
|
107
|
+
console.log(' .exit - Exit the debugger');
|
|
108
|
+
console.log('');
|
|
109
|
+
console.log('Examples:');
|
|
110
|
+
console.log(' .use users');
|
|
111
|
+
console.log(' .find {"age": {"$gt": 25}}');
|
|
112
|
+
console.log(' .find {"name": "John", "age": {"$in": [25, 30, 35]}}');
|
|
113
|
+
console.log(' .sql SELECT * FROM users WHERE json_extract(data, "$.age") > 25');
|
|
114
|
+
console.log('');
|
|
115
|
+
}
|
|
116
|
+
async listCollections() {
|
|
117
|
+
try {
|
|
118
|
+
const collections = await this.client.listCollections().toArray();
|
|
119
|
+
if (collections.length === 0) {
|
|
120
|
+
console.log('No collections found.');
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
console.log('Collections:');
|
|
124
|
+
collections.forEach((collection) => {
|
|
125
|
+
const marker = collection === this.currentCollection ? ' (current)' : '';
|
|
126
|
+
console.log(` - ${collection}${marker}`);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.error('Error listing collections:', error instanceof Error ? error.message : error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async handleFindCommand(filterStr) {
|
|
135
|
+
if (!this.currentCollection) {
|
|
136
|
+
console.log('No collection selected. Use .use <collection> first.');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
let filter;
|
|
140
|
+
try {
|
|
141
|
+
filter = JSON.parse(filterStr);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.log('Invalid JSON filter:', error instanceof Error ? error.message : error);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.lastFilter = filter;
|
|
148
|
+
// Create a collection and a find cursor to extract the SQL
|
|
149
|
+
const collection = this.client.collection(this.currentCollection);
|
|
150
|
+
// We need to access the internal SQL generation. Let's create a custom cursor
|
|
151
|
+
// that exposes the SQL generation logic
|
|
152
|
+
const debugCursor = new DebugFindCursor(collection.db, this.currentCollection, filter, { verbose: true });
|
|
153
|
+
this.lastSqlQuery = debugCursor.getSQL();
|
|
154
|
+
console.log('');
|
|
155
|
+
console.log('📝 Generated SQL:');
|
|
156
|
+
console.log('Query:', this.lastSqlQuery.sql);
|
|
157
|
+
console.log('Parameters:', JSON.stringify(this.lastSqlQuery.params));
|
|
158
|
+
console.log('');
|
|
159
|
+
// Execute the query and show results
|
|
160
|
+
try {
|
|
161
|
+
const results = await debugCursor.toArray();
|
|
162
|
+
console.log(`📊 Results (${results.length} documents):`);
|
|
163
|
+
if (results.length > 0) {
|
|
164
|
+
results.forEach((doc, index) => {
|
|
165
|
+
console.log(`[${index}]`, JSON.stringify(doc, null, 2));
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
console.log('No documents found.');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
console.error('Execution error:', error instanceof Error ? error.message : error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async handleSqlCommand(sqlQuery) {
|
|
177
|
+
if (!this.currentCollection) {
|
|
178
|
+
console.log('No collection selected. Use .use <collection> first.');
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
// Get the database instance to execute raw SQL
|
|
183
|
+
const db = this.client.db;
|
|
184
|
+
console.log('');
|
|
185
|
+
console.log('🔧 Executing SQL:');
|
|
186
|
+
console.log(sqlQuery);
|
|
187
|
+
console.log('');
|
|
188
|
+
const results = await db.all(sqlQuery);
|
|
189
|
+
console.log(`📊 Results (${results.length} rows):`);
|
|
190
|
+
if (results.length > 0) {
|
|
191
|
+
results.forEach((row, index) => {
|
|
192
|
+
console.log(`[${index}]`, JSON.stringify(row, null, 2));
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
console.log('No rows returned.');
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
console.error('SQL execution error:', error instanceof Error ? error.message : error);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
showLastQuery() {
|
|
204
|
+
if (!this.lastSqlQuery) {
|
|
205
|
+
console.log('No previous query to show.');
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
console.log('');
|
|
209
|
+
console.log('📝 Last Generated SQL:');
|
|
210
|
+
console.log('Query:', this.lastSqlQuery.sql);
|
|
211
|
+
console.log('Parameters:', JSON.stringify(this.lastSqlQuery.params));
|
|
212
|
+
if (this.lastFilter) {
|
|
213
|
+
console.log('Original Filter:', JSON.stringify(this.lastFilter, null, 2));
|
|
214
|
+
}
|
|
215
|
+
console.log('');
|
|
216
|
+
}
|
|
217
|
+
async showSample(count) {
|
|
218
|
+
if (!this.currentCollection) {
|
|
219
|
+
console.log('No collection selected. Use .use <collection> first.');
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
const collection = this.client.collection(this.currentCollection);
|
|
224
|
+
const results = await collection.find({}).limit(count).toArray();
|
|
225
|
+
console.log('');
|
|
226
|
+
console.log(`📋 Sample documents from "${this.currentCollection}" (${results.length} of ${count} requested):`);
|
|
227
|
+
if (results.length > 0) {
|
|
228
|
+
results.forEach((doc, index) => {
|
|
229
|
+
console.log(`[${index}]`, JSON.stringify(doc, null, 2));
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
console.log('Collection is empty.');
|
|
234
|
+
}
|
|
235
|
+
console.log('');
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
console.error('Error fetching sample:', error instanceof Error ? error.message : error);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async cleanup() {
|
|
242
|
+
console.log('\nClosing database connection...');
|
|
243
|
+
await this.client.close();
|
|
244
|
+
this.rl.close();
|
|
245
|
+
process.exit(0);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Extended FindCursor that exposes SQL generation
|
|
249
|
+
class DebugFindCursor extends FindCursor {
|
|
250
|
+
constructor(db, collectionName, filter, options = {}) {
|
|
251
|
+
super(db, collectionName, filter, options);
|
|
252
|
+
}
|
|
253
|
+
getSQL() {
|
|
254
|
+
// Access the private queryParts property
|
|
255
|
+
return this.queryParts;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=query-debugger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-debugger.js","sourceRoot":"","sources":["../../src/debugger/query-debugger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,KAAK,QAAQ,MAAM,UAAU,CAAC;AACrC,OAAO,IAAI,MAAM,MAAM,CAAC;AAQxB,MAAM,OAAO,aAAa;IAOxB,YAAY,UAA2B,EAAE;QAJjC,sBAAiB,GAAkB,IAAI,CAAC;QACxC,eAAU,GAAuB,IAAI,CAAC;QACtC,iBAAY,GAA8C,IAAI,CAAC;QAGrE,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,cAAc,CAAC,CAAC;QAC5E,IAAI,CAAC,MAAM,GAAG,IAAI,SAAS,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,OAAO,CA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AC;YACtC,CAAC;YACD,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAClB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,wBAAwB,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,OAAO,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;QAChD,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC1B,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAChB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;CACF;AAED,kDAAkD;AAClD,MAAM,eAAgE,SAAQ,UAAa;IACzF,YACE,EAAO,EACP,cAAsB,EACtB,MAAiB,EACjB,UAAiC,EAAE;QAEnC,KAAK,CAAC,EAAE,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC;IAEM,MAAM;QACX,yCAAyC;QACzC,OAAQ,IAAY,CAAC,UAAU,CAAC;IAClC,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { IDatabaseAdapter, MongoLiteOptions as DBMongoLiteOptions } from './db.js';
|
|
2
|
+
import { MongoLite as MongoLiteBase, MongoLiteBaseOptions } from './mongo-client.js';
|
|
3
|
+
export { MongoLiteCollection } from './collection.js';
|
|
4
|
+
export * from './types.js';
|
|
5
|
+
export { ChangeStream } from './changeStream.js';
|
|
6
|
+
export type { ChangeStreamDocument, ChangeStreamOptions, ChangeOperationType, } from './changeStream.js';
|
|
7
|
+
export type { IDatabaseAdapter } from './db.js';
|
|
8
|
+
export { CloudflareDurableObjectAdapter, } from './adapters/cloudflare.js';
|
|
9
|
+
export type { SqlStorage, SqlStorageCursor, SqlStorageValue, } from './adapters/cloudflare.js';
|
|
10
|
+
export { BrowserSqliteAdapter } from './adapters/browser.js';
|
|
11
|
+
export type { SqlJsDatabase, SqlJsStatement } from './adapters/browser.js';
|
|
12
|
+
export interface MongoLiteClientOptions extends DBMongoLiteOptions {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* MongoLite class is the main entry point for interacting with the SQLite-backed database.
|
|
16
|
+
*
|
|
17
|
+
* You can construct it with:
|
|
18
|
+
* - A file path string — uses the built-in `better-sqlite3` adapter.
|
|
19
|
+
* - A `MongoLiteClientOptions` object — uses the built-in adapter with options.
|
|
20
|
+
* - An `IDatabaseAdapter` instance — use a custom adapter such as
|
|
21
|
+
* `CloudflareDurableObjectAdapter` for Cloudflare Durable Objects.
|
|
22
|
+
*/
|
|
23
|
+
export declare class MongoLite extends MongoLiteBase {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new MongoLite client instance.
|
|
26
|
+
* @param dbPathOrOptions Path to the SQLite database file, an options object,
|
|
27
|
+
* or an `IDatabaseAdapter` for custom backends.
|
|
28
|
+
*/
|
|
29
|
+
constructor(dbPathOrOptions: string | MongoLiteClientOptions | IDatabaseAdapter, options?: MongoLiteBaseOptions);
|
|
30
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { SQLiteDB } from './db.js';
|
|
2
|
+
import { MongoLite as MongoLiteBase } from './mongo-client.js';
|
|
3
|
+
export { MongoLiteCollection } from './collection.js';
|
|
4
|
+
export * from './types.js';
|
|
5
|
+
export { ChangeStream } from './changeStream.js';
|
|
6
|
+
export { CloudflareDurableObjectAdapter, } from './adapters/cloudflare.js';
|
|
7
|
+
export { BrowserSqliteAdapter } from './adapters/browser.js';
|
|
8
|
+
/**
|
|
9
|
+
* MongoLite class is the main entry point for interacting with the SQLite-backed database.
|
|
10
|
+
*
|
|
11
|
+
* You can construct it with:
|
|
12
|
+
* - A file path string — uses the built-in `better-sqlite3` adapter.
|
|
13
|
+
* - A `MongoLiteClientOptions` object — uses the built-in adapter with options.
|
|
14
|
+
* - An `IDatabaseAdapter` instance — use a custom adapter such as
|
|
15
|
+
* `CloudflareDurableObjectAdapter` for Cloudflare Durable Objects.
|
|
16
|
+
*/
|
|
17
|
+
export class MongoLite extends MongoLiteBase {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new MongoLite client instance.
|
|
20
|
+
* @param dbPathOrOptions Path to the SQLite database file, an options object,
|
|
21
|
+
* or an `IDatabaseAdapter` for custom backends.
|
|
22
|
+
*/
|
|
23
|
+
constructor(dbPathOrOptions, options = {}) {
|
|
24
|
+
if (dbPathOrOptions &&
|
|
25
|
+
typeof dbPathOrOptions === 'object' &&
|
|
26
|
+
typeof dbPathOrOptions.connect === 'function' &&
|
|
27
|
+
typeof dbPathOrOptions.run === 'function' &&
|
|
28
|
+
typeof dbPathOrOptions.get === 'function' &&
|
|
29
|
+
typeof dbPathOrOptions.all === 'function' &&
|
|
30
|
+
typeof dbPathOrOptions.exec === 'function' &&
|
|
31
|
+
typeof dbPathOrOptions.close === 'function') {
|
|
32
|
+
// Custom adapter (e.g. CloudflareDurableObjectAdapter)
|
|
33
|
+
super(dbPathOrOptions, options);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
super(new SQLiteDB(dbPathOrOptions), options);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA4D,MAAM,SAAS,CAAC;AAC7F,OAAO,EAAE,SAAS,IAAI,aAAa,EAAwB,MAAM,mBAAmB,CAAC;AAErF,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAOjD,OAAO,EACL,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAMlC,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAK7D;;;;;;;;GAQG;AACH,MAAM,OAAO,SAAU,SAAQ,aAAa;IAC1C;;;;OAIG;IACH,YACE,eAAmE,EACnE,UAAgC,EAAE;QAElC,IACE,eAAe;YACf,OAAO,eAAe,KAAK,QAAQ;YACnC,OAAQ,eAAoC,CAAC,OAAO,KAAK,UAAU;YACnE,OAAQ,eAAoC,CAAC,GAAG,KAAK,UAAU;YAC/D,OAAQ,eAAoC,CAAC,GAAG,KAAK,UAAU;YAC/D,OAAQ,eAAoC,CAAC,GAAG,KAAK,UAAU;YAC/D,OAAQ,eAAoC,CAAC,IAAI,KAAK,UAAU;YAChE,OAAQ,eAAoC,CAAC,KAAK,KAAK,UAAU,EACjE,CAAC;YACD,uDAAuD;YACvD,KAAK,CAAC,eAAmC,EAAE,OAAO,CAAC,CAAC;QACtD,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,IAAI,QAAQ,CAAC,eAAkD,CAAC,EAAE,OAAO,CAAC,CAAC;QACnF,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare-safe MongoLite client base class.
|
|
3
|
+
*
|
|
4
|
+
* This module does NOT import `better-sqlite3` and is safe to use inside
|
|
5
|
+
* Cloudflare Workers / Durable Objects. It exports the core `MongoLite` class
|
|
6
|
+
* that accepts any `IDatabaseAdapter` implementation.
|
|
7
|
+
*
|
|
8
|
+
* Node.js consumers should import from the package root (`@semics-tech/mongolite`) which
|
|
9
|
+
* provides additional constructor overloads accepting a file path or options object.
|
|
10
|
+
*/
|
|
11
|
+
import type { IDatabaseAdapter } from './db.js';
|
|
12
|
+
import { MongoLiteCollection } from './collection.js';
|
|
13
|
+
import { DocumentWithId } from './types.js';
|
|
14
|
+
export type { IDatabaseAdapter };
|
|
15
|
+
export type MongoLiteBaseOptions = {
|
|
16
|
+
verbose?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Core MongoLite client. Accepts any `IDatabaseAdapter` implementation.
|
|
20
|
+
*
|
|
21
|
+
* This class is intentionally free of `better-sqlite3` dependencies so that it
|
|
22
|
+
* can be bundled for Cloudflare Workers / Durable Objects. For Node.js use, prefer
|
|
23
|
+
* importing `MongoLite` from the package root, which adds convenient constructor
|
|
24
|
+
* overloads for file paths and options objects.
|
|
25
|
+
*/
|
|
26
|
+
export declare class MongoLite {
|
|
27
|
+
protected db: IDatabaseAdapter;
|
|
28
|
+
protected options: MongoLiteBaseOptions;
|
|
29
|
+
constructor(adapter: IDatabaseAdapter, options?: MongoLiteBaseOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Connects to the database.
|
|
32
|
+
* For most custom adapters (e.g. Cloudflare) this is a no-op.
|
|
33
|
+
*/
|
|
34
|
+
connect(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the underlying database adapter instance.
|
|
37
|
+
*/
|
|
38
|
+
get database(): IDatabaseAdapter;
|
|
39
|
+
/**
|
|
40
|
+
* Closes the database connection.
|
|
41
|
+
* For most custom adapters (e.g. Cloudflare) this is a no-op.
|
|
42
|
+
*/
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Lists all collections (tables) in the database.
|
|
46
|
+
* @returns An object with a `toArray` method that resolves to an array of collection names.
|
|
47
|
+
*/
|
|
48
|
+
listCollections(): {
|
|
49
|
+
toArray: () => Promise<string[]>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Returns a typed collection handle.
|
|
53
|
+
* @param name The collection (table) name.
|
|
54
|
+
*/
|
|
55
|
+
collection<T extends DocumentWithId = DocumentWithId>(name: string): MongoLiteCollection<T>;
|
|
56
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { MongoLiteCollection } from './collection.js';
|
|
2
|
+
/**
|
|
3
|
+
* Core MongoLite client. Accepts any `IDatabaseAdapter` implementation.
|
|
4
|
+
*
|
|
5
|
+
* This class is intentionally free of `better-sqlite3` dependencies so that it
|
|
6
|
+
* can be bundled for Cloudflare Workers / Durable Objects. For Node.js use, prefer
|
|
7
|
+
* importing `MongoLite` from the package root, which adds convenient constructor
|
|
8
|
+
* overloads for file paths and options objects.
|
|
9
|
+
*/
|
|
10
|
+
export class MongoLite {
|
|
11
|
+
constructor(adapter, options = {}) {
|
|
12
|
+
this.db = adapter;
|
|
13
|
+
this.options = options;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Connects to the database.
|
|
17
|
+
* For most custom adapters (e.g. Cloudflare) this is a no-op.
|
|
18
|
+
*/
|
|
19
|
+
async connect() {
|
|
20
|
+
return this.db.connect();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the underlying database adapter instance.
|
|
24
|
+
*/
|
|
25
|
+
get database() {
|
|
26
|
+
return this.db;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Closes the database connection.
|
|
30
|
+
* For most custom adapters (e.g. Cloudflare) this is a no-op.
|
|
31
|
+
*/
|
|
32
|
+
async close() {
|
|
33
|
+
return this.db.close();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Lists all collections (tables) in the database.
|
|
37
|
+
* @returns An object with a `toArray` method that resolves to an array of collection names.
|
|
38
|
+
*/
|
|
39
|
+
listCollections() {
|
|
40
|
+
return {
|
|
41
|
+
toArray: async () => {
|
|
42
|
+
const result = await this.db.all(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`);
|
|
43
|
+
return result.map((row) => row.name);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns a typed collection handle.
|
|
49
|
+
* @param name The collection (table) name.
|
|
50
|
+
*/
|
|
51
|
+
collection(name) {
|
|
52
|
+
return new MongoLiteCollection(this.db, name, this.options);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=mongo-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo-client.js","sourceRoot":"","sources":["../src/mongo-client.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAStD;;;;;;;GAOG;AACH,MAAM,OAAO,SAAS;IAIpB,YAAY,OAAyB,EAAE,UAAgC,EAAE;QACvE,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,OAAO;QACX,OAAO,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,EAAE,CAAC;IACjB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK;QACT,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,eAAe;QACb,OAAO;YACL,OAAO,EAAE,KAAK,IAAI,EAAE;gBAClB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,GAAG,CAC9B,gFAAgF,CACjF,CAAC;gBACF,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACvC,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,UAAU,CAA4C,IAAY;QAChE,OAAO,IAAI,mBAAmB,CAAI,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACjE,CAAC;CACF"}
|