Last active
August 29, 2015 14:17
-
-
Save formula1/be05b92825231e87f06a to your computer and use it in GitHub Desktop.
Waterline browser proof of concept
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
node_modules | |
npm-debug.log |
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 Waterline = require("waterline"); | |
var $ = require("jquery"); | |
var MemoryAdapter = require("sails-memory"); | |
var db = new Waterline(); | |
var Something = Waterline.Collection.extend({ | |
tableName: 'something', | |
schema: true, | |
connection: "meh", | |
attributes: { | |
date:{ | |
type: "integer", | |
defaultsTo: Date.now | |
}, | |
random:{ | |
type: "float", | |
defaultsTo: Math.random | |
}, | |
}, | |
afterCreate:function(values,next){ | |
$("ul").append("<li>Random: "+values.random+", Date: "+values.date+"</li>"); | |
} | |
}); | |
var config = { | |
adapters:{ temp:MemoryAdapter }, | |
connections: { | |
meh: { | |
adapter: "temp", | |
type: "json", // expected response type | |
} | |
} | |
}; | |
db.loadCollection(Something); | |
db.initialize(config, function(err, db) { | |
if(err) return $("body").prepend("<h1>"+err+"</h1>"); | |
$("button").click(function(){ | |
db.collections.something.create({},function(err,value){ | |
if(err) throw err; | |
}); | |
}) | |
}); |
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> | |
<script type="text/javascript" src="api.js" ></script> | |
</head> | |
<body> | |
<ul></ul> | |
<button>Click to add something</button> | |
</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
{ | |
"name": "waterline-browser-proof", | |
"version": "1.0.0", | |
"description": "Proof of concept for waterline in the browser", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "formula1 (https://github.com/formula1)", | |
"license": "ISC", | |
"dependencies": { | |
"browserify": "^9.0.3", | |
"disc": "^1.3.2", | |
"express": "^4.12.2", | |
"jquery": "^2.1.3", | |
"sails-memory": "^0.10.3", | |
"uglifyify": "^3.0.1", | |
"waterline": "^0.10.18" | |
} | |
} |
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 server = require("express")(); | |
var disc = require("disc"); | |
var browserify = require("browserify"); | |
var path = require("path"); | |
var b = browserify({ | |
fullPaths: true | |
}).add(path.normalize(__dirname+"/browser.js")) | |
.ignore("prompt") | |
.transform({ | |
global: true | |
}, 'uglifyify') | |
.bundle(function(err,buff){ | |
b = buff; | |
server.listen(3000); | |
}); | |
server.get("/", function(req,res){ | |
res.sendFile(__dirname+"/index.html"); | |
}); | |
server.get("/api.js",function(req,res){ | |
res.end(b); | |
}) | |
server.get("/raw.js",function(req,res){ | |
browserify(path.normalize(__dirname+"/browser.js")).bundle().pipe(res); | |
}); | |
server.get("/fresh",function(req,res){ | |
browserify({ | |
fullPaths: true | |
}) | |
.add(path.normalize(__dirname+"/browser.js")) | |
.ignore("prompt") | |
.transform({ | |
global: true | |
}, 'uglifyify') | |
.bundle().pipe(disc()).pipe(res); | |
}) | |
server.get("/disk",function(req,res,next){ | |
disc.bundle(b, function(err,html){ | |
if(err) return next(err); | |
res.end(html); | |
}) | |
}); | |
server.use(function(req,res){ | |
res.redirect("/"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment