import Column from "@entity-access/entity-access/dist/decorators/Column.js"; import Index from "@entity-access/entity-access/dist/decorators/Index.js"; import { RelateTo } from "@entity-access/entity-access/dist/decorators/Relate.js"; import Table from "@entity-access/entity-access/dist/decorators/Table.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import type { StoreAccountAddress } from "./StoreAddress.js"; import type Contact from "./Contact.js"; const geoTypes = [ "country", "state", "city"] as const; export type geoType = typeof geoTypes[number]; /** * Instead of storing City, State, Country as a separate tables, * it is alright to store everything in one table as most likely, * State and Country will be queried infrequently. */ @Table("GeoEntities") @Index({ name: "IX_GeoEntities_Name", columns:[ { name: (x) => x.name, descending: false, operatorClass: "varchar_pattern" }, { name: (x) => x.type, descending: false }, { name: (x) => x.parentID, descending: false }, { name: (x) => x.population, descending: true } ] }) @Index({ name: "IX_GeoEntities_ParentID", columns:[ { name: (x) => x.parentID, descending: false }, ], filter: (x) => x.parentID !== null }) @Index({ name: "UX_GeoEntities_Code", columns: [ { name: (x) => x.code, descending: false }, { name: (x) => x.type, descending: false }, { name: (x) => x.parentID, descending: false } ], unique: true }) @Index({ name: "IX_GeoEntities_Location", columns: [ { name: (x) => x.countryCode, descending: false }, { name: (x) => x.location, descending: false}, ], filter: (x) => x.countryCode !== null }) export default class GeoEntity { @Column({ dataType: "BigInt", key: true, generated: "identity"}) geoEntityID: number; @Column({ dataType: "Char", length: 20, enum: geoTypes }) type: geoType; @Column({ dataType: "Char", length: 200}) code: string; @Column({ dataType: "Char", length: 20, nullable: true}) countryCode: string; /** This can be used for synchronization */ @Column({ dataType: "Char", length: 200, nullable: true}) externalID: string; @Column({ dataType: "Char", length: 200, nullable: true}) fips: string; @Column({ dataType: "Char", length: 200}) name: string; @Column({ dataType: "Char", nullable: true}) fullName: string; @Column({ dataType: "Char", length: 200}) displayName: string; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(GeoEntity, { property: (x) => x.parent, inverseProperty: (x) => x.children }) parentID: number; @Column({ dataType: "Double", default: () => 0}) latitude: number; @Column({ dataType: "Double", default: () => 0}) longitude: number; @Column({ dataType: "Double", default: () => 0}) population: number; @Column({ dataType: "Geography" ,nullable:true}) location:any; @Column({ dataType: "Char", length: 5, nullable: true}) currencyCode: string; @Column({ dataType: "Char", length: 200, nullable: true}) currencyName: string; @Column({ dataType: "Char", length: 200, nullable: true}) isdCode: string; @Column({ dataType: "Char", length: 200, nullable: true}) phoneFormat: string; @Column({ dataType: "Char", length: 200, nullable: true}) timezone: string; /** Since zero is a valid timezoneOffset, we will make it nullable * so unavailable timezone can be set as null. */ @Column({ dataType: "Int", nullable: true}) timezoneOffset: number; @Column({ dataType: "DateTime", default: () => Sql.date.now()}) dateCreated: DateTime; @Column({ dataType: "DateTime", default: () => Sql.date.now()}) dateUpdated: DateTime; parent: GeoEntity; children: GeoEntity[]; cityAddresses: StoreAccountAddress[]; cityContacts: Contact[]; }