Skip to content

Commit 02a50fc

Browse files
committed
fix issues with default keys
1 parent 1ab9637 commit 02a50fc

7 files changed

Lines changed: 82 additions & 28 deletions

File tree

dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/models/Role.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Model } from '@vuex-orm/core'
2+
3+
export default class Role extends Model {
4+
static entity = 'roles'
5+
6+
static fields () {
7+
return {
8+
id: this.attr(null),
9+
name: this.attr('')
10+
}
11+
}
12+
}

src/common/models/User.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Model } from '@vuex-orm/core'
2+
import Role from './Role'
23

34
export default class User extends Model {
45
static entity = 'users'
@@ -8,7 +9,12 @@ export default class User extends Model {
89
id: this.attr(null),
910
name: this.attr(''),
1011
email: this.attr(''),
11-
phone: this.attr('')
12+
phone: this.attr(''),
13+
roleId: this.attr(1),
14+
role: this.belongsTo(Role, 'roleId')
1215
}
1316
}
17+
get formattedPhone () {
18+
return this.phone
19+
}
1420
}

src/dev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import Vuex from 'vuex'
33
import VuexORM from '@vuex-orm/core'
44
import VuexORMSearchPlugin from 'src/index'
55
import User from './common/models/User'
6+
import Role from './common/models/Role'
67

78
Vue.use(Vuex)
89
VuexORM.use(VuexORMSearchPlugin)
910

1011
const database = new VuexORM.Database()
1112
database.register(User, {})
13+
database.register(Role, {})
1214

1315
export const store = new Vuex.Store({
1416
plugins: [VuexORM.install(database)]

src/index.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,37 @@ const plugin = {
2424

2525
// -- breaking change check in 0.23.1
2626
const {Query} = components
27-
27+
/*
2828
const searchKeys = function () {
2929
// -- normal keys
30-
let fields = Object.keys(this.model.fields())
30+
const modelFields = Object.keys(this.model.fields())
3131
// -- mutations
32-
fields.concat(Object.keys(this.model.mutators))
32+
const mutationFields = Object.keys(this.model.mutators)
33+
3334
// -- related (1 deep)
34-
this.load.forEach(({name}) => {
35-
const relatedFields = Object.keys(this.getModel(name).fields())
36-
fields.concat(relatedFields.map(rf => `${name}.${rf}`))
35+
let relatedFields = []
36+
this.load.forEach(relationship => {
37+
const relatedModel = this.getModel(relationship.name)
38+
if (relatedModel) {
39+
relatedFields.concat(
40+
Object.keys(relatedModel.fields())
41+
.map(rf => `${relationship.name}.${rf}`)
42+
)
43+
}
3744
})
38-
return fields
45+
if (process.env.ENVIRONMENT === 'testing') {
46+
console.log(' -- LOADED --')
47+
console.log(this.load)
48+
console.log(' ----- MODEL ---- ')
49+
console.log(modelFields)
50+
console.log(' ----- MUTATORS ---- ')
51+
console.log(mutationFields)
52+
console.log(' ----- RELATED ---- ')
53+
console.log(relatedFields)
54+
}
55+
return modelFields.concat(mutationFields).concat(relatedFields)
3956
}
57+
*/
4058

4159
const searchProcess = function ({records, entity, term, options}) {
4260

@@ -82,15 +100,15 @@ const plugin = {
82100
}
83101

84102
//-- get default keys
85-
const allKeys = searchKeys.call(this)
103+
// const allKeys = searchKeys.call(this)
86104

87105
// -- override default keys if run-time initializes keys
88-
const instanceOptions = {
106+
let instanceOptions = {
89107
...pluginOptions,
90-
...singleOptions,
91-
...{
92-
keys: singleOptions.keys && singleOptions.keys.length ? singleOptions.keys : allKeys
93-
}
108+
...singleOptions
109+
}
110+
if (instanceOptions.keys && !instanceOptions.keys.length) {
111+
instanceOptions.keys = Object.keys(this.model.fields())
94112
}
95113

96114
if (pluginOptions.debug || process.env.ENVIRONMENT === 'testing' || singleOptions.debug) {
@@ -112,7 +130,7 @@ const plugin = {
112130
}
113131

114132
// -- prototype methods
115-
Query.prototype.searchKeys = searchKeys
133+
// Query.prototype.searchKeys = searchKeys
116134
Query.prototype.search = search
117135

118136
}

test/unit/specs/plugin.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,36 @@ describe('Setup unit tests', function () {
1414
id: 1,
1515
name: 'John Walker',
1616
email: 'john@gmail.com',
17-
phone: '(555) 281-4567'
17+
phone: '(555) 281-4567',
18+
roleId: 1
1819
},
1920
{
2021
id: 2,
2122
name: 'Bobby Banana',
2223
email: 'walker.banana@gmail.com',
23-
phone: '(555) 555-4567'
24+
phone: '(555) 555-4567',
25+
roleId: 2
2426
}
2527
]
2628
})
2729

28-
const records = store.getters['entities/users/query']().all()
30+
store.dispatch('entities/roles/create', {
31+
data: [
32+
{
33+
id: 1,
34+
name: 'Admin'
35+
},
36+
{
37+
id: 2,
38+
name: 'Customer'
39+
}
40+
]
41+
})
2942

30-
expect(records).to.have.lengthOf(2)
43+
const records = store.getters['entities/users/query']()
44+
.withAll()
45+
.all()
46+
expect(records).to.have.lengthOf(2)
3147

3248
})
3349

0 commit comments

Comments
 (0)