Use Mongodb-Connection in Cloud Code

Hi Guys!
As I have read, it is not possible to put a unique index on a mongodb field with Parse.Schema. Instead, we have to do that right on the mongodb … right?

I have a cloud function “migrate” as a central point to run update scripts. At this point I would also like to carry out this direct intervention in mongodb.

My question now is whether there is a way to access the server option “databaseURI” within a cloud function? Or is there perhaps a “more elegant” way to use the same database connection as parse?

Thank you for your help,
Thomas

Unique indexes will hopefully be supported internally soon - follow this issue!

You can however use this little hack I’ve just written to create unique indexes:

const Config = require('./../node_modules/parse-server/lib/Config');
const adapter = Config.get('appId').database.adapter;
await adapter.ensureUniqueness(
  'className',
  {
    fields: {
      fieldName: { type: 'String' },
    },
  },
  ['fieldName']
);

works like a charm - thank you!