I'm developing a solution in Typescript and mongoose. I have developed a Category model…
Jump to turn (9)
- 1I'm developing a solution in Typescript and mongoose. I have developed a Categor
- 2Ok, now I need a function that takes a categoryId and a new order for the catego
- 3This is a good starting point. But can you perform this actions directly on db?
- 4This order logic will work also for the subcategories?
- 5I would see an example of the previous function setting the order of subcategori
- 6How can I query the categories keeping the order ?
- 7Do you have some suggestion to improve this function? sync function updateCatego
- 8Apply this suggestions to the code
- 9Continue to improve the function code, applying directly the improvements on the
- User
I'm developing a solution in Typescript and mongoose. I have developed a Category model like that
interface Category { _id: ObjectId; name: string; flow: CategoryFlow; immutable: boolean; companyId: ObjectId; parentId?: ObjectId; transliteratedName?: string; isDemo?: boolean; createdAt: Date; updatedAt: Date; }
enum CategoryFlow { INCOMING = 'INCOMING', OUTGOING = 'OUTGOING', }
const CategorySchema = new Schema({ name: { type: String, required: true, }, flow: { type: String, enum: [ CategoryFlow.INCOMING, CategoryFlow.OUTGOING, ], required: true, }, immutable: { type: Boolean, required: true, default: false, }, companyId: { type: Types.ObjectId, ref: 'Company', required: true, }, parentId: { type: Types.ObjectId, ref: 'Category', }, transliteratedName: { type: String, }, isDemo: { type: Boolean, }, }, {timestamps: {createdAt: true, updatedAt: true}});
export const CategoryModel = model('Category', CategorySchema);
The parentId field indicates the parent of the category. In this way we create a relationship father => child between the categories. Take in mind that a child category cannot be father of another category.
Now i want to add to this model a field that will keep the order of the categories.