"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mongoCrud = void 0;
const mongoCrud = ({ model, procedure, inputSchema, overrides, }) => {
    const createRecord = procedure
        .meta({ description: `Create a ${model.modelName} record` })
        .input(inputSchema.create)
        .mutation(({ input }) => __awaiter(void 0, void 0, void 0, function* () {
        return model.create(input);
    }));
    const updateRecord = procedure
        .meta({ description: `Update a ${model.modelName} record` })
        .input(inputSchema.update)
        .mutation(({ input }) => __awaiter(void 0, void 0, void 0, function* () {
        const { _id } = input, rest = __rest(input, ["_id"]);
        return model.findByIdAndUpdate(_id, rest, { new: true });
    }));
    const getRecords = procedure
        .meta({ description: `Get ${model.modelName} records` })
        .input(inputSchema.get)
        .query(({ input }) => __awaiter(void 0, void 0, void 0, function* () {
        const { pageIndex, pageSize, filters, _id } = input, rest = __rest(input, ["pageIndex", "pageSize", "filters", "_id"]);
        if (_id) {
            return [(yield model.findById(_id))];
        }
        return model.find(rest);
    }));
    const deleteRecord = procedure
        .meta({ description: `Delete a ${model.modelName} record` })
        .input(inputSchema.delete)
        .mutation(({ input }) => __awaiter(void 0, void 0, void 0, function* () {
        return model.findByIdAndDelete(input._id);
    }));
    return {
        create: (overrides === null || overrides === void 0 ? void 0 : overrides.create) || createRecord,
        get: (overrides === null || overrides === void 0 ? void 0 : overrides.get) || getRecords,
        update: (overrides === null || overrides === void 0 ? void 0 : overrides.update) || updateRecord,
        delete: (overrides === null || overrides === void 0 ? void 0 : overrides.delete) || deleteRecord,
    };
};
exports.mongoCrud = mongoCrud;
//# sourceMappingURL=mongo-crud.js.map