diff --git a/src/lib/cloud.js b/src/lib/cloud.js index 052ca3dd..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'); @@ -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/cloud_collection.js similarity index 87% rename from src/lib/object_store.js rename to src/lib/cloud_collection.js index 50d6b8a1..b2c1472a 100644 --- a/src/lib/object_store.js +++ b/src/lib/cloud_collection.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..26159a85 100644 --- a/src/lib/user_session.js +++ b/src/lib/user_session.js @@ -65,12 +65,24 @@ 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, + ); + object.full = response; + return object; + }, + + collectionEntryFromResponse: function(response) { + let object = this.collection(response.collection).entry( response.id, response.data, ); diff --git a/test/integration/cloud/object_store.js b/test/integration/cloud/collection.js similarity index 78% rename from test/integration/cloud/object_store.js rename to test/integration/cloud/collection.js index 11df3d56..ce9a1a06 100644 --- a/test/integration/cloud/object_store.js +++ b/test/integration/cloud/collection.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', @@ -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); });