Skip to content

Commit f069919

Browse files
authored
fix: add query selectors (#3)
* chore: added commitlint * docs: add contributing guidelines and development setup instructions * fix: update access policy rule types and enhance schema field type definitions, updated docs
1 parent 8f36169 commit f069919

File tree

13 files changed

+1153
-47
lines changed

13 files changed

+1153
-47
lines changed

.husky/commit-msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx --no -- commitlint --edit $1

CONTRIBUTING.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Contributing to nestjs-mongoose-dac
2+
3+
Thank you for considering contributing to `nestjs-mongoose-dac`! Whether it's fixing a bug, adding a feature, or improving documentation - all contributions are welcome.
4+
5+
## Quick Start
6+
7+
1. Fork and clone the repo
8+
2. Install dependencies: `npm install`
9+
3. Create a branch for your changes
10+
4. Make your changes
11+
5. Run tests: `npm test`
12+
6. Push your changes
13+
7. Open a pull request
14+
15+
## Development Guide
16+
17+
### Initial Setup
18+
19+
```bash
20+
# Clone your fork
21+
git clone https://github.com/YOUR_USERNAME/nestjs-mongoose-dac.git
22+
23+
# Install dependencies
24+
npm install
25+
26+
# Run tests to verify setup
27+
npm test
28+
```
29+
30+
### Running Tests
31+
32+
```bash
33+
# Run full test suite
34+
npm test
35+
36+
# Run tests in watch mode
37+
npm run test:watch
38+
```
39+
40+
### Testing Your Changes
41+
42+
The best way to test your changes is to use the package in a real NestJS application. Here's a quick way to do that:
43+
44+
1. Create a new NestJS app:
45+
46+
```bash
47+
nest new test-app
48+
```
49+
50+
2. Link your local `nestjs-mongoose-dac`:
51+
52+
```bash
53+
# In nestjs-mongoose-dac directory
54+
npm link
55+
56+
# In your test app directory
57+
npm link nestjs-mongoose-dac
58+
```
59+
60+
3. Use it in your test app:
61+
62+
```typescript
63+
import { MongooseModule } from 'nestjs-mongoose-dac';
64+
65+
@Module({
66+
imports: [
67+
MongooseModule.forRoot('mongodb://localhost:27017/test'),
68+
MongooseModule.forFeature([{ name: 'Cat', schema: CatSchema }]),
69+
],
70+
})
71+
export class AppModule {}
72+
```
73+
74+
## Guidelines
75+
76+
### When submitting a PR:
77+
78+
- Make sure tests pass
79+
- Add tests if you're adding a feature
80+
- Update README if needed
81+
- Update types if you're changing interfaces
82+
83+
### Commit Messages
84+
85+
We follow [Conventional Commits](https://www.conventionalcommits.org/). Each commit message should be structured as follows:
86+
87+
- `feat: <description>`
88+
89+
- New features that add functionality
90+
- Triggers **MINOR** version bump (1.1.0 → 1.2.0)
91+
- Example: `feat: add support for dynamic access policies`
92+
93+
- `fix: <description>`
94+
95+
- Bug fixes and patches
96+
- Triggers **PATCH** version bump (1.1.1 → 1.1.2)
97+
- Example: `fix: resolve issue with pre-hook execution`
98+
99+
- `feat!: <description>`
100+
101+
- Changes that break backward compatibility
102+
- Triggers **MAJOR** version bump (1.0.0 → 2.0.0)
103+
- Example: `feat!: change access policy API to async methods`
104+
105+
- `docs: <description>`
106+
107+
- Documentation changes only
108+
- **No version bump**
109+
- Example: `docs: improve API reference section`
110+
111+
- `test: <description>`
112+
113+
- Adding or modifying tests
114+
- **No version bump**
115+
- Example: `test: add unit tests for access control plugin`
116+
117+
- `refactor: <description>`
118+
119+
- Code changes that neither fix bugs nor add features
120+
- **No version bump**
121+
- Example: `refactor: simplify access policy logic`
122+
123+
- `chore: <description>`
124+
- Maintenance tasks, dependency updates, etc
125+
- **No version bump**
126+
- Example: `chore: update mongoose to v7`
127+
128+
## Need Help?
129+
130+
- 🐛 [Open an issue](https://github.com/idbenami/nestjs-mongoose-dac/issues)
131+
- 🤝 Ask questions in PRs
132+
133+
Don't worry too much about getting everything perfect. We're happy to help guide you through the process and fix any issues along the way.

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,21 @@ For detailed API reference of `@nestjs/mongoose`, please refer to the official [
129129

130130
### `defineRule`
131131

132-
- **`defineRule<T>(schema: Schema, key: string, policy: AccessPolicyRule<T>): void`**
132+
- **`defineRule(schema: Schema, key: string, policy: AccessPolicyRule): void`**
133133
- Defines an access policy for a Mongoose schema.
134134
- `schema`: The Mongoose schema to which the rule applies.
135135
- `key`: The name of the rule - should be unique per schema.
136136
- `policy`: The access policy rule object, which includes:
137-
- `type`: An array of `AccessPolicyRuleType`, each one representing mongoose operations.
138-
- query: `find`, `findOne`, `distinct`
139-
- count: `countDocuments`, `estimatedDocumentCount`
140-
- update: `updateOne`, `updateMany`, `findOneAndUpdate`, `replaceOne`, `findOneAndReplace`
141-
- delete: `deleteOne`, `deleteMany`, `findOneAndDelete`
142-
- save: `save` (document level), `insertMany`
143-
- `rule`: A function that takes a `get` function and returns a query object or a boolean.
137+
- `type`: A single `RuleType` or an array of `RuleType`s, each indicating a type of operation. Supported types and corresponding operations are:
138+
- `'query'`: `find`, `findOne`, `distinct`
139+
- `'count'`: `countDocuments`, `estimatedDocumentCount`
140+
- `'update'`: `updateOne`, `updateMany`, `findOneAndUpdate`, `replaceOne`, `findOneAndReplace`
141+
- `'delete'`: `deleteOne`, `deleteMany`, `findOneAndDelete`
142+
- `'save'`: `save` (document-level), `insertMany`
143+
- `rule`: A function that receives a `get` function (to retrieve dynamic values like user ID)
144+
and returns an object specifying field-level constraints:
145+
- For `query` and `count` types: values can be plain values or MongoDB query selectors (e.g., `$eq`, `$gte`, `$in`).
146+
- For all other types: only plain values are allowed, matching the field's original type in the schema.
144147

145148
### `EnrichmentsService`
146149

@@ -163,6 +166,10 @@ For detailed API reference of `@nestjs/mongoose`, please refer to the official [
163166

164167
`@nestjs/mongoose` is the base package for working with Mongoose in NestJS. However, it does not enhances or offers more "fullstack" tools to ease the work with the database. This package extends `@nestjs/mongoose` to add access-rules capabilities, enrichments and more, making it easier to create and manage complex workflows.
165168

169+
### How does the rules work?
170+
171+
The `defineRule` function allows you to define access policies for Mongoose schemas. Each rule can specify the type of operations it applies to (e.g., query, update, delete) and the logic for determining access. The rules are enforced automatically during Mongoose operations using Mongoose `pre` hook.
172+
166173
### How does the enrichments work?
167174

168175
The `EnrichmentsService` uses `AsyncLocalStorage` to store enrichment data per request lifecycle. This allows you to set contextual data for access control rules without passing it explicitly through the request.

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ['@commitlint/config-conventional'] };

0 commit comments

Comments
 (0)