@objectql/driver-mongo 3.0.1 → 4.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @objectql/driver-mongo
2
2
 
3
+ ## 4.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - **Release Version 4.0.1**
8
+
9
+ This patch release includes the latest repository improvements and infrastructure updates:
10
+ - Added comprehensive GitHub workflows for CI/CD, testing, and quality assurance
11
+ - Enhanced documentation and developer experience
12
+ - Improved build and release processes with Changesets
13
+ - Added Excel driver for reading/writing Excel files as data sources
14
+ - Repository structure and tooling improvements
15
+ - Bug fixes and stability enhancements
16
+
17
+ - Updated dependencies
18
+ - @objectql/types@4.0.1
19
+
3
20
  ## 3.0.1
4
21
 
5
22
  ### Patch Changes
@@ -7,7 +24,6 @@
7
24
  - 79d04e1: Patch release for January 2026 updates
8
25
 
9
26
  This patch includes minor improvements and maintenance updates:
10
-
11
27
  - Enhanced type safety across core packages
12
28
  - Improved error handling in drivers
13
29
  - Documentation updates
@@ -30,7 +46,6 @@
30
46
  This is a coordinated major release that unifies all ObjectQL packages to version 2.0.0, establishing a synchronized versioning strategy across the entire ecosystem.
31
47
 
32
48
  ### 🎯 Key Changes
33
-
34
49
  - **Unified Versioning**: All core packages now share the same version number (2.0.0)
35
50
  - **Fixed Group Management**: Updated changeset configuration to include all @objectql packages in the fixed versioning group
36
51
  - **Simplified Maintenance**: Future releases will automatically maintain version consistency across the entire monorepo
@@ -38,7 +53,6 @@
38
53
  ### 📦 Packages Included
39
54
 
40
55
  All ObjectQL packages are now synchronized at version 2.0.0:
41
-
42
56
  - Foundation: `@objectql/types`, `@objectql/core`, `@objectql/platform-node`
43
57
  - Drivers: `@objectql/driver-sql`, `@objectql/driver-mongo`, `@objectql/driver-redis`, `@objectql/driver-fs`, `@objectql/driver-memory`, `@objectql/driver-localstorage`, `@objectql/driver-excel`, `@objectql/sdk`
44
58
  - Runtime: `@objectql/server`
package/MIGRATION.md ADDED
@@ -0,0 +1,213 @@
1
+ # MongoDB Driver Migration Guide (Phase 4)
2
+
3
+ ## Overview
4
+
5
+ The MongoDB driver has been migrated to support the standard `DriverInterface` from `@objectstack/spec` while maintaining full backward compatibility with the existing `Driver` interface from `@objectql/types`.
6
+
7
+ ## What Changed
8
+
9
+ ### 1. Driver Metadata
10
+
11
+ The driver now exposes metadata for ObjectStack compatibility:
12
+
13
+ ```typescript
14
+ const driver = new MongoDriver(config);
15
+ console.log(driver.name); // 'MongoDriver'
16
+ console.log(driver.version); // '3.0.1'
17
+ console.log(driver.supports); // { transactions: true, joins: false, ... }
18
+ ```
19
+
20
+ ### 2. Lifecycle Methods
21
+
22
+ New optional lifecycle methods for DriverInterface compatibility:
23
+
24
+ ```typescript
25
+ // Connect (ensures connection is established)
26
+ await driver.connect();
27
+
28
+ // Check connection health
29
+ const healthy = await driver.checkHealth(); // true/false
30
+
31
+ // Disconnect (existing method)
32
+ await driver.disconnect();
33
+ ```
34
+
35
+ ### 3. QueryAST Format Support
36
+
37
+ The driver now supports the new QueryAST format from `@objectstack/spec`:
38
+
39
+ #### Legacy UnifiedQuery Format (Still Supported)
40
+ ```typescript
41
+ const query = {
42
+ fields: ['name', 'age'],
43
+ filters: [['age', '>', 18]],
44
+ sort: [['name', 'asc']],
45
+ limit: 10,
46
+ skip: 0
47
+ };
48
+ ```
49
+
50
+ #### New QueryAST Format (Now Supported)
51
+ ```typescript
52
+ const query = {
53
+ object: 'users',
54
+ fields: ['name', 'age'],
55
+ filters: [['age', '>', 18]],
56
+ sort: [{ field: 'name', order: 'asc' }],
57
+ top: 10, // Instead of 'limit'
58
+ skip: 0
59
+ };
60
+ ```
61
+
62
+ ### Key Differences
63
+
64
+ | Aspect | Legacy Format | QueryAST Format |
65
+ |--------|--------------|-----------------|
66
+ | Limit | `limit: 10` | `top: 10` |
67
+ | Sort | `[['field', 'dir']]` | `[{field, order}]` |
68
+
69
+ ## Migration Strategy
70
+
71
+ The driver uses a **normalization layer** that automatically converts QueryAST format to the internal format:
72
+
73
+ ```typescript
74
+ private normalizeQuery(query: any): any {
75
+ // Converts 'top' → 'limit'
76
+ // Handles both sort formats
77
+ }
78
+ ```
79
+
80
+ This means:
81
+ - ✅ Existing code continues to work without changes
82
+ - ✅ New code can use QueryAST format
83
+ - ✅ Both formats work interchangeably
84
+ - ✅ No breaking changes
85
+
86
+ ## Usage Examples
87
+
88
+ ### Using Legacy Format (Unchanged)
89
+ ```typescript
90
+ import { MongoDriver } from '@objectql/driver-mongo';
91
+
92
+ const driver = new MongoDriver({
93
+ url: 'mongodb://localhost:27017',
94
+ dbName: 'mydb'
95
+ });
96
+
97
+ // Works as before
98
+ const results = await driver.find('users', {
99
+ filters: [['active', '=', true]],
100
+ sort: [['created_at', 'desc']],
101
+ limit: 20
102
+ });
103
+ ```
104
+
105
+ ### Using QueryAST Format (New)
106
+ ```typescript
107
+ import { MongoDriver } from '@objectql/driver-mongo';
108
+
109
+ const driver = new MongoDriver({
110
+ url: 'mongodb://localhost:27017',
111
+ dbName: 'mydb'
112
+ });
113
+
114
+ // New format
115
+ const results = await driver.find('users', {
116
+ filters: [['active', '=', true]],
117
+ sort: [{ field: 'created_at', order: 'desc' }],
118
+ top: 20
119
+ });
120
+ ```
121
+
122
+ ### Using with ObjectStack Kernel
123
+ ```typescript
124
+ import { ObjectQL } from '@objectql/core';
125
+ import { MongoDriver } from '@objectql/driver-mongo';
126
+
127
+ const app = new ObjectQL({
128
+ datasources: {
129
+ default: new MongoDriver({
130
+ url: 'mongodb://localhost:27017',
131
+ dbName: 'mydb'
132
+ })
133
+ }
134
+ });
135
+
136
+ await app.init();
137
+
138
+ // The kernel will use QueryAST format internally
139
+ const ctx = app.createContext({ userId: 'user123' });
140
+ const repo = ctx.object('users');
141
+ const users = await repo.find({ filters: [['active', '=', true]] });
142
+ ```
143
+
144
+ ## Testing
145
+
146
+ Comprehensive tests have been added in `test/queryast.test.ts`:
147
+
148
+ ```bash
149
+ npm test -- queryast.test.ts
150
+ ```
151
+
152
+ Test coverage includes:
153
+ - Driver metadata exposure
154
+ - Lifecycle methods (connect, checkHealth, disconnect)
155
+ - QueryAST format with `top` parameter
156
+ - Object-based sort notation
157
+ - Backward compatibility with legacy format
158
+ - Mixed format support
159
+ - Field mapping (id/_id conversion)
160
+
161
+ ## Implementation Details
162
+
163
+ ### Files Changed
164
+ - `package.json`: Added `@objectstack/spec@^0.2.0` dependency
165
+ - `src/index.ts`:
166
+ - Added driver metadata properties
167
+ - Added `normalizeQuery()` method (~45 lines)
168
+ - Added `connect()` and `checkHealth()` methods (~25 lines)
169
+ - Updated `find()` to use normalization
170
+ - Refactored internal `connect()` to `internalConnect()`
171
+ - `test/queryast.test.ts`: New comprehensive test suite (240+ lines)
172
+
173
+ ### Lines of Code
174
+ - **Added**: ~310 lines (including tests and docs)
175
+ - **Modified**: ~15 lines (method signatures and refactoring)
176
+ - **Deleted**: 0 lines
177
+
178
+ ## Driver Capabilities
179
+
180
+ The MongoDB driver supports:
181
+ - **Transactions**: ✅ Yes
182
+ - **Joins**: ❌ No (MongoDB is document-oriented)
183
+ - **Full-Text Search**: ✅ Yes (MongoDB text search)
184
+ - **JSON Fields**: ✅ Yes (native BSON support)
185
+ - **Array Fields**: ✅ Yes (native array support)
186
+
187
+ ## ID Field Mapping
188
+
189
+ The driver maintains smart ID mapping:
190
+ - API uses `id` field
191
+ - MongoDB uses `_id` field
192
+ - Automatic bidirectional conversion
193
+ - Both `id` and `_id` can be used in queries for backward compatibility
194
+
195
+ ## Next Steps
196
+
197
+ With MongoDB driver migration complete, the pattern is established for migrating other drivers:
198
+
199
+ 1. ✅ SQL Driver (completed)
200
+ 2. ✅ MongoDB Driver (completed)
201
+ 3. 🔜 Memory Driver (recommended next - used for testing)
202
+ 4. 🔜 Other drivers (bulk migration)
203
+
204
+ ## Backward Compatibility Guarantee
205
+
206
+ **100% backward compatible** - all existing code using the MongoDB driver will continue to work without any changes. The QueryAST support is additive, not replacing.
207
+
208
+ ## References
209
+
210
+ - [ObjectStack Spec Package](https://www.npmjs.com/package/@objectstack/spec)
211
+ - [SQL Driver Migration Guide](../sql/MIGRATION.md)
212
+ - [Runtime Integration Docs](../../foundation/core/RUNTIME_INTEGRATION.md)
213
+ - [Driver Interface Documentation](../../foundation/types/src/driver.ts)
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  MongoDB driver for ObjectQL. Supports basic CRUD, filtering, and aggregation pipelines on MongoDB.
4
4
 
5
+ **Now with ObjectStack QueryAST support!** This driver implements both the legacy `Driver` interface and the new `DriverInterface` from `@objectstack/spec` for seamless integration with the ObjectStack ecosystem.
6
+
5
7
  ## Installation
6
8
 
7
9
  ```bash
@@ -24,3 +26,59 @@ const objectql = new ObjectQL({
24
26
  }
25
27
  });
26
28
  ```
29
+
30
+ ## Features
31
+
32
+ - ✅ **100% Backward Compatible** - All existing code continues to work
33
+ - ✅ **QueryAST Support** - Supports the new `@objectstack/spec` query format
34
+ - ✅ **Smart ID Mapping** - Automatic conversion between `id` (API) and `_id` (MongoDB)
35
+ - ✅ **Full-Text Search** - MongoDB text search capabilities
36
+ - ✅ **Array & JSON Fields** - Native BSON support for complex data types
37
+ - ✅ **Aggregation Pipelines** - Native MongoDB aggregation support
38
+
39
+ ## Driver Metadata
40
+
41
+ ```typescript
42
+ console.log(driver.name); // 'MongoDriver'
43
+ console.log(driver.version); // '3.0.1'
44
+ console.log(driver.supports);
45
+ // {
46
+ // transactions: true,
47
+ // joins: false,
48
+ // fullTextSearch: true,
49
+ // jsonFields: true,
50
+ // arrayFields: true
51
+ // }
52
+ ```
53
+
54
+ ## QueryAST Format
55
+
56
+ The driver now supports both legacy and QueryAST formats:
57
+
58
+ ### Legacy Format
59
+ ```typescript
60
+ const results = await driver.find('users', {
61
+ filters: [['age', '>', 18]],
62
+ sort: [['name', 'asc']],
63
+ limit: 10,
64
+ skip: 0
65
+ });
66
+ ```
67
+
68
+ ### QueryAST Format
69
+ ```typescript
70
+ const results = await driver.find('users', {
71
+ filters: [['age', '>', 18]],
72
+ sort: [{ field: 'name', order: 'asc' }],
73
+ top: 10, // Instead of 'limit'
74
+ skip: 0
75
+ });
76
+ ```
77
+
78
+ ## Migration Guide
79
+
80
+ See [MIGRATION.md](./MIGRATION.md) for detailed information about the ObjectStack migration and QueryAST format support.
81
+
82
+ ## License
83
+
84
+ MIT
package/dist/index.d.ts CHANGED
@@ -1,5 +1,63 @@
1
+ import { Data } from '@objectstack/spec';
2
+ type QueryAST = Data.QueryAST;
3
+ /**
4
+ * ObjectQL
5
+ * Copyright (c) 2026-present ObjectStack Inc.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
1
10
  import { Driver } from '@objectql/types';
11
+ /**
12
+ * Command interface for executeCommand method
13
+ */
14
+ export interface Command {
15
+ type: 'create' | 'update' | 'delete' | 'bulkCreate' | 'bulkUpdate' | 'bulkDelete';
16
+ object: string;
17
+ data?: any;
18
+ id?: string | number;
19
+ ids?: Array<string | number>;
20
+ records?: any[];
21
+ updates?: Array<{
22
+ id: string | number;
23
+ data: any;
24
+ }>;
25
+ options?: any;
26
+ }
27
+ /**
28
+ * Command result interface
29
+ */
30
+ export interface CommandResult {
31
+ success: boolean;
32
+ data?: any;
33
+ affected: number;
34
+ error?: string;
35
+ }
36
+ /**
37
+ * MongoDB Driver for ObjectQL
38
+ *
39
+ * Implements both the legacy Driver interface from @objectql/types and
40
+ * the standard DriverInterface from @objectstack/spec for compatibility
41
+ * with the new kernel-based plugin system.
42
+ *
43
+ * The driver internally converts QueryAST format to MongoDB query format.
44
+ */
2
45
  export declare class MongoDriver implements Driver {
46
+ readonly name = "MongoDriver";
47
+ readonly version = "3.0.1";
48
+ readonly supports: {
49
+ transactions: boolean;
50
+ joins: boolean;
51
+ fullTextSearch: boolean;
52
+ jsonFields: boolean;
53
+ arrayFields: boolean;
54
+ queryFilters: boolean;
55
+ queryAggregations: boolean;
56
+ querySorting: boolean;
57
+ queryPagination: boolean;
58
+ queryWindowFunctions: boolean;
59
+ querySubqueries: boolean;
60
+ };
3
61
  private client;
4
62
  private db?;
5
63
  private config;
@@ -8,7 +66,19 @@ export declare class MongoDriver implements Driver {
8
66
  url: string;
9
67
  dbName?: string;
10
68
  });
11
- private connect;
69
+ /**
70
+ * Internal connect method used in constructor
71
+ */
72
+ private internalConnect;
73
+ /**
74
+ * Connect to the database (for DriverInterface compatibility)
75
+ * This method ensures the connection is established.
76
+ */
77
+ connect(): Promise<void>;
78
+ /**
79
+ * Check database connection health
80
+ */
81
+ checkHealth(): Promise<boolean>;
12
82
  private getCollection;
13
83
  private normalizeId;
14
84
  /**
@@ -33,6 +103,15 @@ export declare class MongoDriver implements Driver {
33
103
  * Build a single MongoDB condition from field, operator, and value.
34
104
  */
35
105
  private buildSingleCondition;
106
+ /**
107
+ * Normalizes query format to support both legacy UnifiedQuery and QueryAST formats.
108
+ * This ensures backward compatibility while supporting the new @objectstack/spec interface.
109
+ *
110
+ * QueryAST format uses 'top' for limit, while UnifiedQuery uses 'limit'.
111
+ * QueryAST sort is array of {field, order}, while UnifiedQuery is array of [field, order].
112
+ * QueryAST uses 'aggregations', while legacy uses 'aggregate'.
113
+ */
114
+ private normalizeQuery;
36
115
  find(objectName: string, query: any, options?: any): Promise<any[]>;
37
116
  findOne(objectName: string, id: string | number, query?: any, options?: any): Promise<any>;
38
117
  create(objectName: string, data: any, options?: any): Promise<any>;
@@ -44,4 +123,45 @@ export declare class MongoDriver implements Driver {
44
123
  deleteMany(objectName: string, filters: any, options?: any): Promise<any>;
45
124
  aggregate(objectName: string, pipeline: any[], options?: any): Promise<any[]>;
46
125
  disconnect(): Promise<void>;
126
+ /**
127
+ * Execute a query using QueryAST (DriverInterface v4.0 method)
128
+ *
129
+ * This is the new standard method for query execution using the
130
+ * ObjectStack QueryAST format.
131
+ *
132
+ * @param ast - The QueryAST representing the query
133
+ * @param options - Optional execution options
134
+ * @returns Query results with value and count
135
+ */
136
+ executeQuery(ast: QueryAST, options?: any): Promise<{
137
+ value: any[];
138
+ count?: number;
139
+ }>;
140
+ /**
141
+ * Execute a command (DriverInterface v4.0 method)
142
+ *
143
+ * This method handles all mutation operations (create, update, delete)
144
+ * using a unified command interface.
145
+ *
146
+ * @param command - The command to execute
147
+ * @param options - Optional execution options
148
+ * @returns Command execution result
149
+ */
150
+ executeCommand(command: Command, options?: any): Promise<CommandResult>;
151
+ /**
152
+ * Convert FilterNode (QueryAST format) to legacy filter array format
153
+ * This allows reuse of existing filter logic while supporting new QueryAST
154
+ *
155
+ * @private
156
+ */
157
+ private convertFilterNodeToLegacy;
158
+ /**
159
+ * Execute command (alternative signature for compatibility)
160
+ *
161
+ * @param command - Command string or object
162
+ * @param parameters - Command parameters
163
+ * @param options - Execution options
164
+ */
165
+ execute(command: any, parameters?: any[], options?: any): Promise<any>;
47
166
  }
167
+ export {};