Skip to content

Commit f37f7ee

Browse files
committed
added gist constraints on index creation
1 parent 2da3d1e commit f37f7ee

6 files changed

Lines changed: 28 additions & 10 deletions

File tree

src/decorators/IIndex.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

src/decorators/ISqlType.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
};

src/migrations/postgres/PostgresAutomaticMigrations.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ICheckConstraint from "../../decorators/ICheckConstraint.js";
33
import { IColumn } from "../../decorators/IColumn.js";
44
import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js";
55
import { IIndex } from "../../decorators/IIndex.js";
6+
import { isSpatialType } from "../../decorators/ISqlType.js";
67
import EntityType from "../../entity-query/EntityType.js";
78
import type EntityContext from "../../model/EntityContext.js";
89
import 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

src/migrations/postgres/PostgresMigrations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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) {

src/migrations/sql-server/SqlServerAutomaticMigrations.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ICheckConstraint from "../../decorators/ICheckConstraint.js";
33
import { IColumn } from "../../decorators/IColumn.js";
44
import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js";
55
import { IIndex } from "../../decorators/IIndex.js";
6+
import { isSpatialType } from "../../decorators/ISqlType.js";
67
import { BaseConnection } from "../../drivers/base/BaseDriver.js";
78
import ExistingSchema from "../../drivers/base/ExistingSchema.js";
89
import { 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

src/tests/model/ShoppingContext.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff 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
})
7272
export class User {
7373

0 commit comments

Comments
 (0)