@yandjin-mikro-orm/postgresql 6.1.4-rc-sti-changes-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Martin Adámek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,15 @@
1
+ import { AbstractSqlConnection, type Knex } from "@yandjin-mikro-orm/knex";
2
+ export declare class PostgreSqlConnection extends AbstractSqlConnection {
3
+ createKnex(): void;
4
+ getDefaultClientUrl(): string;
5
+ getConnectionOptions(): Knex.PgConnectionConfig;
6
+ protected transformRawResult<T>(res: any, method: "all" | "get" | "run"): T;
7
+ /**
8
+ * monkey patch knex' postgres dialect so it correctly handles column updates (especially enums)
9
+ */
10
+ private patchKnex;
11
+ private addColumn;
12
+ private alterColumnNullable;
13
+ private addColumnDefault;
14
+ private dropColumnDefault;
15
+ }
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PostgreSqlConnection = void 0;
7
+ const type_overrides_1 = __importDefault(require("pg/lib/type-overrides"));
8
+ const postgres_array_1 = __importDefault(require("postgres-array"));
9
+ const knex_1 = require("@yandjin-mikro-orm/knex");
10
+ class PostgreSqlConnection extends knex_1.AbstractSqlConnection {
11
+ createKnex() {
12
+ this.patchKnex();
13
+ this.client = this.createKnexClient("pg");
14
+ this.connected = true;
15
+ }
16
+ getDefaultClientUrl() {
17
+ return "postgresql://postgres@127.0.0.1:5432";
18
+ }
19
+ getConnectionOptions() {
20
+ const ret = super.getConnectionOptions();
21
+ // use `select typname, oid, typarray from pg_type order by oid` to get the list of OIDs
22
+ const types = new type_overrides_1.default();
23
+ [
24
+ 1082, // date
25
+ 1114, // timestamp
26
+ 1184, // timestamptz
27
+ 1186, // interval
28
+ ].forEach((oid) => types.setTypeParser(oid, (str) => str));
29
+ [
30
+ 1182, // date[]
31
+ 1115, // timestamp[]
32
+ 1185, // timestamptz[]
33
+ 1187, // interval[]
34
+ ].forEach((oid) => types.setTypeParser(oid, (str) => postgres_array_1.default.parse(str)));
35
+ ret.types = types;
36
+ return ret;
37
+ }
38
+ transformRawResult(res, method) {
39
+ if (method === "get") {
40
+ return res.rows[0];
41
+ }
42
+ if (method === "all") {
43
+ return res.rows;
44
+ }
45
+ return {
46
+ affectedRows: res.rowCount,
47
+ insertId: res.rows[0] ? res.rows[0].id : 0,
48
+ row: res.rows[0],
49
+ rows: res.rows,
50
+ };
51
+ }
52
+ /**
53
+ * monkey patch knex' postgres dialect so it correctly handles column updates (especially enums)
54
+ */
55
+ patchKnex() {
56
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
57
+ const that = this;
58
+ const { PostgresDialectTableCompiler, TableCompiler } = knex_1.MonkeyPatchable;
59
+ PostgresDialectTableCompiler.prototype.addColumns = function (columns, prefix, colCompilers) {
60
+ if (prefix !== this.alterColumnsPrefix) {
61
+ // base class implementation for normal add
62
+ return TableCompiler.prototype.addColumns.call(this, columns, prefix);
63
+ }
64
+ // alter columns
65
+ for (const col of colCompilers) {
66
+ that.addColumn.call(this, col, that);
67
+ }
68
+ };
69
+ }
70
+ addColumn(col, that) {
71
+ const options = that.config.get("schemaGenerator");
72
+ const quotedTableName = this.tableName();
73
+ const type = col.getColumnType();
74
+ const colName = this.client.wrapIdentifier(col.getColumnName(), col.columnBuilder.queryContext());
75
+ const constraintName = `${this.tableNameRaw.replace(/^.*\.(.*)$/, "$1")}_${col.getColumnName()}_check`;
76
+ const useNative = col.args?.[2]?.useNative;
77
+ const alterType = col.columnBuilder.alterType;
78
+ const alterNullable = col.columnBuilder.alterNullable;
79
+ const defaultTo = col.modified.defaultTo;
80
+ if (defaultTo != null) {
81
+ that.dropColumnDefault.call(this, col, colName);
82
+ }
83
+ if (col.type === "enu" && !useNative) {
84
+ if (alterType) {
85
+ this.pushQuery({
86
+ sql: `alter table ${quotedTableName} alter column ${colName} type text using (${colName}::text)`,
87
+ bindings: [],
88
+ });
89
+ }
90
+ /* istanbul ignore else */
91
+ if (options.createForeignKeyConstraints && alterNullable) {
92
+ this.pushQuery({
93
+ sql: `alter table ${quotedTableName} add constraint "${constraintName}" ${type.replace(/^text /, "")}`,
94
+ bindings: [],
95
+ });
96
+ }
97
+ }
98
+ else if (type === "uuid") {
99
+ // we need to drop the default as it would be invalid
100
+ this.pushQuery({
101
+ sql: `alter table ${quotedTableName} alter column ${colName} drop default`,
102
+ bindings: [],
103
+ });
104
+ this.pushQuery({
105
+ sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::text::uuid)`,
106
+ bindings: [],
107
+ });
108
+ }
109
+ else if (alterType) {
110
+ this.pushQuery({
111
+ sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::${type})`,
112
+ bindings: [],
113
+ });
114
+ }
115
+ that.addColumnDefault.call(this, col, colName);
116
+ that.alterColumnNullable.call(this, col, colName);
117
+ }
118
+ alterColumnNullable(col, colName) {
119
+ const quotedTableName = this.tableName();
120
+ const nullable = col.modified.nullable;
121
+ if (!nullable) {
122
+ return;
123
+ }
124
+ if (nullable[0] === false) {
125
+ this.pushQuery({
126
+ sql: `alter table ${quotedTableName} alter column ${colName} set not null`,
127
+ bindings: [],
128
+ });
129
+ }
130
+ else {
131
+ this.pushQuery({
132
+ sql: `alter table ${quotedTableName} alter column ${colName} drop not null`,
133
+ bindings: [],
134
+ });
135
+ }
136
+ }
137
+ addColumnDefault(col, colName) {
138
+ const quotedTableName = this.tableName();
139
+ const defaultTo = col.modified.defaultTo;
140
+ if (!defaultTo) {
141
+ return;
142
+ }
143
+ if (defaultTo[0] !== null) {
144
+ const modifier = col.defaultTo(...defaultTo);
145
+ this.pushQuery({
146
+ sql: `alter table ${quotedTableName} alter column ${colName} set ${modifier}`,
147
+ bindings: [],
148
+ });
149
+ }
150
+ }
151
+ dropColumnDefault(col, colName) {
152
+ const quotedTableName = this.tableName();
153
+ const defaultTo = col.modified.defaultTo;
154
+ if (defaultTo?.[0] == null) {
155
+ this.pushQuery({
156
+ sql: `alter table ${quotedTableName} alter column ${colName} drop default`,
157
+ bindings: [],
158
+ });
159
+ }
160
+ }
161
+ }
162
+ exports.PostgreSqlConnection = PostgreSqlConnection;
@@ -0,0 +1,6 @@
1
+ import type { Configuration } from "@yandjin-mikro-orm/core";
2
+ import { AbstractSqlDriver } from "@yandjin-mikro-orm/knex";
3
+ import { PostgreSqlConnection } from "./PostgreSqlConnection";
4
+ export declare class PostgreSqlDriver extends AbstractSqlDriver<PostgreSqlConnection> {
5
+ constructor(config: Configuration);
6
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PostgreSqlDriver = void 0;
4
+ const knex_1 = require("@yandjin-mikro-orm/knex");
5
+ const PostgreSqlConnection_1 = require("./PostgreSqlConnection");
6
+ const PostgreSqlPlatform_1 = require("./PostgreSqlPlatform");
7
+ class PostgreSqlDriver extends knex_1.AbstractSqlDriver {
8
+ constructor(config) {
9
+ super(config, new PostgreSqlPlatform_1.PostgreSqlPlatform(), PostgreSqlConnection_1.PostgreSqlConnection, [
10
+ "knex",
11
+ "pg",
12
+ ]);
13
+ }
14
+ }
15
+ exports.PostgreSqlDriver = PostgreSqlDriver;
@@ -0,0 +1,8 @@
1
+ import { ExceptionConverter, type Dictionary, type DriverException } from "@yandjin-mikro-orm/core";
2
+ export declare class PostgreSqlExceptionConverter extends ExceptionConverter {
3
+ /**
4
+ * @link http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
5
+ * @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractPostgreSQLDriver.php
6
+ */
7
+ convertException(exception: Error & Dictionary): DriverException;
8
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PostgreSqlExceptionConverter = void 0;
4
+ const core_1 = require("@yandjin-mikro-orm/core");
5
+ class PostgreSqlExceptionConverter extends core_1.ExceptionConverter {
6
+ /* istanbul ignore next */
7
+ /**
8
+ * @link http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
9
+ * @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractPostgreSQLDriver.php
10
+ */
11
+ convertException(exception) {
12
+ switch (exception.code) {
13
+ case "40001":
14
+ case "40P01":
15
+ return new core_1.DeadlockException(exception);
16
+ case "0A000":
17
+ // Foreign key constraint violations during a TRUNCATE operation
18
+ // are considered "feature not supported" in PostgreSQL.
19
+ if (exception.message.includes("truncate")) {
20
+ return new core_1.ForeignKeyConstraintViolationException(exception);
21
+ }
22
+ break;
23
+ case "23502":
24
+ return new core_1.NotNullConstraintViolationException(exception);
25
+ case "23503":
26
+ return new core_1.ForeignKeyConstraintViolationException(exception);
27
+ case "23505":
28
+ return new core_1.UniqueConstraintViolationException(exception);
29
+ case "23514":
30
+ return new core_1.CheckConstraintViolationException(exception);
31
+ case "42601":
32
+ return new core_1.SyntaxErrorException(exception);
33
+ case "42702":
34
+ return new core_1.NonUniqueFieldNameException(exception);
35
+ case "42703":
36
+ return new core_1.InvalidFieldNameException(exception);
37
+ case "42P01":
38
+ return new core_1.TableNotFoundException(exception);
39
+ case "42P07":
40
+ return new core_1.TableExistsException(exception);
41
+ }
42
+ return super.convertException(exception);
43
+ }
44
+ }
45
+ exports.PostgreSqlExceptionConverter = PostgreSqlExceptionConverter;
@@ -0,0 +1,19 @@
1
+ import { MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from "@yandjin-mikro-orm/core";
2
+ import { PostgreSqlDriver } from "./PostgreSqlDriver";
3
+ import type { SqlEntityManager } from "@yandjin-mikro-orm/knex";
4
+ /**
5
+ * @inheritDoc
6
+ */
7
+ export declare class PostgreSqlMikroORM<EM extends EntityManager = SqlEntityManager> extends MikroORM<PostgreSqlDriver, EM> {
8
+ private static DRIVER;
9
+ /**
10
+ * @inheritDoc
11
+ */
12
+ static init<D extends IDatabaseDriver = PostgreSqlDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options?: Options<D, EM>): Promise<MikroORM<D, EM>>;
13
+ /**
14
+ * @inheritDoc
15
+ */
16
+ static initSync<D extends IDatabaseDriver = PostgreSqlDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options: Options<D, EM>): MikroORM<D, EM>;
17
+ }
18
+ export type PostgreSqlOptions = Options<PostgreSqlDriver>;
19
+ export declare function definePostgreSqlConfig(options: PostgreSqlOptions): Options<PostgreSqlDriver, SqlEntityManager<PostgreSqlDriver> & EntityManager<IDatabaseDriver<import("@yandjin-mikro-orm/core").Connection>>>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.definePostgreSqlConfig = exports.PostgreSqlMikroORM = void 0;
4
+ const core_1 = require("@yandjin-mikro-orm/core");
5
+ const PostgreSqlDriver_1 = require("./PostgreSqlDriver");
6
+ /**
7
+ * @inheritDoc
8
+ */
9
+ class PostgreSqlMikroORM extends core_1.MikroORM {
10
+ static DRIVER = PostgreSqlDriver_1.PostgreSqlDriver;
11
+ /**
12
+ * @inheritDoc
13
+ */
14
+ static async init(options) {
15
+ return super.init(options);
16
+ }
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ static initSync(options) {
21
+ return super.initSync(options);
22
+ }
23
+ }
24
+ exports.PostgreSqlMikroORM = PostgreSqlMikroORM;
25
+ /* istanbul ignore next */
26
+ function definePostgreSqlConfig(options) {
27
+ return (0, core_1.defineConfig)({ driver: PostgreSqlDriver_1.PostgreSqlDriver, ...options });
28
+ }
29
+ exports.definePostgreSqlConfig = definePostgreSqlConfig;
@@ -0,0 +1,96 @@
1
+ import { type IPostgresInterval } from "postgres-interval";
2
+ import { type EntityProperty, Type, type SimpleColumnMeta } from "@yandjin-mikro-orm/core";
3
+ import { AbstractSqlPlatform, type IndexDef } from "@yandjin-mikro-orm/knex";
4
+ import { PostgreSqlSchemaHelper } from "./PostgreSqlSchemaHelper";
5
+ import { PostgreSqlExceptionConverter } from "./PostgreSqlExceptionConverter";
6
+ export declare class PostgreSqlPlatform extends AbstractSqlPlatform {
7
+ protected readonly schemaHelper: PostgreSqlSchemaHelper;
8
+ protected readonly exceptionConverter: PostgreSqlExceptionConverter;
9
+ usesReturningStatement(): boolean;
10
+ usesCascadeStatement(): boolean;
11
+ supportsNativeEnums(): boolean;
12
+ supportsCustomPrimaryKeyNames(): boolean;
13
+ /**
14
+ * Postgres will complain if we try to batch update uniquely constrained property (moving the value from one entity to another).
15
+ * This flag will result in postponing 1:1 updates (removing them from the batched query).
16
+ * @see https://stackoverflow.com/questions/5403437/atomic-multi-row-update-with-a-unique-constraint
17
+ */
18
+ allowsUniqueBatchUpdates(): boolean;
19
+ getCurrentTimestampSQL(length: number): string;
20
+ getDateTimeTypeDeclarationSQL(column: {
21
+ length?: number;
22
+ }): string;
23
+ getDefaultDateTimeLength(): number;
24
+ convertIntervalToJSValue(value: string): unknown;
25
+ convertIntervalToDatabaseValue(value: IPostgresInterval): unknown;
26
+ getTimeTypeDeclarationSQL(): string;
27
+ getIntegerTypeDeclarationSQL(column: {
28
+ length?: number;
29
+ autoincrement?: boolean;
30
+ generated?: string;
31
+ }): string;
32
+ getBigIntTypeDeclarationSQL(column: {
33
+ autoincrement?: boolean;
34
+ }): string;
35
+ getTinyIntTypeDeclarationSQL(column: {
36
+ length?: number;
37
+ unsigned?: boolean;
38
+ autoincrement?: boolean;
39
+ }): string;
40
+ getUuidTypeDeclarationSQL(column: {
41
+ length?: number;
42
+ }): string;
43
+ getFullTextWhereClause(prop: EntityProperty): string;
44
+ supportsCreatingFullTextIndex(): boolean;
45
+ getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string;
46
+ getMappedType(type: string): Type<unknown>;
47
+ getRegExpOperator(val?: unknown, flags?: string): string;
48
+ getRegExpValue(val: RegExp): {
49
+ $re: string;
50
+ $flags?: string;
51
+ };
52
+ isBigIntProperty(prop: EntityProperty): boolean;
53
+ getArrayDeclarationSQL(): string;
54
+ getFloatDeclarationSQL(): string;
55
+ getDoubleDeclarationSQL(): string;
56
+ getEnumTypeDeclarationSQL(column: {
57
+ fieldNames: string[];
58
+ items?: unknown[];
59
+ nativeEnumName?: string;
60
+ }): string;
61
+ supportsMultipleStatements(): boolean;
62
+ marshallArray(values: string[]): string;
63
+ unmarshallArray(value: string): string[];
64
+ getBlobDeclarationSQL(): string;
65
+ getJsonDeclarationSQL(): string;
66
+ getSearchJsonPropertyKey(path: string[], type: string | undefined | Type, aliased: boolean, value?: unknown): string;
67
+ getJsonIndexDefinition(index: IndexDef): string[];
68
+ quoteIdentifier(id: string, quote?: string): string;
69
+ quoteValue(value: any): string;
70
+ indexForeignKeys(): boolean;
71
+ getDefaultMappedType(type: string): Type<unknown>;
72
+ supportsSchemas(): boolean;
73
+ getDefaultSchemaName(): string | undefined;
74
+ /**
75
+ * Returns the default name of index for the given columns
76
+ * cannot go past 64 character length for identifiers in MySQL
77
+ */
78
+ getIndexName(tableName: string, columns: string[], type: "index" | "unique" | "foreign" | "primary" | "sequence"): string;
79
+ getDefaultPrimaryName(tableName: string, columns: string[]): string;
80
+ /**
81
+ * @inheritDoc
82
+ */
83
+ castColumn(prop?: {
84
+ columnTypes?: string[];
85
+ }): string;
86
+ /**
87
+ * @inheritDoc
88
+ */
89
+ castJsonValue(prop?: {
90
+ columnTypes?: string[];
91
+ }): string;
92
+ /**
93
+ * @inheritDoc
94
+ */
95
+ parseDate(value: string | number): Date;
96
+ }