Created
January 17, 2018 22:27
-
-
Save OrenBochman/f727e67d373002166cac2044932beb37 to your computer and use it in GitHub Desktop.
Minimal example of using Lovefield [lovefield demo] // source https://jsbin.com/borane
This file contains hidden or 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" /> | |
<meta name="description" content="[lovefield demo]"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script> | |
<script src="https://unpkg.com/lovefield"></script> | |
<script src="https://unpkg.com/blockly"></script> | |
<title>Minimal example of using Lovefield</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
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); | |
var todoDb; | |
var item; | |
schemaBuilder.connect().then(function(db) { | |
todoDb = db; | |
item = db.getSchema().table('Item'); | |
var row = item.createRow({ | |
'id': 1, | |
'description': 'Get a cup of coffee', | |
'deadline': new Date(), | |
'done': false | |
}); | |
return db.insertOrReplace().into(item).values([row]).exec(); | |
}).then(function() { | |
return todoDb.select().from(item).where(item.done.eq(false)).exec(); | |
}).then(function(results) { | |
results.forEach(function(row) { | |
console.log(row['description'], 'before', row['deadline']); | |
}); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
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); | |
var todoDb; | |
var item; | |
schemaBuilder.connect().then(function(db) { | |
todoDb = db; | |
item = db.getSchema().table('Item'); | |
var row = item.createRow({ | |
'id': 1, | |
'description': 'Get a cup of coffee', | |
'deadline': new Date(), | |
'done': false | |
}); | |
return db.insertOrReplace().into(item).values([row]).exec(); | |
}).then(function() { | |
return todoDb.select().from(item).where(item.done.eq(false)).exec(); | |
}).then(function(results) { | |
results.forEach(function(row) { | |
console.log(row['description'], 'before', row['deadline']); | |
}); | |
}); | |
</script></body> | |
</html> |
This file contains hidden or 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
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); | |
var todoDb; | |
var item; | |
schemaBuilder.connect().then(function(db) { | |
todoDb = db; | |
item = db.getSchema().table('Item'); | |
var row = item.createRow({ | |
'id': 1, | |
'description': 'Get a cup of coffee', | |
'deadline': new Date(), | |
'done': false | |
}); | |
return db.insertOrReplace().into(item).values([row]).exec(); | |
}).then(function() { | |
return todoDb.select().from(item).where(item.done.eq(false)).exec(); | |
}).then(function(results) { | |
results.forEach(function(row) { | |
console.log(row['description'], 'before', row['deadline']); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment