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

Scenarios that includes viewmodels. #81

Closed
3ldar opened this issue Jun 9, 2022 · 2 comments
Closed

Scenarios that includes viewmodels. #81

3ldar opened this issue Jun 9, 2022 · 2 comments
Labels

Comments

@3ldar
Copy link

3ldar commented Jun 9, 2022

First of all, I would like to thank you for this great library. Using multiple ViewModels for the same entity is a common scenario for web applications (Different validation sets for different operations, hiding and exposing some properties depending on the situations, etc.). I wonder how remult handles this kind of scenario. It would be nice if you can redirect me to a sample or documentation.

@noam-honig
Copy link
Collaborator

First of all, thanks :)

With regards to ViewModels, there are several available strategies that you can employ.

  • To control the hiding and exposing of properties, you can use the inclideInApi option of a field, for example:
class User{
...
@Fields.string({ includeInApi:false })
password='';
}

Or an authorization rule:

class User{
...
@Fields.string({ includeInApi:"Admin" })
password='';
}

Or even a condition that is based on the instance itself:

class User{
...
@Fields.string<User>({ includeInApi:(remult)=>/* some rule */  })
password='';
}
  • You can use the serverExpression field option, to enrich the view model with server data. Here's an example from the CRM-Demo

  • You can also use the sqlExpression if you're using Postgres to create more sophisticated sql's

  • You can use multiple entities that view the same database table and optionally use inheritance to reduce duplicity, for example:

@Entity("deliveries")
export class Deliveries {
    @Fields.uuid()
    id?: string;
    @Fields.string()
    name = '';
    @Fields.boolean()
    deleted = false;
}

@Entity<ActiveDeliveries>("activeDeliveries", {
    dbName: 'deliveries',
    backendPrefilter: {
        deleted: false
    }
})
export class ActiveDeliveries extends Deliveries {
}

If you have a specific example that you are interested in, please share it here, and we'll be happy to review it together.

@3ldar
Copy link
Author

3ldar commented Jun 10, 2022

Thanks for the reply. I have been mentoring a group for a while and we have been reviewing some nodejs frameworks these questions came to mind while in that brief review.

@3ldar 3ldar closed this as completed Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants