One to Many relationship in CompoundJS

丶灬走出姿态 提交于 2019-11-29 07:38:13

It seems that the Author of compoundjs has not implemented Model functionality. For now your relationships should be defined at the end of your schema file.

Also, you are overriding the schemea objects by storing the return value of the define function. Remove var Book = and var Author =.

And, the foreignKey is created automatically.

schema.js:

describe('Book', function () {
    property('title', String);
    property('isbn', String);
    set('restPath', pathTo.books);
});

describe('Author', function () {
    property('name', String);
    set('restPath', pathTo.authors);
});

Book.hasMany(Author, {as: 'author',  foreignKey: 'authorId'});
Author.belongsTo(Book, {as: 'books', foreignKey: 'authorId'});

Update:

OH. Your problem is NOT defining the relationships, but using them. jugglingdb's docs are not very clear on this. In order to establish a relationship, you must use the following format: See the DOCS for more info: https://github.com/1602/jugglingdb

Author.find(id_here_as_string, function(err, author_record){
  book_record = new Book({
    title: 'whatever'
    isbn: 'again whatever here'
  });
  book_record.author(author_record);
  book_record.save()
})

OR

Author.find(id_here_as_string, function(err, author_record){
  book_record = author_record.books.build({
    title: 'whatever'
    isbn: 'again whatever here'
  });
  book_record.save()
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!