@imjxsx/localdb
Version:
A lightweight MongoDB-style BSON database for Node.js
3 lines (2 loc) • 1.07 kB
JavaScript
import{randomUUID as u}from"node:crypto";class d{documents;constructor(t){this.documents=t}matches(t,e){return Object.entries(e??{}).every(([n,i])=>t[n]===i)}insertOne(t){const e=u(),n={...t,__uuid:e,updatedAt:new Date,createdAt:new Date};return this.documents[e]=n,n}insertMany(t){return t.map(e=>this.insertOne(e))}findOne(t){return Object.values(this.documents).find(e=>this.matches(e,t))??null}find(t){return Object.values(this.documents).filter(e=>this.matches(e,t))}exists(t){return this.findOne(t)!==null}count(t){return this.find(t).length}updateOne(t,e){const n=this.findOne(t);return n?(Object.assign(n,e,{updatedAt:new Date}),n):null}updateMany(t,e){const n=this.find(t);return n.forEach(i=>Object.assign(i,e,{updatedAt:new Date})),n.length}deleteOne(t){for(const[e,n]of Object.entries(this.documents))if(this.matches(n,t))return delete this.documents[e],!0;return!1}deleteMany(t){let e=0;for(const[n,i]of Object.entries(this.documents))this.matches(i,t)&&(delete this.documents[n],e++);return e}}export{d as Collection};
//# sourceMappingURL=collection.js.map