adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.97k stars 637 forks source link

Append data to model JSON #1003

Closed behnamazimi closed 5 years ago

behnamazimi commented 5 years ago

I'm looking for the Laravel append functionality in lucid. I mean I want to add a parameter to fetched JSON like what append did in the Laravel.

I tried the Mutators but there was not the solution! Another way is loops, like "for" or "map" on the fetched data but it's not best practice! Also, the result is different when I work with ORM and when fetch data by Query Builder.

The Query Builder return a valid array of data but ORM return rows! what is that "rows" ?! Does it mean the rows of my model in DB? If so, how should I loop on it?!

thetutlage commented 5 years ago

@behnamazimi

Please don't expect me to read the Laravel docs and understand what append does and then code the solution for you.

I understand that AdonisJs is inspired by Laravel, but you guys have to also do some hard work and create actionable issues.

It will be nice, if you can share the current shape of your object and what is the desired shape.

behnamazimi commented 5 years ago

@thetutlage , you right. Let me explain by a real model.

I have a user model and this JSON is the result of a pure query in the AdonisJs and also Laravel.

[
  {
    name: "test user",
    username: "testuser",
    email: "user@example.com"
  },
  {
    name: "test user",
    username: "testuser",
    email: "user@example.com"
  },...
]

I wnat to add a new fake parameter (fake_param) to each item of my result. and after that the result must be like below:

[
  {
    name: "test user",
    username: "testuser",
    email: "user@example.com",
    fake_param: "random str"
  },
  {
    name: "test user",
    username: "testuser",
    email: "user@example.com",
    fake_param: "random str"
  },...
]

How should I do this in the AdonisJs?

In the Laravel, to add a new parameter to the result JSON we use append functionality.

Thanks for your time

thetutlage commented 5 years ago

I believe, you can get it working (unless is value is computed asynchronously) using computed properties.

class User extends Model {
  static get computed () {
     return ['fake_param']
  }

  getFakeParam () {
     return 'whatever'
  }
}

and then fetching users will append this property.

const users = await User.all()
users.toJSON()
RomainLanz commented 5 years ago

Closing since no answer from issue reporter.

farzanehp commented 5 years ago

@thetutlage Hi, Can I use get computed conditionally? (For example, append isOwner property if request's userId is equal to object's userId?

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.