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

TypeScript: Add support for arbitrary keys in QueryOptions #10688

Closed
adarshmadrecha opened this issue Sep 7, 2021 · 1 comment
Closed

TypeScript: Add support for arbitrary keys in QueryOptions #10688

adarshmadrecha opened this issue Sep 7, 2021 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@adarshmadrecha
Copy link

adarshmadrecha commented Sep 7, 2021

Do you want to request a feature or report a bug?
feature

Requirement Example

When a record is created in Sales table, a middleware is triggered which updates Inventory table. In this middleware, we need access to User to store which user triggered update in Inventory.

How I solve it currently

When the Shop Query is called, a meta object is passed in the options. Then in the middleware, I call query.getOptions() to get this meta object.

Feature Request

Add official support for meta object. Currently, the VS code gives warning that there is NO Meta property which can be passed in the options.

Sample Code

//  Model & Schema
const salesSchema= new Mongoose.Schema({
   //  schema definition
});
const inventoySchema= new Mongoose.Schema({
   //  schema definition
});
salesSchema.post(['updateOne', 'findOneAndUpdate'], { document: false, query: true }, async function () {
  updateInventory(this)
});

export const Sales = Mongoose.model('sales', salesSchema);
export const Inventory= Mongoose.model('sales', inventoySchema);
// Middleware function
export async function (shopQuery) {
  const meta = shopQuery.getOptions()?.meta ?? {}
  // User is now available in this meta object.
  const {user} = meta;
  //  Now when I am updating Inventory, I can update the userId field 
  await Inventory.create({
    /** other fields  */
    userid: user.id
  })
}
// Sales Update Function 
const sales = await Sales.findByIdAndUpdate(
    id,
    { ...req.body },
    // NOTE: passing user in options which can then be accessed by middleware
    { new: true, meta: {user:{id:1234}} }
)
@IslandRhythms IslandRhythms added the new feature This change adds new functionality, like a new method or class label Sep 7, 2021
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed new feature This change adds new functionality, like a new method or class labels Sep 7, 2021
@vkarpov15 vkarpov15 added this to the 6.0.7 milestone Sep 7, 2021
@vkarpov15
Copy link
Collaborator

We'll add support for arbitrary keys in QueryOptions. AFAIK passing any key you want to QueryOptions should be fine, it's just a TypeScript quirk.

@vkarpov15 vkarpov15 changed the title Feature: Support for meta object in Query Options Add support for arbitrary keys in QueryOptions Sep 18, 2021
@vkarpov15 vkarpov15 changed the title Add support for arbitrary keys in QueryOptions TypeScript: Add support for arbitrary keys in QueryOptions Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants