Skip to content

Commit 4587db7

Browse files
authored
feat: add mapRepos helper (#5)
1 parent ff9a990 commit 4587db7

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"url": "https://github.com/vuex-orm/vuex-orm-next/issues"
4949
},
5050
"peerDependencies": {
51+
"vue": ">=2.6.0",
5152
"vuex": ">=3.1.0"
5253
},
5354
"dependencies": {

src/helpers/Helpers.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Vue from 'vue'
2+
import { Model } from '../model/Model'
3+
import { Repository } from '../repository/Repository'
4+
5+
export type MappedRepositories<MR extends ModelsOrRepositories> = {
6+
[K in keyof MR]: MR[K] extends typeof Model
7+
? () => Repository<InstanceType<MR[K]>>
8+
: () => InstanceType<MR[K]>
9+
}
10+
11+
export type ModelOrRepository<
12+
M extends typeof Model,
13+
R extends typeof Repository
14+
> = M | R
15+
16+
export type ModelsOrRepositories<
17+
M extends typeof Model = any,
18+
R extends typeof Repository = any
19+
> = Record<string, ModelOrRepository<M, R>>
20+
21+
/**
22+
* Map given models or repositories to the Vue Component.
23+
*/
24+
export function mapRepos<MR extends ModelsOrRepositories>(
25+
modelsOrRepositories: MR
26+
): MappedRepositories<MR> {
27+
const repositories = {} as MappedRepositories<MR>
28+
29+
for (const name in modelsOrRepositories) {
30+
;(repositories as any)[name] = function (this: Vue) {
31+
return this.$store.$repo(modelsOrRepositories[name])
32+
}
33+
}
34+
35+
return repositories
36+
}

src/index.cjs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import './polyfills/Polyfills'
22

33
import { install } from './store/Store'
44
import { use } from './plugin/Plugin'
5+
import { mapRepos } from './helpers/Helpers'
56
import { Database } from './database/Database'
67
import { Schema } from './schema/Schema'
78
import { Model } from './model/Model'
@@ -29,6 +30,7 @@ import { Connection } from './connection/Connection'
2930
export default {
3031
install,
3132
use,
33+
mapRepos,
3234
Database,
3335
Schema,
3436
Model,

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ export * from './interpreter/Interpreter'
3434
export * from './query/Query'
3535
export * from './query/Options'
3636
export * from './connection/Connection'
37+
export * from './helpers/Helpers'
3738
export * from './plugin/Plugin'
3839

3940
import { install } from './store/Store'
4041
import { use } from './plugin/Plugin'
42+
import { mapRepos } from './helpers/Helpers'
4143
import { Database } from './database/Database'
4244
import { Schema } from './schema/Schema'
4345
import { Model } from './model/Model'
@@ -58,6 +60,7 @@ import { Connection } from './connection/Connection'
5860
export default {
5961
install,
6062
use,
63+
mapRepos,
6164
Database,
6265
Schema,
6366
Model,

test/feature/helpers/helpers.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Vue from 'vue'
2+
import { createStore } from 'test/Helpers'
3+
import { Model, Attr, Str, Repository, mapRepos } from '@/index'
4+
5+
describe('feature/helpers/helpers', () => {
6+
class User extends Model {
7+
static entity = 'users'
8+
9+
@Attr() id!: any
10+
@Str('') name!: string
11+
}
12+
13+
class UserRepository extends Repository<User> {
14+
use = User
15+
}
16+
17+
it('maps repositories to vue component from models', async () => {
18+
const store = createStore([User])
19+
20+
const vm = new Vue({
21+
store,
22+
computed: mapRepos({
23+
userRepo: User
24+
})
25+
})
26+
27+
expect(vm.userRepo).toBeInstanceOf(Repository)
28+
expect(vm.userRepo.getModel()).toBeInstanceOf(User)
29+
})
30+
31+
it('maps repositories to vue component from repositories', async () => {
32+
const store = createStore([User])
33+
34+
const vm = new Vue({
35+
store,
36+
computed: mapRepos({
37+
userRepo: UserRepository
38+
})
39+
})
40+
41+
expect(vm.userRepo).toBeInstanceOf(Repository)
42+
expect(vm.userRepo.getModel()).toBeInstanceOf(User)
43+
})
44+
})

0 commit comments

Comments
 (0)