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
5 changes: 3 additions & 2 deletions packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export class Model {
*/
static clearBootedModels(): void {
this.booted = {}
this.original = {}
this.schemas = {}
this.fieldMutators = {}
this.fieldCasts = {}
Expand Down Expand Up @@ -745,7 +746,7 @@ export class Model {
this[key] = this[key] ?? keyValue
}

operation === 'set' && (this.$self().original = this.$getAttributes())
operation === 'set' && (this.$self().original[this.$getKey(this, true) as string] = this.$getAttributes())

modelConfig.withMeta && operation === 'set' && this.$fillMeta(options.action)

Expand Down Expand Up @@ -930,7 +931,7 @@ export class Model {
* Get the original values of the model instance
*/
$getOriginal(): Element {
return this.$self().original
return this.$self().original[this.$getKey(this, true) as string]
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/src/store/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { FilledInstallOptions } from './Store'
export const CONFIG_DEFAULTS = {
model: {
withMeta: false,
hidden: ['_meta'],
hidden: ['_meta', 'original'],
visible: ['*'],
},
cache: {
Expand Down
26 changes: 25 additions & 1 deletion packages/pinia-orm/tests/unit/model/Model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import { computed } from 'vue-demi'

import { Model } from '../../../src'
import { Model, useRepo } from '../../../src'
import { Attr } from '../../../src/decorators'

describe('unit/model/Model', () => {
Expand Down Expand Up @@ -40,4 +41,27 @@ describe('unit/model/Model', () => {
expect(user.lastName).toBe('John Doe')
expect(() => user.$isDirty('name')).toThrowError()
})

it('it displays states correctly with multiple same models', () => {
const userRepo = useRepo(User)
userRepo.save([
{
id: 1,
lastName: 'JohnK',
},
{
id: 2,
lastName: 'JaneD',
},
{
id: 3,
lastName: 'TomH',
},
])

const users = computed(() => userRepo.all())
const usersOriginal = computed(() => users.value.map(u => u.$getOriginal()))

expect(users.value.map(user => user.$toJson())).toEqual(usersOriginal.value)
})
})