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

feat(core): allow using SQL expressions with custom types #1389

Merged
merged 1 commit into from
Feb 3, 2021

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Feb 3, 2021

Adds two methods to the custom type interface that allows to adjust the SQL fragment
used to select or update the value.

Example usage:

export class PointType extends Type<Point, string> {

  convertToDatabaseValue(value: Point): string {
    return `point(${value.latitude} ${value.longitude})`;
  }

  convertToJSValue(value: string): Point {
    const m = value.match(/point\((\d+(\.\d+)?) (\d+(\.\d+)?)\)/i);
    return new Point(+m[1], +m[3]);
  }

  convertToJSValueSQL(key: string) {
    return `ST_AsText(${key})`;
  }

  convertToDatabaseValueSQL(key: string) {
    return `ST_PointFromText(${key})`;
  }

  getColumnType(): string {
    return 'point';
  }

}

Closes #735

Adds two methods to the custom type interface that allows to adjust the SQL fragment
used to select or update the value.

Example usage:

```ts
export class PointType extends Type<Point, string> {

  convertToDatabaseValue(value: Point): string {
    return `point(${value.latitude} ${value.longitude})`;
  }

  convertToJSValue(value: string): Point {
    const m = value.match(/point\((\d+(\.\d+)?) (\d+(\.\d+)?)\)/i);
    return new Point(+m[1], +m[3]);
  }

  convertToJSValueSQL(key: string) {
    return `ST_AsText(${key})`;
  }

  convertToDatabaseValueSQL(key: string) {
    return `ST_PointFromText(${key})`;
  }

  getColumnType(): string {
    return 'point';
  }

}
```

Closes #735
@B4nan B4nan merged commit 83fe6ea into master Feb 3, 2021
@B4nan B4nan deleted the advanced-custom-types branch February 3, 2021 16:19
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

Successfully merging this pull request may close these issues.

It's possible use native data transformation features of the DB?
1 participant