const express = require('express'); const path = require('path'); const RepositorySystem = require('../systems/RepositorySystem'); const Actions = require('../systems/ActionSystem'); class OneToMany { constructor(name, inceptor){ this.name = name; this.scheme = { auto_increment: 1, fields: [ { key: "name", type: "text", keyLength: 200 }, { key: inceptor + "_id", type: "integer", fk: { "table": this.inceptor, "key": "id" } } ] }; this.repository = RepositorySystem.create(this.name, this.scheme); this.inceptor = inceptor; this.inceptorScheme = { fields: [ ] }; this.inceptorRepo = RepositorySystem.create(this.inceptor, this.inceptorScheme); this._init2(); } _init2() { Actions.on('model'+this.inceptor, (model) => { return this.repository.getPage(0,1000) .then((categories) => { return { ...model, [this.name]: { Filter: { type: "onetomany", options: categories.reduce((data,a) => {return {...data, [a.id]: a.name }}, {}), name: this.name, field: this.name, } } } }) }); Actions.on('get'+this.inceptor, (product) => { if(!product.id) return product; return this.repository.raw().where(this.inceptor + '_id', product.id) .then((categories) => { product[this.name] = categories; return product; }); }); Actions.on('post' + this.name, (address, req, res) => { if(!req.user) { res.sendStatus(401); throw new Error("No user on req object"); } console.log("ADDRESS") console.log(address) address[this.inceptor + "_id"] = req.user.id; return address; }); console.log(Actions); } } module.exports = OneToMany;