/
CockroachDriver.ts
751 lines (632 loc) · 25.9 KB
/
CockroachDriver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import {Driver} from "../Driver";
import {ConnectionIsNotSetError} from "../../error/ConnectionIsNotSetError";
import {ObjectLiteral} from "../../common/ObjectLiteral";
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
import {DriverUtils} from "../DriverUtils";
import {ColumnMetadata} from "../../metadata/ColumnMetadata";
import {CockroachConnectionCredentialsOptions} from "./CockroachConnectionCredentialsOptions";
import {CockroachConnectionOptions} from "./CockroachConnectionOptions";
import {DateUtils} from "../../util/DateUtils";
import {PlatformTools} from "../../platform/PlatformTools";
import {Connection} from "../../connection/Connection";
import {RdbmsSchemaBuilder} from "../../schema-builder/RdbmsSchemaBuilder";
import {MappedColumnTypes} from "../types/MappedColumnTypes";
import {ColumnType} from "../types/ColumnTypes";
import {QueryRunner} from "../../query-runner/QueryRunner";
import {DataTypeDefaults} from "../types/DataTypeDefaults";
import {TableColumn} from "../../schema-builder/table/TableColumn";
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {OrmUtils} from "../../util/OrmUtils";
import {CockroachQueryRunner} from "./CockroachQueryRunner";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
/**
* Organizes communication with Cockroach DBMS.
*/
export class CockroachDriver implements Driver {
// -------------------------------------------------------------------------
// Public Properties
// -------------------------------------------------------------------------
/**
* Connection used by driver.
*/
connection: Connection;
/**
* Cockroach underlying library.
*/
postgres: any;
/**
* Pool for master database.
*/
master: any;
/**
* Pool for slave databases.
* Used in replication.
*/
slaves: any[] = [];
/**
* We store all created query runners because we need to release them.
*/
connectedQueryRunners: QueryRunner[] = [];
// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------
/**
* Connection options.
*/
options: CockroachConnectionOptions;
/**
* Master database used to perform all write queries.
*/
database?: string;
/**
* Indicates if replication is enabled.
*/
isReplicated: boolean = false;
/**
* Indicates if tree tables are supported by this driver.
*/
treeSupport = true;
/**
* Gets list of supported column data types by a driver.
*
* @see https://www.cockroachlabs.com/docs/stable/data-types.html
*/
supportedDataTypes: ColumnType[] = [
"array",
"bool",
"boolean",
"bytes",
"bytea",
"blob",
"date",
"numeric",
"decimal",
"dec",
"float",
"float4",
"float8",
"double precision",
"real",
"inet",
"int",
"int4",
"integer",
"int2",
"int8",
"int64",
"smallint",
"bigint",
"interval",
"string",
"character varying",
"character",
"char",
"char varying",
"varchar",
"text",
"time",
"time without time zone",
"timestamp",
"timestamptz",
"timestamp without time zone",
"timestamp with time zone",
"json",
"jsonb",
"uuid",
];
/**
* Gets list of spatial column data types.
*/
spatialTypes: ColumnType[] = [];
/**
* Gets list of column data types that support length by a driver.
*/
withLengthColumnTypes: ColumnType[] = [
"character varying",
"char varying",
"varchar",
"character",
"char",
"string",
];
/**
* Gets list of column data types that support precision by a driver.
*/
withPrecisionColumnTypes: ColumnType[] = [
"numeric",
"decimal",
"dec",
];
/**
* Gets list of column data types that support scale by a driver.
*/
withScaleColumnTypes: ColumnType[] = [
"numeric",
"decimal",
"dec"
];
/**
* Orm has special columns and we need to know what database column types should be for those types.
* Column types are driver dependant.
*/
mappedDataTypes: MappedColumnTypes = {
createDate: "timestamptz",
createDateDefault: "now()",
updateDate: "timestamptz",
updateDateDefault: "now()",
deleteDate: "timestamptz",
deleteDateNullable: true,
version: Number,
treeLevel: Number,
migrationId: Number,
migrationName: "varchar",
migrationTimestamp: "int8",
cacheId: Number,
cacheIdentifier: "varchar",
cacheTime: "int8",
cacheDuration: Number,
cacheQuery: "string",
cacheResult: "string",
metadataType: "varchar",
metadataDatabase: "varchar",
metadataSchema: "varchar",
metadataTable: "varchar",
metadataName: "varchar",
metadataValue: "string",
};
/**
* Default values of length, precision and scale depends on column data type.
* Used in the cases when length/precision/scale is not specified by user.
*/
dataTypeDefaults: DataTypeDefaults = {
"char": { length: 1 },
};
/**
* No documentation specifying a maximum length for identifiers could be found
* for CockroarchDb.
*/
maxAliasLength?: number;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(connection: Connection) {
this.connection = connection;
this.options = connection.options as CockroachConnectionOptions;
this.isReplicated = this.options.replication ? true : false;
// load postgres package
this.loadDependencies();
// ObjectUtils.assign(this.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
// validate options to make sure everything is set
// todo: revisit validation with replication in mind
// if (!this.options.host)
// throw new DriverOptionNotSetError("host");
// if (!this.options.username)
// throw new DriverOptionNotSetError("username");
// if (!this.options.database)
// throw new DriverOptionNotSetError("database");
}
// -------------------------------------------------------------------------
// Public Implemented Methods
// -------------------------------------------------------------------------
/**
* Performs connection to the database.
* Based on pooling options, it can either create connection immediately,
* either create a pool and create connection when needed.
*/
async connect(): Promise<void> {
if (this.options.replication) {
this.slaves = await Promise.all(this.options.replication.slaves.map(slave => {
return this.createPool(this.options, slave);
}));
this.master = await this.createPool(this.options, this.options.replication.master);
this.database = this.options.replication.master.database;
} else {
this.master = await this.createPool(this.options, this.options);
this.database = this.options.database;
}
}
/**
* Makes any action after connection (e.g. create extensions in Postgres driver).
*/
async afterConnect(): Promise<void> {
return Promise.resolve();
}
/**
* Closes connection with database.
*/
async disconnect(): Promise<void> {
if (!this.master)
return Promise.reject(new ConnectionIsNotSetError("cockroachdb"));
await this.closePool(this.master);
await Promise.all(this.slaves.map(slave => this.closePool(slave)));
this.master = undefined;
this.slaves = [];
}
/**
* Creates a schema builder used to build and sync a schema.
*/
createSchemaBuilder() {
return new RdbmsSchemaBuilder(this.connection);
}
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master") {
return new CockroachQueryRunner(this, mode);
}
/**
* Prepares given value to a value to be persisted, based on its column type and metadata.
*/
preparePersistentValue(value: any, columnMetadata: ColumnMetadata): any {
if (columnMetadata.transformer)
value = ApplyValueTransformers.transformTo(columnMetadata.transformer, value);
if (value === null || value === undefined)
return value;
if (columnMetadata.type === Boolean) {
return value === true ? 1 : 0;
} else if (columnMetadata.type === "date") {
return DateUtils.mixedDateToDateString(value);
} else if (columnMetadata.type === "time") {
return DateUtils.mixedDateToTimeString(value);
} else if (columnMetadata.type === "datetime"
|| columnMetadata.type === Date
|| columnMetadata.type === "timestamp"
|| columnMetadata.type === "timestamptz"
|| columnMetadata.type === "timestamp with time zone"
|| columnMetadata.type === "timestamp without time zone") {
return DateUtils.mixedDateToDate(value);
} else if (["json", "jsonb", ...this.spatialTypes].indexOf(columnMetadata.type) >= 0) {
return JSON.stringify(value);
} else if (columnMetadata.type === "simple-array") {
return DateUtils.simpleArrayToString(value);
} else if (columnMetadata.type === "simple-json") {
return DateUtils.simpleJsonToString(value);
}
return value;
}
/**
* Prepares given value to a value to be persisted, based on its column type or metadata.
*/
prepareHydratedValue(value: any, columnMetadata: ColumnMetadata): any {
if (value === null || value === undefined)
return columnMetadata.transformer ? ApplyValueTransformers.transformFrom(columnMetadata.transformer, value) : value;
// unique_rowid() generates bigint value and should not be converted to number
if (([Number, "int4", "smallint", "int2"].some(v => v === columnMetadata.type)
&& !columnMetadata.isArray) || columnMetadata.generationStrategy === "increment") {
value = parseInt(value);
} else if (columnMetadata.type === Boolean) {
value = value ? true : false;
} else if (columnMetadata.type === "datetime"
|| columnMetadata.type === Date
|| columnMetadata.type === "timestamp"
|| columnMetadata.type === "timestamptz"
|| columnMetadata.type === "timestamp with time zone"
|| columnMetadata.type === "timestamp without time zone") {
value = DateUtils.normalizeHydratedDate(value);
} else if (columnMetadata.type === "date") {
value = DateUtils.mixedDateToDateString(value);
} else if (columnMetadata.type === "time") {
value = DateUtils.mixedTimeToString(value);
} else if (columnMetadata.type === "simple-array") {
value = DateUtils.stringToSimpleArray(value);
} else if (columnMetadata.type === "simple-json") {
value = DateUtils.stringToSimpleJson(value);
}
if (columnMetadata.transformer)
value = ApplyValueTransformers.transformFrom(columnMetadata.transformer, value);
return value;
}
/**
* Replaces parameters in the given sql with special escaping character
* and an array of parameter names to be passed to a query.
*/
escapeQueryWithParameters(sql: string, parameters: ObjectLiteral, nativeParameters: ObjectLiteral): [string, any[]] {
const builtParameters: any[] = Object.keys(nativeParameters).map(key => nativeParameters[key]);
if (!parameters || !Object.keys(parameters).length)
return [sql, builtParameters];
const keys = Object.keys(parameters).map(parameter => "(:(\\.\\.\\.)?" + parameter + "\\b)").join("|");
sql = sql.replace(new RegExp(keys, "g"), (key: string): string => {
let value: any;
let isArray = false;
if (key.substr(0, 4) === ":...") {
isArray = true;
value = parameters[key.substr(4)];
} else {
value = parameters[key.substr(1)];
}
if (isArray) {
return value.map((v: any) => {
builtParameters.push(v);
return "$" + builtParameters.length;
}).join(", ");
} else if (value instanceof Function) {
return value();
} else {
builtParameters.push(value);
return "$" + builtParameters.length;
}
}); // todo: make replace only in value statements, otherwise problems
return [sql, builtParameters];
}
/**
* Escapes a column name.
*/
escape(columnName: string): string {
return "\"" + columnName + "\"";
}
/**
* Build full table name with schema name and table name.
* E.g. "mySchema"."myTable"
*/
buildTableName(tableName: string, schema?: string): string {
return schema ? `${schema}.${tableName}` : tableName;
}
/**
* Creates a database type from a given column metadata.
*/
normalizeType(column: { type?: ColumnType, length?: number | string, precision?: number|null, scale?: number, isArray?: boolean, isGenerated?: boolean, generationStrategy?: "increment"|"uuid"|"rowid" }): string {
if (column.type === Number || column.type === "integer" || column.type === "int" || column.type === "bigint" || column.type === "int64") {
return "int8";
} else if (column.type === String || column.type === "character varying" || column.type === "char varying") {
return "varchar";
} else if (column.type === Date || column.type === "timestamp without time zone") {
return "timestamp";
} else if (column.type === "timestamp with time zone") {
return "timestamptz";
} else if (column.type === "time without time zone") {
return "time";
} else if (column.type === Boolean || column.type === "boolean") {
return "bool";
} else if (column.type === "simple-array" || column.type === "simple-json" || column.type === "text") {
return "string";
} else if (column.type === "bytea" || column.type === "blob") {
return "bytes";
} else if (column.type === "smallint") {
return "int2";
} else if (column.type === "numeric" || column.type === "dec") {
return "decimal";
} else if (column.type === "double precision" || column.type === "float") {
return "float8";
} else if (column.type === "real") {
return "float4";
} else if (column.type === "character") {
return "char";
} else if (column.type === "json") {
return "jsonb";
} else {
return column.type as string || "";
}
}
/**
* Normalizes "default" value of the column.
*/
normalizeDefault(columnMetadata: ColumnMetadata): string {
const defaultValue = columnMetadata.default;
const arrayCast = columnMetadata.isArray ? `::${columnMetadata.type}[]` : "";
if (typeof defaultValue === "number") {
return "" + defaultValue;
} else if (typeof defaultValue === "boolean") {
return defaultValue === true ? "true" : "false";
} else if (typeof defaultValue === "function") {
return defaultValue();
} else if (typeof defaultValue === "string") {
return `'${defaultValue}'${arrayCast}`;
} else if (defaultValue === null) {
return `null`;
} else if (typeof defaultValue === "object") {
return `'${JSON.stringify(defaultValue)}'`;
} else {
return defaultValue;
}
}
/**
* Normalizes "isUnique" value of the column.
*/
normalizeIsUnique(column: ColumnMetadata): boolean {
return column.entityMetadata.uniques.some(uq => uq.columns.length === 1 && uq.columns[0] === column);
}
/**
* Returns default column lengths, which is required on column creation.
*/
getColumnLength(column: ColumnMetadata): string {
return column.length ? column.length.toString() : "";
}
/**
* Creates column type definition including length, precision and scale
*/
createFullType(column: TableColumn): string {
let type = column.type;
if (column.length) {
type += "(" + column.length + ")";
} else if (column.precision !== null && column.precision !== undefined && column.scale !== null && column.scale !== undefined) {
type += "(" + column.precision + "," + column.scale + ")";
} else if (column.precision !== null && column.precision !== undefined) {
type += "(" + column.precision + ")";
}
if (column.isArray)
type += " array";
return type;
}
/**
* Obtains a new database connection to a master server.
* Used for replication.
* If replication is not setup then returns default connection's database connection.
*/
obtainMasterConnection(): Promise<any> {
return new Promise((ok, fail) => {
this.master.connect((err: any, connection: any, release: any) => {
err ? fail(err) : ok([connection, release]);
});
});
}
/**
* Obtains a new database connection to a slave server.
* Used for replication.
* If replication is not setup then returns master (default) connection's database connection.
*/
obtainSlaveConnection(): Promise<any> {
if (!this.slaves.length)
return this.obtainMasterConnection();
return new Promise((ok, fail) => {
const random = Math.floor(Math.random() * this.slaves.length);
this.slaves[random].connect((err: any, connection: any, release: any) => {
err ? fail(err) : ok([connection, release]);
});
});
}
/**
* Creates generated map of values generated or returned by database after INSERT query.
*
* todo: slow. optimize Object.keys(), OrmUtils.mergeDeep and column.createValueMap parts
*/
createGeneratedMap(metadata: EntityMetadata, insertResult: ObjectLiteral) {
if (!insertResult)
return undefined;
return Object.keys(insertResult).reduce((map, key) => {
const column = metadata.findColumnWithDatabaseName(key);
if (column) {
OrmUtils.mergeDeep(map, column.createValueMap(this.prepareHydratedValue(insertResult[key], column)));
}
return map;
}, {} as ObjectLiteral);
}
/**
* Differentiate columns of this table and columns from the given column metadatas columns
* and returns only changed.
*/
findChangedColumns(tableColumns: TableColumn[], columnMetadatas: ColumnMetadata[]): ColumnMetadata[] {
return columnMetadatas.filter(columnMetadata => {
const tableColumn = tableColumns.find(c => c.name === columnMetadata.databaseName);
if (!tableColumn)
return false; // we don't need new columns, we only need exist and changed
// console.log("table:", columnMetadata.entityMetadata.tableName);
// console.log("name:", tableColumn.name, columnMetadata.databaseName);
// console.log("type:", tableColumn.type, this.normalizeType(columnMetadata));
// console.log("length:", tableColumn.length, columnMetadata.length);
// console.log("width:", tableColumn.width, columnMetadata.width);
// console.log("precision:", tableColumn.precision, columnMetadata.precision);
// console.log("scale:", tableColumn.scale, columnMetadata.scale);
// console.log("comment:", tableColumn.comment, columnMetadata.comment);
// console.log("default:", tableColumn.default, columnMetadata.default);
// console.log("default changed:", !this.compareDefaultValues(this.normalizeDefault(columnMetadata), tableColumn.default));
// console.log("isPrimary:", tableColumn.isPrimary, columnMetadata.isPrimary);
// console.log("isNullable:", tableColumn.isNullable, columnMetadata.isNullable);
// console.log("isUnique:", tableColumn.isUnique, this.normalizeIsUnique(columnMetadata));
// console.log("isGenerated:", tableColumn.isGenerated, columnMetadata.isGenerated);
// console.log("==========================================");
return tableColumn.name !== columnMetadata.databaseName
|| tableColumn.type !== this.normalizeType(columnMetadata)
|| tableColumn.length !== columnMetadata.length
|| tableColumn.precision !== columnMetadata.precision
|| tableColumn.scale !== columnMetadata.scale
// || tableColumn.comment !== columnMetadata.comment // todo
|| (!tableColumn.isGenerated && this.lowerDefaultValueIfNecessary(this.normalizeDefault(columnMetadata)) !== tableColumn.default) // we included check for generated here, because generated columns already can have default values
|| tableColumn.isPrimary !== columnMetadata.isPrimary
|| tableColumn.isNullable !== columnMetadata.isNullable
|| tableColumn.isUnique !== this.normalizeIsUnique(columnMetadata)
|| tableColumn.isGenerated !== columnMetadata.isGenerated;
});
}
private lowerDefaultValueIfNecessary(value: string | undefined) {
if (!value) {
return value;
}
return value.split(`'`).map((v, i) => {
return i % 2 === 1 ? v : v.toLowerCase();
}).join(`'`);
}
/**
* Returns true if driver supports RETURNING / OUTPUT statement.
*/
isReturningSqlSupported(): boolean {
return true;
}
/**
* Returns true if driver supports uuid values generation on its own.
*/
isUUIDGenerationSupported(): boolean {
return true;
}
/**
* Returns true if driver supports fulltext indices.
*/
isFullTextColumnTypeSupported(): boolean {
return false;
}
/**
* Creates an escaped parameter.
*/
createParameter(parameterName: string, index: number): string {
return "$" + (index + 1);
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Loads postgres query stream package.
*/
loadStreamDependency() {
try {
return PlatformTools.load("pg-query-stream");
} catch (e) { // todo: better error for browser env
throw new Error(`To use streams you should install pg-query-stream package. Please run npm i pg-query-stream --save command.`);
}
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
try {
this.postgres = PlatformTools.load("pg");
try {
const pgNative = PlatformTools.load("pg-native");
if (pgNative && this.postgres.native) this.postgres = this.postgres.native;
} catch (e) { }
} catch (e) { // todo: better error for browser env
throw new DriverPackageNotInstalledError("Postgres", "pg");
}
}
/**
* Creates a new connection pool for a given database credentials.
*/
protected async createPool(options: CockroachConnectionOptions, credentials: CockroachConnectionCredentialsOptions): Promise<any> {
credentials = Object.assign({}, credentials, DriverUtils.buildDriverOptions(credentials)); // todo: do it better way
// build connection options for the driver
const connectionOptions = Object.assign({}, {
host: credentials.host,
user: credentials.username,
password: credentials.password,
database: credentials.database,
port: credentials.port,
ssl: credentials.ssl
}, options.extra || {});
// create a connection pool
const pool = new this.postgres.Pool(connectionOptions);
const { logger } = this.connection;
const poolErrorHandler = options.poolErrorHandler || ((error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
/*
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
cause the hosting app to crash.
*/
pool.on("error", poolErrorHandler);
return new Promise((ok, fail) => {
pool.connect((err: any, connection: any, release: Function) => {
if (err) return fail(err);
release();
ok(pool);
});
});
}
/**
* Closes connection pool.
*/
protected async closePool(pool: any): Promise<void> {
await Promise.all(this.connectedQueryRunners.map(queryRunner => queryRunner.release()));
return new Promise<void>((ok, fail) => {
pool.end((err: any) => err ? fail(err) : ok());
});
}
}