allow changing id
This commit is contained in:
		
							parent
							
								
									4a2dde22c8
								
							
						
					
					
						commit
						c825e7af06
					
				@ -81,10 +81,10 @@ API
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
It's kinda CRUDdy... but don't let that scare you.
 | 
					It's kinda CRUDdy... but don't let that scare you.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* `upsert(id, data)` - creates or updates based on existence in DB (use this)
 | 
					* `upsert(id, data[, oldId])` - creates or updates based on existence in DB (use this)
 | 
				
			||||||
  * modifies `createdAt` and or `updatedAt`
 | 
					  * modifies `createdAt` and or `updatedAt`
 | 
				
			||||||
* `create(id, obj)` - same as above, but fails if the object exists
 | 
					* `create(id, obj)` - same as above, but fails if the object exists
 | 
				
			||||||
* `save(data)` - (just don't use this, please) creates or updates based on presence of ID
 | 
					* `save(data[, oldId])` - (just don't use this, please) creates or updates based on presence of ID
 | 
				
			||||||
* `destroy(id)` - mark a record as `deletedAt` from DB
 | 
					* `destroy(id)` - mark a record as `deletedAt` from DB
 | 
				
			||||||
* `get(id)` - grab one by id
 | 
					* `get(id)` - grab one by id
 | 
				
			||||||
* `find(attrs, opts)` - grab many by indexable attributes
 | 
					* `find(attrs, opts)` - grab many by indexable attributes
 | 
				
			||||||
 | 
				
			|||||||
@ -303,13 +303,13 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
      }).then(simpleParse);
 | 
					      }).then(simpleParse);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DB.upsert = function (id, data) {
 | 
					    DB.upsert = function (id, data, oldId) {
 | 
				
			||||||
      if (!data) {
 | 
					      if (!data) {
 | 
				
			||||||
        data = id;
 | 
					        data = id;
 | 
				
			||||||
        id = data[idnameCased];
 | 
					        id = data[idnameCased];
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return DB.set(id, data).then(function (result) {
 | 
					      return DB.set(oldId || id, data, oldId).then(function (result) {
 | 
				
			||||||
        var success = result.changes >= 1;
 | 
					        var success = result.changes >= 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (success) {
 | 
					        if (success) {
 | 
				
			||||||
@ -320,8 +320,8 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
      });
 | 
					      });
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DB.save = function (data) {
 | 
					    DB.save = function (data, oldId) {
 | 
				
			||||||
      if (!data[idnameCased]) {
 | 
					      if (!data[idnameCased] && !oldId) {
 | 
				
			||||||
        // NOTE saving the id both in the object and the id for now
 | 
					        // NOTE saving the id both in the object and the id for now
 | 
				
			||||||
        var UUID = require('node-uuid');
 | 
					        var UUID = require('node-uuid');
 | 
				
			||||||
        data[idnameCased] = UUID.v4();
 | 
					        data[idnameCased] = UUID.v4();
 | 
				
			||||||
@ -331,7 +331,7 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return DB.set(data[idnameCased], data).then(function (result) {
 | 
					      return DB.set(oldId || data[idnameCased], data, oldId).then(function (result) {
 | 
				
			||||||
        var success = result.changes >= 1;
 | 
					        var success = result.changes >= 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (success) {
 | 
					        if (success) {
 | 
				
			||||||
@ -396,7 +396,7 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // pull indices from object
 | 
					    // pull indices from object
 | 
				
			||||||
    function strainUpdate(id, data/*, vals*/, cb) {
 | 
					    function strainUpdate(id, data/*, vals*/, cb, oldId) {
 | 
				
			||||||
      var fieldable = [];
 | 
					      var fieldable = [];
 | 
				
			||||||
      var json;
 | 
					      var json;
 | 
				
			||||||
      var sql;
 | 
					      var sql;
 | 
				
			||||||
@ -441,7 +441,9 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
        delete data[camelCase(col.name)];
 | 
					        delete data[camelCase(col.name)];
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      delete data[idnameCased];
 | 
					      if (!oldId) {
 | 
				
			||||||
 | 
					        delete data[idnameCased];
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (!fieldable.length || Object.keys(data).length) {
 | 
					      if (!fieldable.length || Object.keys(data).length) {
 | 
				
			||||||
        json = JSON.stringify(data);
 | 
					        json = JSON.stringify(data);
 | 
				
			||||||
@ -467,7 +469,7 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
      return sql;
 | 
					      return sql;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DB.set = function (id, obj) {
 | 
					    DB.set = function (id, obj, oldId) {
 | 
				
			||||||
      obj.updatedAt = Date.now();
 | 
					      obj.updatedAt = Date.now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var json = JSON.stringify(obj);
 | 
					      var json = JSON.stringify(obj);
 | 
				
			||||||
@ -485,7 +487,7 @@ function wrap(db, dir, dbsMap) {
 | 
				
			|||||||
        //var vals = [];
 | 
					        //var vals = [];
 | 
				
			||||||
        // removes known fields from data
 | 
					        // removes known fields from data
 | 
				
			||||||
        data.updated_at = Date.now();
 | 
					        data.updated_at = Date.now();
 | 
				
			||||||
        var sql = strainUpdate(id, data/*, vals*/, sqlTpl);
 | 
					        var sql = strainUpdate(id, data/*, vals*/, sqlTpl, oldId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //console.log('[debug] DB.set() sql:', sql);
 | 
					        //console.log('[debug] DB.set() sql:', sql);
 | 
				
			||||||
        db.run(sql, /*vals*/[], function (err) {
 | 
					        db.run(sql, /*vals*/[], function (err) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user