Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pinia-orm/.eslintcache

Large diffs are not rendered by default.

39 changes: 31 additions & 8 deletions packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,26 +737,49 @@ export class Model {
if (operation === 'get' && !this.isFieldVisible(key, this.$hidden(), this.$visible(), options as Required<ModelOptions>)) { continue }

const attr = fields[key]
let value = attributes[key]
const value = attributes[key]

if (attr instanceof Relation && !fillRelation) { continue }

const mutator = mutators?.[key]
const cast = this.$casts()[key]?.newRawInstance(fields)
if (mutator && operation === 'get') {
value = typeof mutator === 'function'
? mutator(value)
: typeof mutator.get === 'function' ? mutator.get(value) : value
}

if (cast && operation === 'get') { value = cast.get(value) }

let keyValue = this.$fillField(key, attr, value)

if (mutator && typeof mutator !== 'function' && operation === 'set' && mutator.set) { keyValue = mutator.set(keyValue) }

if (cast && operation === 'set') { keyValue = cast.set(keyValue) }

if (cast || mutator) {
Object.defineProperty(this, '_' + key, {
configurable: true,
writable: true,
enumerable: false,
})

Object.defineProperty(this, key, {
configurable: true,
enumerable: true,
get () {
let scopeValue = this['_' + key]
if (scopeValue === undefined) { return keyValue }
if (operation === 'set') { return scopeValue }
if (mutator) {
scopeValue = typeof mutator === 'function'
? mutator(scopeValue)
: typeof mutator.get === 'function' ? mutator.get(scopeValue) : scopeValue
}
if (cast) {
scopeValue = cast.get(scopeValue)
}
return scopeValue
},
set (value) {
this['_' + key] = value
},
})
}

this[key] = this[key] ?? keyValue
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ export class Query<M extends Model = Model> {
!this.getNewHydrated &&
options?.operation !== 'set' &&
savedHydratedModel
) { return savedHydratedModel }
) { return savedHydratedModel.$fill(record, options) }

const modelByType = this.model.$types()[record[this.model.$typeKey()]]
const getNewInsance = (newOptions?: ModelOptions) => (modelByType ? modelByType.newRawInstance() as M : this.model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { computed, defineComponent, nextTick, onUpdated } from 'vue-demi'
import { Model, useRepo } from '../../src'
import { Num, Str } from '../../src/decorators'

describe.skip('performance/prevent_rerender_of_child_components', () => {
describe('performance/prevent_rerender_of_child_components', () => {
class Post extends Model {
static entity = 'posts'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Model, useRepo } from '../../src'
import { Attr, BelongsToMany, Num, Str } from '../../src/decorators'

/* eslint-disable no-console */
describe.skip('performance/save_belongs_to_many_relation.spec', () => {
describe('performance/save_belongs_to_many_relation.spec', () => {
class Role extends Model {
static entity = 'roles'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Model, useRepo } from '../../src'
import { HasMany, Num, Str } from '../../src/decorators'

/* eslint-disable no-console */
describe.skip('performance/save_has_many_relation', () => {
describe('performance/save_has_many_relation', () => {
class Post extends Model {
static entity = 'posts'

Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/tests/performance/speed_find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ref } from 'vue-demi'
import { Model, useRepo } from '../../src'

/* eslint-disable no-console */
describe.skip('performance/save_has_many_relation', () => {
describe('performance/save_has_many_relation', () => {
class Todo extends Model {
static entity = 'todos'

Expand Down
11 changes: 9 additions & 2 deletions packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('unit/model/Model_Mutators', () => {
return {
name: {
set: (value: any) => { return value.toUpperCase() },
get: (value: any) => value.toLowerCase(),
},
}
}
Expand All @@ -112,13 +113,19 @@ describe('unit/model/Model_Mutators', () => {
},
})

expect(userRepo.find(1)?.name).toBe('JOHN DOE')
expect(userRepo.find(1)?.name).toBe('john doe')

userRepo.save({
id: 1,
name: 'Jane Doe',
})

expect(userRepo.find(1)?.name).toBe('JANE DOE')
assertState({
users: {
1: { id: 1, name: 'JANE DOE' },
},
})

expect(userRepo.find(1)?.name).toBe('jane doe')
})
})