Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #5 from rooftopsparrow/auto-id

Auto id generation when no id is supplied
  • Loading branch information...
commit e59ef256fca72d91d014fa5a05a260e738b056ad 2 parents b4c8754 + bb290e2
Pavan Kumar Sunkara authored
8 README.md
Source Rendered
@@ -64,13 +64,17 @@ db.destroySync();
64 64
65 65 ### Create a document
66 66
  67 +If `obj.id` (or `obj._id` ) is not supplied, `obj.id` is auto generated.
  68 +
67 69 ```js
68 70 // Async
69   -db.post(obj, function (err) {
  71 +db.post(obj, function (err, id) {
70 72 if (err) throw err;
  73 + console.log(id);
71 74 });
72 75
73 76 // Sync
  77 +// returns id
74 78 db.postSync(obj);
75 79 ```
76 80
@@ -147,9 +151,9 @@ Here is a list of [Contributors](http://github.com/pksunkara/nosqlite/contributo
147 151
148 152 ### TODO
149 153
  154 +- ~~Auto ID generation~~
150 155 - Authentication system
151 156 - Map-Reduce (views)
152   -- Auto ID generation
153 157 - Buffer writes internally
154 158 - Concurrent writes from multi processes
155 159
21 lib/nosqlite.js
... ... @@ -1,6 +1,6 @@
1 1 // Generated by CoffeeScript 1.6.3
2 2 (function() {
3   - var fs, nosqlite, path, utile;
  3 + var cuid, fs, nosqlite, path, utile;
4 4
5 5 nosqlite = module.exports;
6 6
@@ -10,6 +10,8 @@
10 10
11 11 utile = require('utile');
12 12
  13 + cuid = require('cuid');
  14 +
13 15 nosqlite.path = path.join(__dirname, '..', 'data');
14 16
15 17 nosqlite.Connection = function(arg) {
@@ -108,10 +110,23 @@
108 110 return this._writeSync(id, JSON.stringify(data, null, 2));
109 111 },
110 112 post: function(obj, cb) {
111   - return this._write(obj.id || obj._id, JSON.stringify(obj, null, 2), cb);
  113 + if (!(obj.id || obj._id)) {
  114 + obj.id = cuid();
  115 + }
  116 + return this._write(obj.id || obj._id, JSON.stringify(obj, null, 2), function(err) {
  117 + if (err) {
  118 + return cb(err);
  119 + } else {
  120 + return cb(null, obj.id || obj._id);
  121 + }
  122 + });
112 123 },
113 124 postSync: function(obj) {
114   - return this._writeSync(obj.id || obj._id, JSON.stringify(obj, null, 2));
  125 + if (!(obj.id || obj._id)) {
  126 + obj.id = cuid();
  127 + }
  128 + this._writeSync(obj.id || obj._id, JSON.stringify(obj, null, 2));
  129 + return obj.id || obj._id;
115 130 },
116 131 find: function(cond, cb) {
117 132 var _this = this;
5 package.json
... ... @@ -1,6 +1,6 @@
1 1 {
2 2 "name": "nosqlite",
3   - "version": "0.2.0",
  3 + "version": "0.2.1",
4 4 "author": "Pavan Kumar Sunkara <pavan.sss1991@gmail.com>",
5 5 "description": "human readable nosql type filesystem json store",
6 6 "main": "./lib/nosqlite",
@@ -27,7 +27,8 @@
27 27 }
28 28 ],
29 29 "dependencies": {
30   - "utile": "0.2.x"
  30 + "utile": "0.2.x",
  31 + "cuid": "~1.1.7"
31 32 },
32 33 "devDependencies": {
33 34 "coffee-script": "1.6.x",
7 src/nosqlite.coffee
@@ -10,6 +10,7 @@ nosqlite = module.exports
10 10 path = require 'path'
11 11 fs = require 'fs'
12 12 utile = require 'utile'
  13 +cuid = require 'cuid'
13 14
14 15 # Declaring variables
15 16 nosqlite.path = path.join __dirname, '..', 'data'
@@ -104,10 +105,14 @@ nosqlite.Connection::database = (name, mode) ->
104 105
105 106 # Create doc
106 107 post: (obj, cb) ->
107   - @_write obj.id or obj._id, JSON.stringify(obj, null, 2), cb
  108 + obj.id = cuid() unless obj.id or obj._id
  109 + @_write obj.id or obj._id, JSON.stringify(obj, null, 2), (err) ->
  110 + if err then cb err else cb null, obj.id or obj._id
108 111
109 112 postSync: (obj) ->
  113 + obj.id = cuid() unless obj.id or obj._id
110 114 @_writeSync obj.id or obj._id, JSON.stringify(obj, null, 2)
  115 + obj.id or obj._id
111 116
112 117 # Find a doc
113 118 find: (cond, cb) ->
53 test/database-test.coffee
@@ -170,8 +170,8 @@ vows
170 170 age: 31
171 171 , @callback
172 172
173   - 'should be successful': (err) ->
174   - assert.isUndefined err
  173 + 'should be successful': (err, id) ->
  174 + assert.isNull err
175 175
176 176 .addBatch
177 177 'Database "test"':
@@ -323,6 +323,51 @@ vows
323 323 topic: () ->
324 324 connection.database 'test'
325 325
  326 + 'postSync() without an id':
  327 + topic: (db) ->
  328 + @db = db
  329 + db.postSync
  330 + name: 'randal'
  331 + age: 24
  332 +
  333 + 'should generate one': (id) ->
  334 + assert.isString id
  335 +
  336 + 'should save successfully': (id) ->
  337 + data = @db.getSync id
  338 + assert.equal data.id, id
  339 + assert.equal data.name, 'randal'
  340 + assert.equal data.age, 24
  341 + @db.deleteSync id
  342 +
  343 + .addBatch
  344 + 'Database "test"':
  345 + topic: () ->
  346 + connection.database 'test'
  347 +
  348 + 'post() without an id':
  349 + topic: (db) ->
  350 + @db = db
  351 + db.post
  352 + name: 'randal'
  353 + age: 24
  354 + , @callback
  355 +
  356 + 'should generate one': (err, id) ->
  357 + assert.isString id
  358 +
  359 + 'should save successfully': (err, id) ->
  360 + data = @db.getSync id
  361 + assert.equal data.id, id
  362 + assert.equal data.name, 'randal'
  363 + assert.equal data.age, 24
  364 + @db.deleteSync id
  365 +
  366 + .addBatch
  367 + 'Database "test"':
  368 + topic: () ->
  369 + connection.database 'test'
  370 +
326 371 'find()':
327 372 topic: (db) ->
328 373 db.find age: 35, @callback
@@ -366,8 +411,8 @@ vows
366 411 topic: (db) ->
367 412 db.put 'bob', age: 31, @callback
368 413
369   - 'should be successful': (err) ->
370   - assert.isUndefined err
  414 + 'should be successful': (err, id) ->
  415 + assert.isNull err
371 416
372 417 .addBatch
373 418 'Database "test"':

0 comments on commit e59ef25

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