Created
July 29, 2017 17:41
-
-
Save michael-mckenna/60825702faf8b3f70b1652a94978ecfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("Starting function"); | |
// TODO: find a way to pull the admin_token.base64 without copying and pasting it | |
const Realm = require('realm'); | |
const adminUser = Realm.Sync.User.adminUser('******'); | |
const server_url = 'realm://****.com:9080'; | |
module.exports = function(changeEvent) { | |
console.log('Change detected at path: ' + changeEvent.path); | |
const changes = changeEvent.changes["UserSearch"]; | |
// no reason to do any work if there are no requests | |
if (typeof changes === "undefined") | |
return; | |
if (changes.insertions.length === 0 && changes.modifications.length === 0) | |
return; | |
const userSchema = { | |
name: 'User', | |
properties: { | |
username: 'string' | |
} | |
}; | |
let accountsRealm = new Realm({ sync: {user: adminUser, url: server_url + '/userObjects'}, schema: [userSchema] }); | |
const searchRealm = changeEvent.realm; // workaround for GN returning new instance on every access | |
const requests = searchRealm.objects("UserSearch"); | |
const accounts = accountsRealm.objects("User"); | |
// Find users matching the request | |
searchRealm.write(() => { | |
// handle new requests | |
changes.insertions.forEach((index) => { | |
const obj = requests[index]; | |
if (obj.pattern !== "") { | |
const matches = accounts.filtered("username CONTAINS[c] $0", obj.pattern); | |
matches.forEach((match) => { | |
obj.users.push({ username: match.username }); | |
}); | |
} | |
obj.resultPattern = obj.pattern; | |
}); | |
// live update result if request is modified | |
for (let index of changes.modifications) { | |
const obj = requests[index]; | |
if (obj.pattern === "") { | |
searchRealm.delete(obj.users); | |
console.log("Deleted all"); | |
} | |
else { | |
const matches = accounts.filtered("username CONTAINS[c] $0", obj.pattern); | |
// remove users that are no longer matching | |
const toDelete = []; | |
for (let profile of obj.users) { | |
if (matches.filtered("username == $0", profile.username).length === 0) { | |
toDelete.push(profile); | |
console.log("Delete " + profile.uername); | |
} | |
} | |
searchRealm.delete(toDelete); | |
// add new matches | |
for (let match of matches) { | |
if (obj.users.filtered("username == $0", match.username).length === 0) { | |
console.log("Matched: " + match.username); | |
obj.users.push({username: match.username}); | |
} | |
} | |
} | |
obj.resultPattern = obj.pattern; | |
} | |
}); | |
}; | |
// ---------------------------------------------------------------------------------------------- | |
// NOTICE: The Developer Edition is currently limited to 3 running functions in the Beta version. | |
// Other limitations may apply in the final release. | |
// Professional and Enterprise Editions does not have any limitations. | |
// ---------------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment