JavaScript SDK
About Hexabase JavaScript SDK
Supported environments and software
The following environment and software are required to use the Hexabase JavaScript SDK:
- OS
Windows, macOS, Linux - Node.js
Node.js supports the latest two versions.
Getting started
Hexabase JavaScript SDK is used in Node.js applications (including for the front-end). First, create a directory.
$ mkdir my-app
$ cd my-app
Next, initialize the Node.js project.
$ npm init -y
If the command is successful、the file package.json
with the following contents will be created:
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Installation of SDK
Continue by installing the Hexabase JavaScript SDK.
npm i @hexabase/hexabase-js
If the command is successful, a directory called node_modules
will be created, and the Hexabase JavaScript SDK will be installed. A file called package-lock.json
will be created as well.
The preparations are now complete.
SDK usage
Demo accounts and workspaces are available; please see the information below for further details.
Variable name | Item | Value |
---|---|---|
workspaceId | Workspace ID | demo_workspace |
projectId | Project ID | sdk |
datastoreId | Datastore ID | hello |
Login email address | [email protected] | |
password | Login password | hexabase.com5678 |
First, prepare a JavaScript file. Please name the file index.mjs
(not .js).
Import Hexabase SDK.
import { HexabaseClient } from '@hexabase/hexabase-js';
Next, initialize HexabaseClient.
const client = new HexabaseClient();
Define variables
Define the variables listed above
const workspaceId = 'demo_workspace';
const projectId = 'sdk';
const datastoreId = 'hello';
const email = '[email protected]';
const password = 'hexabase.com5678';
Login
Since Hexabase makes extensive use of asynchronous processing, the entire process is wrapped with async
.
(async () => {
// Write the following code here
})();
First, login
await client.login({email, password});
Set up a workspace project
After logging in successfully, set up your workspace.
await client.setWorkspace(workspaceId);
Continue to get the project.
const project = await client.currentWorkspace.project(projectId);
Finally, get the datastore.
const datastore = await project.datastore(datastoreId);
Try saving the data
Now, let's save data to this datastore. Our datastore Hello
has the following fields:
Field name(English) | Field name(Japanese) | Field type |
---|---|---|
message | メッセージ | String |
First, create the data.
const item = await datastore.item();
Next, set the fields for this item
.
item.set('message', 'Hello, Hexabase!');
Save your data.
await item.save();
If it is saved properly, you will get the ID of the item.
console.log(item.id);
Execute the code
Now let's execute this code.
node index.mjs
If a string like 653f41f032d80f256a06015f
is returned, it is a success.
The entire code
The code up to this point can be summarized as follows:
// Import Hexabase JavaScript SDK
import { HexabaseClient } from '@hexabase/hexabase-js';
// Initialize HexabaseClient
const client = new HexabaseClient();
// Demo workspace, project, credentials, etc.
const workspaceId = 'demo_workspace';
const projectId = 'sdk';
const datastoreId = 'hello';
const email = '[email protected]';
const password = 'hexabase.com5678';
(async () => {
// Login
await client.login({email, password});
// Set up a workspace
await client.setWorkspace(workspaceId);
// Get project
const project = await client.currentWorkspace.project(projectId);
// Get datastore
const datastore = await project.datastore(datastoreId);
// Create new data
const item = await datastore.item();
// Set field
item.set('message', 'Hello, Hexabase!');
// Save data
await item.save();
// Show item ID
console.log(item.id);
})();
For more information on how to use the SDK, select a class from the menu on the right.
License
Hexabase JavaScript SDK is available at MIT licensed.