12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- const MongoClient = require("mongodb").MongoClient;
- 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', 'admin'])
- class User extends SlicedSavable {
- constructor(...params){
- super(...params)
- this.orders = this.orders instanceof Array ? this.orders : (this.orders ? [this.orders] : [])
- }
- static get relations(){
- return {
- orders: "user"
- }
- }
- }
- SlicedSavable.addClass(User)
- class Good extends SlicedSavable {
- constructor(...params){
- super(...params)
- this.categories = this.category instanceof Array ? this.category : (this.category ? [this.category] : [])
- this.orderGoods = this.orderGoods instanceof Array ? this.orderGoods : (this.orderGoods ? [this.orderGoods] : [])
- }
- static get relations(){
- return {
- categories: "goods",
- orderGoods: "good",
- }
- }
- }
- SlicedSavable.addClass(Good)
- class Category extends SlicedSavable {
- constructor(...params){
- super(...params)
- this.goods = this.goods instanceof Array ? this.goods : (this.goods ? [this.goods] : [])
- }
- static get relations(){
- return {
- goods: "categories",
- }
- }
- }
- SlicedSavable.addClass(Category)
- class Order extends SlicedSavable {
- constructor(...params){
- super(...params)
- this.orderGoods = this.orderGoods instanceof Array ? this.orderGoods : (this.orderGoods ? [this.orderGoods] : [])
- }
- static get relations(){
- return {
- user: "orders",
- orderGoods: "order"
- }
- }
- }
- SlicedSavable.addClass(Order)
- class OrderGood extends SlicedSavable {
- constructor(...params){
- super(...params)
- }
- static get relations(){
- return {
- good: "orderGoods",
- order: "orderGoods"
- }
- }
- }
- SlicedSavable.addClass(OrderGood)
- const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
- return {
- SlicedSavable, User, Good, Category, Order, OrderGood
- }
- }
- return {
- Savable,
- slice,
- getModels
- }
- }
|