0

Using Mongoose and a RESTful API I'd like to have 2 collections (organizations and projects), whereas the Schema of the former should hold a field projectsof type [ObjectId], which references documents of the latter - and vice versa. Creating a new organization (POST /organizations) one could either (a) leave the projects field blank and add project documents later (POST /projects) or (b) create some projects with it already.

The problems for (b):

  1. It seems to be in conflict with RESTfulness, because posting a new organization should have a different API point than posting a new project - but we want to achieve both in one go.

  2. When we create a new organization the field projects should hold an array of ObjectIds referring to the corresponding projects - but these won't have IDs yet.

There are probably many ways to accomplish the goal - i.e. 2 successive POST requests: Projects first, then the organization. But I'd like to learn about best-practice for this szenario.

Oranization Schema:

{
    name: { type: String, required: true },
    address: { type: String },
    // projects = array of ObjectIds referring to Project documents
    projects: { type: [Mongoose.Schema.Types.ObjectId] }
}

Project Schema:

{
    name: { type: String, required: true },
    start: { type: Date },
    end: { type: Date },
    organization: { type: Mongoose.Schema.Types.ObjectId }
}

Provided problem description and code are simplified examples for a bigger project that will have many models/collections with a lot of cross-referencing.

One could have projects as an array of sub-documents of an organization document, but I'd prefer to have the projects as an independent collection, since it should be possible to request the whole collection for a projects view, without having to crawl through the organizations documents.

user1521685
  • 210
  • 2
  • 13

1 Answers1

0

OK, so I found some good ideas in another post (which is about posting multiple items of the same resource type - as opposed to posting one item which includes another item of a different resource type):

@RESTfulness:

resources don't need to map to your database or app models

So, basically I could treat "Organization" as a resource which can include a project and do a POST request /organizations, and server-side I create a new document for the projects collection, then a new document for the organizations collection (where the ObjectId of the project doc is being pushed to the field projects).

The problem with this would be the response, when something goes wrong with updating the DB: The response should hold information of which document creation failed.

Alternatively one could send a POST request /organizations as soon as name and address are filled out in the form. This will result in adding an organization to the organizations collection of the DB. Then, in case a project is added inside the same form, we do another POST request /projects passing the newly created organization id with it. Server-side we create a new project document, then push its id to the projects array of the organization document.

user1521685
  • 210
  • 2
  • 13