Created
August 2, 2017 01:08
-
-
Save michael-mckenna/76eedf5ce55db9e279098718b2ab26ae 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"); | |
const Realm = require('realm'); | |
const adminUser = Realm.Sync.User.adminUser('**') | |
const server_url = 'realm://**'; | |
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 globalUserSchema = { | |
name: 'GlobalUser', | |
properties: { | |
email: {type: 'string', indexed: true}, | |
username: {type: 'string', indexed: true} | |
} | |
}; | |
let accountsRealm = new Realm({ sync: {user: adminUser, url: server_url + '/globalUsers', schema: [globalUserSchema] }}); | |
const searchRealm = changeEvent.realm; // workaround for GN returning new instance on every access | |
const requests = searchRealm.objects("UserSearch"); | |
const accounts = accountsRealm.objects("GlobalUser"); | |
// 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 = []; | |
if(obj.users.length > 0) { | |
obj.users.forEach((profile) => { | |
if (matches.filtered("email == $0", profile.email).length == 0) { | |
toDelete.push(profile); | |
console.log("Delete " + profile.email); | |
} | |
}); | |
} | |
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