|
| 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) |
0 commit comments