Live Query on client side, need to remove fields

Im using the JS SDK and Cloud Code, I have setup a LiveQuery server, everything works fine but I am really not happy having the main ParseQuery code client side in the app, Ideally this would be done behind the scenes where I could filter some user sensitive fields out before it hits the client side. I know there is protectedFields but is this kind of code below really. supposed to sit client side ? am i doing it right ?

chatSync = async () => {
    const currentUID = this._api.current().id
    const currentSession = this._api.current().getSessionToken()     
    const query = new Parse.Query('SpecialObject').contains('users', currentUID );
    const subscription = await query.subscribe(currentSession);
    let data: Array<any> = [];
    subscription.on('open', async () => {
        let results = await query.find()
        let threadsParse =  JSON.parse(JSON.stringify(results)) 
        for (let index = 0; index < threadsParse.length; index++) {
            const thread = threadsParse[index];
            data.push({...thread})
            console.log(`data ${JSON.stringify(data)}`)        
        }
    });
}  
1 Like

I also need to do some stuff with masterkey, how can i work with cloud code and still subscribe to realtime events

You can use live query just for receive the notification that something changed and then you can call a cloud code function to retrieve the sensitive data.

You can also use live query triggers to manipulate the data before sending to the client on the server side: Cloud Code Guide | Parse

1 Like

awesome, had no idea there was separate triggers for live queries. thanks man

@davimacedo i need to fetch a profile pointer and then strip some fields out of it before returning to the query clientside, i dont see any way to find with masterkey in the following hook, is there a way to do this ?

Parse.Cloud.beforeSubscribe('MyObject', request => {
    if (!request.user.get('Admin')) {
        throw new Parse.Error(101, 'You are not authorized to subscribe to MyObject.');
    }
    let query = request.query; // the Parse.Query
    query.select("name","year")
});

You can use the { useMasterKey: true } option as you would do in any other trigger or cloud function.