Files

41 lines
840 B
JavaScript

import Model from "./model.js";
// Create
export const createProduct = async (data) => {
return await Model.create(data);
};
// Read (Get by ID)
export const getProductById = async (id) => {
return await Model.findById(id);
};
// Read (Get by Query)
export const getProductByQuery = async (query) => {
return await Model.findOne(query);
}
// Read (Get all)
export const getAllProducts = async () => {
return await Model.aggregate([
{
$lookup: {
from: "vendors",
localField: "vendors",
foreignField: "_id",
as: "vendors"
}
}
]);
};
// Update
export const updateProduct = async (id, data) => {
return await Model.findByIdAndUpdate(id, data, { new: true });
};
// Aggregate
export const aggregateProduct = async (pipeline) => {
return await Model.aggregate(pipeline);
};