From 82c09d57826d472bb89b12d0097d314aca8c7575 Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Thu, 11 Oct 2018 14:16:45 +0200 Subject: [PATCH 1/6] Create a collection allias for storage to consolidate the naming --- src/lib/cloud.js | 4 ++++ src/lib/object_store.js | 40 ++++++++++++++++++++++------------------ src/lib/user_session.js | 5 ++++- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/lib/cloud.js b/src/lib/cloud.js index 052ca3dd..c603982e 100644 --- a/src/lib/cloud.js +++ b/src/lib/cloud.js @@ -34,6 +34,10 @@ StreamCloudClient.prototype.storage = function(name, token) { return new StreamObjectStore(this, name, token); }; +StreamCloudClient.prototype.collection = function(name, token) { + return new StreamObjectStore(this, name, token); +}; + StreamCloudClient.prototype.files = function(token) { return new StreamFileStore(this, token); }; diff --git a/src/lib/object_store.js b/src/lib/object_store.js index 50d6b8a1..b2c1472a 100644 --- a/src/lib/object_store.js +++ b/src/lib/object_store.js @@ -1,17 +1,17 @@ -var StreamObjectStore = function() { +var FrontendCollection = function() { this.initialize.apply(this, arguments); }; -StreamObjectStore.prototype = { +FrontendCollection.prototype = { initialize: function(client, name, token) { /** * Initialize a feed object * @method intialize - * @memberof StreamObjectStore.prototype + * @memberof FrontendCollection.prototype * @param {StreamCloudClient} client Stream client this collection is constructed from * @param {string} name ObjectStore name * @param {string} token JWT token - * @example new StreamObjectStore(client, "food", "eyJhbGciOiJIUzI1...") + * @example new FrontendCollection(client, "food", "eyJhbGciOiJIUzI1...") */ this.client = client; this.collection = name; @@ -27,15 +27,19 @@ StreamObjectStore.prototype = { return url + itemId + '/'; }, + entry: function(itemId, itemData) { + return new CollectionEntry(this, itemId, itemData); + }, + object: function(itemId, itemData) { - return new StreamObject(this, itemId, itemData); + return new CollectionEntry(this, itemId, itemData); }, items: function(options, callback) { /** * get all items from collection * @method items - * @memberof StreamObjectStore.prototype + * @memberof FrontendCollection.prototype * @param {object} options {limit:} * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object @@ -55,7 +59,7 @@ StreamObjectStore.prototype = { /** * get item from collection * @method get - * @memberof StreamObjectStore.prototype + * @memberof FrontendCollection.prototype * @param {object} itemId ObjectStore object id * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object @@ -74,7 +78,7 @@ StreamObjectStore.prototype = { /** * Add item to collection * @method add - * @memberof StreamObjectStore.prototype + * @memberof FrontendCollection.prototype * @param {string} itemId ObjectStore id * @param {object} itemData ObjectStore data * @param {requestCallback} callback Callback to call on completion @@ -100,9 +104,9 @@ StreamObjectStore.prototype = { update: function(itemId, objectData, callback) { /** - * Update item in the object storage + * Update item in the object collection * @method update - * @memberof StreamObjectStore.prototype + * @memberof FrontendCollection.prototype * @param {object} itemId ObjectStore object id * @param {object} objectData ObjectStore data * @param {requestCallback} callback Callback to call on completion @@ -127,7 +131,7 @@ StreamObjectStore.prototype = { /** * Delete item from collection * @method delete - * @memberof StreamObjectStore.prototype + * @memberof FrontendCollection.prototype * @param {object} itemId ObjectStore object id * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object @@ -143,11 +147,11 @@ StreamObjectStore.prototype = { }, }; -var StreamObject = function() { +var CollectionEntry = function() { this.initialize.apply(this, arguments); }; -StreamObject.prototype = { +CollectionEntry.prototype = { initialize: function(store, id, data) { this.collection = store.collection; this.store = store; @@ -163,7 +167,7 @@ StreamObject.prototype = { /** * get item from collection and sync data * @method get - * @memberof StreamObjectStore.prototype + * @memberof CollectionEntry.prototype * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object * @example collection.get("0c7db91c-67f9-11e8-bcd9-fe00a9219401") @@ -182,7 +186,7 @@ StreamObject.prototype = { /** * Add item to collection * @method add - * @memberof StreamObjectStore.prototype + * @memberof CollectionEntry.prototype * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object * @example collection.add("cheese101", {"name": "cheese burger","toppings": "cheese"}) @@ -201,7 +205,7 @@ StreamObject.prototype = { /** * Update item in the object storage * @method update - * @memberof StreamObjectStore.prototype + * @memberof CollectionEntry.prototype * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object * @example store.update("0c7db91c-67f9-11e8-bcd9-fe00a9219401", {"name": "cheese burger","toppings": "cheese"}) @@ -221,7 +225,7 @@ StreamObject.prototype = { /** * Delete item from collection * @method delete - * @memberof StreamObjectStore.prototype + * @memberof CollectionEntry.prototype * @param {requestCallback} callback Callback to call on completion * @return {Promise} Promise object * @example collection.delete("cheese101") @@ -237,4 +241,4 @@ StreamObject.prototype = { }, }; -module.exports = StreamObjectStore; +module.exports = FrontendCollection; diff --git a/src/lib/user_session.js b/src/lib/user_session.js index 0d4783d4..957df366 100644 --- a/src/lib/user_session.js +++ b/src/lib/user_session.js @@ -65,12 +65,15 @@ StreamUserSession.prototype = { storage: function(collection) { return this.client.storage(collection, this.token); }, + collection: function(name) { + return this.client.collection(name, this.token); + }, react: function(kind, activityId, data) { return this.reactions.add(kind, activityId, data); }, objectFromResponse: function(response) { - let object = this.storage(response.collection).object( + let object = this.collection(response.collection).object( response.id, response.data, ); From 8764c340b916088f343062e789a17e7a832aa516 Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Thu, 11 Oct 2018 14:21:41 +0200 Subject: [PATCH 2/6] Use new collection alias in tests --- test/integration/cloud/object_store.js | 46 +++++++++++++++----------- test/integration/cloud/utils.js | 2 +- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/test/integration/cloud/object_store.js b/test/integration/cloud/object_store.js index 11df3d56..f231b556 100644 --- a/test/integration/cloud/object_store.js +++ b/test/integration/cloud/object_store.js @@ -11,7 +11,9 @@ describe('Object Store CRUD behaviours', () => { describe('When alice tries to get the cheeseburger', () => { ctx.requestShouldNotError(async () => { - ctx.response = await ctx.alice.storage('food').get(ctx.cheeseBurger.id); + ctx.response = await ctx.alice + .collection('food') + .get(ctx.cheeseBurger.id); ctx.prevResponse = ctx.response; }); @@ -25,7 +27,7 @@ describe('Object Store CRUD behaviours', () => { describe('When bob tries to get the cheeseburger', () => { ctx.requestShouldNotError(async () => { - ctx.response = await ctx.bob.storage('food').get(ctx.cheeseBurger.id); + ctx.response = await ctx.bob.collection('food').get(ctx.cheeseBurger.id); }); ctx.responseShouldEqualPreviousResponse(); @@ -34,7 +36,7 @@ describe('Object Store CRUD behaviours', () => { describe('When bob tries to add the improved cheeseburger with the same ID', () => { ctx.requestShouldError(409, async () => { ctx.response = await ctx.bob - .storage('food') + .collection('food') .add(ctx.cheeseBurger.id, improvedCheeseBurgerData); }); }); @@ -42,7 +44,7 @@ describe('Object Store CRUD behaviours', () => { describe('When alice tries to add the improved cheeseburger with the same ID', () => { ctx.requestShouldError(409, async () => { ctx.response = await ctx.alice - .storage('food') + .collection('food') .add(ctx.cheeseBurger.id, improvedCheeseBurgerData); }); }); @@ -50,7 +52,7 @@ describe('Object Store CRUD behaviours', () => { describe('When bob tries to update the cheeseburger', () => { ctx.requestShouldError(403, async () => { ctx.response = await ctx.bob - .storage('food') + .collection('food') .update(ctx.cheeseBurger.id, improvedCheeseBurgerData); }); }); @@ -58,7 +60,7 @@ describe('Object Store CRUD behaviours', () => { describe('When alice tries to update the cheeseburger', () => { ctx.requestShouldNotError(async () => { ctx.response = await ctx.alice - .storage('food') + .collection('food') .update(ctx.cheeseBurger.id, improvedCheeseBurgerData); }); ctx.responseShouldHaveNewUpdatedAt(); @@ -67,7 +69,9 @@ describe('Object Store CRUD behaviours', () => { describe('When alice then tries to get the cheeseburger', () => { ctx.requestShouldNotError(async () => { ctx.prevResponse = ctx.response; - ctx.response = await ctx.alice.storage('food').get(ctx.cheeseBurger.id); + ctx.response = await ctx.alice + .collection('food') + .get(ctx.cheeseBurger.id); }); ctx.responseShouldEqualPreviousResponse(); @@ -75,29 +79,31 @@ describe('Object Store CRUD behaviours', () => { describe('When alice tries to change the ID of cheeseburger in an update call', () => { ctx.requestShouldError(400, async () => { - let storage = ctx.alice.storage('food'); + let collection = ctx.alice.collection('food'); var body = { id: 1234, data: improvedCheeseBurgerData, }; - await storage.client.put({ - url: storage.buildURL(ctx.cheeseBurger.id), + await collection.client.put({ + url: collection.buildURL(ctx.cheeseBurger.id), body: body, - signature: storage.signature, + signature: collection.signature, }); }); }); describe('When bob tries to delete the cheeseburger', () => { ctx.requestShouldError(403, async () => { - ctx.response = await ctx.bob.storage('food').delete(ctx.cheeseBurger.id); + ctx.response = await ctx.bob + .collection('food') + .delete(ctx.cheeseBurger.id); }); }); describe('When alice tries to delete the cheeseburger', () => { ctx.requestShouldNotError(async () => { ctx.response = await ctx.alice - .storage('food') + .collection('food') .delete(ctx.cheeseBurger.id); }); @@ -108,13 +114,13 @@ describe('Object Store CRUD behaviours', () => { describe('When alice then tries to get the cheeseburger', () => { ctx.requestShouldError(404, async () => { - await ctx.alice.storage('food').get(ctx.cheeseBurger.id); + await ctx.alice.collection('food').get(ctx.cheeseBurger.id); }); }); describe('When alice tries to create an object with an illegal character in the id', () => { ctx.requestShouldError(400, async () => { - await ctx.alice.storage('food').add('!abcdee!', {}); + await ctx.alice.collection('food').add('!abcdee!', {}); }); }); @@ -124,7 +130,7 @@ describe('Object Store CRUD behaviours', () => { let newCheeseBurgerID = randUserId('cheeseburger'); ctx.requestShouldNotError(async () => { ctx.response = await ctx.alice - .storage('food') + .collection('food') .add(newCheeseBurgerID, ctx.cheeseBurgerData); }); @@ -146,7 +152,7 @@ describe('Object Store CRUD behaviours', () => { describe('When alice tries to get the new cheeseburger', () => { ctx.requestShouldNotError(async () => { - ctx.response = await ctx.alice.storage('food').get(newCheeseBurger.id); + ctx.response = await ctx.alice.collection('food').get(newCheeseBurger.id); }); ctx.responseShould( 'be the same as when the new cheeseburger was added', @@ -158,13 +164,15 @@ describe('Object Store CRUD behaviours', () => { describe('When alice tries to add an object with a string as data', () => { ctx.requestShouldError(400, async () => { - ctx.response = await ctx.alice.storage('food').add(null, 'some string'); + ctx.response = await ctx.alice + .collection('food') + .add(null, 'some string'); }); }); describe('When alice tries to add an object with empty data', () => { ctx.requestShouldError(400, async () => { - ctx.response = await ctx.alice.storage('food').add(null, {}); + ctx.response = await ctx.alice.collection('food').add(null, {}); }); }); }); diff --git a/test/integration/cloud/utils.js b/test/integration/cloud/utils.js index 0527c91d..693e6e66 100644 --- a/test/integration/cloud/utils.js +++ b/test/integration/cloud/utils.js @@ -222,7 +222,7 @@ class CloudContext { describe('When alice adds a cheese burger to the food collection', () => { this.requestShouldNotError(async () => { this.response = await this.alice - .storage('food') + .collection('food') .add(null, this.cheeseBurgerData); }); From 95c36843cf104abf8a8095925c347c71772d96be Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Thu, 11 Oct 2018 14:23:22 +0200 Subject: [PATCH 3/6] Rename test name to to match collection instead of object store --- test/integration/cloud/object_store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/cloud/object_store.js b/test/integration/cloud/object_store.js index f231b556..ce9a1a06 100644 --- a/test/integration/cloud/object_store.js +++ b/test/integration/cloud/object_store.js @@ -1,7 +1,7 @@ var { CloudContext } = require('./utils'); var randUserId = require('../utils/hooks').randUserId; -describe('Object Store CRUD behaviours', () => { +describe('Collection CRUD behaviours', () => { let ctx = new CloudContext(); let improvedCheeseBurgerData = { name: 'The improved cheese burger', From 6fe93f66b36e9f70e2c68e8d012e45a25e926155 Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Thu, 11 Oct 2018 14:25:52 +0200 Subject: [PATCH 4/6] Rename files --- src/lib/{object_store.js => cloud_collection.js} | 0 test/integration/cloud/{object_store.js => collection.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/lib/{object_store.js => cloud_collection.js} (100%) rename test/integration/cloud/{object_store.js => collection.js} (100%) diff --git a/src/lib/object_store.js b/src/lib/cloud_collection.js similarity index 100% rename from src/lib/object_store.js rename to src/lib/cloud_collection.js diff --git a/test/integration/cloud/object_store.js b/test/integration/cloud/collection.js similarity index 100% rename from test/integration/cloud/object_store.js rename to test/integration/cloud/collection.js From 4399a155c4e931fdcaafdd0ab0f3540eb6862e66 Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Mon, 15 Oct 2018 13:39:23 +0200 Subject: [PATCH 5/6] UserSession: Add collectionEntryFromResponse --- src/lib/user_session.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/user_session.js b/src/lib/user_session.js index 957df366..26159a85 100644 --- a/src/lib/user_session.js +++ b/src/lib/user_session.js @@ -81,6 +81,15 @@ StreamUserSession.prototype = { return object; }, + collectionEntryFromResponse: function(response) { + let object = this.collection(response.collection).entry( + response.id, + response.data, + ); + object.full = response; + return object; + }, + og: function(url) { return this.client.get({ url: 'og/', From da31fc647620d6913000a53c43ddb44fdb57789b Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Mon, 15 Oct 2018 13:47:01 +0200 Subject: [PATCH 6/6] Fix import --- src/lib/cloud.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/cloud.js b/src/lib/cloud.js index c603982e..e993e1df 100644 --- a/src/lib/cloud.js +++ b/src/lib/cloud.js @@ -1,6 +1,6 @@ var StreamClient = require('./client'); var StreamFeed = require('./feed'); -var StreamObjectStore = require('./object_store'); +var StreamObjectStore = require('./cloud_collection'); var StreamUserSession = require('./user_session'); var StreamReaction = require('./reaction'); var StreamFileStore = require('./files');