Skip to content

Instantly share code, notes, and snippets.

@mpobrien
Created February 25, 2020 21:42
Show Gist options
  • Save mpobrien/ca6a10dc87d8d4f6c1a22f09826c6758 to your computer and use it in GitHub Desktop.
Save mpobrien/ca6a10dc87d8d4f6c1a22f09826c6758 to your computer and use it in GitHub Desktop.
const Realm = require('realm');
const uuid = require('uuid').v4;
const process = require('process');
const argv = process.argv.slice(2);
const STITCH_APP_ID = 'foo2-tqxot';
Realm.Sync.setLogLevel('all');
// Uncomment these instead to hit ROS
/*
const SERVER_URL = "http://localhost:9080"
const REALM_SERVER_URL = 'realm://localhost:9080/~/foo';
const creds = Realm.Sync.Credentials.usernamePassword("mikeo", "password")
*/
const SERVER_URL = `http://localhost:8080/api/realm/v1.0/app/${STITCH_APP_ID}`;
const REALM_SERVER_URL = 'realm://localhost:8282/';
const creds = Realm.Sync.Credentials.anonymous()
const TestSchema = {
name: 'Test',
primaryKey: '_id',
properties: {
_id: 'string',
owner: 'string',
},
};
function repaint(items) {
console.log('\n');
items.forEach(item => {
console.log(item.owner, item._id);
});
}
async function listsFullSync(user) {}
(async () => {
const user = await Realm.Sync.User.login(
SERVER_URL,
creds,
);
const config = await user.createConfiguration({
schema: [TestSchema],
sync: {
url: REALM_SERVER_URL,
custom_http_headers: {
stitch_app_id: STITCH_APP_ID,
},
fullSynchronization: true,
},
});
console.log('OK OPENING REALM....');
const realm = await Realm.open(config);
console.log('DONE.');
const items = realm.objects('Test');
if(argv[0] == "write"){
console.log("writing!")
realm.write(() => {
realm.create('Test', {
owner: user.identity,
_id: uuid(),
});
});
}
repaint(items);
items.addListener(change => {
console.log("got a change.")
repaint(items);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment