Skip to content

Commit d0cd5e6

Browse files
committed
feat(pinia-orm): Add setModel for Repository
This is handy if you want to create a custom `useRepo` composable
1 parent 368370e commit d0cd5e6

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/pinia-orm/src/repository/Repository.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ export class Repository<M extends Model = Model> {
8282
this.hydratedDataCache = hydratedDataCache as Map<string, M>
8383
}
8484

85+
static setModel(model: Model) {
86+
this.useModel = model
87+
return this
88+
}
89+
8590
/**
8691
* Set the global config
8792
*/

packages/pinia-orm/tests/unit/repository/Repository.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ describe('unit/repository/Repository', () => {
8888
expect(postRepo.getModel()).toBeInstanceOf(Post)
8989
})
9090

91+
it('can create a new repository from the custom repository without use defined', () => {
92+
class PostRepository extends Repository<Post> {
93+
}
94+
95+
const userRepo = useRepo(User)
96+
97+
const postRepo = userRepo.repo(PostRepository.setModel(Post))
98+
99+
expect(postRepo.getModel()).toBeInstanceOf(Post)
100+
})
101+
91102
it('can trigger cache & hook from the pinia store action', () => {
92103
const userRepo = useRepo(User)
93104
expect(userRepo.hydratedDataCache.size).toBe(0)

0 commit comments

Comments
 (0)