30 lines
978 B
JavaScript
30 lines
978 B
JavaScript
import mongoose from 'mongoose';
|
|
const { Schema } = mongoose;
|
|
|
|
// Define the main part schema
|
|
const partSchema = new Schema(
|
|
{
|
|
name: { type: String, required: true },
|
|
fileName: { type: String, required: false },
|
|
product: { type: mongoose.Schema.Types.ObjectId, ref: 'product' },
|
|
globalPricing: { type: Boolean, default: true },
|
|
priceMode: { type: String, default: 'margin' },
|
|
amount: { type: Number, required: false },
|
|
margin: { type: Number, required: false },
|
|
vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true },
|
|
file: { type: mongoose.SchemaTypes.ObjectId, ref: 'file', required: false },
|
|
},
|
|
{ timestamps: true }
|
|
);
|
|
|
|
// Add virtual id getter
|
|
partSchema.virtual('id').get(function () {
|
|
return this._id.toHexString();
|
|
});
|
|
|
|
// Configure JSON serialization to include virtuals
|
|
partSchema.set('toJSON', { virtuals: true });
|
|
|
|
// Create and export the model
|
|
export const partModel = mongoose.model('part', partSchema);
|