Ionic sqlite: insert with many to many relationship

Hi,

When i have the following json for example:

var book = {
    title: "title",
    publisher: "publisher",
    author: 
    [
        {
            name: "author 1"
        },
        {
            name: "author 2"
        }
    ]
};

I have a many to many relationship between book and author. so i have three tables:

CREATE TABLE IF NOT EXISTS BOOK(ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE VARCHAR(255) NOT NULL, PUBLISHER VARCHAR(255) NOT NULL)
CREATE TABLE IF NOT EXISTS AUTHOR(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME VARCHAR(255))
CREATE TABLE IF NOT EXISTS BOOK_AUTHOR(BOOK_ID INTEGER NOT NULL REFERENCES BOOK(ID), AUTHOR_ID INTEGER NOT NULL REFERENCES AUTHOR(ID))

I started with the following code:

function insertBook(book) {
    database.transaction(function (transaction) {
        transaction.executeSql(insert_book, [book.title, book.publisher],
            function (transaction, result) {
                insertId = result.insertId:
            });
        }
    }, function (error) {
        // some error appeared
    }, function () {
        // everything ok
    });
}

But i have no idea how to continue from here. I only can insert the authors after i inserted the book because i need the book id. If one author insert fails i need to rallback all authors and the book insert so i need the transaction. I could not find an example for that.

Thx in advance for help.

You should use async/await, while don’t finish the first operation don’t follow. But anyway, if you use promise i think that work.