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
3 changes: 2 additions & 1 deletion docs/content/1.guide/2.model/5.lifecycle-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class Post extends Model {

static creating (model, record) {
model.published = true
// record is the orginal data passed to the model
// model has the updated data already
// record is the old data
if (record.notInModelProperty === 1) {
model.published = false
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/.eslintcache

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/pinia-orm/src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,14 +794,14 @@ export class Query<M extends Model = Model> {
? this.hydrate({ ...existing, ...record }, { operation: 'set', action: 'update' })
: this.hydrate(record, { operation: 'set', action: 'save' })

const isSaving = model.$self().saving(model, record)
const isUpdatingOrCreating = existing ? model.$self().updating(model, record) : model.$self().creating(model, record)
const isSaving = model.$self().saving(model, existing ?? {})
const isUpdatingOrCreating = existing ? model.$self().updating(model, existing ?? {}) : model.$self().creating(model, existing ?? {})
if (isSaving === false || isUpdatingOrCreating === false) { continue }

if (model.$isDirty()) { model = this.hydrate(model.$getAttributes(), { operation: 'set', action: existing ? 'update' : 'save' }) }

afterSavingHooks.push(() => model.$self().saved(model, record))
afterSavingHooks.push(() => existing ? model.$self().updated(model, record) : model.$self().created(model, record))
afterSavingHooks.push(() => model.$self().saved(model, existing ?? {}))
afterSavingHooks.push(() => existing ? model.$self().updated(model, existing ?? {}) : model.$self().created(model, existing ?? {}))
newData[id] = model.$getAttributes()
if (Object.values(model.$types()).length > 0 && !newData[id][model.$typeKey()]) { newData[id][model.$typeKey()] = record[model.$typeKey()] }
}
Expand Down
8 changes: 4 additions & 4 deletions packages/pinia-orm/tests/feature/hooks/saving.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ describe('feature/hooks/saving', () => {
@BelongsTo(() => Post, 'postId') declare post: User | null

static saving (model: Model, record?: Element) {
model.name = 'John'
expect(model.name).not.toBe(record.name)
const fields = model.$fields()
for (const name in fields) {
if (fields[name] instanceof BelongsToClass && record && record[name] === null) { model[(fields[name] as BelongsToClass).foreignKey] = null }
if (fields[name] instanceof BelongsToClass && record && record.name === 'John Doe') { model[(fields[name] as BelongsToClass).foreignKey] = null }
}
}
}
Expand All @@ -62,14 +62,14 @@ describe('feature/hooks/saving', () => {

assertState({
users: {
1: { id: 1, name: 'John', age: 30, postId: 1 },
1: { id: 1, name: 'John Doe', age: 30, postId: 1 },
},
posts: {
1: { id: 1, title: 'News' },
},
})

useRepo(User).save({ id: 1, name: 'John Doe', age: 30, post: null })
useRepo(User).save({ id: 1, name: 'John', age: 30, post: null })

assertState({
users: {
Expand Down