const ObjectID = require("mongodb").ObjectID; const {connect} = require('mm') module.exports = async (dbName='shop') => { const {Savable, slice} = await connect(dbName) async function getModels({id}){ const SlicedSavable = slice([id, 'user']) class User extends SlicedSavable { constructor(...params){ super(...params) //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes //cached like count, which incremented and decremented // //following and followers array } static get relations(){ //don't needed due to ___owner in most cases return { avatar : "userAvatar", } } } SlicedSavable.addClass(User) class OwnerSlicedSavable extends SlicedSavable { get owner(){ if (!this.___owner) return this.___owner return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)}) } } class Image extends OwnerSlicedSavable { constructor(...params){ super(...params) } static async fromFileData(fileData){ let image = new Image({}) image.fileData = fileData image.url = `images/${fileData.filename}` image.originalFileName = fileData.originalname await image.save() return image; } async save(...params){ if (this.userAvatar){ if (this.userAvatar._id.toString() !== id){ throw new ReferenceError(`You can't set ava for other user`) } } return await super.save(...params) } static get relations(){ return { userAvatar: "avatar", //if it is ava good: ["images"], //if it is ava category: "image", //if it is ava } } } SlicedSavable.addClass(Image) class Good extends SlicedSavable { constructor(...params){ super(...params) } static get relations(){ return { categories: ["goods"], orderGoods: "good", images: "good" } } static get guestRelations(){ return ["categories", "orderGoods"] } } SlicedSavable.addClass(Good) class Category extends SlicedSavable { constructor(...params){ super(...params) } static get relations(){ return { goods: ["categories"], image: "category", } } static get defaultPermissions(){ return { //savable refs, objectid's, words like 'tags' or 'roles' read: ['owner', 'user'], write: ['owner', 'admin'], create: ['user'], delete: ['admin', 'owner'], /*permission * TODO: permissions for read and write permissions * */ } } } SlicedSavable.addClass(Category) class Order extends OwnerSlicedSavable { constructor(...params){ super(...params) } get total(){ return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => (a.total || a) + b.total, 0))() } static get relations(){ return { orderGoods: "order" } } static get defaultPermissions(){ return { //savable refs, objectid's, words like 'tags' or 'roles' read: ['owner', 'admin'], write: ['owner', 'admin'], create: ['user'], delete: ['admin'], /*permission * TODO: permissions for read and write permissions * */ } } } SlicedSavable.addClass(Order) class OrderGood extends OwnerSlicedSavable { constructor(...params){ super(...params) } get total(){ return this.price*this.count } async save(...params){ if (!this.price && this.good && this.good.price){ this.price = this.good.price } return await super.save(...params) } static get relations(){ return { good: ["orderGoods"], order: ["orderGoods"] } } } SlicedSavable.addClass(OrderGood) const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)}) return {models: { SlicedSavable, ...SlicedSavable.classes }, thisUser} } return { Savable, slice, getModels } }