Skip to content

Commit f7c5352

Browse files
committed
Merge branch 'main' into dev
2 parents 497f5c3 + 1ab7e64 commit f7c5352

File tree

15 files changed

+462
-245
lines changed

15 files changed

+462
-245
lines changed

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.0.490",
3+
"version": "1.0.492",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/common/CIMap.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export default class CIMap<V> implements Map<string, V> {
2+
3+
[Symbol.toStringTag] = "CIMap";
4+
5+
get size() {
6+
return this.map.size;
7+
}
8+
9+
private map = new Map<string,V>();
10+
11+
12+
clear() {
13+
this.map.clear();
14+
}
15+
delete(key: string): boolean {
16+
key = key?.toLowerCase();
17+
return this.map.delete(key);
18+
}
19+
forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void, thisArg?: any): void {
20+
return this.map.forEach(callbackfn);
21+
}
22+
get(key: string): V {
23+
key = key?.toLowerCase();
24+
return this.map.get(key);
25+
}
26+
has(key: string): boolean {
27+
key = key?.toLowerCase();
28+
return this.map.has(key);
29+
}
30+
set(key: string, value: V): this {
31+
key = key?.toLowerCase();
32+
this.map.set(key, value);
33+
return this;
34+
}
35+
36+
entries() {
37+
return this.map.entries();
38+
}
39+
keys() {
40+
return this.map.keys();
41+
}
42+
values() {
43+
return this.map.values();
44+
}
45+
[Symbol.iterator]() {
46+
return this.map[Symbol.iterator]();
47+
}
48+
49+
50+
}

src/common/IColumnSchema.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@ export default interface IColumnSchema {
88
computed?: any;
99
ownerName?: string;
1010
ownerType?: string;
11+
}
12+
13+
export interface IIndexSchema {
14+
name: string;
15+
}
16+
17+
export interface IConstraintSchema {
18+
name: string;
19+
}
20+
21+
export interface IForeignKeyConstraintSchema {
22+
name: string;
1123
}

src/drivers/base/BaseDriver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ChangeEntry, { IChange } from "../../model/changes/ChangeEntry.js";
1212
import type EntityContext from "../../model/EntityContext.js";
1313
import { BinaryExpression, Constant, DeleteStatement, Expression, Identifier, InsertStatement, ReturnUpdated, SelectStatement, TableLiteral, UpdateStatement, UpsertStatement, ValuesStatement } from "../../query/ast/Expressions.js";
1414
import { Query } from "../../query/Query.js";
15+
import type ExistingSchema from "./ExistingSchema.js";
1516

1617
export interface IRecord {
1718
[key: string]: string | boolean | number | Date | Uint8Array | Blob;
@@ -210,9 +211,6 @@ export abstract class BaseConnection {
210211
return tx;
211212
}
212213

213-
abstract getColumnSchema(schema: string): Promise<IColumnSchema[]>;
214-
215-
216214
protected abstract createDbTransaction(): Promise<EntityTransaction>;
217215

218216
}

src/drivers/base/ExistingSchema.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import CIMap from "../../common/CIMap.js";
2+
import IColumnSchema, { IConstraintSchema, IForeignKeyConstraintSchema, IIndexSchema } from "../../common/IColumnSchema.js";
3+
import { BaseConnection } from "./BaseDriver.js";
4+
5+
export default class ExistingSchema {
6+
7+
public readonly tables: Map<string, Map<string, IColumnSchema>>;
8+
public readonly indexes: Map<string, IIndexSchema>;
9+
public readonly constraints: Map<string, IConstraintSchema>;
10+
public readonly foreignKeys: Map<string, IForeignKeyConstraintSchema>;
11+
12+
constructor(
13+
caseInsensitive = false,
14+
{
15+
columns,
16+
indexes,
17+
constraints,
18+
foreignKeys
19+
}: {
20+
columns: IColumnSchema[],
21+
indexes?: IIndexSchema[],
22+
constraints?: IConstraintSchema[],
23+
foreignKeys?: IForeignKeyConstraintSchema[]
24+
}
25+
) {
26+
27+
this.tables = caseInsensitive
28+
? new CIMap<CIMap<IColumnSchema>>()
29+
: new Map<string,Map<string, IColumnSchema>>();
30+
31+
for (const c of columns) {
32+
const table = c.ownerName;
33+
let list = this.tables.get(table);
34+
if (!list) {
35+
list = caseInsensitive ? new CIMap<IColumnSchema>() : new Map<string, IColumnSchema>();
36+
this.tables.set(table, list);
37+
}
38+
list.set(c.name, c);
39+
}
40+
41+
this.indexes = new CIMap<IIndexSchema>();
42+
43+
for (const index of indexes) {
44+
this.indexes.set(index.name, index);
45+
}
46+
47+
this.constraints = new CIMap<IConstraintSchema>();
48+
49+
for (const constraint of constraints) {
50+
this.constraints.set(constraint.name, constraint);
51+
}
52+
53+
this.foreignKeys = new CIMap<IForeignKeyConstraintSchema>();
54+
55+
for (const fk of foreignKeys) {
56+
this.foreignKeys.set(fk.name, fk);
57+
}
58+
}
59+
60+
}

src/drivers/postgres/PostgreSqlDriver.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import DateTime from "../../types/DateTime.js";
1111
import { BaseConnection, BaseDriver, EntityTransaction, IDbConnectionString, IDbReader, IQuery, toQuery } from "../base/BaseDriver.js";
1212
import pg from "pg";
1313
import Cursor from "pg-cursor";
14+
import ExistingSchema from "../base/ExistingSchema.js";
1415
export interface IPgSqlConnectionString extends IDbConnectionString {
1516

1617
user?: string, // default process.env.PGUSER || process.env.USER
@@ -220,38 +221,6 @@ class PostgreSqlConnection extends BaseConnection {
220221
return new PostgresAutomaticMigrations(context);
221222
}
222223

223-
async getColumnSchema(schema: string): Promise<IColumnSchema[]> {
224-
const text = `
225-
select
226-
column_name as "columnName",
227-
case data_type
228-
when 'bigint' then 'BigInt'
229-
when 'boolean' then 'Boolean'
230-
when 'timestamp' then 'DateTime'
231-
when 'timestamp with time zone' then 'DateTime'
232-
when 'timestamp without time zone' then 'DateTime'
233-
when 'integer' then 'Int'
234-
when 'real' then 'Double'
235-
when 'numeric' then 'Decimal'
236-
else 'Char' end as "dataType",
237-
case
238-
when is_nullable = 'YES' then true
239-
else false end as "nullable",
240-
character_maximum_length as "length",
241-
case
242-
when is_identity = 'YES' then 'identity'
243-
else null end as "identity",
244-
case
245-
when is_generated = 'YES' then '() => 1'
246-
else null end as "computed",
247-
table_name as "ownerName",
248-
'table' as "ownerType"
249-
from information_schema.columns
250-
where table_schema = $1`;
251-
const r = await this.executeQuery({ text, values: [ schema ]});
252-
return r.rows;
253-
}
254-
255224
public async executeReader(command: IQuery, signal?: AbortSignal): Promise<IDbReader> {
256225
return new DbReader(command, this, signal);
257226
}

src/drivers/sql-server/SqlServerDriver.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import EntityType from "../../entity-query/EntityType.js";
1111
import DateTime from "../../types/DateTime.js";
1212
import IColumnSchema from "../../common/IColumnSchema.js";
1313
import type EntityContext from "../../model/EntityContext.js";
14+
import ExistingSchema from "../base/ExistingSchema.js";
1415

1516
export type ISqlServerConnectionString = IDbConnectionString & sql.config;
1617

@@ -129,48 +130,6 @@ export class SqlServerConnection extends BaseConnection {
129130
super(driver);
130131
}
131132

132-
async getColumnSchema(schema: string): Promise<IColumnSchema[]> {
133-
const text = `
134-
SELECT
135-
COLUMN_NAME as [name],
136-
CASE DATA_TYPE
137-
WHEN 'bit' THEN 'Boolean'
138-
WHEN 'int' Then 'Int'
139-
WHEN 'bigint' THEN 'BigInt'
140-
WHEN 'date' then 'DateTime'
141-
WHEN 'datetime' then 'DateTime'
142-
WHEN 'datetime2' then 'DateTime'
143-
WHEN 'real' then 'Float'
144-
WHEN 'double' then 'Double'
145-
WHEN 'decimal' then 'Decimal'
146-
WHEN 'identity' then 'UUID'
147-
WHEN 'varbinary' then 'ByteArray'
148-
WHEN 'geometry' then 'Geometry'
149-
ELSE 'Char'
150-
END as [dataType],
151-
CASE WHEN IS_NULLABLE = 'YES' THEN 1 ELSE 0 END as [nullable],
152-
CHARACTER_MAXIMUM_LENGTH as [length],
153-
CASE
154-
WHEN COLUMN_DEFAULT = 'getutcdate()' then '() => Sql.date.now()'
155-
WHEN COLUMN_DEFAULT = '(getutcdate())' then '() => Sql.date.now()'
156-
WHEN COLUMN_DEFAULT = '(newid())' then '() => Sql.crypto.randomUUID()'
157-
WHEN (COLUMN_DEFAULT = '(0)' OR COLUMN_DEFAULT = '((0))')
158-
AND DATA_TYPE = 'bit' THEN '() => false'
159-
WHEN (COLUMN_DEFAULT = '(1)' OR COLUMN_DEFAULT = '((1))')
160-
AND DATA_TYPE = 'bit' THEN '() => true'
161-
WHEN COLUMN_DEFAULT is NULL THEN ''
162-
ELSE '() => ' + COLUMN_DEFAULT
163-
END as [default],
164-
ColumnProperty(OBJECT_ID(TABLE_SCHEMA+'.'+TABLE_NAME),COLUMN_NAME,'IsComputed') as [computed],
165-
TABLE_NAME as [ownerName],
166-
'table' as [ownerType]
167-
FROM INFORMATION_SCHEMA.COLUMNS
168-
WHERE TABLE_SCHEMA = $1
169-
`;
170-
const r = await this.executeQuery({ text, values: [schema] });
171-
return r.rows;
172-
}
173-
174133
public async executeReader(command: IQuery, signal?: AbortSignal): Promise<IDbReader> {
175134
command = toQuery(command);
176135
let rq = await this.newRequest(signal);

src/migrations/ExistingSchema.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)