Created
January 28, 2016 08:37
-
-
Save kenjitayama/6c0d04ceb4a50eb63c61 to your computer and use it in GitHub Desktop.
Sample for trying out Lovefield DB Inspector
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>Sample for trying out Lovefield DB Inspector</title> | |
<script type="text/javascript" src="https://rawgithub.com/google/lovefield/8ad30503657a8e52587640678be541415984b8de/dist/lovefield.js"></script> | |
</head> | |
<body> | |
<script> | |
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({enableInspector: true}).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