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.
The driver now exposes metadata for ObjectStack compatibility:
const driver = new MongoDriver(config);
console.log(driver.name); // 'MongoDriver'
console.log(driver.version); // '3.0.1'
console.log(driver.supports); // { transactions: true, joins: false, ... }New optional lifecycle methods for DriverInterface compatibility:
// Connect (ensures connection is established)
await driver.connect();
// Check connection health
const healthy = await driver.checkHealth(); // true/false
// Disconnect (existing method)
await driver.disconnect();The driver now supports the new QueryAST format from @objectstack/spec:
const query = {
fields: ['name', 'age'],
filters: [['age', '>', 18]],
sort: [['name', 'asc']],
limit: 10,
skip: 0
};const query = {
object: 'users',
fields: ['name', 'age'],
filters: [['age', '>', 18]],
sort: [{ field: 'name', order: 'asc' }],
top: 10, // Instead of 'limit'
skip: 0
};| Aspect | Legacy Format | QueryAST Format |
|---|---|---|
| Limit | limit: 10 |
top: 10 |
| Sort | [['field', 'dir']] |
[{field, order}] |
The driver uses a normalization layer that automatically converts QueryAST format to the internal format:
private normalizeQuery(query: any): any {
// Converts 'top' → 'limit'
// Handles both sort formats
}This means:
- ✅ Existing code continues to work without changes
- ✅ New code can use QueryAST format
- ✅ Both formats work interchangeably
- ✅ No breaking changes
import { MongoDriver } from '@objectql/driver-mongo';
const driver = new MongoDriver({
url: 'mongodb://localhost:27017',
dbName: 'mydb'
});
// Works as before
const results = await driver.find('users', {
filters: [['active', '=', true]],
sort: [['created_at', 'desc']],
limit: 20
});import { MongoDriver } from '@objectql/driver-mongo';
const driver = new MongoDriver({
url: 'mongodb://localhost:27017',
dbName: 'mydb'
});
// New format
const results = await driver.find('users', {
filters: [['active', '=', true]],
sort: [{ field: 'created_at', order: 'desc' }],
top: 20
});import { ObjectQL } from '@objectql/core';
import { MongoDriver } from '@objectql/driver-mongo';
const app = new ObjectQL({
datasources: {
default: new MongoDriver({
url: 'mongodb://localhost:27017',
dbName: 'mydb'
})
}
});
await app.init();
// The kernel will use QueryAST format internally
const ctx = app.createContext({ userId: 'user123' });
const repo = ctx.object('users');
const users = await repo.find({ filters: [['active', '=', true]] });Comprehensive tests have been added in test/queryast.test.ts:
npm test -- queryast.test.tsTest coverage includes:
- Driver metadata exposure
- Lifecycle methods (connect, checkHealth, disconnect)
- QueryAST format with
topparameter - Object-based sort notation
- Backward compatibility with legacy format
- Mixed format support
- Field mapping (id/_id conversion)
package.json: Added@objectstack/spec@^0.2.0dependencysrc/index.ts:- Added driver metadata properties
- Added
normalizeQuery()method (~45 lines) - Added
connect()andcheckHealth()methods (~25 lines) - Updated
find()to use normalization - Refactored internal
connect()tointernalConnect()
test/queryast.test.ts: New comprehensive test suite (240+ lines)
- Added: ~310 lines (including tests and docs)
- Modified: ~15 lines (method signatures and refactoring)
- Deleted: 0 lines
The MongoDB driver supports:
- Transactions: ✅ Yes
- Joins: ❌ No (MongoDB is document-oriented)
- Full-Text Search: ✅ Yes (MongoDB text search)
- JSON Fields: ✅ Yes (native BSON support)
- Array Fields: ✅ Yes (native array support)
The driver maintains smart ID mapping:
- API uses
idfield - MongoDB uses
_idfield - Automatic bidirectional conversion
- Both
idand_idcan be used in queries for backward compatibility
With MongoDB driver migration complete, the pattern is established for migrating other drivers:
- ✅ SQL Driver (completed)
- ✅ MongoDB Driver (completed)
- 🔜 Memory Driver (recommended next - used for testing)
- 🔜 Other drivers (bulk migration)
100% backward compatible - all existing code using the MongoDB driver will continue to work without any changes. The QueryAST support is additive, not replacing.