Workerbase SDK for JavaScript
Installing
Install with npm
npm install @workerbase/sdk
Typescript
Workerbase SDK fully supports typescript with .d.ts
files and the types will be maintained in the next releases.
Usage
You can use a default import with typescript
import workerbase from '@workerbase/sdk';
However we recommend using the following syntax with CommonJS to get the typings for autocomplete.
const { Workerbase } = require('@workerbase/sdk');
Authentication setup
You can configure the Workerbase SDK authentication options wiht the auth
mehtod:
workerbase.auth({
url: 'https://myWorkerbaseDomain.workerbase.io',
token: TOKEN,
});
If you don't use the auth
method will use the following env variables as token and url
WB_ACCESS_TOKEN=token
WB_API_URL=url
However if you are using the Workerbase SDK directly in a Workerbase Function, you don't need to use the specify the authentication config as the SDK will directly use the environment variables within the Workerbase Function.
Example
const workerbase = require('@workerbase/sdk');
const updateUserName = async (userId) => {
const { firstName } = await workerbase.users.get(userId);
await workerbase.users.update(userId, { firstName: firstName + "v2" });
};
const createDbAndInsertItems = async () => {
const database = await workerbase.databases.create({
name: "test",
columns: [
{ name: "field1", type: "String" },
{ name: "value", type: "Integer" },
],
});
await database.items.create({ field1: "abc" }, "item1");
await database.items.update("item1", { payload: { value: "5" } });
await database.items.create({ field1: "abcd" }, "item2");
await database.items.delete("item2");
};
};
const getAllUserDepartments = () =>
workerbase.users.list().distinctBy("department");
const getAllUsersWithName = (userLastname) =>
workerbase.users.list().filterBy({ lastName: userLastname });
const getAllUserDepartmentsWithName = (userLastName) =>
workerbase.users
.list()
.filterBy({ lastName: userLastName })
.distinctBy("department");
const invokeFunction = async (functionId) => {
try {
return await workerbase.functions.invoke(functionId, { some: "data" });
} catch (err) {
console.log(err.logs);
}
};
Methods
Databases
workerbase.databases.list([listConfig])
workerbase.databases.get(id)
workerbase.databases.create(value)
workerbase.databases.update(id,value)
workerbase.databases.delete(id)
Database Items
Database.items.list([listConfig])
Database.items.get(id)
Database.items.create(value[,externalId])
Database.items.createMany(values)
Database.items.update(id,value)
Database.items.delete(id)
Events
workerbase.events.list([listConfig])
workerbase.events.get(id)
workerbase.events.create(value)
workerbase.events.update(id,value)
workerbase.events.delete(id)
workerbase.events.trigger(id,payload)
Functions
workerbase.functions.list([listConfig])
workerbase.functions.get(id)
workerbase.functions.create(value)
workerbase.functions.update(id,value)
workerbase.functions.delete(id)
workerbase.functions.deploy(id)
workerbase.events.invoke(id,payload)
Locations
workerbase.locations.list([listConfig])
workerbase.locations.get(id)
workerbase.locations.create(value)
workerbase.locations.update(id,value)
workerbase.locations.delete(id)
workerbase.locations.getPartsByLevel(levelId)
workerbase.locations.getLevels()
Media
workerbase.media.list([listConfig])
workerbase.media.get(id)
workerbase.media.delete(id)
Roles
workerbase.roles.list([listConfig])
workerbase.roles.get(id)
workerbase.roles.create(value)
workerbase.roles.update(id,value)
workerbase.roles.delete(id)
Skills
workerbase.skills.list([listConfig])
workerbase.skills.get(id)
workerbase.skills.create(value)
workerbase.skills.update(id,value)
workerbase.skills.delete(id)
Users
workerbase.users.list([listConfig])
workerbase.users.get(id)
workerbase.users.create(value)
workerbase.users.update(id,value)
workerbase.users.delete(id)
List documentation
ListConfig
interface ListConfig {
page?: number;
perpage?: number;
sort?: string;
order?: 'desc' | 'asc';
fields?: string;
}
List Response Methods
We added some builtin methods in Workerbase SDK to help you work with the data on the list responses.
See the examples above, to see how to use them.
list().filterBy(condition)
Filter the list to keep only the values that match the condition object.
list().distinctBy(field)
Return the list of all distinct values of the specified field.