parse-community / parse-server

Parse Server for Node.js / Express
https://parseplatform.org
Apache License 2.0
20.91k stars 4.78k forks source link

Officially expose MongoDB client #8786

Open mtrezza opened 1 year ago

mtrezza commented 1 year ago

New Feature / Enhancement Checklist

Current Limitation

Parse Server uses a richer interface to interact with the database than it exposes for developers to use. For example upserting (upsertOneObject), mass-updating by query (updateObjectsByQuery) or findOneAndUpdate. These are currently only accessible via:

Given these method's versatility and efficiency, we can assume that they are widely in use. Especially since the Parse Server configuration has been made more accessible with Parse.Server in https://github.com/parse-community/parse-server/issues/7869.

As an example for efficiency consider the case of upserting an object:

However, these methods are all internal. Changing them would not be considered breaking changes and it wouldn't even be in the changelog. It is even suggested that they may be removed if possible:

https://github.com/parse-community/parse-server/blob/fe02d3e8aa875f362377cc877a65831b010b00a6/src/Adapters/Storage/Mongo/MongoStorageAdapter.js#L586-L587

Feature / Enhancement Description

Parse Server should officially expose a method in Cloud Code similar to the Parse.Server.database.update method. Exposing Parse.Server.database.update directly would likely be unpractical, since the method is not well structured in its design and is used internally throughout the code. It could be available in Cloud Code via something like await Parse.Cloud.Database.execute({ ... }).

Availability in the Parse JS SDK is out of scope for this feature, see also https://github.com/parse-community/parse-server/issues/8787.

Example Use Case

parse-github-assistant[bot] commented 1 year ago

Thanks for opening this issue!

mtrezza commented 1 year ago

I'm revising my suggestion. After playing around with Parse.Server.database.update and the lower level Parse.Server.database.adapter.findOneAndUpdate I found that their behavior is really weird. They are applying various Parse <-> MongoDB transformations that are really confusing the way they are implemented.

For example, a date value has to be supplied in a different format (JavaScript Date object, Parse JSON with date ISO string) depending on whether it's set in the update object or the query object. In addition it's converted differently whether is the createdAt, updatedAt field, a custom date field or an atomic operator like $setOnInsert. All this also makes contributing to Parse Server difficult and should probably be cleaned up at some point.

I don't think these API should be officially exposed, because:

So instead I'm suggesting to expose the MongoDB adapter in a more convenient way. It should give total freedom of what command to execute, without any Parse <-> MongoDB transformations. This is in line with the MongoDB aggregation pipeline which also does not (should not) apply any transformations.

Moumouls commented 3 days ago

@mtrezza here a simple "trick" to access the mongo client, may be it should be exposed under a specific method ?

An easy one i guess finally here

export const getMongoClient = async (): Promise<GetMongoClient> => {
    await parseServer.config.databaseController.adapter.connect()
    return {
        db: parseServer.config.databaseController.adapter.database,
        client: parseServer.config.databaseController.adapter.client,
    }
}
mtrezza commented 2 days ago

I was thinking about an officially documented method within the Parse namespace.