Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] - Inlcude multiple data entities for a single service #2

Open
go4cas opened this issue Jan 19, 2018 · 10 comments
Open

Comments

@go4cas
Copy link

go4cas commented Jan 19, 2018

At present moleculer-db allows only for a single data model per service. There might be use cases where a service needs to manage multiple data entities (tables in a data schema).

Example:
Service: Access Control Service - a service managing role based access control for the stack.
Data Entities:

  • Roles: Access roles (e.g. administrator, user, super user, etc)
  • Role Groups: Grouping access roles together (e.g. finance, technical, marketing, etc)
  • Functions: System functions that users might have access to (e.g. edit Customer, delete account, etc)
  • Role Functions: Mapping Roles to Functions

Currently, I would have to create a service for each data entity, i.e. role-service, roleGroup-service, function-service, roleRunction-service.

Each of these services will then link to the underlying data table, using the model property of the service, e.g.:

model: {
    name: "roles",
    define: {
        _id: Sequelize.INTEGER,
        title: Sequelize.STRING,
        status: Sequelize.STRING
    }
}

Accessing the model methods:

this.model.findById(ctx.params.id)

It would be great if the model property could accept an array of model objects. Accessing these from the service, would look something like:

this.model.roles.findById(ctx.params.id)
@mradkov
Copy link

mradkov commented Dec 17, 2018

+1 Yeah, this is a key feature, that needs to be added.

@Hexenon
Copy link

Hexenon commented Mar 4, 2019

is it still open?

@icebob
Copy link
Member

icebob commented Mar 4, 2019

Yes, but it won't be implemented in this version of moleculer-db.

@bytesoverflow
Copy link
Contributor

Im happy to take a look at implementing this. I don't think it's too difficult but we would need to decide how to change the api for update / find / delete etc.

@icebob do you have any ideas on how the interface should work?

@b0bben
Copy link

b0bben commented Mar 2, 2020

Any news on this?

@d0whc3r
Copy link

d0whc3r commented May 8, 2020

any workaround? do I need to create 1 service per table and communicate with each other?

@JS-AK
Copy link
Contributor

JS-AK commented May 20, 2022

with modules
moleculer-db
moleculer-db-adapter-mongoose

u can make one service with multiple tables like this

const DbService = require("moleculer-db");
const MongooseAdapter = require("moleculer-db-adapter-mongoose");
const { MONGO_DB_URI } = process.env

const User = require("../model/user.model");
const Post = require("../model/post.model");

/*
...
*/


module.exports = {
  adapter: new MongooseAdapter(MONGO_DB_URI),
  mixins: [DbService],
  model: {
    User,
    Post,
  },
  
  
  // and handle in this service in customHandle action
  
  actions: {
    customHandle: {
      async handler(ctx) {
        const { User, Post } = this.adapter.model;
        const [users, posts] = await Promise.all([User.find(), Post.find()]
        return { users, posts };
    }
  }
}

@cavinpabua
Copy link

cavinpabua commented Aug 25, 2022

Can we have a sneak peek with what's inside:
const User = require("../model/user.model");

I'm having the same problem. @JS-AK

@truesteps
Copy link

Is it possible with the sequelize adapter? Maybe show us what's inside the .model file?

@JS-AK
Copy link
Contributor

JS-AK commented Mar 25, 2023

Can we have a sneak peek with what's inside: const User = require("../model/user.model");

I'm having the same problem. @JS-AK

const mongoose = require("mongoose");

const schema = new mongoose.Schema({
	email: {type: String},
	firstName: {type: String},
}, {timestamps: true});

module.exports = mongoose.model("User", schema);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants