How to create relationships using the HTML 5 Indexed Database API?

Asked

Viewed 201 times

3

Can anyone tell me how to best relate data using HTML 5’s Indexed Databases? I’ve seen many examples of how the API works, but in none of them have I seen data being related. For example, what relationships would look like 1:N... 1:1... N:N...

  • 2

    It is not possible, but you can store an object as field value, although this does not guarantee integrity, at least it will allow you to navigate between entities.

  • what @Tobymosque says is right, the HTML5 DB is more a nosql than an SQL

1 answer

0

I recommend using Google lib for Indexeddb lovefield.

Example of use:

# pseudo código da query
SELECT * FROM photo
  INNER JOIN album
    ON photo.albumId = album.id
  WHERE album.id = '1'
// Usando o lovefield para executar a query com innerJoin
var schemaBuilder = lf.schema.create('albums', 1);

schemaBuilder.createTable('Album')
  .addColumn('id', lf.Type.INTEGER)
  .addColumn('title', lf.Type.STRING);

schemaBuilder.createTable('Photo')
  .addColumn('photoId', lf.Type.INTEGER)
  .addColumn('title', lf.Type.STRING)
  .addColumn('albumId', lf.Type.INTEGER);

schemaBuilder.connect().then(function(db) {
  var schema = db.getSchema();
  var p = schema.table('Photo');
  var a = schema.table('Album');

  db.select()
    .from(p)
    .innerJoin(a, p.albumId.eq(a.id))
    .where(a.id.eq(1))
    .exec();
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.