123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- const ObjectID = require("mongodb").ObjectID;
- const {connect} = require('mm')
- module.exports = async (dbName='hipstagram') => {
- const {Savable, slice} = await connect(dbName)
- async function getModels({id}){
- const SlicedSavable = slice([id, 'user', 'admin'])
- 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 Like extends SlicedSavable {
- constructor(...params){
- super(...params)
- //TODO:
- //entity
- //entityLikesCount?
- }
- static get relations(){
- return {
- }
- }
- }
- SlicedSavable.addClass(Like)
- class Post 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
- }
- static get relations(){
- return {
- images: "posts",
- comments: "post",
- directs: "post",
- collections: "posts"
- }
- }
- }
- SlicedSavable.addClass(Post)
- class Image extends SlicedSavable {
- constructor(...params){
- super(...params)
- //TODO: multer, file data and so
- }
- static get relations(){
- return {
- userAvatar: "avatar", //if it is ava
- posts: "images", //if in post: m2m with posts
- directs: "image", //if in direct: m2o with directs
- }
- }
- }
- SlicedSavable.addClass(Image)
- class Comment 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
- }
- static get relations(){
- return {
- post: "comments",
- answers: "answerTo",
- answerTo: "answers",
- }
- }
- }
- SlicedSavable.addClass(Comment)
- class Direct extends SlicedSavable {
- constructor(...params){
- super(...params)
- }
- static get relations(){
- return {
- to: "incomings",
- attach: "directs",
- likes: "entity",
- }
- }
- }
- SlicedSavable.addClass(Direct)
- class Collection extends SlicedSavable {
- constructor(...params){
- super(...params)
- }
- static get relations(){
- return {
- posts: "collections"
- }
- }
- }
- SlicedSavable.addClass(Collection)
- const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
- return {models: {
- SlicedSavable, ...SlicedSavable.classes
- },
- thisUser}
- }
- return {
- Savable,
- slice,
- getModels
- }
- }
|