Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created March 7, 2010 18:21
Show Gist options
  • Save ryanflorence/324521 to your computer and use it in GitHub Desktop.
Save ryanflorence/324521 to your computer and use it in GitHub Desktop.
Hypothetical HTML5 Database Interface with MooTools
// create a database
var myDB = new DB('html5_database_with_mootools',{
adapter: 'html5'
/*
// gears?
adapter: 'gears',
// node.js ?
adapter: 'MySQL'
user: 'root',
password: 'root',
port: '8889'
*/
}).create();
// define models
var Note = new Class({
Extends: DB.Model,
title: DB.String,
content: DB.Text,
authorId: DB.Integer,
relationships: {
belongsTo: [Author, { foreignKey: 'authorId' }]
// belongsTo: Author
},
getLength: function(){
return this.content.length;
}
});
var Author = new Class({
Extends: DB.Model,
name: DB.String,
hasMany: Note
});
// create
var me = new Author({ name: 'Ryan Florence' }).save();
var note = new Note({
author: me,
title: 'This note is awesome',
content: 'Fluid App + Local Database Storage = easy desktop apps for javascript developers'
}).save();
// read
Note.find([1,2,3]);
Note.find('all');
Note.find('all',{
where: "title != 'lame note'",
order: 'title DESC',
limit: 20,
include: Author
});
me.notes.each(function(note, index){
var html = '<h1>' + note.title + '</h1><p>' + note.content + '</p><p>' + note.author.name + '</p>';
new Element('aside',{ html: html }).inject(document.body);
});
// update
me.update('name','rpflo');
someNote.update({
title: 'New title',
body: 'New body'
});
// delete
me.delete();
Note.delete('all'); // same as find
// manipulate
me.name = 'Ryan Peterson Florence';
me.save();
me.set('name','Ryan Florence').save();
// Dreaming
/*
Arel syntax would rule ...
http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/
just returns a query object until records are actually
iterated over, then it executes the query
*/
Note.order('title DESC').limit(20).includes(Author);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment