'on collection create' event

Is there a way to hook into the collection create an event? My use case is if the application is run on the blank database I want to populate static roles objects.

No. There is no hook for that yet as long as I know. What you can do is actually create a cloud function that creates the collection and populares the collection right after.

I can create cloud function, no problem. But what’s the optimal way to call it. I just want it to be called if the database is empty. I know, I can probably do it in MongoDB in express itself. But I was thinking this maybe should go with migrations code?

You can check the Schema to know if the collection already exists or not. Anyways, maybe you also want to check out the brand new migrations feature: feat: add user-defined schema and migrations by sadortun · Pull Request #7418 · parse-community/parse-server · GitHub

Yes, I am already using it. But it schema only right? Anyway to modify data as part of migrations?

I actually haven’t tried it yet so I’m not sure.

Hi @jascodes

If you need to seed your database you have many options like:

serverStartComplete option on ParseServer instance.

If you use schemas new feature you can also provided a afterMigration, beforeMigration

A special note on these functions, if you use ParseServer.start, internally Parse do not currently wait startup complete before running app.listen(). This issue is already know and we need to adjust some details to avoid this use case. On my parse server i define a custom route on the express app to tell to my orchestrator (Kubernetes) when the app is ready to accept traffic once everything is initialized.

i add this line at the end of the serverStartComplete fn

// const parseServer = ParseServer.start({
serverStartComplete: async () => {
  parseServer.expressApp.get('/ready', (req: any, res: any) => {
    res.send('true')
  })
}
1 Like

First of all, thanks very much @Moumouls for taking time out and replying. Understood how to hook into serverStartComplete.

I am using schemas, and afterMigration is what I was looking for and didn’t find in API doc. I think it’s yet to be added. Where to pass these? Do they also go in ParseSeverOptions or somewhere else?

@jascodes some docs are coming for schema feature you can check md files of the docs here:

afterMigration fn is available under schemas key, at the same level of definitions key.

{
  schemas: {
   definitions: []
   afterMigration: () => { console.log("Here")}
  }
}
1 Like

afterMigration is just a simple function triggered by parse once all schemas operations are finished.

I think we can make some improvements and feel free to make a PR to improve this function.

Internally the Parse schema feature use a poll & diff pattern, so Parse is aware of class creation, deletion, fields creation, fields deletion.

It could be interesting to add all event/ops performed by the system available as arg of the afterMigration fn. like

// afterMigration arg suggestion
[
{type: "classCreation", className: "MyClass"},
{type: "fieldDeletion", className: "MyClass2", fieldName: "myField"}
]

Then developers will be able to trigger custom business logic in afterMigration fn depending of changes committed by the internal parse schema system. (It will be super easy to implement this feature)
@jascodes

1 Like