Docs 菜单
Docs 主页
/ /
Atlas Device SDKs
/ /

将数据流式传输到 Atlas - Node.js SDK

在此页面上

  • 从客户端应用程序单向同步数据
  • 定义非对称对象
  • 使用 App Services App 连接并进行身份验证
  • 打开 Realm
  • 创建非对称对象

您可以使用数据导入将数据从客户端应用程序流式传输到启用了 Flexible Sync 的 Atlas App Services App。

您可能希望在物联网(IoT)应用程序中单向同步数据,例如将数据发送到云的天气传感器。数据导入对于写入不需要冲突解决的其他类型的不可变数据也很有用,例如从零售应用程序创建发票或记录应用程序事件。

数据导入经过优化,可为繁重的客户端仅插入工作负载提供性能改进。

1

非对称对象单向同步数据。 通过在对象模型中将 asymmetric设置为true来定义非对称对象

class WeatherSensor extends Realm.Object {
static schema = {
name: "WeatherSensor",
// Sync WeatherSensor objects one way from your device
// to your Atlas database.
asymmetric: true,
primaryKey: "_id",
properties: {
_id: "objectId",
deviceId: "string",
temperatureInFahrenheit: "int",
barometricPressureInHg: "float",
windSpeedInMph: "float",
},
};
}
class WeatherSensor extends Realm.Object<WeatherSensor> {
_id!: Realm.BSON.ObjectId;
deviceId!: string;
temperatureInFahrenheit!: number;
barometricPressureInHg!: number;
windSpeedInMph!: number;
static schema: ObjectSchema = {
name: "WeatherSensor",
// sync WeatherSensor objects one way from your device
// to your Atlas database.
asymmetric: true,
primaryKey: "_id",
properties: {
_id: "objectId",
deviceId: "string",
temperatureInFahrenheit: "int",
barometricPressureInHg: "float",
windSpeedInMph: "float",
},
};
}

有关如何定义非对称对象的更多信息,请参阅定义非对称对象。

2

要将数据从客户端流式传输到后端应用程序,您必须连接到 App Services 后端并对用户进行身份验证。

// Create an anonymous credential
const credentials = Realm.Credentials.anonymous();
const user = await app.logIn(credentials);
// Create an anonymous credential
const credentials = Realm.Credentials.anonymous();
const user = await app.logIn(credentials);

数据导入是Flexible Sync的一项功能,因此您连接的应用程序必须使用Flexible Sync。

3

拥有经过身份验证的用户后,您可以使用 Flexible Sync配置对象打开同步 Realm。

const realm = await Realm.open({
schema: [WeatherSensor],
sync: {
user: app.currentUser,
flexible: true,
},
});

与BI不同,数据导入不使用Flexible Sync 订阅。

您无法查询非对称对象或将其写入本地 Realm,因此非对称对象与双向 Flexible Sync、基于分区的同步或本地 Realm 使用不兼容。

4

打开 Realm 后,您可以使用Realm.create()在写事务中创建不对称对象。 创建非对称对象时, Realm.create()会返回undefined ,而不是对象本身。

realm.write(() => {
realm.create(WeatherSensor, {
_id: new BSON.objectId(),
deviceId: "WX1278UIT",
temperatureInFahrenheit: 66.7,
barometricPressureInHg: 29.65,
windSpeedInMph: 2,
});
});

您无法读取这些对象。 创建后,它们会同步到 App Services 后端和链接的 Atlas 数据库。

Atlas Device Sync 全面管理这些数据的生命周期。它会保留在设备上,直到数据摄入同步完成,然后从设备中删除。

后退

设置客户端日志级别