Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL : Count result accessible only via ‘$extras’ field #28

Closed
ad-momo opened this issue Sep 24, 2024 · 2 comments
Closed

MySQL : Count result accessible only via ‘$extras’ field #28

ad-momo opened this issue Sep 24, 2024 · 2 comments

Comments

@ad-momo
Copy link

ad-momo commented Sep 24, 2024

Package version

@adonisjs/lucid 21.1.0

Describe the bug

Hello 👋,

For a count('*', alias) query, the result cannot be retrieved directly with the alias. You have to use the $extras field.

Database engine : MySQL 8.0.39 (Homebrew)
OS : macos14.4 (arm64)
MySQL Client NodeJS : mysql2 3.10.3

const users = await db
  .from('users')
  .count('*', 'total')

console.log(users[0].total) // undefined
console.log(users[0].$extras.total) // work correctly 

Thanks 😀

Reproduction repo

No response

@dunhamjared
Copy link
Contributor

That is by design.

Any attributes that don't fit into the model will be placed into: $extras

If you want to override this behavior, and have the total with the other attributes on serialization, you could define a custom extras serializer in a model, like so:

export default class Example extends BaseModel {
    
    serializeExtras() {
        return this.$extras
    }

}

Hope this helps!

@thetutlage
Copy link
Member

Closing, as nothing needs to be changed in the docs repo for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants