How to Structure Mongoose Collections with User Permissed Id's ? (Javascript/MongoDB)

Viewed 71

If I were to have two MongoDB collections: User and Inventory. The User collection would store the user information like name and email while the Inventory collection would store the item list-objects like the number and stock. With that said, if I added user permissions as most modern apps have for each list so that each list is accessible to the appropriate users given the correct permissions. What would the best way to structure my collection or Schema?

Example1: The Inventory collections lists would have an array of allowed user id's and permissions

Example2: The User Collections users would have an array of available list id's and permissions

Example1 Collections:

db.collection.inventory = [{
    id: "123",
    listName: "one",
    items: [{
      number: "1",
      stock: "10"
    }, {
      number: "2",
      stock: "20"
    }, ]
    permittedUsers: [{
      id: "456"
    }, {
      id: "789"
    }, {
      id: "654"
    }]
  },
  {
    ...more lists
  }
]

db.collection.user = [{
  name: "steve",
  id: "456"
}, {
  name: "bill",
  id: "789"
}, {
  name: "jeff",
  id: "654"
},
{
 ...more users
}

]

Example2 Collections:

db.collection.inventory = [{
  id: "123",
  listName: "one",
  items: [{
    number: "1",
    stock: "10"
  }, {
    number: "2",
    stock: "20"
  }, ]
} {
  ...more lists
}]

db.collection.user = [{
    name: "steve",
    id: "456",
    allowedListIds: ["123", "456"]
  }, {
    name: "bill",
    id: "789",
    allowedListIds: ["123", "789"]
  }, {
    name: "jeff",
    id: "654",
    allowedListIds: ["789"]
  },
  { ...more users
  }
]

I realize this can be very subjective and the question is very generalized. But I'm looking to create a source for information that can be useful for the community about schema and collection data structures. I also understand that you have to weigh your options depending on the ease of querying and strength of security, but again this is just a general question for general tips or helpful information.

Thanks! happy early new year

0 Answers
Related