javascript – How to create relationships using HTML 5 Indexed Database API?

Question:

Can anyone tell me the best way to relate data using HTML 5 Indexed Databases? I've seen many examples of how the API works, but none of them have I seen data related. For example, how would 1:N… 1:1… N:N…

Answer:

I recommend using the 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();
});
Scroll to Top