zumito-db
Version:
Multi-driver database abstraction layer with decorator-based models
67 lines (66 loc) • 1.98 kB
JavaScript
import { QueryBuilder } from './QueryBuilder.js';
export class Repository {
driver;
metadata;
constructor(driver, metadata) {
this.driver = driver;
this.metadata = metadata;
}
get collection() {
return this.metadata.collection;
}
async find(where) {
const ir = {
collection: this.metadata.collection,
type: 'find',
where: where ? this.toWhereClauses(where) : [],
};
return this.driver.find(this.metadata.collection, ir);
}
async findOne(where) {
const ir = {
collection: this.metadata.collection,
type: 'findOne',
where: this.toWhereClauses(where),
};
return this.driver.findOne(this.metadata.collection, ir);
}
async insert(data) {
return this.driver.insert(this.metadata.collection, data);
}
async update(where, data) {
const ir = {
collection: this.metadata.collection,
type: 'update',
where: this.toWhereClauses(where),
};
return this.driver.update(this.metadata.collection, ir, data);
}
async delete(where) {
const ir = {
collection: this.metadata.collection,
type: 'delete',
where: this.toWhereClauses(where),
};
return this.driver.delete(this.metadata.collection, ir);
}
async count(where) {
const ir = {
collection: this.metadata.collection,
type: 'count',
where: where ? this.toWhereClauses(where) : [],
};
return this.driver.count(this.metadata.collection, ir);
}
query() {
return new QueryBuilder(this.driver, this.metadata.collection);
}
toWhereClauses(where) {
return Object.entries(where).map(([field, value]) => ({
field,
operator: 'eq',
value,
logic: 'and',
}));
}
}