Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Officially expose MongoDB client #8786

Open
3 tasks done
mtrezza opened this issue Oct 23, 2023 · 4 comments
Open
3 tasks done

Officially expose MongoDB client #8786

mtrezza opened this issue Oct 23, 2023 · 4 comments
Labels
bounty:$100 Bounty applies for fixing this issue (Parse Bounty Program) type:feature New feature or improvement of existing feature

Comments

@mtrezza
Copy link
Member

mtrezza commented Oct 23, 2023

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:

  • the internal database adapter, for example Parse.Server.database.adapter.findOneAndUpdate
  • the higher level Parse.Server.database.update which adds some convenience, like supplying the schema for the target class and transforming high-level Parse references to raw MongoDB references

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 #7869.

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

  • With he public interface an upsert requires 1 unique index, 1 write request, catching the error if the object exists and then 1 update request.
  • With the internal interface an upsert requires 1 upsert upsertOneObject request.

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:

// Hopefully we can get rid of this. It's only used for config and hooks.
upsertOneObject(

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 #8787.

Example Use Case

@parse-github-assistant
Copy link

parse-github-assistant bot commented Oct 23, 2023

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!

@mtrezza mtrezza added type:feature New feature or improvement of existing feature bounty:$100 Bounty applies for fixing this issue (Parse Bounty Program) labels Oct 23, 2023
@mtrezza
Copy link
Member Author

mtrezza commented Oct 27, 2023

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:

  • It's really difficult to follow the data transformations.
  • It's easy to store values as the wrong type in the DB because a wrong data type seems to work for certain operations but then fails for others on the same data.
  • It's again limiting developers to the lower level methods Parse Server provides.

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.

@mtrezza mtrezza changed the title Officially expose primitive database methods Officially expose MongoDB client Oct 27, 2023
@Moumouls
Copy link
Member

@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
Copy link
Member Author

mtrezza commented Oct 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bounty:$100 Bounty applies for fixing this issue (Parse Bounty Program) type:feature New feature or improvement of existing feature
Projects
None yet
Development

No branches or pull requests

2 participants