jeudi 13 avril 2017

ember-data relationships One-to-Many : good practice?

I'm wondering if I'm populating my database correctly in my ember app.

Basically, I need to import a lot of books and their authors when a button is clicked. To do so, I've create a book and an author model. Then, in my controller, I have an action addBook in which I try to add just one book for the moment.

To simplify things, I consider that a book have one and only one author, but an author can have written multiple books. Thus, this is a One-to-Many relationship.

Here are the models:

models/book.js

import Model from 'ember-pouch/model';
import DS from 'ember-data';

export default Model.extend({
  title: DS.attr('string'),
  author: DS.belongsTo('author')
});

models/author.js

import Model from 'ember-pouch/model';
import DS from 'ember-data';

export default Model.extend({
  name       : DS.attr('string'),
  books       : DS.hasMany('book')
});

In my addBook action, I have attempted 3 things to get to know relationships in ember-data.

Here is my code for the three methods:

Method 1: Create a book and bind it to an author that is already in the db

const book = this.store.createRecord('book',{
  title: this.get('titleinput'),
});

this.store.queryRecord('author',  {filter: {name: this.get('authorinput')}}).then((author) => {
  author.get('books').pushObject(book);
  book.save().then(() => author.save());
});

Method 2: Create a new book and a new author

const book = this.store.createRecord('book',{
  title: this.get('titleinput'),
});

let authorToStore = this.get('store').createRecord('author', {
  name: this.get('authorinput')
});

authorToStore.save().then(()=>{
  this.store.queryRecord('author',  {filter: {name: this.get('authorinput')}}).then((author) => {
    author.get('books').pushObject(book);
    book.save().then(() => author.save());
  });
});

Method 3: Create a book. Then, if the author is already in the db, then bind this book to this author. If it is not the case, create a new author and bind it to the book.

let authorPromise = new Promise((resolve, reject) => {
  let res;
  this.get('store').queryRecord('author', { filter: { name: this.get('authorinput') } }).then((author) => {
    if (author == null){
      res = this.get('store').createRecord('author', {
        name: this.get('authorinput'),
      });
    }
    else{
      res = author;
     }
    resolve(res);
  });
});

let bookToStore = this.get('store').createRecord('book', {
  title: this.get('titleinput')
});

authorPromise.then((author) => {
  bookToStore.save().then(()=>{
    this.store.queryRecord('book',  {filter: {title: this.get('titleinput')}}).then((book) => {
      author.get('books').pushObject(book);
      author.save().then(() => book.save());
    });
  });
});

This seems a lot of code, specially the Method 3 for just a simple relationship. Am I doing this the recommended way?

Maybe it would be more simple to do all the operations in the model and then save the model to the store? Is it possible in this case though?




Aucun commentaire:

Enregistrer un commentaire