JSORM the isomorphic, framework-agnostic Javascript ORM
Nested Writes
You can write a Model
and all of its relationships in a single
request. Keep in mind normal dirty tracking rules still apply - nothing
is sent to the server unless it is dirty.
Typescript
Javascript
let author = new Author()
let comment = new Comment({ author })
let post = new Post({ comments: [comment] })
// post.save({ with: "comments" })
// post.save({ with: ["comments", "blog"] })
post.save({ with: { comments: 'author' }})
var author = new Author();
var comment = new Comment({ author: author });
var post = new Post({ comments: [comment] });
// post.save({ with: "comments" })
// post.save({ with: ["comments", "blog"] })
post.save({ with: { comments: "author" }});
Use model.isMarkedForDestruction = true
to delete the associated
object. Use model.isMarkedForDisassociation = true
to remove the association
without deleting the underlying object:
Typescript
Javascript
let post = (await Post.includes("comments").first()).data
post.comments[0].isMarkedForDestruction = true
post.comments[1].isMarkedForDisassociation = true
// destroys the first comment
// disassociates the second comment
await post.save({ with: "comments" })
Post.includes("comments").first().then(function(response) {
var post = response.data;
post.comments[0].isMarkedForDestruction = true;
post.comments[1].isMarkedForDisassociation = true;
// destroys the first comment
// disassociates the second comment
post.save({ with: "comments" })
});
You may want to send only the id
of the related object to the server - ensuring the models are associated without updating attributes by
accident. Just add .id
to the relationship name:
Typescript
Javascript
post.save({ with: "comments.id" })
post.save({ with: "comments.id" })