Commit 1b3203be authored by Romain Courteaud's avatar Romain Courteaud

Add UuidStorage to generate new id when calling jio.post

parent c287286c
...@@ -171,6 +171,7 @@ module.exports = function (grunt) { ...@@ -171,6 +171,7 @@ module.exports = function (grunt) {
'src/jio.js', 'src/jio.js',
'src/jio.storage/uuidstorage.js',
'src/jio.storage/memorystorage.js', 'src/jio.storage/memorystorage.js',
'src/jio.storage/localstorage.js', 'src/jio.storage/localstorage.js',
'src/jio.storage/davstorage.js', 'src/jio.storage/davstorage.js',
......
...@@ -131,25 +131,6 @@ ...@@ -131,25 +131,6 @@
} }
util.deepClone = deepClone; util.deepClone = deepClone;
/**
* An Universal Unique ID generator
*
* @return {String} The new UUID.
*/
function generateUuid() {
function S4() {
return ('0000' + Math.floor(
Math.random() * 0x10000 /* 65536 */
).toString(16)).slice(-4);
}
return S4() + S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + S4() + S4();
}
util.generateUuid = generateUuid;
function readBlobAsText(blob, encoding) { function readBlobAsText(blob, encoding) {
...@@ -326,12 +307,23 @@ ...@@ -326,12 +307,23 @@
return argument_list[0]._id; return argument_list[0]._id;
}); });
// listeners JioProxyStorage.prototype.post = function (param) {
declareMethod(JioProxyStorage, "post", function (param, storage, method_name) { var context = this;
if (param._id !== undefined) { return new RSVP.Queue()
return checkId(param, storage, method_name); .push(function () {
} if (param._id === undefined) {
}); var storage_method = context.__storage.post;
if (storage_method === undefined) {
throw new jIO.util.jIOError(
"Capacity 'post' is not implemented on '" + context.__type + "'",
501
);
}
return context.__storage.post(param);
}
return context.put(param);
});
};
declareMethod(JioProxyStorage, 'putAttachment', function (param, storage, method_name) { declareMethod(JioProxyStorage, 'putAttachment', function (param, storage, method_name) {
checkId(param, storage, method_name); checkId(param, storage, method_name);
......
/*jslint nomen: true*/
(function (jIO) {
"use strict";
/**
* The jIO UUIDStorage extension
*
* @class UUIDStorage
* @constructor
*/
function UUIDStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
UUIDStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.post = function (param) {
function S4() {
return ('0000' + Math.floor(
Math.random() * 0x10000 /* 65536 */
).toString(16)).slice(-4);
}
param._id = S4() + S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + S4() + S4();
return this.put(param);
};
UUIDStorage.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.getAttachment = function () {
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.putAttachment = function () {
return this._sub_storage.putAttachment.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.removeAttachment = function () {
return this._sub_storage.removeAttachment.apply(this._sub_storage,
arguments);
};
UUIDStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.hasCapacity(name);
};
UUIDStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage,
arguments);
};
jIO.addStorage('uuid', UUIDStorage);
}(jIO));
...@@ -40,8 +40,8 @@ ...@@ -40,8 +40,8 @@
return param._id; return param._id;
}; };
Storage200.prototype.post = function (param) { Storage200.prototype.post = function (param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "post 200 called"); deepEqual(param, {"title": "foo"}, "post 200 called");
return param._id; return "bar";
}; };
Storage200.prototype.hasCapacity = function () { Storage200.prototype.hasCapacity = function () {
return true; return true;
...@@ -302,7 +302,7 @@ ...@@ -302,7 +302,7 @@
}] }]
}); });
jio.post({"_id": "bar", "title": "foo"}) jio.post({"title": "foo"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
......
/*jslint nomen: true*/
/*global Blob*/
(function (jIO, QUnit, Blob) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module,
throws = QUnit.throws;
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
/////////////////////////////////////////////////////////////////
function Storage200() {
return this;
}
jIO.addStorage('uuidstorage200', Storage200);
/////////////////////////////////////////////////////////////////
// uuidStorage.constructor
/////////////////////////////////////////////////////////////////
module("uuidStorage.constructor");
test("create substorage", function () {
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "uuidstorage200");
});
/////////////////////////////////////////////////////////////////
// uuidStorage.get
/////////////////////////////////////////////////////////////////
module("uuidStorage.get");
test("get called substorage get", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
Storage200.prototype.get = function (param) {
equal(param._id, "bar", "get 200 called");
return {title: "foo"};
};
jio.get({"_id": "bar"})
.then(function (result) {
deepEqual(result, {
"_id": "bar",
"title": "foo"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.post
/////////////////////////////////////////////////////////////////
module("uuidStorage.post");
test("post called substorage put with a new id", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
}),
uuid;
function isUuid(uuid) {
var x = "[0-9a-fA-F]";
if (typeof uuid !== "string") {
return false;
}
return (uuid.match(
"^" + x + "{8}-" + x + "{4}-" +
x + "{4}-" + x + "{4}-" + x + "{12}$"
) === null ? false : true);
}
Storage200.prototype.put = function (param) {
uuid = param._id;
deepEqual(param, {"_id": uuid, "title": "foo"}, "post 200 called");
return "bar";
};
jio.post({"title": "foo"})
.then(function (result) {
equal(result, uuid);
ok(isUuid(uuid), uuid);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.put
/////////////////////////////////////////////////////////////////
module("uuidStorage.put");
test("put called substorage put", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
Storage200.prototype.put = function (param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 200 called");
return param._id;
};
jio.put({"_id": "bar", "title": "foo"})
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.remove
/////////////////////////////////////////////////////////////////
module("uuidStorage.remove");
test("remove called substorage remove", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
Storage200.prototype.remove = function (param) {
deepEqual(param, {"_id": "bar"}, "remove 200 called");
return param._id;
};
jio.remove({"_id": "bar"})
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("uuidStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
}),
blob = new Blob([""]);
Storage200.prototype.getAttachment = function (param) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"},
"getAttachment 200 called");
return {data: blob};
};
jio.getAttachment({"_id": "bar", "_attachment": "foo"})
.then(function (result) {
equal(result.data, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("uuidStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
}),
blob = new Blob([""]);
Storage200.prototype.putAttachment = function (param) {
deepEqual(param, {"_id": "bar", "_attachment": "foo", "_blob": blob},
"putAttachment 200 called");
return "OK";
};
jio.putAttachment({"_id": "bar", "_attachment": "foo", "_blob": blob})
.then(function (result) {
equal(result, "OK");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("uuidStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
Storage200.prototype.removeAttachment = function (param) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"},
"removeAttachment 200 called");
return "Removed";
};
jio.removeAttachment({"_id": "bar", "_attachment": "foo"})
.then(function (result) {
equal(result, "Removed");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("uuidStorage.hasCapacity");
test("hasCapacity return substorage value", function () {
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
delete Storage200.prototype.hasCapacity;
throws(
function () {
jio.hasCapacity("foo");
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'foo' is not implemented on 'uuidstorage200'");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// uuidStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("uuidStorage.buildQuery");
test("buildQuery return substorage buildQuery", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
Storage200.prototype.hasCapacity = function () {
return true;
};
Storage200.prototype.buildQuery = function (options) {
deepEqual(options, {
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
uuid: 'title: "two"'
}, "allDocs parameter");
return "bar";
};
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
uuid: 'title: "two"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: "bar",
total_rows: 3
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
/*jslint indent: 2, maxlen: 80 */
/*global define, exports, window, require, localStorage, start, ok, deepEqual,
sinon, setTimeout, clearTimeout */
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
module(exports, require('sinon_qunit'));
}
if (typeof window === 'object') {
window.test_util = {};
module(window.test_util);
}
}(['exports', 'sinon_qunit'], function (exports) {
"use strict";
//////////////////////////////////////////////////////////////////////////////
// Tools
/**
* Test if the string is an Uuid
*
* @param {String} uuid The string to test
* @return {Boolean} true if is uuid, else false
*/
function isUuid(uuid) {
var x = "[0-9a-fA-F]";
if (typeof uuid !== "string") {
return false;
}
return (uuid.match(
"^" + x + "{8}-" + x + "{4}-" +
x + "{4}-" + x + "{4}-" + x + "{12}$"
) === null ? false : true);
}
exports.isUuid = isUuid;
}));
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
<script src="jio.storage/unionstorage.tests.js"></script> <script src="jio.storage/unionstorage.tests.js"></script>
<script src="jio.storage/erp5storage.tests.js"></script> <script src="jio.storage/erp5storage.tests.js"></script>
<script src="jio.storage/indexeddbstorage.tests.js"></script> <script src="jio.storage/indexeddbstorage.tests.js"></script>
<script src="jio.storage/uuidstorage.tests.js"></script>
<!--script src="jio.storage/indexstorage.tests.js"></script--> <!--script src="jio.storage/indexstorage.tests.js"></script-->
<!--script src="jio.storage/dropboxstorage.tests.js"></script--> <!--script src="jio.storage/dropboxstorage.tests.js"></script-->
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment