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

const attr = fields[key]
const value = attributes[key]
let 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
18 changes: 11 additions & 7 deletions packages/pinia-orm/src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,8 @@ export class Query<M extends Model = Model> {
const isDeleting = currentModel.$self().deleting(currentModel)

if (isDeleting === false) { notDeletableIds.push(currentModel.$getIndexId()) } else {
this.hydratedDataCache.delete(this.model.$entity() + currentModel.$getIndexId())
this.hydratedDataCache.delete('set' + this.model.$entity() + currentModel.$getIndexId())
this.hydratedDataCache.delete('get' + this.model.$entity() + currentModel.$getIndexId())
afterHooks.push(() => currentModel.$self().deleted(currentModel))
this.checkAndDeleteRelations(currentModel)
}
Expand Down Expand Up @@ -1015,12 +1016,17 @@ export class Query<M extends Model = Model> {
* an update event trigger in vue if the object is used.
*/
protected getHydratedModel (record: Element, options?: ModelOptions): M {
const id = this.model.$getKey(record, true)
const savedHydratedModel = id && options?.operation !== 'set' && this.hydratedDataCache.get(this.model.$entity() + id)
const id = this.model.$entity() + this.model.$getKey(record, true)
const operationId = options?.operation + id
let savedHydratedModel = this.hydratedDataCache.get(operationId)

if (options?.action === 'update') {
this.hydratedDataCache.delete('get' + id)
savedHydratedModel = undefined
}

if (
!this.getNewHydrated &&
options?.operation !== 'set' &&
savedHydratedModel
) { return savedHydratedModel }

Expand All @@ -1029,9 +1035,7 @@ export class Query<M extends Model = Model> {
.$newInstance(record, { relations: false, ...(options || {}), ...newOptions })
const hydratedModel = getNewInsance()

if (id && !this.getNewHydrated && options?.operation !== 'set') { this.hydratedDataCache.set(this.model.$entity() + id, hydratedModel) }

if (id && options?.action === 'update') { this.hydratedDataCache.set(this.model.$entity() + id, getNewInsance({ operation: 'get' })) }
if (isEmpty(this.eagerLoad) && options?.operation !== 'set') { this.hydratedDataCache.set(operationId, hydratedModel) }

return hydratedModel
}
Expand Down
54 changes: 54 additions & 0 deletions packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,59 @@ describe('unit/model/Model_Mutators', () => {
})

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

userRepo.where('id', 1).update({ name: 'Stefan Raab' })

assertState({
users: {
1: { id: 1, name: 'STEFAN RAAB' },
},
})

expect(userRepo.find(1)?.name).toBe('stefan raab')
})

it('should mutate data in the store with set', () => {
class User extends Model {
static entity = 'users'

@Attr(0) id!: number
@Attr('') name!: string

static mutators () {
return {
name: {
set: (value: any) => { return value + ' (modified)' },
},
}
}
}

const userRepo = useRepo(User)
userRepo.save({
id: 1,
name: 'test',
})

assertState({
users: {
1: { id: 1, name: 'test (modified)' },
},
})

expect(userRepo.find(1)?.name).toBe('test (modified)')

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

assertState({
users: {
1: { id: 1, name: 'jane (modified)' },
},
})

expect(userRepo.find(1)?.name).toBe('jane (modified)')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('unit/repository/Repository', () => {
expect(userRepo.hydratedDataCache.size).toBe(1)

userRepo.piniaStore().update({ 1: { id: 1, name: 'John 2' } })
expect(userRepo.hydratedDataCache.size).toBe(1)
expect(userRepo.hydratedDataCache.size).toBe(0)

userRepo.piniaStore().insert({ 2: { id: 2, name: 'John 3' } })
userRepo.piniaStore().insert({ 3: { id: 3, name: 'John 4' } })
Expand Down