How to apply aftersave on dynamic class

Hi team,
We have a dynamic class, I want to do some operations on after save and also want to use multiple cloud triggers on these dynamic classes. is it possible?

This is how we have created a dynamic class of students for each school.
image

You can run Parse.Cloud.beforeSave() (for example) at any time from cloud code. So it would actually be possible to do it on the fly after creating a certain dynamic class, but you’d have to make sure that the code run in all your parse server instances. You can also run a custom code in cloud code initialization, check the schema and dynamically add a trigger to the classes you want.

Hi @davimacedo , pardon for asking but Can you please help me out by sharing an example code? how to write down that cloud code to achieve so.
I have these Student’s classes, I want to update “qr” field on after save. records on these classes
image

I don’t have an example code but I’d try the following steps:

  • In main.js or in your app.js (depending on your deployment), right after Parse Server initialization, I’d retrieve all schemas using the schema API: JavaScript Developers Guide | Parse
  • Then I’d filter out all classes that start with Students_
  • Then, for each of them, I’d call Parse.Cloud.afterSave(className, someFunction)

Please have it a try and share the code you’ve tried. Then we can try to help you to figure it out.

1 Like

hi @davimacedo, Sure I will give it a try and let you know.

Hi @davimacedo Thanks for giving a direction.
Here is the code, written in function.js which is imported in main.js of cloud code

const getClassNamesFromSchema = async () => {

const schemas =

const mySchema = await Parse.Schema.all()

mySchema.map(el => el.className.startsWith(“Student”) && schemas.push(el.className))

console.log(“mySchema”, schemas)

schemas.forEach((e) => {

console.log("first", e)

Parse.Cloud.afterSave(e, (req, resp) => {

  console.log("After Save call from Dynamic classes")

  // resto of the code here

})

});

}

Parse.Cloud.define(“addAfterSave”, async (request) => {

getClassNamesFromSchema();

})

It started working on dynamically added classes.
I defined a cloud Function which will be called from client-side after adding a new dynamic class.
Is it the right way to do so?

I believe it is important to call this function in initialization (and not only on a cloud code function call). I see it working well for 1 parse server instance. But it will not work if you have multiple instances. Probably only one of them will receive the request and add the trigger. The others will have the trigger only after initialization.

Another idea would turn the classes write access private and write a cloud code function to write on these classes. The cloud code function could save the object and, after that, fire the “trigger”.

1 Like

AS of now, We are planning to run on a single instance and single app only.

But as you said it is important to call this function in initialization.
but where exactly did you tell me to write this code, I am new to the Parse server I couldn’t understand the terms.
I am using the parse-server-example project to run this project, I have the index.js File in root dir .

do you want me to write that code next below this lines on index.js
// Serve the Parse API on the /parse URL prefix
const mountPath = process.env.PARSE_MOUNT || ‘/parse’;
if (!test) {

const api = new ParseServer(config);

app.use(mountPath, api);

}

Apart from this conversation, your database design is bad choice. You shouldn’t create a different table for different users. Instead, you should create a relation table. And use the user IDs as the identifier.

1 Like

Yes You are right but they need like that We have multiple schools, and each school has their student and their daily attendance. one school can have more than 3k - 5k students and the daily in out log will be stored in the dynamic table so therefore we have to go with this approach that store attendance and student along with School id table, and when need to query a table we can only have to query on the short number of data rather than finding students or their attendance in a large table.

Hi @manish.yes

query on the short number of data rather than finding students or their attendance in a large table.

Some limitations exists with databases, like MongoDB on Altas (max 500 collections). Also i think you are wrong on the sentence above. In this case you should use indexes and build indexes on the main Collection/Class. You should not create multiple collections for performance purpose.
I think that it’s a better approach to use pointers or relations and create appropriate indexes on these relations/pointers.

For example you can create indexes on pointers with Parse.Schema like indexes: { indexForExampleField: {p_anExampleField: 1}} “anExampleField” is a “Pointer” field. To create indexes on Pointers you need to prefix your index field with p_

2 Likes