How to delete all objects of Class in Parse Server? - parse-platform

I want to delete all object of my class with jobs. I have created my job and I wrote below code to achieve my goal but my code did not work.
Parse.Cloud.job("deleteWeeklyScore", (request) => {
const mySchema = new Parse.Schema('WeeklyGameScore');
mySchema.purge();
});
I can delete field from my schema but delete all objects does not work

purge() returns a promise. You have to resolve your promise.
schema.purge().then(...) or await schema.purge() or return schema.purge()
I don't know if Job resolves a promise.

Related

how graphql tools addSchemaLevelResolver function works

As I understood addSchemaLevelResolver function add a root level resolver on schema and cut off the all other resolvers. And I tried that method like in the below.
if (process.env.DEV === true) {
schema = addSchemaLevelResolver(schema, () => {
throw new Error("Internal Server Error!");
});
}
Yes this function resolving and throw error when first call on some query or mutation on the schema. But after that it's not throwing the error as I expected and not blocking resolvers in the schema. Is this function only work for first call or is that a bug or am I doing it wrong?
addSchemaLevelResolver has been removed in favor of Resolvers Composition.
I believe addSchemaLevelResolver and the new Resolvers Composition would literally traverse the schema and add resolve function to each Type and Field.

How to clone a model in tensorflowjs

After training one model on a set of data, I'd like to get a copy of it and train each copy on different data sets. Is there a way to clone a model in such a way that after the cloning each of them can be trained separately?
I've already tried to save the model in my localstorage and generate a copy from there but tensorflow complains that some variable names are already in use.
In case someone else is interested, I have eventually solved it getting all the weights of the model I wanted to copy with the getweights() function, creating a new model with the same architecture and then copying back the other model's weight to it with setweights()
Step 1. You extract the model with tf.io.withSaveHandler via save(), it's callback style so you may want to wrap it with a Promise for modern codebases.
const modelArtifacts = await new Promise<tf.io.ModelArtifacts>(resolve => {
model.save(
tf.io.withSaveHandler(artifacts => {
resolve(model);
// Optional, prevents type error.
return Promise.resolve({
modelArtifactsInfo: {
dateSaved: new Date(),
modelTopologyType: "JSON"
}
});
})
);
});
Step 2. You load the extracted contents into a new model with tf.io.fromMemory.
const newModel = await tf.loadLayersModel(tf.io.fromMemory(modelArtifacts));

NGXS State documentation

I'm new to NGXS and I'm trying to fully understand the docs so I can start using it knowing what I'm doing.
There is one thing I don't understand in this code snippet from here.
export class ZooState {
constructor(private animalService: AnimalService) {}
#Action(FeedAnimals)
feedAnimals(ctx: StateContext<ZooStateModel>, action: FeedAnimals) {
return this.animalService.feed(action.animalsToFeed).pipe(tap((animalsToFeedResult) => {
const state = ctx.getState();
ctx.setState({
...state,
feedAnimals: [
...state.feedAnimals,
animalsToFeedResult,
]
});
}));
}
}
Just below this code, it says:
You might notice I returned the Observable and just did a tap. If we
return the Observable, the framework will automatically subscribe to
it for us, so we don't have to deal with that ourselves. Additionally,
if we want the stores dispatch function to be able to complete only
once the operation is completed, we need to return that so it knows
that.
The framework will subscribe to this.animalService.feed, but why?
The action, FeedAnimals, uses the injected service, AnimalService to feed the animals passed in the action's payload. Presumably the service is operates asynchronously and returns an Observable. The value of that Observable is accessed via the tap function and is used to update the ZooState state context based on completing successfully.
In order to use NGXS specifically and Angular in general, you really have to understand RxJS... here's my goto doc page for it

Saving objects in Cloud Code function uses master key instead of user key

I have a Cloud Code function that is being called by users. In that function, I create a new object, "Invitation", which triggers a before_save handler when saving.
Since a user is running the Cloud Code function, I would expect request.user in the before_save function to be the same, but it appears to be saving the Invitation object as master, resulting in request.user being null.
Parse.Cloud.define("sendInvite", function(request, response) {
var invitation = new Invitation();
invitation.save().then(function(invitation){
response.success();
}, function(error){
response.error(error);
});
});
Parse.Cloud.beforeSave("Invitation", function(request, response) {
var sender = request.user;
// sender is null, since master key is being used...
});
This doesn't seem like correct behaviour – the cloud code function shouldn't default to executing using the master key unless explicitly told to do so.
Any thoughts?
The issue was that Parse.Cloud.useMasterKey() was used outside of a function in one of our requires for Parse Hosting (require(cloud/app.js)).
The two cases when CloudCode uses the master key are when the CloudCode calls Parse.Cloud.useMasterKey() or when the CloudCode was invoked by a caller which uses the master key. Check whether sendInvites is being called from an environment which uses the master key.

Updating existing Parse object in Cloud Code

I am using Parse for my backend and want to search for existing friend requests and update those instead of creating new ones (if there is already an existing one).
I thought I figured out how to do it but when I submit new friend requests they get created as new objects instead of updating the old one, even though I found an existing request.
Here is the code I am using:
Parse.Cloud.beforeSave("FriendRequest", function(request, response) {
//search for an existing friend request with the same "from" and "to"
var query = new Parse.Query("FriendRequest");
query.equalTo("from", request.object.get("from"))
.equalTo("to", request.object.get("to"));
query.find({
success: function(results) {
if(results.length > 0)
{
var result = results[0];
//the new request id is undefined as expected
console.log("request id: " + request.object.id);
//the result id is valid for an object in the db as expected
console.log("result id: " + results[0].id);
//set the id of the request to the id of the existing db object
request.object.id = results[0].id;
//the valid id is now in the request object id
console.log("request id: " + request.object.id);
//after response.success, the database shows a new entry
//with a different id
//instead of updating the existing entry
response.success();
}
}
});
});
There isn't a lot going on here. The query does come back successful with the correct entry in the database. I can confirm that I get the correct objectId for the existing item in the database. Any thoughts or ideas are appreciated!
You can't manually set the objectId of an object.
If you want beforeSave to NOT create a new object (which is what you're about to do when beforeSave is called), you need to manually update the existing object and then respond with a failure. If you respond with response.success(), the object will be saved normally.
In your code, you don't seem to make any changes to the existing object. All you really need to do is to return response.error (https://parse.com/docs/cloud_code_guide#functions-onsave)
Of course, you should also handle this in your code somehow. Either by alerting the user, or handling it silently.
However; why does your code attempt to save a new friend request if one already exist? Your app should know that one exists and disable the friend request button or whatever the UI offers.

Resources