Skip to content

feat(pinia-orm): add record to saving hooks#491

Merged
CodeDredd merged 1 commit intomasterfrom
record-to-hooks
Oct 27, 2022
Merged

feat(pinia-orm): add record to saving hooks#491
CodeDredd merged 1 commit intomasterfrom
record-to-hooks

Conversation

@CodeDredd
Copy link
Copy Markdown
Owner

@CodeDredd CodeDredd commented Oct 26, 2022

🔗 Linked issue

#468

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Passing the orginal data passed to a model to hooks like saving, saved, creating, created, updating and updated

  static updated (model, record) {
     ...
  }

Also now every property of relations are declared public so they can be accessed. So you can make queries dependend on field declarations.

Example:

import type { Element } from 'pinia-orm'
import { BelongsTo as BelongsToClass, Model, useRepo } from 'pinia-orm'
import { Attr, BelongsTo, Num, Str } from 'pinia-orm/dist/decorators'
import Post from '~/models/Post'

export default class User extends Model {
  static entity = 'users'

  @Num(0) declare id: number
  @Attr() declare postId: number | null
  @Str('') declare name: string
  @Num(0) declare age: number
  @BelongsTo(() => Post, 'postId') declare post: Post | null

  static saving(model: Model, record?: Element) {
    model.name = 'John'
    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
    }
  }
}

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@CodeDredd CodeDredd added the enhancement New feature or request label Oct 26, 2022
@CodeDredd CodeDredd merged commit 3153101 into master Oct 27, 2022
@CodeDredd CodeDredd deleted the record-to-hooks branch October 27, 2022 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant