Couchdb best practices for updating related documents

Asked

Viewed 45 times

3

I’m using CouchDB and NodeJs with these examples of documents.

Role {
  _id,
  name
}

User {
  _id,
  email,
  password,
  role: {
    _id,
    name
  }
}

What is the best practice for when I update a Role, also update it within User if the _id of Roles are the same.

This is just one example, in the application will have duplicate documents cases in several parts of the bank, needing this type of update to avoid inconsistencies.

1 answer

1


The couchdb is a little different from relational databases, so the way we build the applications also changes a little. Responding to your case, what I do is create a "roles" template in the Ode backend and another "user". After every time a new user is created it will be written to the Couch bd as json document and will have a role object. If that user is later updated, in couchdb, that will be a new document with a new _rev, that is, to record that update, you will have to pass the complete document, and then if the roles have also been changed, they will be updated. If you need to maintain compatibility with previous roles, you can include a "version" parameter in the scroll object.

Example:

{
_id: "someUser",
"doc_type": "user",
"name": "his name",
"role": {
  "Version": 1,
  "description": "admin"
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.