Short Description of the Feature
Implement a MongoDB adapter (MongoBatchEntityReader and MongoBatchEntityWriter) for BatchJS-Data. This is the first NoSQL adapter and establishes the pattern for NoSQL implementations: database-specific query syntax, flexible pagination, and no transactions.
Expected Benefits
- Most popular NoSQL database: MongoDB support expands the user base significantly.
- Document flexibility: Users can work with nested JSON documents easily.
- Native patterns: Uses MongoDB-native filters (
{ age: { $gt: 18 } }) and operations (insertMany(), bulkWrite()).
Acceptance Criteria
Documentation
API Design
MongoBatchEntityReader
export interface MongoBatchEntityReaderOptions<T> {
client: MongoClient; // MongoDB client instance
database: string; // Database name
collection: string; // Collection name
filter: Filter<T>; // MongoDB filter JSON (e.g., { age: { $gt: 18 } })
rowToEntity: (doc: any) => T; // Convert MongoDB document to entity
batchSize: number; // Batch size
}
export class MongoBatchEntityReader<T> extends AbstractNoSQLBatchEntityReaderStream<T> {
constructor(options: MongoBatchEntityReaderOptions<T>);
}
MongoBatchEntityWriter
export interface MongoBatchEntityWriterOptions<T> {
client: MongoClient; // MongoDB client instance
database: string; // Database name
collection: string; // Collection name
entityToDocument: (entity: T) => any; // Convert entity to MongoDB document
batchSize: number; // Batch size
}
export class MongoBatchEntityWriter<T> extends AbstractNoSQLBatchEntityWriterStream<T> {
constructor(options: MongoBatchEntityWriterOptions<T>);
}
Usage Example
import { MongoClient } from "mongodb";
import { MongoBatchEntityReader, MongoBatchEntityWriter } from "batchjs-data/mongodb";
const client = new MongoClient("mongodb://localhost:27017");
class UserBatchReader extends MongoBatchEntityReader<UserDTO> {
constructor(batchSize: number) {
super({
batchSize,
client,
database: "myapp",
collection: "users",
filter: { age: { $gt: 18 } },
rowToEntity: (doc) => ({ id: doc._id.toString(), username: doc.name, age: doc.age })
});
}
}
class UserBatchWriter extends MongoBatchEntityWriter<UserDTO> {
constructor(batchSize: number) {
super({
batchSize,
client,
database: "myapp",
collection: "users",
entityToDocument: (entity) => ({ _id: entity.id, name: entity.username, age: entity.age })
});
}
}
Pagination Implementation
await collection
.find(this.filter)
.skip(this.entitiesRead)
.limit(size)
.toArray()
Batch Write Implementation
await collection.insertMany(chunk.map(this.entityToDocument));
Driver Compatibility
- mongodb version: ^5.0.0 || ^6.0.0
- MongoDB versions: 4.4, 5.0, 6.0, 7.0 (as supported by driver)
- Connection handling: Uses
client.db(database).collection(collection) pattern
Additional Comments
- MongoDB documents have
_id fields. Users should handle this in rowToEntity/entityToDocument.
skip()/limit() pagination becomes inefficient for large offsets. Consider using cursor-based pagination as an alternative in future versions.
- No transaction support —
insertMany() is not transactional in stand-alone mode (only in replica sets with transactions enabled). The adapter documents this as best-effort batch write.
Feature Request Checklist
Short Description of the Feature
Implement a MongoDB adapter (
MongoBatchEntityReaderandMongoBatchEntityWriter) for BatchJS-Data. This is the first NoSQL adapter and establishes the pattern for NoSQL implementations: database-specific query syntax, flexible pagination, and no transactions.Expected Benefits
{ age: { $gt: 18 } }) and operations (insertMany(),bulkWrite()).Acceptance Criteria
src/mongodb/classes/MongoBatchEntityReader.tsimplementsAbstractNoSQLBatchEntityReaderStreamusing the official MongoDB Node.js driver.src/mongodb/classes/MongoBatchEntityWriter.tsimplementsAbstractNoSQLBatchEntityWriterStreamusing the official MongoDB Node.js driver.skip(offset).limit(size).MongoClientpassed in options.collection.insertMany()orcollection.bulkWrite().mongodbis an optional peer dependency inpackage.json(>=5.x || >=6.x).MongoBatchEntityReaderOptions<T>andMongoBatchEntityWriterOptions<T>interfaces exist.src/mongodb/index.ts.npm run docs:mongodbgeneratesdocs/mongodb-api.md.testcontainersfor MongoDB).README.mdincludes a MongoDB usage example.batchjs-data/mongodb.Documentation
API Design
MongoBatchEntityReader
MongoBatchEntityWriter
Usage Example
Pagination Implementation
Batch Write Implementation
Driver Compatibility
client.db(database).collection(collection)patternAdditional Comments
_idfields. Users should handle this inrowToEntity/entityToDocument.skip()/limit()pagination becomes inefficient for large offsets. Consider usingcursor-based pagination as an alternative in future versions.insertMany()is not transactional in stand-alone mode (only in replica sets with transactions enabled). The adapter documents this as best-effort batch write.Feature Request Checklist