{"id":19578658,"url":"https://github.com/pierrecabriere/mongoose-subquery","last_synced_at":"2025-04-27T06:34:20.127Z","repository":{"id":50481176,"uuid":"196463930","full_name":"pierrecabriere/mongoose-subquery","owner":"pierrecabriere","description":"mongoose-subquery provides a new operator to query on a reference document field","archived":false,"fork":false,"pushed_at":"2023-08-30T22:31:19.000Z","size":122,"stargazers_count":6,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-14T07:58:19.951Z","etag":null,"topics":["mongoose","plugin","query","reference","subquery"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pierrecabriere.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-11T20:54:14.000Z","updated_at":"2022-07-21T08:01:02.000Z","dependencies_parsed_at":"2023-01-23T04:46:34.218Z","dependency_job_id":null,"html_url":"https://github.com/pierrecabriere/mongoose-subquery","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pierrecabriere%2Fmongoose-subquery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pierrecabriere%2Fmongoose-subquery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pierrecabriere%2Fmongoose-subquery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pierrecabriere%2Fmongoose-subquery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pierrecabriere","download_url":"https://codeload.github.com/pierrecabriere/mongoose-subquery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224062730,"owners_count":17249291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongoose","plugin","query","reference","subquery"],"created_at":"2024-11-11T07:12:32.244Z","updated_at":"2024-11-11T07:12:33.277Z","avatar_url":"https://github.com/pierrecabriere.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-subquery\n\n[![npm version](https://badge.fury.io/js/mongoose-subquery.svg)](https://badge.fury.io/js/mongoose-subquery)\n\n**mongoose-subquery** provides a new `$subquery` operator to make a query on a reference document field.\n\n# Installation\n\nThe `mongoose-subquery` module adds a method to decode $subquery operator in your queries. By default, the plugin bind this method with all pre middlewares to automatically decode the input payload.\nHowever, you can configure the plugin to prevent binding some middlewares and then calling the `Query.decodeSubquery` function.\n\n```typescript\nimport mongooseSubquery from \"mongoose-subquery\";\n\nconst schema = new mongoose.Schema({\n    ...\n});\nschema.plugin(mongooseSubquery, { ...options });\n```\n\n# Configuration\n\n```typescript\ninterface MongooseSubqueryOptions {\n  beforeDecode?: (query: mongoose.Query\u003cany, any, any, any\u003e, obj: object) =\u003e void | Promise\u003cvoid\u003e;\n  initQuery?: (query: mongoose.Query\u003cany, any, any, any\u003e, key: string, obj: object, modelName: string) =\u003e void | Promise\u003cvoid\u003e;\n  resolve?: (subquery: mongoose.Query\u003cany, any, any, any\u003e, query: mongoose.Query\u003cany, any, any, any\u003e) =\u003e any | Promise\u003cany\u003e;\n  bindHooks?: string[];\n}\n\nconst defaultOptions: MongooseSubqueryOptions = {\n  resolve: (subquery) =\u003e subquery.find(),\n  bindHooks: [\n    \"count\",\n    \"countDocuments\",\n    \"deleteMany\",\n    \"deleteOne\",\n    \"estimatedDocumentCount\",\n    \"find\",\n    \"findOne\",\n    \"findOneAndDelete\",\n    \"findOneAndRemove\",\n    \"findOneAndReplace\",\n    \"findOneAndUpdate\",\n    \"remove\",\n    \"replaceOne\",\n    \"update\",\n    \"updateOne\",\n    \"updateMany\",\n  ],\n};\n```\n\n### beforeDecode\n\nThe `beforeDecode` function is executed when the initial query (with `$subquery` within conditions) is decoded\n\n### initQuery\n\nYou can provide a `initQuery` function that will be called each time a new mongoose.Query is instantiate by mongoose-subquery\n\n```typescript\nschema.plugin(mongooseSubquery, {\n    initQuery: (query, key, obj, modelName) =\u003e {\n        console.log(`new query created on ${modelName} with conds ${query.getQuery()}`);\n    }\n});\n```\n\n### resolve\n\nThe `resolve` option allows you to override the default behavior that simply run a `subquery.find()`, for more complex operations\n\n```typescript\nschema.plugin(mongooseSubquery, {\n    resolve: async (subquery) =\u003e {\n        const countQuery = subquery.clone();\n        const count = await countQuery.estimatedDocumentCount();\n        \n        if (count \u003e 100) {\n            throw new Error(`This query returns too many docs`);\n        }\n        \n        return await subquery.find();\n    }\n});\n```\n\n# Example\n\n```typescript\nconst roleSchema = new Schema({\n  name: String,\n  admin: Boolean\n});\nconst Role = mongoose.model('Role', roleSchema, 'Roles');\n\nconst ruleSchema = new Schema({\n  role: { type: ObjectId, ref: 'Role' }\n});\nruleSchema.plugin(require('mongoose-subquery'));\nconst Rule = mongoose.model('Rule', ruleSchema, 'Rules');\n\nRule.find({ role: { $subquery: { admin: true } } });\n```\n\nWill return all rules where the referenced role matches the subquery `{ admin: true }`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpierrecabriere%2Fmongoose-subquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpierrecabriere%2Fmongoose-subquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpierrecabriere%2Fmongoose-subquery/lists"}