New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@workerbase/sdk

Package Overview
Dependencies
Maintainers
3
Versions
114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@workerbase/sdk - npm Package Compare versions

Comparing version 0.1.14 to 0.1.15-beta.1

dist/resources/Buttons.d.ts

16

dist/resources.d.ts

@@ -20,11 +20,11 @@ import { DatabasesSDK } from './resources/Databases';

declare const _default: {
databases: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
roles: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
skills: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
users: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
locations: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
media: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
functions: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
events: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string) => void;
databases: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
roles: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
skills: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
users: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
locations: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
media: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
functions: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
events: (workerbase: import(".").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
};
export default _default;
import { DatabaseItem, ItemPayload, DatabaseItemPOST } from '../types/databaseItems';
import { BasicMethods, BasicMethodTypes } from '../utils/WorkerbaseRessource';
import { ListOptions } from '../types/pagination';
export interface DatabaseItemSDK extends Pick<BasicMethodTypes<{

@@ -10,4 +11,10 @@ GET: DatabaseItem;

createMany: (items: DatabaseItemPOST[]) => Promise<DatabaseItem[]>;
graphql: (payload: any, options?: ListOptions) => Promise<{
success: boolean;
}>;
query: (payload: any, options?: ListOptions) => Promise<{
success: boolean;
}>;
}
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {

@@ -58,2 +69,3 @@ if (k2 === undefined) k2 = k;

Object.defineProperty(exports, "__esModule", { value: true });
var gql_query_builder_1 = require("gql-query-builder");
var doRequest_1 = require("../utils/doRequest");

@@ -70,2 +82,79 @@ var WorkerbaseRessource_1 = __importStar(require("../utils/WorkerbaseRessource"));

methods: {
graphql: function (spec) { return function (payload, options) {
if (options === void 0) { options = {}; }
return __awaiter(void 0, void 0, void 0, function () {
var graphqlQuery, data;
var _a, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
graphqlQuery = gql_query_builder_1.query({
operation: 'databaseitems',
fields: [
{
edges: [
'_id',
'externalId',
'payload',
{ meta: ['createdAt', 'updatedAt'] },
],
},
{
pageInfo: [
'page',
'perpage',
'sort',
'order',
'maxpage',
'totalItems',
],
},
],
variables: {
page: options.page || 1,
perpage: options.perpage || 100,
sort: options.sort || 'meta.createdAt',
order: {
value: options.order || 'desc',
type: 'GraphQLSortOrder',
},
textSearch: options.textSearch || '',
filter: {
type: 'WorkerbaseDatabaseitemsFilter',
value: __assign(__assign({}, (payload || {})), { database: {
EQ: (_a = spec === null || spec === void 0 ? void 0 : spec.parent) === null || _a === void 0 ? void 0 : _a.slug,
} }),
},
},
});
return [4 /*yield*/, doRequest_1.doRequest(spec._api, 'graphql', {
method: 'POST',
data: graphqlQuery,
params: __assign({}, options),
}, process.env.DATABASES_SERVICE_HOST)];
case 1:
data = (_d.sent()).data;
return [2 /*return*/, (_c = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.databaseitems) === null || _c === void 0 ? void 0 : _c.edges];
}
});
});
}; },
query: function (spec) { return function (payload, options) {
if (options === void 0) { options = {}; }
return __awaiter(void 0, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, doRequest_1.doRequest(spec._api, spec.basePath + "/query", {
method: 'POST',
data: payload,
params: __assign({ perpage: 1000 }, options),
}, process.env.DATABASES_SERVICE_HOST)];
case 1:
data = (_a.sent()).data;
return [2 /*return*/, data.data];
}
});
});
}; },
create: function (spec) { return function (payload, externalId) { return __awaiter(void 0, void 0, void 0, function () {

@@ -72,0 +161,0 @@ var path, data;

@@ -12,3 +12,3 @@ import { Database, DatabasePOST, DatabasePUT } from '../types/databases';

}>;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -12,3 +12,3 @@ import { Event, EventPOST, EventPUT } from '../types/events';

}
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -13,3 +13,3 @@ import { WorkerbaseFunction, WorkerbaseFunctionPOST, WorkerbaseFunctionPUT } from '../types/functions';

}
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -12,3 +12,3 @@ import { Location, LocationPUT, LocationPOST, LocationPart, LocationLevel } from '../types/locations';

}
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -8,3 +8,3 @@ import { MediaBase } from '../types/media';

}>, BasicMethods.LIST | BasicMethods.DELETE>;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -69,3 +69,3 @@ "use strict";

switch (_a.label) {
case 0: return [4 /*yield*/, doRequest_1.doRequest(spec._api, spec.basePath + "/as-json/" + id, {}, process.env.MEDIA_SERVICE_HOST)];
case 0: return [4 /*yield*/, doRequest_1.doRequest(spec._api, spec.basePath + "/" + id + "/meta", {}, process.env.MEDIA_SERVICE_HOST)];
case 1:

@@ -72,0 +72,0 @@ data = (_a.sent()).data;

@@ -8,3 +8,3 @@ import { Role, RolePUT, RolePOST } from '../types/roles';

}>;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -8,3 +8,3 @@ import { Skill, SkillPUT, SkillPOST } from '../types/skills';

}>;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -8,3 +8,3 @@ import { User, UserPUT, UserPOST } from '../types/users';

}>;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string) => void;
declare const _default: (workerbase: import("..").WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;
export default _default;

@@ -7,4 +7,3 @@ export interface MediaBase {

contentType: string;
fileBase64: string;
filename: string;
}
export interface ListOptions {
page?: number;
perpage?: number;
populate?: boolean;
textSearch?: string;
sort?: string;

@@ -5,0 +7,0 @@ order?: 'desc' | 'asc';

import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { API } from '..';
export declare const doRequest: (api: API, endpoint: string, options?: AxiosRequestConfig, internalHostname?: null | string) => Promise<AxiosResponse<any>>;
export default doRequest;

@@ -72,6 +72,6 @@ "use strict";

return __awaiter(void 0, void 0, void 0, function () {
var baseUrl, url, response, err_1, errorToThrow, _a, errorMessage, success, otherErrorData;
var _b, _c, _d, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
var baseUrl, url, response, err_1, errorMessage, errorToThrow, _a, tmpVar1, tmpVar2, otherErrorData;
var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8;
return __generator(this, function (_9) {
switch (_9.label) {
case 0:

@@ -91,9 +91,12 @@ if (!api.url) {

if (process.env.WB_ENABLE_INTERNAL_ROUTING && internalHostname) {
api.url = 'http://' + internalHostname + ':3000/';
// eslint-disable-next-line no-param-reassign
api.url = "http://" + internalHostname + ":3000/";
}
baseUrl = parseurl_1.default({ url: api.url }).href;
url = baseUrl + "api/" + apiVersion + "/" + endpoint;
_g.label = 1;
url = endpoint === 'graphql'
? baseUrl + "graphql"
: baseUrl + "api/" + apiVersion + "/" + endpoint;
_9.label = 1;
case 1:
_g.trys.push([1, 3, , 4]);
_9.trys.push([1, 3, , 4]);
if (process.env.DEBUG) {

@@ -104,11 +107,38 @@ console.log(__assign({ url: url, headers: __assign({ accesstoken: api.accesstoken, 'User-Agent': "workerbase-sdk (" + api.version + ")", 'Content-Type': 'application/json' }, options.headers), method: 'GET' }, options));

case 2:
response = _g.sent();
response = _9.sent();
return [2 /*return*/, response];
case 3:
err_1 = _g.sent();
errorToThrow = new Error("Request to " + url + " failed. " + ((_b = err_1.response) === null || _b === void 0 ? void 0 : _b.status) + " " + (JSON.stringify((_d = (_c = err_1.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.errorMessage) ||
JSON.stringify((_e = err_1.response) === null || _e === void 0 ? void 0 : _e.data) ||
err_1.message));
if ((_f = err_1.response) === null || _f === void 0 ? void 0 : _f.data) {
_a = err_1.response.data, errorMessage = _a.errorMessage, success = _a.success, otherErrorData = __rest(_a, ["errorMessage", "success"]);
err_1 = _9.sent();
errorMessage = err_1.message;
if (((_c = (_b = err_1.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.errorMessage) === 'HTTP Trigger failed' && ((_g = (_f = (_e = (_d = err_1.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.error) === null || _f === void 0 ? void 0 : _f.additionalData) === null || _g === void 0 ? void 0 : _g.errors)) {
errorMessage = (_l = (_k = (_j = (_h = err_1.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.error) === null || _k === void 0 ? void 0 : _k.additionalData) === null || _l === void 0 ? void 0 : _l.errors.map(function (ruleError) {
var _a, _b;
var ruleErrorReason = ((_a = ruleError === null || ruleError === void 0 ? void 0 : ruleError.err) === null || _a === void 0 ? void 0 : _a.message) ? ": " + ((_b = ruleError === null || ruleError === void 0 ? void 0 : ruleError.err) === null || _b === void 0 ? void 0 : _b.message)
: '';
var message = "Connector triggering RuleId " + ruleError.ruleId + " failed " + ruleErrorReason;
return message.trim();
}).join(', ');
}
else if (((_o = (_m = err_1.response) === null || _m === void 0 ? void 0 : _m.data) === null || _o === void 0 ? void 0 : _o.errorMessage) &&
typeof ((_q = (_p = err_1.response) === null || _p === void 0 ? void 0 : _p.data) === null || _q === void 0 ? void 0 : _q.errorMessage) === 'string') {
errorMessage = (_s = (_r = err_1.response) === null || _r === void 0 ? void 0 : _r.data) === null || _s === void 0 ? void 0 : _s.errorMessage;
}
else if (((_u = (_t = err_1.response) === null || _t === void 0 ? void 0 : _t.data) === null || _u === void 0 ? void 0 : _u.errorMessage) &&
typeof ((_w = (_v = err_1.response) === null || _v === void 0 ? void 0 : _v.data) === null || _w === void 0 ? void 0 : _w.errorMessage) === 'object') {
errorMessage = JSON.stringify((_y = (_x = err_1.response) === null || _x === void 0 ? void 0 : _x.data) === null || _y === void 0 ? void 0 : _y.errorMessage);
}
else if ((_z = err_1.response) === null || _z === void 0 ? void 0 : _z.data) {
errorMessage = JSON.stringify((_0 = err_1.response) === null || _0 === void 0 ? void 0 : _0.data);
}
errorToThrow = new Error(errorMessage);
Object.assign(errorToThrow, {
requestUrl: ((_2 = (_1 = err_1 === null || err_1 === void 0 ? void 0 : err_1.request) === null || _1 === void 0 ? void 0 : _1.res) === null || _2 === void 0 ? void 0 : _2.responseUrl) || url,
requestMethod: (_3 = err_1 === null || err_1 === void 0 ? void 0 : err_1.request) === null || _3 === void 0 ? void 0 : _3.method,
requestData: (_4 = err_1 === null || err_1 === void 0 ? void 0 : err_1.config) === null || _4 === void 0 ? void 0 : _4.data,
requestParams: (_5 = err_1 === null || err_1 === void 0 ? void 0 : err_1.config) === null || _5 === void 0 ? void 0 : _5.params,
responseStatusCode: (_6 = err_1 === null || err_1 === void 0 ? void 0 : err_1.response) === null || _6 === void 0 ? void 0 : _6.status,
responseData: (_7 = err_1 === null || err_1 === void 0 ? void 0 : err_1.response) === null || _7 === void 0 ? void 0 : _7.data,
});
if ((_8 = err_1.response) === null || _8 === void 0 ? void 0 : _8.data) {
_a = err_1.response.data, tmpVar1 = _a.errorMessage, tmpVar2 = _a.success, otherErrorData = __rest(_a, ["errorMessage", "success"]);
Object.assign(errorToThrow, otherErrorData);

@@ -122,2 +152,6 @@ }

};
var helloWorld = function () {
console.log('Hello, World!');
};
exports.default = exports.doRequest;
//# sourceMappingURL=doRequest.js.map

@@ -38,6 +38,7 @@ import { WorkerbaseSdk, API } from '..';

basePath: string;
parent: any;
}) => Function>;
internalHostname?: string;
childApis?: Record<string, (workerbase: WorkerbaseSdk, parentAPIPath?: string) => void>;
childApis?: Record<string, (workerbase: WorkerbaseSdk, parentAPIPath?: string, parent?: any) => void>;
transformData?: (data: Item['GET']) => TransformedItem;
}): (workerbase: WorkerbaseSdk, parentAPIPath?: string) => void;
}): (workerbase: WorkerbaseSdk, parentAPIPath?: string, parent?: {}) => void;

@@ -78,14 +78,19 @@ "use strict";

},
_a[BasicMethods.GET] = function (id) { return __awaiter(void 0, void 0, void 0, function () {
var data, item;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, doRequest_1.doRequest(_api, endpoint + "/" + id, {}, internalHostname)];
case 1:
data = (_a.sent()).data;
item = data.data;
return [2 /*return*/, transformData(item)];
}
_a[BasicMethods.GET] = function (id, options) {
if (options === void 0) { options = {}; }
return __awaiter(void 0, void 0, void 0, function () {
var data, item;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, doRequest_1.doRequest(_api, endpoint + "/" + id, {
params: options,
}, internalHostname)];
case 1:
data = (_a.sent()).data;
item = data.data;
return [2 /*return*/, transformData(item)];
}
});
});
}); },
},
_a[BasicMethods.UPDATE] = function (id, changes) { return __awaiter(void 0, void 0, void 0, function () {

@@ -137,5 +142,6 @@ var data, item;

function default_1(params) {
return function (workerbase, parentAPIPath) {
return function (workerbase, parentAPIPath, parent) {
var _this = this;
if (parentAPIPath === void 0) { parentAPIPath = ''; }
if (parent === void 0) { parent = {}; }
var childApis = params.childApis, includeBasics = params.includeBasics, path = params.path, methods = params.methods, internalHostname = params.internalHostname;

@@ -149,3 +155,3 @@ var fullPath = parentAPIPath + path;

Object.keys(childApis).forEach(function (key) {
transformedData[key] = new childApis[key](workerbase, fullPath + "/" + data._id + "/");
transformedData[key] = new childApis[key](workerbase, fullPath + "/" + data._id + "/", data);
});

@@ -165,2 +171,3 @@ return transformedData;

basePath: fullPath,
parent: parent,
});

@@ -167,0 +174,0 @@ });

{
"name": "@workerbase/sdk",
"version": "0.1.14",
"version": "0.1.15-beta.1",
"description": "",

@@ -49,4 +49,5 @@ "scripts": {

"axios": "^0.19.2",
"gql-query-builder": "^3.6.0",
"parseurl": "^1.3.3"
}
}
}

@@ -73,2 +73,3 @@ # Workerbase SDK for JavaScript

await database.items.delete("item2");
await database.items.query({$or: [{field1: "abc"}, {value: {$gt: 4}}]}, {populate: true});
};

@@ -127,2 +128,4 @@ };

**Database.items.query(query, options)** ([Documentation](#database-items-query))
### Events

@@ -156,3 +159,3 @@

**workerbase.events.invoke(id,payload)**
**workerbase.functions.invoke(id,payload)**

@@ -225,6 +228,6 @@ ### Locations

interface ListConfig {
// page number
// Starts at 1
page?: number;
// number of items per page (default 1000)
// Default is 1000
perpage?: number;

@@ -235,3 +238,2 @@

// order of the sorting
order?: 'desc' | 'asc';

@@ -256,1 +258,338 @@

Return the list of all distinct values of the specified field.
## Database Items Query
### Getting started
The `Database.items.query()` function allows complex queries to Workerbase databases.<br>
The following example explains a simple first query:
The example is done with the database `Employee`.
**`Employee` database**
| ID | **Name** | **Email** | **Birthday** | **Department** | **Office** |
| -------- | -------------- | ------------------ | ------------ | --------------------------- | ------------------------ |
| _String_ | _String_ | _String_ | _Date_ | _Reference<br>[Department]_ | _Location_ |
| 1 | Vannie Doulden | vannie@company.com | 12.11.1988 | 1 | North America / New York |
| 2 | Marijo Stede | marijo@company.com | 03.06.1994 | 2 | Europe / Munich |
| 3 | Jade Plante | jade@company.com | 27.02.1990 | 2 | Europe / Munich |
All employees where the birthday is after the `01.01.1989` and the name ends on `Stede` should be queried.
```typescript
const EmployeeDB = async workerbase.databases.get("Employee");
const result = await EmployeeDB.items.query({
Birthday: {
$gt: "01.01.1989"
},
Name: {
$like: "%Stede"
}
})
```
The `result` variable will contain the employee `Marijo Stede`.
```JSON
[
{
"_id": "634fc1e9d64ce49dd554c2db",
"meta": {
"createdAt": "2022-10-19T09:22:49.782Z",
"updatedAt": "2022-10-19T09:27:50.733Z"
},
"database": "634fbc5cd64ce49dd554bdb0",
"payload": {
"Name": "Marijo Stede",
"Email": "marijo@company.com",
"Birthday": "1994-06-03T00:00:00.000Z",
"Department": "634fbc8dd64ce49dd554bde6",
"Office": "634fc1825025ac5347b21ffc"
},
"deleted": false,
"externalId": "2"
}
]
```
Advanced features of the `Database.items.query()` function are described in the sections [Query Operators](#query-operators), [Pagination](#pagination), [Sorting](#sorting), [Population](#population), [Nested Property Queries](#nested-property-queries)
Before looking into advanced features we recomment to review the [Typings Definition](#typings-definition) of the query function.
### Typings Definition
```typescript
type QueryFunction = (query: Query, options: ListOptions): Promise<DatabaseItem[]>
type Query = Record<QueryOperator, string | number | boolean | Query>;
enum QueryOperator {
EQ = '$eq',
NE = '$ne',
GT = '$gt',
GTE = '$gte',
LT = '$lt',
LTE = '$lte',
IN = '$in',
NIN = '$nin',
EXISTS = '$exists',
AND = '$and',
OR = '$or',
LIKE = '$like',
NOT_LIKE = '$notLike',
}
interface ListOptions {
page?: number; // Starts at 1
perpage?: number;
sort?: string;
order?: 'desc' | 'asc';
populate?: boolean;
}
interface DatabaseItem {
_id: string;
externalId?: string;
payload: ItemPayload;
}
type ItemPayload = Record<string, any>;
```
### Query Operators
The following query operators are supported.
```typescript
enum QueryOperator {
// "equals"
EQ = '$eq',
// "not equals"
NE = '$ne',
// "greater than"
GT = '$gt',
// "greater than or equals"
GTE = '$gte',
// "less than"
LT = '$lt',
// "less than or equals"
LTE = '$lte',
// "in"
// Expects a value to be in an array of values.
// Example: Get all employees whose name is in an array with values
// "Vannie Doulden" and "Jade Plante".
// Query: { Name: { $in: [ "Vannie Doulden", "Jade Plante" ] } }
IN = '$in',
// "not in"
// Expects a value not in an array of values.
NIN = '$nin',
// "exists"
// Checks whether a value is defined.
// Example: Get all employees where Department is not defined.
// Query: { Department: { $exists: false } }
EXISTS = '$exists',
// "and"
// Expects multiple conditions to be true.
// Example: Get all employees whose name is
// "Jade Plante" and where Department is "Product Management".
// Query:
// { $and: [
// { Name: "Jade Plante" },
// { Department: "Product Management" }
// ]}
AND = '$and',
// "or"
// Expects one of the conditions to be true
// Example: Get all employees where Department is
// either "Engineering" or "Product Management".
// Query
// { $or: [
// { Department: "Engineering" },
// { Department: "Product Management" }
// ]}
OR = '$or',
// "like"
// Expects a column of type string to match a pattern.
// Patterns can include the wildcards "%" and "_":
// => "%" stands for zero or more arbitrary characters.
// => "_" stands for one arbitrary characters.
// Example: Get all employees where the Name starts with "Jade".
// Query: { Name: { $like: "Jade%" } }
LIKE = '$like',
// "not like"
// Expects a column of type string not to match a pattern.
NOT_LIKE = '$notLike',
}
```
### Pagination
The result of the query function can be paginated with the `page` and `perpage` properties of the `options` parameter.
```typescript
type QueryFunction = (query: Query, options: ListOptions): Promise<DatabaseItem[]>
interface ListOptions {
page?: number; // Starts at 1
perpage?: number;
sort?: string;
order?: 'desc' | 'asc';
populate?: boolean;
}
```
Example: If the database `Employee` contains `100` items, the last 10 items (91-100) can be requested with the following query.
```typescript
const EmployeeDB = async workerbase.databases.get("Employee");
const result = await EmployeeDB.items.query({}, { page: 10, perpage: 10 });
```
### Sorting
Items in the result can be sorted with the `sort` and `order` properties of the `options` parameter.
Example: In order to sort items from the `Employee` database by their age starting with the oldest one, the following query has to be run.
```typescript
const EmployeeDB = async workerbase.databases.get("Employee");
const result = await EmployeeDB.items.query({}, { sort: 'Birthday', order: 'desc' });
```
### Population
Databases Items in Workerbase can have references to other Database Items and to `User`, `Role` and `Location` items in the platform. Those references can be directly included into the result by adding `{ populate: true }` to the options parameter.
Example: The Department column of the `Employee` database is of type `Reference` and points to the database `Department`. Furthermore the `Employee` database has a column `Office` which is of type `Location` and points to locations in the Workerbase platform.
**`Employee` database**
| ID | **Name** | **Email** | **Birthday** | **Department** | **Office** |
|----------|----------------|--------------------|--------------|-------------------------|------------|
| _String_ | _String_ | _String_ | _Date_ | _Reference<br>[Department]_ | _Location_ |
| 1 | Vannie Doulden | vannie@company.com | 12.11.1988 | 1 | North America / New York |
| 2 | Marijo Stede | marijo@company.com | 03.06.1994 | 2 | Europe / Munich |
| 3 | Jade Plante | jade@company.com | 27.02.1990 | 2 | Europe / Munich |
**`Department` database**
| ID | **Name** | **Headcount** |
|----------|--------------------|---------------|
| _String_ | _String_ | _Number_ |
| 1 | Product Management | 10 |
| 2 | Engineering | 25 |
The employee `Vannie Doulden` is queried with the `{ populate: true }` option, which includes the references `Department` and `Location` as objects into the result.
When the `{ populate: true }` option is not set, the `IDs` of the references `Department` and `Location` are added instead of objects to the result.
```typescript
const EmployeeDB = async workerbase.databases.get("Employee");
const result = await EmployeeDB.items.query({ Name: 'Vannie Doulden' }, { populate: true });
```
The `result` variable will contain the employee `Vannie Doulden` with populated references.
```JSON
[
{
"_id": "634fc1b3d64ce49dd554c24c",
"meta": {
"createdAt": "2022-10-19T09:21:55.713Z",
"updatedAt": "2022-10-19T09:22:24.916Z"
},
"database": "634fbc5cd64ce49dd554bdb0",
"payload": {
"Name": "Vannie Doulden",
"Email": "vannie@company.com",
"Birthday": "1988-11-12T00:00:00.000Z",
"Department": {
"_id": "634fbc85d64ce49dd554bdd1",
"meta": {
"createdAt": "2022-10-19T08:59:49.755Z",
"updatedAt": "2022-10-19T09:00:04.742Z"
},
"database": "634fbc75d64ce49dd554bdc2",
"payload": {
"Name": "Product Management",
"Headcount": 10
},
"deleted": false,
"externalId": "1"
},
"Office": {
"_id": "634fc1905025ac5347b22020",
"externalId": "649f9110-4f8f-11ed-967c-0b922c03bae6",
"name": "North America / New York"
}
},
"deleted": false,
"externalId": "1"
}
]
```
### Nested Property Queries
In order to filter on references of items, conditions on nested properties have to be added to the `query` parameter. Nested properties are specified by adding `$` in the beginning and end of a path i.e. `$Office.Name$`.
> Please keep in mind that nested property queries can only be used when `{ populate: true }` is added to the `options` parameter.
Example: All employees whose `Department` has a `Headcount` less than `20` should be queried.
The example is based on the data shown in the section [Population](#population).
```typescript
const EmployeeDB = async workerbase.databases.get("Employee");
const result = await EmployeeDB.items.query({ "$Department.Headcount$": { $lt: 20 } }, { populate: true });
```
The `result` variable will contain the employee `Vannie Doulden`.
```JSON
[
{
"_id": "634fc1b3d64ce49dd554c24c",
"meta": {
"createdAt": "2022-10-19T09:21:55.713Z",
"updatedAt": "2022-10-19T09:22:24.916Z"
},
"database": "634fbc5cd64ce49dd554bdb0",
"payload": {
"Name": "Vannie Doulden",
"Email": "vannie@company.com",
"Birthday": "1988-11-12T00:00:00.000Z",
"Department": {
"_id": "634fbc85d64ce49dd554bdd1",
"meta": {
"createdAt": "2022-10-19T08:59:49.755Z",
"updatedAt": "2022-10-19T09:00:04.742Z"
},
"database": "634fbc75d64ce49dd554bdc2",
"payload": {
"Name": "Product Management",
"Headcount": 10
},
"deleted": false,
"externalId": "1"
},
"Office": {
"_id": "634fc1905025ac5347b22020",
"externalId": "649f9110-4f8f-11ed-967c-0b922c03bae6",
"name": "North America / New York"
}
},
"deleted": false,
"externalId": "1"
}
]
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc