块状SDK
Bulbthings SDK允许您方便地与Bulbthings API互动. SDK兼容任何支持JavaScript/TypeScript的环境,包括网页浏览器,React Industrial或Node.js应用程序.
更多关于Bulbthings的资讯: https://bulbthings.com (中文(简体) )
完整文档
安装
npm install @bulbthings/bulbthings-sdk
认证
import { Bulbthings } from '@bulbthings/bulbthings-sdk';
const bulbthings = new Bulbthings({ apiToken: '83945b4eaf...7a37a' });
// Authentication can also be overridden per request
bulbthings.entities.findAll({ apiToken: '83945b4eaf...7a37a' });
快速启动示例
// Fetch your workspace
const workspace = await bulbthings.companies.findById('WORKSPACE_ID');
// Create a category of entities
const customCategory = await bulbthings.entityTypes.create({
companyId: workspace.id,
label: 'Category',
});
// Show the category in the workspace
await bulbthings.catalogMappings.create({
companyId: workspace.id,
entityTypeId: customCategory.id,
catalogId: workspace.catalogId,
});
// Create an entity
const entity = await bulbthings.entities.create({
companyId: workspace.id,
entityTypeId: customCategory.id,
});
// Create a custom field
const customField = await bulbthings.attributeTypes.create({
companyId: workspace.id,
schema: { title: 'Custom property', type: 'string' },
});
// Show the field on the category profiles
await bulbthings.entityTypeMappings.create({
companyId: workspace.id,
entityTypeId: customCategory.id,
type: 'attributeType',
attributeTypeId: customField.id,
});
// Update the field value for the entity
await bulbthings.measurements.create({
entityId: entity.id,
attributeTypeId: customField.id,
value: 'test',
});
API 资源
大多数API资源都采用相同的CRUD模式,方法如下:
查找全部
此方法返回资源列表 .
const entities = await bulbthings.entities.findAll({
filter: `and(
gt(attributes.mileage.km, 10000),
ne(attributes.license, null)
)`,
include: ['entityType'],
page: { limit: 3 },
});
// Or with pagination metadata
const { data: entities, meta } = await bulbthings.entities.findAll(
{ page: { limit: 3 } },
true
);
找到 ById 键盘
此方法返回单一资源, 使用其标识符 :
const entity = await bulbthings.entities.findById('31845...c8ad6e', {
include: ['entityType'],
});
创建
这种方法创建了新的资源:
const measurement = await bulbthings.measurements.create({
entityId: entity.id,
attributeTypeId: 'mileage',
value: 42000,
period: {
from: new Date(),
to: new Date(),
},
unitId: 'km',
});
更新ById
这种方法更新了资源 :
const updatedMeasurement = await bulbthings.measurements.update(
measurement.id,
{ value: 43000 }
);
删除字节
此方法删除资源 :
await bulbthings.entities.deleteById(entity.id);
活动
每次创建、修改或删除资源都会引发系统事件(不与实际的 events 资源您可以使用 SDK 实时接收和反应.
示例用途
// Event types follow the format: [resource][Created|Updated|Deleted]
const eventType = 'entityCreated';
bulbthings.on([eventType], (event) => {
// `data.resource` contains the resource described by the event
// In case of an update event, `data.previousData` contains
// the values of updated properties before the change
const entity = event.data.resource as Entity;
});