Initialized Project with default middlewares and helpers, connected to Atlas Instance and created Swagger Setup. Created necessary DB models and authentication-authorization logic
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { model, Schema, Types } from "mongoose";
|
||||
|
||||
const schema = new Schema({
|
||||
sku: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
buyingPrice: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
sellingPrice: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
gst: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
stock: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
createdOn: {
|
||||
type: Date,
|
||||
required: true,
|
||||
},
|
||||
createdBy: {
|
||||
type: Types.ObjectId,
|
||||
required: true,
|
||||
ref: "user",
|
||||
},
|
||||
vendors: {
|
||||
type: [Types.ObjectId],
|
||||
required: true,
|
||||
ref: "vendor",
|
||||
},
|
||||
});
|
||||
|
||||
export default model("product", schema);
|
||||
@@ -0,0 +1,31 @@
|
||||
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.find();
|
||||
};
|
||||
|
||||
// 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);
|
||||
};
|
||||
Reference in New Issue
Block a user