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

Persisted getter properties #3936

Closed
dkeleee opened this issue Jan 12, 2023 · 1 comment
Closed

Persisted getter properties #3936

dkeleee opened this issue Jan 12, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@dkeleee
Copy link

dkeleee commented Jan 12, 2023

Consider an entity that defines a getter. The getter uses other properties to return a result.

Simple example:

@Entity()
class Test {
  @PrimaryKey()
  id!: number;

  @Property()
  value!: number;

  @Property({ persist: true })
  get doubleValue(): number {
    return this.value * 2;
  }
}

I would like to have an option that makes doubleValue have the following features:

  • it returns the correct value immediately after value is updated, without flushing
  • it can be used as a filter when querying for entities
  • it can be indexed

The simplest solution seems to be to always use the getter value in application code and to persist it to the database to make it useable in queries.

The code above seems to save the getter value on first insert and then exclusively uses the database value. This is described in #2760.

const test = new Test();
test.value = 5;
em.persist(test);
await em.flush();

console.log(test.doubleValue); // "10"
test.value = 7;
console.log(test.doubleValue); // "10", but "14" is expected

Considered alternatives:

@Property({ onUpdate: (e) => e.value * 2 }) and @Formula()
Both won't return the correct doubleValue after value is updated but before a flush.

A potential alternative solution would be to allow a @Formula() to be combined with a getter.
The formula snippet would be used inside SQL queries and the getter would be used inside application code.
Nothing would be persisted.

This feature exists in SQLAlchemy:
https://docs.sqlalchemy.org/en/20/orm/extensions/hybrid.html#defining-expression-behavior-distinct-from-attribute-behavior

The downside of this approach is that some operations that can be written in application code might not be possible to represent with SQL.

@dkeleee dkeleee added the enhancement New feature or request label Jan 12, 2023
@B4nan
Copy link
Member

B4nan commented Jan 12, 2023

Maybe we could add explicit hydrate option, which could be used to omit the property from hydration functions.

  @Property({ persist: true, hydrate: false })
  get doubleValue(): number {
    return this.value * 2;
  }

Sounds like an easy fix, PR welcome for that.

@B4nan B4nan added the good first issue Good for newcomers label Jan 12, 2023
@B4nan B4nan closed this as completed in f4ba092 Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants