Skip to content
Browse files

experimental, not working

  • Loading branch information...
1 parent 6a25d16 commit 77910a14450fd4246d0bca2d0e4a62740b3c1b0b @mrjjwright committed
Showing with 103 additions and 4 deletions.
  1. +27 −0 README.md
  2. +32 −0 nosqlite.coffee
  3. +42 −0 nosqlite.js
  4. +1 −2 test/test_nosqlite.coffee
  5. +1 −2 test/test_nosqlite.js
View
27 README.md
@@ -74,6 +74,33 @@ The above function applies the form of the predicate to each member of the array
See the [nosqlite tests](http://github.com/mrjjwright/NoSQLite/blob/master/test/test_nosqlite.coffee) for more info as well as the [docco](http://jashkenas.github.com/docco/) styled docs in the docs directory.
+Web mode
+-------------------
+
+You can start nosqlite in web mode by executing
+
+ nosqlite.listen(5000, delegate)
+
+This only works with nosqlite running in node and will cause node to start a simple node based http server to service requests. The port and delegate are optional. The portdefaults to 5000 and delegate will be explained below.
+
+If you have a node based webserver (e.g. a secure server) already running servicing other requests and you simply want to service NoSQLite web requests you can pass
+
+ nosqlite.web_handler(request, response, delegate)
+
+as a callback to your node webserver, or by invoking it directly, where `request` and `response` are node `http.ServerRequest` and `http.ServerResponse` objects respectively.
+
+Here is how to use the web API.
+
+The API only reads query params and the HTTP post body so you can map it to any url you want to.
+
+Query Params
+
+* __db__ (optional) - All requests by default will be executed against the db passed into NoSQLite when it is created. If the param `db` is supplied then NoSQLite will invoke a method against the named sqlite file using the delegate to resolve the path name to the file (see delegate documentation below).
+
+* __table__ - The table name in SQLite. The NoSQLite API is oriented around one table per object, so you will always be dealing with one table.
+
+
+
Currently Requires
----------------
View
32 nosqlite.coffee
@@ -219,6 +219,38 @@ class NoSQLite
@errobj.column = err.split("no column named ")[1].trim()
else
@errobj.code = UNRECOGNIZED_ERROR
+
+
+ # Web API
+ # --------------------------------------
+ # Starts a webserver on the supplied port to serve http requests
+ # for the instance's associated database.
+ # If NoSQLite has already started a webserver on that port
+ # this method returns silently.
+ listen: (port) ->
+ http: require "http" if not http?
+ obj_json: ""
+ self: this
+ http.createServer( (request, response) ->
+ # Parse the url to see what the user wants to do
+ url: require("url").parse(request.url, true)
+ table: url.query.table
+ request.addListener"data", (data) ->
+ obj_json += data
+ request.addListener "done", ->
+ obj: JSON.parse(obj_json)
+ switch url.query.method
+ when "save"
+ self.save(table, obj, (err, res) ->
+ response.writeHead(200, {"Content-Type": "text/plain"})
+ response.write("Success")
+ response.close();
+ )
+ else
+ response.writeHead(200, {"Content-Type": "text/plain"})
+ response.write(obj_json + "\n")
+ response.close();
+ ).listen(port)
NO_SUCH_TABLE: 0
View
42 nosqlite.js
@@ -246,6 +246,48 @@
return (this.errobj.code = UNRECOGNIZED_ERROR);
}
};
+ // Web API
+ // --------------------------------------
+ // Starts a webserver on the supplied port to serve http requests
+ // for the instance's associated database.
+ // If NoSQLite has already started a webserver on that port
+ // this method returns silently.
+ NoSQLite.prototype.listen = function listen(port) {
+ var http, obj_json, self;
+ if (!(typeof http !== "undefined" && http !== null)) {
+ http = require("http");
+ }
+ obj_json = "";
+ self = this;
+ return http.createServer(function(request, response) {
+ var table, url;
+ // Parse the url to see what the user wants to do
+ url = require("url").parse(request.url, true);
+ table = url.query.table;
+ request.addListener("data", function(data) {
+ return obj_json += data;
+ });
+ return request.addListener("done", function() {
+ var _a, obj;
+ obj = JSON.parse(obj_json);
+ if ((_a = url.query.method) === "save") {
+ return self.save(table, obj, function(err, res) {
+ response.writeHead(200, {
+ "Content-Type": "text/plain"
+ });
+ response.write("Success");
+ return response.close();
+ });
+ } else {
+ response.writeHead(200, {
+ "Content-Type": "text/plain"
+ });
+ response.write(obj_json + "\n");
+ return response.close();
+ }
+ });
+ }).listen(port);
+ };
NO_SUCH_TABLE = 0;
NO_SUCH_COLUMN = 1;
UNRECOGNIZED_ERROR = 99;
View
3 test/test_nosqlite.coffee
@@ -254,5 +254,4 @@ test_listen: ->
db: nosqlite.connect(db_file)
db.listen(5000)
-test_save()
-test_find()
+test_listen()
View
3 test/test_nosqlite.js
@@ -275,6 +275,5 @@
db = nosqlite.connect(db_file);
return db.listen(5000);
};
- test_save();
- test_find();
+ test_listen();
})();

0 comments on commit 77910a1

Please sign in to comment.
Something went wrong with that request. Please try again.