Skip to content

Commit 238460d

Browse files
authored
Merge pull request #67 from Entity-Access/main
Merge
2 parents b17f7bf + 14aca19 commit 238460d

9 files changed

Lines changed: 35 additions & 15 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@entity-access/entity-access",
3-
"version": "1.1.9",
3+
"version": "1.1.16",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

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/decorators/Index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default function Index<T>(
1414
unique,
1515
include,
1616
indexType,
17-
spatial,
1817
filter
1918
}
2019
: IIndexDef<T>) {
@@ -28,7 +27,6 @@ export default function Index<T>(
2827
include: include ? include.map(NameParser.parseMember) : void 0,
2928
dropNames,
3029
indexType,
31-
spatial,
3230
filter,
3331
columns
3432
};

src/migrations/postgres/PostgresAutomaticMigrations.ts

Lines changed: 11 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,14 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
108109
: type.name;
109110
const indexName = index.name;
110111
const columns = [];
112+
let spatial = false;
113+
114+
for(const column of index.columns) {
115+
const c = type.getColumn(column.name);
116+
const isColumnSpatial = isSpatialType(c.dataType);
117+
spatial ||= isColumnSpatial;
118+
}
119+
111120
for (const column of index.columns) {
112121
const columnName = column.name;
113122
let operatorClass = "";
@@ -124,11 +133,11 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
124133
break;
125134
}
126135
}
127-
columns.push(`${columnName} ${operatorClass} ${ index.spatial ? "" : (column.descending ? "DESC" : "ASC")}`);
136+
columns.push(`${columnName} ${operatorClass} ${ spatial ? "" : (column.descending ? "DESC" : "ASC")}`);
128137
}
129138
let query = `CREATE ${index.unique ? "UNIQUE" : ""} INDEX IF NOT EXISTS ${indexName} ON ${name}`;
130139

131-
if (index.spatial) {
140+
if (spatial) {
132141
query += " USING GIST ";
133142
}
134143

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)