diff --git a/lib/model.js b/lib/model.js index 4784c60..ac9a596 100644 --- a/lib/model.js +++ b/lib/model.js @@ -20,10 +20,6 @@ exports.Model = function(name, store, schema){ store = JSFile((require("settings").dataFolder || "data") + "/" + name); } store.id = name; - if(!store.create){ - // map create to put in case it only implements the WebSimpleDB API - store.create = store.put; - } schema.id = name; schemas[name] = schema; diff --git a/lib/store/memory.js b/lib/store/memory.js index 9a14908..4948fef 100644 --- a/lib/store/memory.js +++ b/lib/store/memory.js @@ -7,23 +7,25 @@ exports.Memory = function(options){ var store = ReadonlyMemory(options); var uniqueKeys = {}; // start with the read-only memory store and add write support - store.put = function(object, metadata){ - var id = object.id = metadata.id || object.id || Math.round(Math.random()*10000000000000); - if("overwrite" in metadata){ - if(metadata.overwrite){ - if(!(id in this.index)){ + store.put = function(object, directives){ + directives = directives || {}; + var id = object.id = directives.id || object.id || Math.round(Math.random()*10000000000000); + var isNew = !(id in this.index); + if("overwrite" in directives){ + if(directives.overwrite){ + if(isNew){ throw new PreconditionFailed(id + " does not exist to overwrite"); } } else{ - if(id in this.index){ + if(!isNew){ throw new PreconditionFailed(id + " exists, and can't be overwritten"); } } } updateIndexes.call(this, id, object); this.index[id] = object; - return id; + return isNew && id; }; store["delete"] = function(id){ updateIndexes.call(this, id); diff --git a/lib/store/sql.js b/lib/store/sql.js index 66e3d8f..f5e9f15 100644 --- a/lib/store/sql.js +++ b/lib/store/sql.js @@ -29,9 +29,9 @@ exports.SQLStore = function(config){ "delete": function(id){ store.executeSql("DELETE FROM " + config.table + " WHERE " + config.idColumn + "=?", [id]); }, - put: function(object, metadata){ - id = metadata.id || object[config.idColumn]; - var overwrite = metadata.overwrite; + put: function(object, directives){ + id = directives.id || object[config.idColumn]; + var overwrite = directives.overwrite; if(overwrite === undefined){ overwrite = this.get(id); } @@ -48,6 +48,7 @@ exports.SQLStore = function(config){ first = false; } } + params.idColumn = config.idColumn; var results = store.executeSql("INSERT INTO " + config.table + " (" + columnsString + ") values (" + valuesPlacement + ")", params); id = results.insertId; object[config.idColumn] = id; @@ -70,8 +71,6 @@ exports.SQLStore = function(config){ sql += " WHERE " + config.idColumn + "=?"; params.push(object[config.idColumn]); store.executeSql(sql, params); - - return id; }, query: function(query, options){ options = options || {};