{"_id":"@andryeyev/typeorm-scoped","_rev":"5-824047edeac3aa44cef9999dffee22cd","time":{"created":"2024-02-07T10:20:42.502Z","1.0.9":"2024-02-07T10:12:13.263Z","modified":"2024-02-07T10:25:26.104Z","1.0.9-a":"2024-02-07T10:20:42.707Z","1.0.9-b":"2024-02-07T10:23:33.943Z","1.0.10":"2024-02-07T10:25:25.953Z"},"name":"@andryeyev/typeorm-scoped","dist-tags":{"latest":"1.0.10"},"versions":{"1.0.9-a":{"name":"@andryeyev/typeorm-scoped","version":"1.0.9-a","license":"ISC","description":"TypeORM extension which adds scopes and default scopes to entities and repositories.","scripts":{"dev":"nodemon -w 'src/**/*' --ignore 'src/**/*.spec.ts' -e ts -x 'yarn build'","build":"rimraf dist && tsc","prepublish":"yarn build","publish":"npm publish --access=public"},"main":"dist/index.js","types":"dist/index.d.ts","repository":{"type":"git","url":"git+https://github.com/andryeyev/typeorm-scoped.git"},"keywords":["typeorm","scope","scoped","scopes","nestjs","default-scopes"],"author":{"name":"David Mikayelyan","email":"davmik2601@gmail.com"},"dependencies":{},"devDependencies":{"@types/jest":"^26.0.0","@types/node":"^16.0.0","babel-jest":"^27.0.0","babel-plugin-transform-typescript-metadata":"^0.3.0","core-js":"^3.4.8","jest":"^27.0.0","nodemon":"^2.0.1","pg":"^8.0.0","prettier":"^2.0.1","typeorm":"^0.2.45||^0.3.17","typescript":"^4.0.2"},"peerDependencies":{"typeorm":"^0.2.45||^0.3.17"},"jest":{"moduleFileExtensions":["js","json","ts"],"watchPathIgnorePatterns":["<rootDir>/dist/","<rootDir>/node_modules/"],"coveragePathIgnorePatterns":["/node_modules/","/test/"]},"_id":"@andryeyev/typeorm-scoped@1.0.9-a","gitHead":"c3041665de1fa89e43fad3dccf046f8d725168c2","bugs":{"url":"https://github.com/andryeyev/typeorm-scoped/issues"},"homepage":"https://github.com/andryeyev/typeorm-scoped#readme","_nodeVersion":"20.11.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-oEZl0OdOrjGXn/1fMM/pRINo8aNh95JCL9HCbMajPQm/Bd5Ln1a3Euqod/IB/slmKLEKcUjYgPdVfLvkyBhZew==","shasum":"f2276b639016c97830a4ff2b51c57ca32d32d164","tarball":"https://registry.npmjs.org/@andryeyev/typeorm-scoped/-/typeorm-scoped-1.0.9-a.tgz","fileCount":23,"unpackedSize":23462,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCID8wyGGL6ju5zqemt8ewJ+l8D1c7YZW9BEVvLoNxxVUfAiBjVG2LXyKG+pUh9iM6R5CBo0UVq/p1/lXHqM+kG7uhcQ=="}]},"_npmUser":{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"},"directories":{},"maintainers":[{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/typeorm-scoped_1.0.9-a_1707301242561_0.4881445482720268"},"_hasShrinkwrap":false},"1.0.9-b":{"name":"@andryeyev/typeorm-scoped","version":"1.0.9-b","license":"ISC","description":"TypeORM extension which adds scopes and default scopes to entities and repositories.","scripts":{"dev":"nodemon -w 'src/**/*' --ignore 'src/**/*.spec.ts' -e ts -x 'yarn build'","build":"rimraf dist && tsc","prepublish":"yarn build","publish":"npm publish --access=public"},"main":"dist/index.js","types":"dist/index.d.ts","repository":{"type":"git","url":"git+https://github.com/andryeyev/typeorm-scoped.git"},"keywords":["typeorm","scope","scoped","scopes","nestjs","default-scopes"],"author":{"name":"David Mikayelyan","email":"davmik2601@gmail.com"},"dependencies":{},"devDependencies":{"@types/jest":"^26.0.0","@types/node":"^16.0.0","babel-jest":"^27.0.0","babel-plugin-transform-typescript-metadata":"^0.3.0","core-js":"^3.4.8","jest":"^27.0.0","nodemon":"^2.0.1","pg":"^8.0.0","prettier":"^2.0.1","typeorm":"^0.2.45||^0.3.17","typescript":"^4.0.2||^5.2.2"},"peerDependencies":{"typeorm":"^0.2.45||^0.3.17"},"jest":{"moduleFileExtensions":["js","json","ts"],"watchPathIgnorePatterns":["<rootDir>/dist/","<rootDir>/node_modules/"],"coveragePathIgnorePatterns":["/node_modules/","/test/"]},"_id":"@andryeyev/typeorm-scoped@1.0.9-b","gitHead":"c3041665de1fa89e43fad3dccf046f8d725168c2","bugs":{"url":"https://github.com/andryeyev/typeorm-scoped/issues"},"homepage":"https://github.com/andryeyev/typeorm-scoped#readme","_nodeVersion":"20.11.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-kqlAYPBlK/IM2BTviKTfjDxrnF4Of00Ix7vE/yGsZ4Kp6aQsS4lttrP1kZx6bfLUxq2xZ1weh8SgQa516ir+hw==","shasum":"50faac9c5b03fe5d7761c69a2a9ccb71284f892b","tarball":"https://registry.npmjs.org/@andryeyev/typeorm-scoped/-/typeorm-scoped-1.0.9-b.tgz","fileCount":23,"unpackedSize":23470,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBnstBLXU3eZP56TIlvET8qZgcROEfh0wVk7RcTMDII/AiBuo47aRh9gMz/MweMTLD6N0Y/iikt9jnat6YfPLzfXhA=="}]},"_npmUser":{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"},"directories":{},"maintainers":[{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/typeorm-scoped_1.0.9-b_1707301413802_0.22860785739580303"},"_hasShrinkwrap":false},"1.0.10":{"name":"@andryeyev/typeorm-scoped","version":"1.0.10","license":"ISC","description":"TypeORM extension which adds scopes and default scopes to entities and repositories.","scripts":{"dev":"nodemon -w 'src/**/*' --ignore 'src/**/*.spec.ts' -e ts -x 'yarn build'","build":"rimraf dist && tsc","prepublish":"yarn build","publish":"npm publish --access=public"},"main":"dist/index.js","types":"dist/index.d.ts","repository":{"type":"git","url":"git+https://github.com/andryeyev/typeorm-scoped.git"},"keywords":["typeorm","scope","scoped","scopes","nestjs","default-scopes"],"author":{"name":"David Mikayelyan","email":"davmik2601@gmail.com"},"dependencies":{},"devDependencies":{"@types/jest":"^26.0.0","@types/node":"^16.0.0","babel-jest":"^27.0.0","babel-plugin-transform-typescript-metadata":"^0.3.0","core-js":"^3.4.8","jest":"^27.0.0","nodemon":"^2.0.1","pg":"^8.0.0","prettier":"^2.0.1","typeorm":"^0.2.45||^0.3.17","typescript":"^4.0.2"},"peerDependencies":{"typeorm":"^0.2.45||^0.3.17"},"jest":{"moduleFileExtensions":["js","json","ts"],"watchPathIgnorePatterns":["<rootDir>/dist/","<rootDir>/node_modules/"],"coveragePathIgnorePatterns":["/node_modules/","/test/"]},"_id":"@andryeyev/typeorm-scoped@1.0.10","gitHead":"c3041665de1fa89e43fad3dccf046f8d725168c2","bugs":{"url":"https://github.com/andryeyev/typeorm-scoped/issues"},"homepage":"https://github.com/andryeyev/typeorm-scoped#readme","_nodeVersion":"20.11.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-JxfhMv48eBuvSURZoXVe4gGe1axWlpGFPy6GftYi3+4aVhmfQpdVX+TmvqeHyngrl9jEbtr4Px2vXxoQ/APTrw==","shasum":"541aecfebf7967717e238c0907f3cc46b0d279a5","tarball":"https://registry.npmjs.org/@andryeyev/typeorm-scoped/-/typeorm-scoped-1.0.10.tgz","fileCount":23,"unpackedSize":23461,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICrCx3s3eyxl8ksyxoC8x9pENXAvInRsJ+qNYWh4SE1cAiBPzPeOP+wdqmvfBFJ1Lw4aYOguKlEw68KWTSk9LQUfrA=="}]},"_npmUser":{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"},"directories":{},"maintainers":[{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/typeorm-scoped_1.0.10_1707301525780_0.4515097057478117"},"_hasShrinkwrap":false}},"maintainers":[{"name":"andryeyev","email":"volodymyr.andryeyev@gmail.com"}],"description":"TypeORM extension which adds scopes and default scopes to entities and repositories.","homepage":"https://github.com/andryeyev/typeorm-scoped#readme","keywords":["typeorm","scope","scoped","scopes","nestjs","default-scopes"],"repository":{"type":"git","url":"git+https://github.com/andryeyev/typeorm-scoped.git"},"author":{"name":"David Mikayelyan","email":"davmik2601@gmail.com"},"bugs":{"url":"https://github.com/andryeyev/typeorm-scoped/issues"},"license":"ISC","readme":"# typeorm-scoped\n\n## Description\n\n✅ This lib supports Scopes for both `Active Record pattern` (working with Entitiies), and `Data Mapper pattern` (\nworking with Repositories).\n\nIt's very easy to use this lib.\nYou can define scopes and default scopes for entities.\n\n## Installation\n\n```shell\nyarn add typeorm-scoped\n# or\nnpm install typeorm-scoped --save\n```\n\n## Initialization (Global Setup for Project)\n\nBefore usage you need to patch TypeORM before calling any database method.\n\n```typescript\nimport {patchSelectQueryBuilder} from 'typeorm-scope'\n\n...\npatchSelectQueryBuilder()  // <-- call this function\n...\nconst app = new Koa() // or const app = express() ...\n```\n\nIn `NestJS` you can call this function in `main.ts`, in `bootstrap()` function before creating app.\n\n```typescript\nimport {patchSelectQueryBuilder} from 'typeorm-scoped'\n\n...\n\nasync function bootstrap() {\n  patchSelectQueryBuilder() // <-- call\n...\n  const app = await NestFactory.create(AppModule, {...})\n...\n```\n\n## Default Scopes\n\nyou can define a default scope(scopes) for an entity adding the `@DefaultScopes({ ... })` decorator before\nthe `@Entity()`.\n\n```typescript\nimport {Entity, PrimaryGeneratedColumn, Column, BaseEntity} from \"typeorm\"\nimport {Scopes, DefaultScopes} from \"typeorm-scoped\"\n\n@DefaultScopes<User>({\n  existed: (qb, alias) => qb.andWhere(`${alias}.deletedAt IS NULL`),\n  ...\n})\n@Entity()\nexport class User extends BaseEntity {\n  @PrimaryGeneratedColumn()\n  id: number\n\n  @Column()\n  name: string\n\n  @Column({nullable: true})\n  deletedAt?: Date\n}\n```\n\n### Querying\n\n`DefaultScopes` will work automatically and there shouldn't be any changes in your services or repositories. You can use\nthe `EntityManager` or any `Repository` or `CustomRepository` and the `default scopes` will automatically be added to\nthe resulting query. It will also work with queries created using the `QueryBuilder`. For example, the following snippet\n\n```typescript\nUser.find({where: {name: \"John\"}})\n\n// or\n\nuserRepository.find({where: {name: \"John\"}})\n\n// or with createQueryBuilder() ...\n```\n\nwill produce an SQL query like\n\n```sql\nSELECT \"User\".\"id\" AS \"User_id\", \"User\".\"name\" AS \"User_name\" \nFROM \"user\" \"User\" \nWHERE \"User\".\"name\" = ? AND \"User\".\"deletedAt\" IS NULL\n-- PARAMETERS: [\"John\"]\n```\n\n## Custom Scopes\n\nTo define a scope(scopes) for an entity you need to add the `@Scopes({ ... })` decorator before the `@Entity()`.\n\n```typescript\nimport {Entity, PrimaryGeneratedColumn, Column} from \"typeorm\"\nimport {Scopes, DefaultScopes} from \"typeorm-scoped\"\nimport {ScopeEntity} from \"./scope.entity\";\n\n@Scopes<User>({\n  females: (qb, alias) => qb.andWhere(`${alias}.gender = :g`, {g: \"Female\"}),\n  adultUsers: (qb, alias) => qb.andWhere(`${alias}.age > :adultAge`, {adultAge: 17}),\n  ...\n})\n// You can also use @Scopes(...) and @DefaultScopes(...) together !\n@Entity()\nexport class User extends ScopeEntity {\n  @PrimaryGeneratedColumn()\n  id: number\n\n  @Column()\n  name: string\n\n  @Column()\n  age: number\n\n  @Column()\n  gender: string // or GenderEnum -> \"Male\", \"Female\", ...\n\n  @Column({nullable: true})\n  deletedAt?: Date\n}\n```\n\n### Active Record Pattern\n\nIf you use `Active Record` pattern, you need to extend from `ScopeEntity`:\n\n```typescript\n@Scopes<User>({\n  ...\n})\n@Entity()\nexport class User extends ScopeEntity { // <-- our ScopeEntity already extends from BaseEntity\n...\n}\n```\n\n### Data Mapper (Repository) Pattern\n\nIf you use `Data Mapper(Repository)` pattern, then for custom scopes you should use Custom Repositories, which should\nbe `extends` from `ScopeRepository`:\n\n#### Typeorm version 2.x\n\n```typescript\n@EntityRepository(User)\nexport class UserRepository extends ScopeRepository<User> { // <-- our ScopeRepository already extends from Repository<Entity>\n...\n}\n```\n\n#### Typeorm version 3.x\n\n```typescript\n// @Injectable()  // <-- If you use NestJS\nexport class UserRepository extends ScopeRepository<User> {\n  constructor(private dataSource: DataSource) {\n    super(User, dataSource.createEntityManager());\n  }\n}\n```\n\n#### For NestJS\n\n⚠ __Important!__ &nbsp;&nbsp; In NestJS you will add `@Injectable()` on your custom repository `UserRepository` (for\nversion 3.x).\nAnd then add it in your `UserModule` (if typeorm v2.x -> add into TypeOrmModule.forFeature function, if typeorm v3.x,\nadd into providers).\nAfter that use it in services:\n\n```typescript\nconstructor(\n  ...\n    private\nreadonly\nuserRepository: UserRepository\n)\n{\n}\n```\n\n### Querying\n\nYou will use custom scopes like this:\n\n```typescript\nuserRepository.scoped(\"females\", \"adultUsers\").find({where: {name: \"John\"}})\n\n// or\n\nUser.scoped(\"females\", \"adultUsers\").find({where: {name: \"John\"}})\n\n// or with createQueryBuilder() ...\n```\n\nwill produce an SQL query like\n\n```sql\nSELECT \"User\".\"id\" AS \"User_id\", \"User\".\"name\" AS \"User_name\", \"User\".\"age\" AS \"User_age\", \"User\".\"gender\" AS \"User_gender\" \nFROM \"user\" \"User\" \nWHERE \"User\".\"gender\" = ? AND \"User\".\"age\" > ? AND \"User\".\"name\" = ?\n-- PARAMETERS: [\"Female\", 17, \"John\"]\n```\n\n## Disabling Default Scopes\n\nYou are able to disable `default scopes` by calling a method `unscoped`.\n\n```typescript\n// if you dont send parametrs to unscoped method, it unscoped all default scopes  !!!\nuserRepository.unscoped().find({where: {name: \"John\"}})\n\n// or unscope only specific default scopes\nuserRepository.unscoped(\"existed\").find({where: {name: \"John\"}})\n\n// or\n\nUser.unscoped().find({where: {name: \"John\"}})\n...\n\n// or with createQueryBuilder() ...\n...\n```\n\nYou can also continue with scoped() method, like this:\n\n```typescript\nuserRepository.unscoped(\"existed\").scoped(\"females\").find({where: {name: \"John\"}})\n```\n\n---\n\n","readmeFilename":"README.md"}