One to many relationships in CompoundJS

I am new to CompoundJS, and I had a problem setting up a one-to-many relationship with jugglingDB. I use MySQL as a database.

I created two models of the book and the author.

There are many authors in the book.

This is my schema.js(db / schema.js):

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

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

I have established relationships in /Book.js models. This is mine Book.js(models / Book.js):

module.exports = function (compound, Book) {
  Book.hasMany(compound.models.Author,   {as: 'author',  foreignKey: 'authorId'});
};

This is mine Author.js(models / Author.js):

module.exports = function (compound, Author) {
 Author.belongsTo(compound.models.Book, {as: 'books', foreignKey: 'authorId'});
};

The problem is that I cannot create this relationship. When I check the table, the foreign key will not be set in the table.

I remove the relation from the Book.js and Author.js models and put the relation in schema.js itself

After that, schema.js looks like this:

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

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

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

but the result is the same.

Are there any problems in the above code? if so, how can I solve this?

+5
1

, complexjs . .

, , define. var Book = var Author =.

, foreignKey .

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:

. , . jugglingdb docs . , : . DOCS : 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()
})

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()
})
+4

All Articles