Created
August 3, 2015 07:23
-
-
Save kenjitayama/97c1250ebfbfe037bbf5 to your computer and use it in GitHub Desktop.
workaround for "LoveField inner join not working in Safari" due to IndexedDB bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Minimal example of using Lovefield</title> | |
<script type="text/javascript" src="https://rawgithub.com/google/lovefield/207765f3dda39a0ad1e350a46f3a6f8edc222e05/dist/lovefield.js"></script> | |
</head> | |
<body> | |
<script> | |
//indexedDB.deleteDatabase('todo'); | |
var schemaBuilder = lf.schema.create('todo', 1); | |
schemaBuilder.createTable('Item'). | |
addColumn('id', lf.Type.INTEGER). | |
addColumn('description', lf.Type.STRING). | |
addColumn('deadline', lf.Type.DATE_TIME). | |
addColumn('done', lf.Type.BOOLEAN). | |
addPrimaryKey(['id']). | |
addIndex('idxDeadline', ['deadline'], false, lf.Order.DESC); | |
schemaBuilder.createTable('Comment'). | |
addColumn('id', lf.Type.INTEGER). | |
addColumn('itemId', lf.Type.INTEGER). | |
addColumn('text', lf.Type.STRING). | |
addPrimaryKey(['id']); | |
var todoDb; | |
var item; | |
var comment; | |
schemaBuilder.connect({storeType: lf.schema.DataStoreType.WEB_SQL}).then(function(db) { | |
todoDb = db; | |
item = db.getSchema().table('Item'); | |
comment = db.getSchema().table('Comment'); | |
var row = item.createRow({ | |
'id': 1, | |
'description': 'Get a cup of coffee', | |
'deadline': new Date(), | |
'done': false | |
}); | |
var commentRow = comment.createRow({ | |
'id': 1, | |
'itemId': 1, | |
'text': 'OK!' | |
}); | |
return db.insertOrReplace().into(item).values([row]).exec(). | |
then(function() { | |
return db.insertOrReplace().into(comment).values([commentRow]).exec(); | |
}); | |
}).then(function() { | |
return todoDb.select(). | |
from(item). | |
innerJoin(comment, comment.itemId.eq(item.id)). | |
where(item.done.eq(false)).exec(); | |
}).then(function(results) { | |
results.forEach(function(row) { | |
console.log(row.Item.description, 'before', row.Item.deadline, row.Comment.text); | |
document.body.textContent = | |
row.Item.description + ' before ' + row.Item.deadline + '. ' + row.Comment.text; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
worked around version of gist https://gist.github.com/kenjitayama/5d4ad3b0a45b7a7af82e