Data Ingestを使用して、クライアント アプリケーションから Flexible Sync が有効化されている Atlas App Services アプリにデータをストリーミングできます。
クラウドにデータを送信する気象センサーなど、IoT アプリケーションでデータを一方向に同期したい場合があります。 Data Ingest は、小売アプリからの請求書作成や、アプリケーション イベントのログ記録など、競合の解決を必要としない他のタイプの不変データを書き込む場合にも役立ちます。
Data Ingest は、クライアント側の挿入専用ワークロードのパフォーマンスを向上させるために最適化されています。
クライアント アプリケーションから一方向にデータを同期
非対称オブジェクトの定義
非対称オブジェクトはデータを一方向に同期します。 オブジェクトモデルで 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", }, }; }
非対称オブジェクトを定義する方法の詳細については、「 非対称オブジェクトの定義 」を参照してください。
App Services Appによる接続と認証
クライアントからバックエンド アプリにデータをストリーミングするには、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);
Data Ingest は Flexible Sync の機能であるため、接続するアプリはFlexible Sync を使用する必要があります。
Realm を開く
認証されたユーザーが作成されたら、Flexible Sync構成オブジェクトを使用して同期された Realm を開くことができます。
const realm = await Realm.open({ schema: [WeatherSensor], sync: { user: app.currentUser, flexible: true, }, });
双方向同期とは異なり、Data Ingest はFlexible Sync サブスクライブを使用しません。
非対称オブジェクトをクエリしたり、ローカル Realm に書き込んだりすることはできないため、非対称オブジェクトは双方向 Flexible Sync、パーティションベースの同期、またはローカル Realm の使用と互換性がありません。
非対称オブジェクトの作成
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 は、このデータのライフサイクルを完全に管理します。 Data Ingest 同期が完了するまでデバイス上に保持され、その後デバイスから削除されます。