Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/lib/cloud.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
};
Expand Down
40 changes: 22 additions & 18 deletions src/lib/object_store.js → src/lib/cloud_collection.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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")
Expand All @@ -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"})
Expand All @@ -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"})
Expand All @@ -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")
Expand All @@ -237,4 +241,4 @@ StreamObject.prototype = {
},
};

module.exports = StreamObjectStore;
module.exports = FrontendCollection;
14 changes: 13 additions & 1 deletion src/lib/user_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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;
});

Expand All @@ -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();
Expand All @@ -34,31 +36,31 @@ 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);
});
});

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);
});
});

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);
});
});

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();
Expand All @@ -67,37 +69,41 @@ 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();
});

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);
});

Expand All @@ -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!', {});
});
});

Expand All @@ -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);
});

Expand All @@ -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',
Expand All @@ -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, {});
});
});
});
2 changes: 1 addition & 1 deletion test/integration/cloud/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down