forked from TomDoesTech/Testing-Express-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct.model.ts
More file actions
39 lines (34 loc) · 999 Bytes
/
Copy pathproduct.model.ts
File metadata and controls
39 lines (34 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import mongoose from "mongoose";
import { customAlphabet } from "nanoid";
import { UserDocument } from "./user.model";
const nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyz0123456789", 10);
export interface ProductDocument extends mongoose.Document {
user: UserDocument["_id"];
productId: string;
title: string;
description: string;
price: number;
image: string;
createdAt: Date;
updatedAt: Date;
}
const productSchema = new mongoose.Schema(
{
productId: {
type: String,
required: true,
unique: true,
default: () => `product_${nanoid()}`,
},
user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
title: { type: String, required: true },
description: { type: String, required: true },
price: { type: Number, required: true },
image: { type: String, required: true },
},
{
timestamps: true,
}
);
const ProductModel = mongoose.model<ProductDocument>("Product", productSchema);
export default ProductModel;