File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ export default interface IIndexDef<T = any> {
1515 unique ?: boolean ;
1616 include ?: ( ( x :T ) => any ) [ ] ;
1717 indexType ?: string ;
18- spatial ?: boolean ;
1918 filter ?: ( x : T ) => boolean ;
2019}
2120
@@ -26,6 +25,5 @@ export interface IIndex{
2625 unique ?: boolean ;
2726 include ?: string [ ] ;
2827 indexType ?: string ;
29- spatial ?: boolean ;
3028 filter ?: ( ( x : any ) => boolean ) | string ;
3129}
Original file line number Diff line number Diff line change @@ -72,3 +72,12 @@ export type ISqlType =
7272 /** Geography - for SQL Server and other Geography compatible databases*/
7373 "Geography"
7474 ;
75+
76+
77+ export const isSpatialType = ( type : ISqlType ) => {
78+ switch ( type ) {
79+ case "Geography" :
80+ return true ;
81+ }
82+ return false ;
83+ } ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import ICheckConstraint from "../../decorators/ICheckConstraint.js";
33import { IColumn } from "../../decorators/IColumn.js" ;
44import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js" ;
55import { IIndex } from "../../decorators/IIndex.js" ;
6+ import { isSpatialType } from "../../decorators/ISqlType.js" ;
67import EntityType from "../../entity-query/EntityType.js" ;
78import type EntityContext from "../../model/EntityContext.js" ;
89import PostgresMigrations from "./PostgresMigrations.js" ;
@@ -108,6 +109,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
108109 : type . name ;
109110 const indexName = index . name ;
110111 const columns = [ ] ;
112+ let spatial = false ;
111113 for ( const column of index . columns ) {
112114 const columnName = column . name ;
113115 let operatorClass = "" ;
@@ -124,11 +126,14 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
124126 break ;
125127 }
126128 }
127- columns . push ( `${ columnName } ${ operatorClass } ${ index . spatial ? "" : ( column . descending ? "DESC" : "ASC" ) } ` ) ;
129+ const c = type . getColumn ( column . name ) ;
130+ const isColumnSpatial = isSpatialType ( c . dataType ) ;
131+ spatial ||= isColumnSpatial ;
132+ columns . push ( `${ columnName } ${ operatorClass } ${ isColumnSpatial ? "" : ( column . descending ? "DESC" : "ASC" ) } ` ) ;
128133 }
129134 let query = `CREATE ${ index . unique ? "UNIQUE" : "" } INDEX IF NOT EXISTS ${ indexName } ON ${ name } ` ;
130135
131- if ( index . spatial ) {
136+ if ( spatial ) {
132137 query += " USING GIST " ;
133138 }
134139
Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ export default abstract class PostgresMigrations extends Migrations {
7979
8080 async enableGeoSpatialTypes ( ) : Promise < void > {
8181 await this . connection . executeQuery ( `CREATE EXTENSION IF NOT EXISTS postgis;` ) ;
82+ await this . connection . executeQuery ( `CREATE EXTENSION IF NOT EXISTS btree_gist;` ) ;
8283 }
8384
8485 protected getColumnDefinition ( iterator : IColumn ) {
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import ICheckConstraint from "../../decorators/ICheckConstraint.js";
33import { IColumn } from "../../decorators/IColumn.js" ;
44import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js" ;
55import { IIndex } from "../../decorators/IIndex.js" ;
6+ import { isSpatialType } from "../../decorators/ISqlType.js" ;
67import { BaseConnection } from "../../drivers/base/BaseDriver.js" ;
78import ExistingSchema from "../../drivers/base/ExistingSchema.js" ;
89import { SqlServerLiteral } from "../../drivers/sql-server/SqlServerLiteral.js" ;
@@ -122,12 +123,16 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
122123 : type . name ;
123124 const indexName = index . name ;
124125 const columns = [ ] ;
126+ let spatial = true ;
125127 for ( const column of index . columns ) {
126128 const columnName = column . name ;
127- columns . push ( `${ columnName } ${ index . spatial ? "" : ( column . descending ? "DESC" : "ASC" ) } ` ) ;
129+ const c = type . getColumn ( column . name ) ;
130+ const isColumnSpatial = isSpatialType ( c . dataType ) ;
131+ spatial &&= isColumnSpatial ;
132+ columns . push ( `${ columnName } ${ isSpatialType ( c . dataType ) ? "" : ( column . descending ? "DESC" : "ASC" ) } ` ) ;
128133 }
129134
130- const indexType = index . spatial ? " SPATIAL " : "" ;
135+ const indexType = spatial ? " SPATIAL " : "" ;
131136
132137 let query = `IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = '${ indexName } ' AND object_id = OBJECT_ID('${ name } '))
133138 BEGIN
Original file line number Diff line number Diff line change @@ -64,10 +64,10 @@ export class CachedItem {
6464} )
6565@Index ( {
6666 name : "IX_Users_Geo" ,
67- columns : [ {
68- name :( x ) => x . location , descending : false
69- } ] ,
70- spatial : true
67+ columns : [
68+ { name :( x ) => x . location , descending : false } ,
69+ { name : ( x ) => x . userName , descending : false } ,
70+ ] ,
7171} )
7272export class User {
7373
You can’t perform that action at this time.
0 commit comments