Skip to content
Closed
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
86 changes: 49 additions & 37 deletions dist/web/pubnub.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions get-entities-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env node
'use strict';

/**
* Playground for getEntities filtering on class `product`.
*
* The docs expression is unchanged. Pass it as `filterFast`. On this keyset
* `filter` still returns [] because the search index has no `name` value.
*
* node get-entities-filter.js
*/

const PubNub = require('./lib/node');

// DataSync keyset (Access Manager + DataSync). Same three values as DS_PUBLISH_KEY,
// DS_SUBSCRIBE_KEY and DS_SECRET_KEY.
const publishKey = 'pub-c-4f743b03-8bcd-47be-96af-862b6585f724';
const subscribeKey = 'sub-c-bd83ac05-432a-4770-828b-40189dbd28eb';
const secretKey = 'sec-c-Y2VlOGUwYWMtZjAxMi00Mjc5LTkxMzctYmZmMjk2ZGEyOGNl';

const PRODUCT_ID = 'product-sneaker-42';
const FILTER = 'name LIKE "*sneaker*" && !(status == "discontinued")';

function client(userId) {
return new PubNub({
publishKey,
subscribeKey,
secretKey,
userId,
heartbeatInterval: 0,
});
}

function statusCode(error) {
return error && error.status && error.status.statusCode;
}

function idsOf(response) {
return (response.data || []).map((entity) => entity.id);
}

async function removeIfPresent(setup, id) {
try {
await setup.dataSync.removeEntity({ id });
} catch (error) {
if (statusCode(error) !== 404) throw error;
}
}

async function setupProduct(setup) {
// await removeIfPresent(setup, PRODUCT_ID);
// // Probe rows from the expression check. Removed so they do not stay on the keyset.
// await removeIfPresent(setup, 'product-sneaker-old');
// await removeIfPresent(setup, 'product-hat-1');

// const created = await setup.dataSync.createEntity({
// id: PRODUCT_ID,
// class: 'product',
// data: {
// classVersion: 1,
// payload: { name: 'Retro sneaker', price: 89.99, stock: 12 },
// },
// });
// console.log('setup: createEntity', created.status, created.data.id, created.data.payload);
}

async function readWithFilter(reader) {
try {
const stored = await reader.dataSync.getEntities({ class: 'product' });
console.log(
'stored:',
stored.status,
stored.data.map((entity) => ({ id: entity.id, status: entity.status, name: entity.payload && entity.payload.name })),
);

const response = await reader.dataSync.getEntities({
class: 'product',
filterFast: FILTER,
});
console.log('filter:', response.status, 'ids', idsOf(response));

const byId = await reader.dataSync.getEntities({
class: 'product',
filter: `id == "${PRODUCT_ID}"`,
});
console.log('filter id ==:', byId.status, 'ids', idsOf(byId));

const nameNull = await reader.dataSync.getEntities({
class: 'product',
filter: 'name == null',
});
console.log('filter name == null:', nameNull.status, 'ids', idsOf(nameNull));
} catch (error) {
console.error('getEntities failed:', error.status && error.status.errorData);
}
}

async function main() {
if ([publishKey, subscribeKey, secretKey].some((value) => !value || value.startsWith('YOUR_'))) {
console.error('Fill in publishKey, subscribeKey and secretKey at the top of get-entities-filter.js.');
process.exitCode = 1;
return;
}

const setup = client('datasync-setup');
try {
await setupProduct(setup);
} finally {
setup.destroy();
}

const reader = client('datasync-filter');
try {
await readWithFilter(reader);
} finally {
reader.destroy();
}
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
5 changes: 4 additions & 1 deletion lib/core/components/retry-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const categories_1 = __importDefault(require("../constants/categories"));
/**
* HTTP status codes which represent definitive client-side outcomes and shouldn't be retried.
*
* `412` is a failed `If-Match` / `If-None-Match` precondition: resending the same conditional
* request can never succeed.
*
* @internal
*/
const NON_RETRIABLE_STATUS_CODES = [404, 409];
const NON_RETRIABLE_STATUS_CODES = [404, 409, 412];
// --------------------------------------------------------
// ------------------------ Types -------------------------
// --------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions lib/core/endpoints/data_sync/channel/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class CreateChannelRequest extends request_1.AbstractRequest {
return `/v1/datasync/subkeys/${subscribeKey}/channels`;
}
get body() {
const { id, class: entityClass, classLevel, data } = this.parameters;
const { id, class: entityClass, classLevel, ttl, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id !== undefined ? { id } : {})), (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id !== undefined ? { id } : {})), (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (ttl !== undefined ? { entityTtlSec: ttl } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/core/endpoints/data_sync/channel/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* Set Channel REST API module.
*
* Full resource replacement via PUT.
* Create or replace (upsert) via PUT: creates the channel when no live record exists at the path ID
* (HTTP 201), otherwise replaces it (HTTP 200). An omitted class creates a Global `Channel`.
*
* @internal
*/
Expand Down Expand Up @@ -59,16 +60,18 @@ class SetChannelRequest extends request_1.AbstractRequest {
let headers = (_a = super.headers) !== null && _a !== void 0 ? _a : {};
if (this.parameters.ifMatchesEtag)
headers = Object.assign(Object.assign({}, headers), { 'If-Match': this.parameters.ifMatchesEtag });
if (this.parameters.ifNoneMatch)
headers = Object.assign(Object.assign({}, headers), { 'If-None-Match': this.parameters.ifNoneMatch });
return Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/vnd.pubnub.objects.channel+json;version=1' });
}
get path() {
const { keySet: { subscribeKey }, id, } = this.parameters;
return `/v1/datasync/subkeys/${subscribeKey}/channels/${(0, utils_1.encodeString)(id)}`;
}
get body() {
const { data } = this.parameters;
const { class: entityClass, classLevel, ttl, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign({ entityClassVersion: data.classVersion }, (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (ttl !== undefined ? { entityTtlSec: ttl } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core/endpoints/data_sync/entity/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class CreateEntityRequest extends request_1.AbstractRequest {
return `/v1/datasync/subkeys/${subscribeKey}/entities`;
}
get body() {
const { id, class: entityClass, classLevel, data } = this.parameters;
const { id, class: entityClass, classLevel, ttl, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id !== undefined ? { id } : {})), { entityClass, entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id !== undefined ? { id } : {})), { entityClass, entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (ttl !== undefined ? { entityTtlSec: ttl } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
11 changes: 7 additions & 4 deletions lib/core/endpoints/data_sync/entity/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/**
* Set Entity REST API module.
*
* Full resource replacement via PUT.
* Note: `entityClass` is immutable and cannot be changed via update.
* Create or replace (upsert) via PUT: creates the entity when no live record exists at the path ID
* (HTTP 201), otherwise replaces it (HTTP 200). Creation requires `entityClass`; on replacement a
* supplied class / level is an immutable identity assertion.
*
* @internal
*/
Expand Down Expand Up @@ -60,16 +61,18 @@ class SetEntityRequest extends request_1.AbstractRequest {
let headers = (_a = super.headers) !== null && _a !== void 0 ? _a : {};
if (this.parameters.ifMatchesEtag)
headers = Object.assign(Object.assign({}, headers), { 'If-Match': this.parameters.ifMatchesEtag });
if (this.parameters.ifNoneMatch)
headers = Object.assign(Object.assign({}, headers), { 'If-None-Match': this.parameters.ifNoneMatch });
return Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/vnd.pubnub.objects.entity+json;version=1' });
}
get path() {
const { keySet: { subscribeKey }, id, } = this.parameters;
return `/v1/datasync/subkeys/${subscribeKey}/entities/${(0, utils_1.encodeString)(id)}`;
}
get body() {
const { data } = this.parameters;
const { class: entityClass, classLevel, ttl, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign({ entityClassVersion: data.classVersion }, (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (ttl !== undefined ? { entityTtlSec: ttl } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/core/endpoints/data_sync/membership/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* Set Membership REST API module.
*
* Full resource replacement via PUT.
* Create or replace (upsert) via PUT: creates the membership when no live record exists at the
* path ID (HTTP 201), otherwise replaces it (HTTP 200). The class is always Global `Membership`.
*
* @internal
*/
Expand Down Expand Up @@ -47,12 +48,10 @@ class SetMembershipRequest extends request_1.AbstractRequest {
});
}
validate() {
// `userId` / `channelId` are optional: only creation needs them, and the service decides
// whether the PUT creates or replaces.
if (!this.parameters.id)
return 'Membership id cannot be empty';
if (!this.parameters.userId)
return 'User id cannot be empty';
if (!this.parameters.channelId)
return 'Channel id cannot be empty';
if (!this.parameters.data)
return 'Membership data cannot be empty';
if (!this.parameters.data.classVersion)
Expand All @@ -63,6 +62,8 @@ class SetMembershipRequest extends request_1.AbstractRequest {
let headers = (_a = super.headers) !== null && _a !== void 0 ? _a : {};
if (this.parameters.ifMatchesEtag)
headers = Object.assign(Object.assign({}, headers), { 'If-Match': this.parameters.ifMatchesEtag });
if (this.parameters.ifNoneMatch)
headers = Object.assign(Object.assign({}, headers), { 'If-None-Match': this.parameters.ifNoneMatch });
return Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/vnd.pubnub.objects.membership+json;version=1' });
}
get path() {
Expand All @@ -72,8 +73,7 @@ class SetMembershipRequest extends request_1.AbstractRequest {
get body() {
const { userId, channelId, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign({ userId,
channelId, relationshipClassVersion: data.classVersion }, (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (userId !== undefined ? { userId } : {})), (channelId !== undefined ? { channelId } : {})), { relationshipClassVersion: data.classVersion }), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/core/endpoints/data_sync/relationship/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* Set Relationship REST API module.
*
* Full resource replacement via PUT.
* Create or replace (upsert) via PUT: creates the relationship when no live record exists at the
* path ID (HTTP 201), otherwise replaces it (HTTP 200).
*
* @internal
*/
Expand Down Expand Up @@ -47,12 +48,10 @@ class SetRelationshipRequest extends request_1.AbstractRequest {
});
}
validate() {
// `class` / `entityAId` / `entityBId` are optional: only creation needs them, and the service
// decides whether the PUT creates or replaces.
if (!this.parameters.id)
return 'Relationship id cannot be empty';
if (!this.parameters.entityAId)
return 'Entity A id cannot be empty';
if (!this.parameters.entityBId)
return 'Entity B id cannot be empty';
if (!this.parameters.data)
return 'Relationship data cannot be empty';
if (!this.parameters.data.classVersion)
Expand All @@ -63,17 +62,18 @@ class SetRelationshipRequest extends request_1.AbstractRequest {
let headers = (_a = super.headers) !== null && _a !== void 0 ? _a : {};
if (this.parameters.ifMatchesEtag)
headers = Object.assign(Object.assign({}, headers), { 'If-Match': this.parameters.ifMatchesEtag });
if (this.parameters.ifNoneMatch)
headers = Object.assign(Object.assign({}, headers), { 'If-None-Match': this.parameters.ifNoneMatch });
return Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/vnd.pubnub.objects.relationship+json;version=1' });
}
get path() {
const { keySet: { subscribeKey }, id, } = this.parameters;
return `/v1/datasync/subkeys/${subscribeKey}/relationships/${(0, utils_1.encodeString)(id)}`;
}
get body() {
const { entityAId, entityBId, data } = this.parameters;
const { class: relationshipClass, entityAId, entityBId, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign({ entityAId,
entityBId, relationshipClassVersion: data.classVersion }, (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (entityAId !== undefined ? { entityAId } : {})), (entityBId !== undefined ? { entityBId } : {})), (relationshipClass !== undefined ? { relationshipClass } : {})), { relationshipClassVersion: data.classVersion }), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core/endpoints/data_sync/user/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class CreateUserRequest extends request_1.AbstractRequest {
return `/v1/datasync/subkeys/${subscribeKey}/users`;
}
get body() {
const { id, class: entityClass, classLevel, data } = this.parameters;
const { id, class: entityClass, classLevel, ttl, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id !== undefined ? { id } : {})), (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id !== undefined ? { id } : {})), (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (ttl !== undefined ? { entityTtlSec: ttl } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/core/endpoints/data_sync/user/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* Set User REST API module.
*
* Full resource replacement via PUT.
* Create or replace (upsert) via PUT: creates the user when no live record exists at the path ID
* (HTTP 201), otherwise replaces it (HTTP 200). An omitted class creates a Global `User`.
*
* @internal
*/
Expand Down Expand Up @@ -59,16 +60,18 @@ class SetUserRequest extends request_1.AbstractRequest {
let headers = (_a = super.headers) !== null && _a !== void 0 ? _a : {};
if (this.parameters.ifMatchesEtag)
headers = Object.assign(Object.assign({}, headers), { 'If-Match': this.parameters.ifMatchesEtag });
if (this.parameters.ifNoneMatch)
headers = Object.assign(Object.assign({}, headers), { 'If-None-Match': this.parameters.ifNoneMatch });
return Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/vnd.pubnub.objects.user+json;version=1' });
}
get path() {
const { keySet: { subscribeKey }, id, } = this.parameters;
return `/v1/datasync/subkeys/${subscribeKey}/users/${(0, utils_1.encodeString)(id)}`;
}
get body() {
const { data } = this.parameters;
const { class: entityClass, classLevel, ttl, data } = this.parameters;
return JSON.stringify({
data: Object.assign(Object.assign({ entityClassVersion: data.classVersion }, (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (entityClass !== undefined ? { entityClass } : {})), { entityClassVersion: data.classVersion }), (classLevel !== undefined ? { entityClassLevel: classLevel } : {})), (ttl !== undefined ? { entityTtlSec: ttl } : {})), (data.status !== undefined ? { status: data.status } : {})), (data.payload !== undefined ? { payload: data.payload } : {})),
});
}
}
Expand Down
Loading
Loading