MongoDB / Mongoose Adapter (Work in Progress)
Hi, I’m currently working on a MongoDB/Mongoose adapter for brackets-manager using the CRUD interface.
The implementation is mostly functional, but still in progress...
If this is something you’re interested in, I’d be happy to refine and contribute a proper MongoDB/Mongoose adapter (or example) to the project once finalized.
Would love to know if this aligns with the project direction or if there are any preferred patterns I should follow.
Current Adapter Implementation
class MongoAdapter {
constructor(models) {
this.models = models;
}
// -----------------------------
// Generate next ID
// -----------------------------
async getNextId(Model) {
const last = await Model
.findOne({}, { id: 1, _id: 0 })
.sort({ id: -1 })
.lean();
return (last?.id ?? -1) + 1;
}
// -----------------------------
// INSERT
// -----------------------------
async insert(table, valueOrValues) {
const Model = this.models[table];
if (Array.isArray(valueOrValues)) {
for (let value of valueOrValues) {
value.id = await this.getNextId(Model);
}
await Model.insertMany(valueOrValues);
return true;
}
const nextId = await this.getNextId(Model);
valueOrValues.id = nextId;
await Model.create(valueOrValues);
return nextId; // return generated id
}
// -----------------------------
// SELECT
// -----------------------------
async select(table, param) {
const Model = this.models[table];
if (param === undefined) {
return await Model.find().lean();
}
if (typeof param === 'number') {
return await Model.findOne({ id: param }).lean();
}
return await Model.find(param).lean();
}
// -----------------------------
// UPDATE
// -----------------------------
async update(table, param, value) {
const Model = this.models[table];
if (typeof param === 'number') {
await Model.updateOne({ id: param }, value);
return true;
}
await Model.updateMany(param, { $set: value });
return true;
}
// -----------------------------
// DELETE
// -----------------------------
async delete(table, filter) {
const Model = this.models[table];
if (!filter) {
await Model.deleteMany({});
return true;
}
await Model.deleteMany(filter);
return true;
}
}
module.exports = MongoAdapter;
Notes
-
Current getNextId uses findOne().sort({ id: -1 })
-
This is not concurrency-safe under parallel inserts
-
Looking for recommended approach for:
- safe numeric ID generation
- expected constraints for
brackets-manager
- whether an official Mongo adapter would be useful
MongoDB / Mongoose Adapter (Work in Progress)
Hi, I’m currently working on a MongoDB/Mongoose adapter for
brackets-managerusing the CRUD interface.The implementation is mostly functional, but still in progress...
If this is something you’re interested in, I’d be happy to refine and contribute a proper MongoDB/Mongoose adapter (or example) to the project once finalized.
Would love to know if this aligns with the project direction or if there are any preferred patterns I should follow.
Current Adapter Implementation
Notes
Current
getNextIdusesfindOne().sort({ id: -1 })This is not concurrency-safe under parallel inserts
Looking for recommended approach for:
brackets-manager