@dryv/typeormv0.6.0
TypeORM entities with typed columns, relations, checks and indexes.
dryv pack add @dryv/typeorm{#- Column naming follows the pack input: snake (cart_id) or camel (cartId). -#}
{%- macro column(name) -%}
{%- if pack.inputs.naming_strategy == 'camel' -%}{{ name.camel }}{%- else -%}{{ name.snake }}{%- endif -%}
{%- endmacro -%}
{#- The generated symbol of another planned file, found by the subject it represents. -#}
{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{#- TypeORM column options for a field that is stored in a column. -#}
{%- macro column_options(field) -%}
{%- set value = field.value -%}
{%- if value.is.geometry -%}
{%- set spatial = value.geometry -%}
type: "geometry", spatialFeatureType: "{{ 'Geometry' if spatial.shape == 'any' else spatial.shape | capitalize }}{{ (spatial.dimensions | string)[2:] | upper }}", srid: {{ spatial.srid }}
{%- elif value.is.property and value.is.primitive and field.array is none -%}
{%- set primitive = value.property.primitive -%}
{%- if primitive.is.string and primitive.is.uuid -%}type: "uuid"
{%- elif primitive.is.string and primitive.constraints.length is not none and primitive.constraints.length.has_max -%}type: "varchar", length: {{ primitive.constraints.length.max }}
{%- elif primitive.is.string and primitive.constraints.length is not none and primitive.constraints.length.has_exact -%}type: "char", length: {{ primitive.constraints.length.exact }}
{%- elif primitive.is.string -%}type: "text"
{%- elif primitive.is.boolean -%}type: "boolean"
{%- elif primitive.money is not none and primitive.money.minor_units -%}type: "bigint", transformer: new ColumnNumericTransformer()
{%- elif primitive.is.integer and primitive.constraints.bits == 64 -%}type: "bigint", transformer: new ColumnNumericTransformer()
{%- elif primitive.is.integer -%}type: "integer"
{%- elif primitive.is.decimal -%}type: "numeric", precision: {{ primitive.constraints.precision }}, scale: {{ primitive.constraints.scale }}, transformer: new ColumnNumericTransformer()
{%- else -%}type: "double precision"
{%- endif -%}
{%- elif value.is.property and value.is.enum and field.array is none -%}type: "enum", enum: {{ symbol_for(value.property.enum) }}
{%- elif value.is.property and value.is.temporal and field.array is none -%}
{%- set temporal = value.property.temporal -%}
{%- if temporal.is.date -%}type: "date"
{%- elif temporal.is.time -%}type: "time"
{%- elif temporal.is.duration or temporal.is.interval -%}type: "interval"
{%- else -%}type: "timestamptz"
{%- endif -%}
{%- else -%}type: "jsonb"
{%- endif -%}
{%- endmacro -%}
{#- The TypeScript type of a stored field. -#}
{%- macro ts_type(field) -%}
{%- set value = field.value -%}
{#- Named properties use the declarations of the bound types pack; date-like columns are Date. -#}
{%- if value.is.geometry -%}
{%- set base = 'import("typeorm").Geometry' -%}
{%- elif value.is.property and value.is.primitive -%}
{%- set base = symbol_for(value.property.primitive) -%}
{%- elif value.is.property and value.is.enum -%}
{%- set base = symbol_for(value.property.enum) -%}
{%- elif value.is.property and value.is.temporal and not value.property.temporal.is.duration and not value.property.temporal.is.interval -%}
{%- set base = 'Date' -%}
{%- elif value.is.property and value.is.temporal -%}
{%- set base = symbol_for(value.property.temporal) -%}
{%- elif value.is.property and value.is.record -%}
{%- set base = symbol_for(value.property.record) -%}
{%- elif value.is.property and value.is.composite -%}
{%- set base = symbol_for(value.property.composite) -%}
{%- elif value.is.property and value.is.literal -%}
{%- set base = symbol_for(value.property.literal) -%}
{%- elif value.is.property and value.is.union -%}
{%- set base = symbol_for(value.property.union) -%}
{%- elif value.is.property and value.is.tuple -%}
{%- set base = symbol_for(value.property.tuple) -%}
{%- elif value.is.property and value.is.generic -%}
{%- set base = symbol_for(value.property.generic) -%}
{%- else -%}
{%- set base = 'unknown' -%}
{%- endif -%}
{%- if field.array is not none -%}{{ base }}[]{%- else -%}{{ base }}{%- endif -%}
{%- endmacro -%}
{%- macro is_primary(storage, field) -%}
{%- if storage is not none -%}{%- for key in storage.primary_key -%}{%- if key.raw == field.name.raw -%}yes{%- endif -%}{%- endfor -%}{%- endif -%}
{%- endmacro -%}
{%- macro on_delete(value) -%}
{%- if value == 'cascade' -%}CASCADE{%- elif value == 'restrict' -%}RESTRICT{%- elif value == 'detach' -%}SET NULL{%- else -%}SET DEFAULT{%- endif -%}
{%- endmacro -%}
{#- One side of an invariant comparison, as SQL over column names. -#}
{%- macro sql_operand(operand) -%}
{%- if operand.is.field -%}{{ column(operand.field) }}
{%- elif operand.is.literal -%}{{ operand.value | tojson | replace('"', "'") }}
{%- else -%}({% for term in operand.terms %}{% if term.kind == 'field' %}{{ column(term.field) }}{% else %}{{ term.value | tojson | replace('"', "'") }}{% endif %}{% if not loop.last %} {% if operand.operator == 'add' %}+{% elif operand.operator == 'subtract' %}-{% elif operand.operator == 'multiply' %}*{% else %}/{% endif %} {% endif %}{% endfor %})
{%- endif -%}
{%- endmacro -%}
{%- macro sql_operator(operator) -%}
{%- if operator == 'equal' -%}={%- elif operator == 'not-equal' -%}<>{%- elif operator == 'less-than' -%}<{%- elif operator == 'less-than-or-equal' -%}<={%- elif operator == 'greater-than' -%}>{%- else -%}>={%- endif -%}
{%- endmacro -%}
{#- TypeORM takes a SQL predicate, so use the stored column name for a direct null check. -#}
{%- macro index_predicate(index) -%}
{%- if index.where is not none and index.where.form.is.comparison -%}
{%- for comparison in index.where.form.comparisons -%}
{%- if comparison.operator in ['equal', 'not-equal'] -%}
{%- set left = comparison.left -%}
{%- set right = comparison.right -%}
{%- if left.is.field and right.is.literal and right.value is none -%}
"{{ column(left.field) | replace('"', '""') }}" IS {% if comparison.operator == 'not-equal' %}NOT {% endif %}NULL
{%- elif right.is.field and left.is.literal and left.value is none -%}
"{{ column(right.field) | replace('"', '""') }}" IS {% if comparison.operator == 'not-equal' %}NOT {% endif %}NULL
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{#- One class member: a column, a relation, or both. Each starts with a blank line. -#}
{#- The relation property is named after its foreign-key field (actingAffiliationId ->
actingAffiliation), so two keys to the same entity never produce the same property. -#}
{%- macro relation_name(key) -%}
{#- Strip a trailing "Id" only: the sentinel marks the end of the name. -#}
{%- set stripped = (key ~ '#') | replace('Id#', '') | replace('#', '') -%}
{%- if stripped != key and stripped != '' -%}{{ stripped }}{%- else -%}{{ key }}Relation{%- endif -%}
{%- endmacro -%}
{%- macro member(schema, storage, field) -%}
{%- if field.related is not none and symbol_for(field.related.via.schema) == '' %}
{#- The other side isn't a generated entity, so there's no navigation to declare. -#}
{%- elif field.related is not none %}
@OneToMany(() => {{ symbol_for(field.related.via.schema) }}, (item) => item.{{ relation_name(field.related.via.field.camel) }})
{{ field.name.camel }}?: Relation<{{ symbol_for(field.related.via.schema) }}[]>;
{%- elif field.value.is.schema and field.relation is none %}
@Column({ name: "{{ column(field.name) }}", type: "jsonb"{% if field.optional or field.nullable %}, nullable: true{% endif %} })
{% if field.generated is not none %}readonly {% endif %}{{ field.name.camel }}{% if field.optional or field.nullable %}?{% else %}!{% endif %}: unknown{% if field.optional or field.nullable %} | null{% endif %};
{%- else %}
{%- set primary = is_primary(storage, field) == 'yes' %}
{% if primary and field.mutation == 'system' and field.value.is.property and field.value.is.primitive and field.value.property.primitive.is.uuid %}@PrimaryGeneratedColumn("uuid", { name: "{{ column(field.name) }}" }){% elif primary %}@PrimaryColumn({ name: "{{ column(field.name) }}", {{ column_options(field) }} }){% else %}@Column({ name: "{{ column(field.name) }}", {{ column_options(field) }}{% if field.optional or field.nullable %}, nullable: true{% endif %}{% if field.default is not none and field.default.kind == 'value' %}, default: {{ field.default.value | tojson }}{% endif %}{% if field.generated is not none %}, insert: false, update: false{% endif %}{{ comment(field) }} }){% endif %}
{% if field.generated is not none %}readonly {% endif %}{{ field.name.camel }}{% if field.optional or field.nullable %}?{% else %}!{% endif %}: {{ ts_type(field) }}{% if field.optional or field.nullable %} | null{% endif %};
{%- if field.relation is not none and symbol_for(field.relation.target.schema) == '' %}
// Relation target {{ field.relation.target.schema.id }} has no generated entity here; TypeORM FK and navigation are omitted.
{%- endif %}
{%- if field.relation is not none and symbol_for(field.relation.target.schema) %}
@{% if field.relation.is.one_to_one %}OneToOne{% else %}ManyToOne{% endif %}(() => {{ symbol_for(field.relation.target.schema) }}{% if field.relation.on_delete is not none %}, { onDelete: "{{ on_delete(field.relation.on_delete) }}" }{% endif %})
@JoinColumn({ name: "{{ column(field.name) }}" })
{{ relation_name(field.name.camel) }}?: Relation<{{ symbol_for(field.relation.target.schema) }}>;
{%- endif %}
{%- endif %}
{%- endmacro -%}
{#- A column comment from the property's description, when it has one. -#}
{%- macro comment(field) -%}
{%- if field.value.is.property and field.value.is.primitive and field.value.property.primitive.description is not none -%}, comment: {{ field.value.property.primitive.description | tojson }}{%- endif -%}
{%- endmacro -%}
{%- set storage = schema.facets.storage -%}
import {
Check,
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
OneToMany,
OneToOne,
PrimaryColumn,
PrimaryGeneratedColumn,
type Relation,
} from "typeorm";
import { ColumnNumericTransformer } from "{{ (file.path.import_base ~ 'common/schema/entities/base/column-numeric.transformer.ts') if file.path.import_base else '../../../common/schema/entities/base/column-numeric.transformer.ts' }}";
{%- for dependency in file.dependencies %}{% if dependency.symbol and dependency.symbol != file.symbol %}
import {% if dependency.slot in ['property.types', 'schema.types'] %}type {% endif %}{ {{ dependency.symbol }} } from "{{ (dependency.path.prefixed or ('./' ~ dependency.path.relative)) }}";
{%- endif %}{% endfor %}
/**
* {{ schema.description or schema.name.title }}
*/
@Entity({ {% if storage is not none and storage.namespace is not none %}schema: "{{ storage.namespace }}", {% endif %}name: "{% if storage is not none and storage.name is not none %}{{ storage.name }}{% else %}{{ schema.name.snake }}{% endif %}" })
{%- if storage is not none %}
{%- for index in storage.indexes if index.method == "btree" %}
{%- set predicate = index_predicate(index) | trim %}
{%- if index.where is none or predicate %}
@Index({% if index.name is not none %}{{ index.name | tojson }}, {% endif %}[{% for item in index.fields %}"{{ item.camel }}"{% if not loop.last %}, {% endif %}{% endfor %}]{% if index.unique or predicate %}, { {% if index.unique %}unique: true{% if predicate %}, {% endif %}{% endif %}{% if predicate %}where: {{ predicate | tojson }}{% endif %} }{% endif %})
{%- endif %}
{%- endfor %}
{%- endif %}
{%- for invariant in schema.invariants %}
{%- if invariant.form.is.opaque %}
// Invariant "{{ invariant.name.raw }}" has no simple form, so it isn't enforced as a database check.
{%- else %}
@Check("chk_{{ schema.name.snake }}_{{ invariant.name.snake }}", `{% for comparison in invariant.form.comparisons %}{{ sql_operand(comparison.left) }} {{ sql_operator(comparison.operator) }} {{ sql_operand(comparison.right) }}{% if not loop.last %}{% if invariant.form.is.all %} AND {% else %} OR {% endif %}{% endif %}{% endfor %}`)
{%- endif %}
{%- endfor %}
export class {{ file.symbol }} {
{%- for field in schema.fields %}{% if is_primary(storage, field) == 'yes' %}{{ member(schema, storage, field) }}{% endif %}{% endfor %}
{%- for field in schema.fields %}{% if is_primary(storage, field) != 'yes' %}{{ member(schema, storage, field) }}{% endif %}{% endfor %}
}