"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { ObjectIdUtils: () => ObjectIdUtils, collection: () => collection, mongodb: () => mongodb }); module.exports = __toCommonJS(src_exports); // src/mongo.ts var import_mongodb2 = require("mongodb"); // src/utils/object-id-utils.ts var import_mongodb = require("mongodb"); var ObjectIdUtils; ((ObjectIdUtils2) => { function generate() { return new import_mongodb.ObjectId().toHexString(); } ObjectIdUtils2.generate = generate; function isValid(id) { return import_mongodb.ObjectId.isValid(id); } ObjectIdUtils2.isValid = isValid; function fromString(id) { return new import_mongodb.ObjectId(id); } ObjectIdUtils2.fromString = fromString; function fromStringSafe(id) { return import_mongodb.ObjectId.isValid(id) ? new import_mongodb.ObjectId(id) : id; } ObjectIdUtils2.fromStringSafe = fromStringSafe; function toString(id) { return id.toHexString(); } ObjectIdUtils2.toString = toString; function toStringSafe(id) { if (id instanceof import_mongodb.ObjectId) return id.toHexString(); return id.toString(); } ObjectIdUtils2.toStringSafe = toStringSafe; })(ObjectIdUtils || (ObjectIdUtils = {})); // src/filters/filter-helpers.ts function mapWhereOptionToMongoFilter(where, objectIdKeys) { if ("and" in where) { const and = where.and; return { $and: and.map((it) => mapWhereOptionToMongoFilter(it, objectIdKeys)) }; } else if ("or" in where) { const or = where.or; return { $or: or.map((it) => mapWhereOptionToMongoFilter(it, objectIdKeys)) }; } else { return Object.fromEntries( Object.entries(where).map(([key, value]) => { if (key === "id") { return ["_id", mapFilterToMongoFilter(value, (it) => ObjectIdUtils.fromStringSafe(it))]; } if (objectIdKeys.includes(key)) { return [key, mapFilterToMongoFilter(value, (it) => ObjectIdUtils.fromStringSafe(it))]; } return [key, mapFilterToMongoFilter(value)]; }) ); } } function mapFilterToMongoFilter(filter, transform = (it) => it) { if (typeof filter === "object") { const mongoFilter = {}; if ("eq" in filter) mongoFilter.$eq = transform(filter.eq); if ("ne" in filter) mongoFilter.$ne = transform(filter.ne); if ("regex" in filter) mongoFilter.$regex = new RegExp(filter.regex, filter.flags); if ("gt" in filter) mongoFilter.$gt = transform(filter.gt); if ("gte" in filter) mongoFilter.$gte = transform(filter.gte); if ("lt" in filter) mongoFilter.$lt = transform(filter.lt); if ("lte" in filter) mongoFilter.$lte = transform(filter.lte); if ("in" in filter) mongoFilter.$in = filter.in?.map(transform) ?? []; if ("nin" in filter) mongoFilter.$nin = filter.nin?.map(transform) ?? []; return mongoFilter; } return transform(filter); } // src/repo/mongo-repo-impl.ts function mongoRepoImplFn(db) { return function(options) { const collection2 = db.collection(options.name); const objectIdKeys = options.objectIdKeys ?? []; const toDocument = (entity) => { const result = {}; for (let key in entity) { if (key == "id") result["_id"] = ObjectIdUtils.fromStringSafe(entity["id"]); else if (objectIdKeys.includes(key)) result[key] = ObjectIdUtils.fromStringSafe(entity[key]); else result[key] = entity[key]; } return result; }; const toEntity = (document) => { const entity = {}; for (let key in document) { if (key == "_id") entity["id"] = ObjectIdUtils.toStringSafe(document["_id"]); else if (objectIdKeys.includes(key)) entity[key] = ObjectIdUtils.toStringSafe(document[key]); else entity[key] = document[key]; } return options.documentToEntityMapper?.(entity) ?? entity; }; const nullableToEntity = (document) => { return document == null ? null : toEntity(document); }; const mapWhere = (where) => mapWhereOptionToMongoFilter(where ?? {}, objectIdKeys); return { async findById(id) { return collection2.findOne({ _id: ObjectIdUtils.fromStringSafe(id) }).then(nullableToEntity); }, async findOneBy(where) { return collection2.findOne(mapWhere(where)).then(nullableToEntity); }, async find(options2) { if (options2.limit == 0) return []; let query = collection2.find(mapWhere(options2.where)); if (options2.sort != null) { const r = Object.entries(options2.sort).map(([key, value]) => [key, value]); query = query.sort(r); } if (options2.skip) query = query.skip(options2.skip); if (options2.limit != null) query = query.limit(options2.limit); return query.toArray().then((it) => it.map(toEntity)); }, async insert(entity) { const document = toDocument(entity); await collection2.insertOne(document); }, async insertMany(entity, options2) { const documents = entity.map(toDocument); await collection2.insertMany(documents, { ordered: options2.ordered ?? false }); }, async updateById(id, update) { await collection2.updateOne( { _id: ObjectIdUtils.fromStringSafe(id) }, { $set: update } ); }, async updateOneBy(where, update) { await collection2.updateOne( mapWhere(where), { $set: update } ); }, async updateMany(where, update) { return collection2.updateMany(mapWhere(where), { $set: update }).then((it) => ({ matched: it.matchedCount, modified: it.modifiedCount })); }, async count(where) { return collection2.countDocuments(mapWhere(where)); }, async exists(where) { return collection2.countDocuments(mapWhere(where)).then((it) => it > 0); }, async deleteById(id) { return collection2.deleteOne({ _id: ObjectIdUtils.fromStringSafe(id) }).then((it) => it.deletedCount > 0); }, async deleteBy(where) { return collection2.deleteMany(mapWhere(where)).then((it) => it.deletedCount); }, async aggregate(stages) { return collection2.aggregate(stages).toArray(); }, async *watch(options2) { throw new Error(`Not implemented ${options2}`); } }; }; } // src/mongo.ts var collection = (option) => option; async function mongodb(options) { const client = new import_mongodb2.MongoClient(options.url, options.clientOptions); await client.connect(); const db = client.db(); for (let collectionsKey in options.collections) { const option = options.collections[collectionsKey]; const collection2 = await db.createCollection(option.name, option.createOption); await Promise.all((option.indexes ?? []).map((index) => collection2.createIndex({ [index.key]: index.type }, index.option))); } const repos = { db }; const fn = mongoRepoImplFn(db); for (let collectionsKey in options.collections) { const option = options.collections[collectionsKey]; repos[collectionsKey] = fn(option); } return repos; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ObjectIdUtils, collection, mongodb }); //# sourceMappingURL=index.cjs.map