'use strict';var fs=require('fs'),m=require('path'),flatted=require('flatted'),promises=require('fs/promises'),r=require('sift'),thenby=require('thenby'),zod=require('zod');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var m__default=/*#__PURE__*/_interopDefault(m);var r__default=/*#__PURE__*/_interopDefault(r);function h(){let o="abcdefghijklmnopqrstuvwxyz0123456789",e=Date.now().toString(36),a=Array.from(crypto.getRandomValues(new Uint8Array(12))).map(t=>o[t%o.length]).join("");return `c${e}${a}`}var f=zod.z.object({id:zod.z.string().cuid(),createdAt:zod.z.coerce.date(),updatedAt:zod.z.coerce.date()});var l=class{constructor(e){this.options=e;}filePath="";data={};operations={};async initialize(){let e=m__default.default.parse(this.options.basePath);if(!e.ext||e.ext!==".json")throw new Error("Base path must be a valid JSON file");let a=m__default.default.join(process.cwd(),e.dir);if(this.filePath=m__default.default.join(a,e.base),fs.existsSync(a)||await promises.mkdir(a,{recursive:!0}),fs.existsSync(this.filePath)){let t=await promises.readFile(this.filePath,"utf-8"),i=flatted.parse(t),n=!1;for(let s of this.options.collections)s.name in i||(this.data[s.name]={},n||(n=!0));this.data={...this.data,...i},n&&await this.saveToFile();}else {let t=this.options.collections.reduce((i,n)=>(i[n.name]={},i),{});await promises.writeFile(this.filePath,flatted.stringify(t)),this.data=t;}for(let t of this.options.collections)this.operations[t.name]=this.createOperations(t);}async saveToFile(){await promises.writeFile(this.filePath,flatted.stringify(this.data));}applySorting(e,a){let t=null;for(let[i,n]of Object.entries(a)){let s=thenby.firstBy(i,{direction:n==="desc"?-1:1});t=t?t.thenBy(i,{direction:n==="desc"?-1:1}):s;}return t?[...e].sort(t):e}createOperations(e){return {create:async a=>{let t=e.schema.parse(a),i=new Date().toISOString(),n={...t,id:h(),createdAt:i,updatedAt:i};return this.data[e.name]||(this.data[e.name]={}),this.data[e.name][n.id]=n,await this.saveToFile(),n},createMany:async a=>{let t=[];for(let i of a){let n=await this.operations[e.name]?.create(i);n&&t.push(n);}return t},findOne:async a=>Object.values(this.data[e.name]||{}).find(r__default.default(a))||null,findMany:async(a,t={})=>{let{sort:i}=t,n=Object.values(this.data[e.name]||{}).filter(r__default.default(a));return i&&(n=this.applySorting(n,i)),n},update:async(a,t)=>{let i=this.data[e.name]||{},n=Object.values(i).find(r__default.default(a));if(!n)return null;let s={...n,...t,updatedAt:new Date().toISOString()},d=await f.merge(e.schema).parseAsync(s);return i[n.id]=d,this.data[e.name]=i,await this.saveToFile(),s},updateMany:async(a,t)=>{let i=this.data[e.name]||{},n=Object.values(i).filter(r__default.default(a)),s=[];for(let d of n){let p=await this.operations[e.name]?.update({id:d.id},t);p&&s.push(p);}return s},delete:async a=>{let t=this.data[e.name]||{},i=Object.values(t).find(r__default.default(a));return i?(delete t[i.id],this.data[e.name]=t,await this.saveToFile(),!0):!1},deleteMany:async a=>{let t=this.data[e.name]||{},i=Object.values(t).filter(r__default.default(a));for(let n of i)delete t[n.id];return this.data[e.name]=t,await this.saveToFile(),i.length},count:async a=>Object.values(this.data[e.name]||{}).filter(r__default.default(a||{})).length}}getCollection(e){let a=this.operations[e];if(!a)throw new Error(`Collection ${e} not found`);return a}};function v(o){return new l(o)}function g(o){return Object.freeze(o)}exports.createBumpy=v;exports.createCollection=g;//# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map