vendredi 17 avril 2015

How to automatically process sqlite export to get JSON that reflect relations DB?

I'm exporting data from a sqlite database (no choice here) to generate a JSON for an AngulaJS app.


JSON export from SQLite


sqlite3 CSV export flatten the relational structure and add duplication:



[
{
"bid": 5, // book id
"aid": 4, // author's id
"tags": 3,
"title": "Jacques le fataliste et son maître",
},
{
"bid": 5,
"aid": 23, // same book another author
"tags": 8, // same book another tag
"title": "Jacques le fataliste et son maître",
}

]


SQLite to JSON command


The sqlite database is available on the github and the command I use to export/convert is:



sqlite3 -csv -header app/data/data.sqlite3 \
"SELECT b.id as bid, title, b.sort as sort_book, a.id as aid, a.sort as sort_author, path, name FROM books as b inner join books_authors_link as b_a ON b.id = b_a.book INNER JOIN authors as a ON a.id = b_a.author" \
| ./node_modules/csvtojson/bin/csvtojson \
> authors-books.json


JSON Goal


I designed the final JSON with my customer in a document-oriented approach:



  • all I need in an object ;

  • references to other resources as

    • an id/primary key for forein key ;

    • a list of _id_s/primary keys for many-to-many relations.




Here is an example: of my goal:



[
{
"id": 2,
"authors": [4, 23],
"tags": [3,8,29,69],
"title": "Jacques le fataliste et son maître",

}

]


If you speak French, here is the github issue from the project


Question


So how can I get this result using a command line pipe or javascript-based tool?


Aucun commentaire:

Enregistrer un commentaire