docs/replication-microsoft-onedrive.html
On this page
The replication-microsoft-onedrive plugin allows you to replicate your client-side RxDB database to a folder in the user's Microsoft OneDrive. This enables cross-device sync for single users without requiring any backend server.
The replication uses the Microsoft Graph API.
Beta Feature (available since version 17.0.0)
This feature is in beta and may have breaking changes without a major RxDB version release.
1
You need to register your application in the Azure portal and create credentials (OAuth 2.0 Client ID) with Files.ReadWrite permissions for your application.
2
Your application must handle the OAuth flow to get an accessToken from Microsoft. You can use libraries like @azure/msal-browser or @azure/msal-react.
3
Once you have the accessToken, you can start the replication.
import {
replicateMicrosoftOneDrive
} from 'rxdb/plugins/replication-microsoft-onedrive';
const replicationState = await replicateMicrosoftOneDrive({
replicationIdentifier: 'my-app-onedrive-sync',
collection: myRxCollection, // RxCollection
oneDrive: {
authToken: 'USER_ACCESS_TOKEN',
folderPath: 'my-app-data/user-1'
},
live: true,
pull: {
batchSize: 60,
modifier: doc => doc // (optional) modify invalid data
},
push: {
batchSize: 60,
modifier: doc => doc // (optional) modify before sending
}
});
// Observe replication states
replicationState.error$.subscribe(err => {
console.error('Replication error:', err);
});
replicationState.awaitInitialReplication().then(() => {
console.log('Initial replication done');
});
Microsoft OneDrive does not provide real-time events for file changes that a client can easily subscribe to in the browser. If a user changes data on User Device A , User Device B would not know about it until it periodically polls the API. To achieve real-time updates, this plugin uses WebRTC to signal changes between connected devices.
signaling subfolder on OneDrive.WebRTC is native in browsers but requires a polyfill in Node.js.
import wrtc from 'node-datachannel/polyfill'; // or 'wrtc' package
// ...
const replicationState = await replicateMicrosoftOneDrive({
// ...
signalingOptions: {
wrtc // Pass the polyfill here
}
});
authToken string: The valid access token associated with the user.
folderPath string: The path to the folder in Microsoft OneDrive where data should be stored.
docs (for data) and signaling (for WebRTC).apiEndpoint string (optional): Defaults to https://graph.microsoft.com/v1.0/me/drive. Useful for mocking or proxies.
transactionTimeout number (optional): Default 10000 (10s). The plugin uses a transaction.json file in OneDrive to ensure data integrity during writes. This is the timeout after which a lock is considered stale.
Standard RxDB Replication Options for batch size, modifiers, etc.
docs subfolder.[primaryKey].json.lastModifiedDateTime of files in Microsoft OneDrive.transaction.json file is locked by another device, the write retries until the lock is released or times out.For testing, it is recommended to use microsoft-onedrive-mock. It simulates the Microsoft Graph API so you can run tests without real credentials.