|
|
@@ -3,9 +3,9 @@ import { NestFactory } from '@nestjs/core';
|
|
|
import { DefaultLogger, Logger, VendureConfig } from '@vendure/core';
|
|
|
import { preBootstrapConfig } from '@vendure/core/dist/bootstrap';
|
|
|
import fs from 'fs';
|
|
|
-import { Connection } from 'mysql';
|
|
|
import path from 'path';
|
|
|
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions';
|
|
|
+import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
|
|
|
import { SqljsConnectionOptions } from 'typeorm/driver/sqljs/SqljsConnectionOptions';
|
|
|
|
|
|
import { populateForTesting } from './data-population/populate-for-testing';
|
|
|
@@ -34,15 +34,16 @@ export class TestServer {
|
|
|
*/
|
|
|
async init(options: TestServerOptions): Promise<void> {
|
|
|
const { type } = this.vendureConfig.dbConnectionOptions;
|
|
|
+ const { dbConnectionOptions } = this.vendureConfig;
|
|
|
switch (type) {
|
|
|
case 'sqljs':
|
|
|
await this.initSqljs(options);
|
|
|
break;
|
|
|
case 'mysql':
|
|
|
- await this.initMysql(
|
|
|
- this.vendureConfig.dbConnectionOptions as MysqlConnectionOptions,
|
|
|
- options,
|
|
|
- );
|
|
|
+ await this.initMysql(dbConnectionOptions as MysqlConnectionOptions, options);
|
|
|
+ break;
|
|
|
+ case 'postgres':
|
|
|
+ await this.initPostgres(dbConnectionOptions as PostgresConnectionOptions, options);
|
|
|
break;
|
|
|
default:
|
|
|
throw new Error(`The TestServer does not support the database type "${type}"`);
|
|
|
@@ -89,8 +90,8 @@ export class TestServer {
|
|
|
|
|
|
private async initMysql(connectionOptions: MysqlConnectionOptions, options: TestServerOptions) {
|
|
|
const filename = this.getCallerFilename(2);
|
|
|
+ const dbName = this.getDbNameFromFilename(filename);
|
|
|
const conn = await this.getMysqlConnection(connectionOptions);
|
|
|
- const dbName = 'e2e_' + path.basename(filename).replace(/[^a-z0-9_]/gi, '_');
|
|
|
(connectionOptions as any).database = dbName;
|
|
|
(connectionOptions as any).synchronize = true;
|
|
|
await new Promise((resolve, reject) => {
|
|
|
@@ -117,7 +118,21 @@ export class TestServer {
|
|
|
conn.destroy();
|
|
|
}
|
|
|
|
|
|
- private async getMysqlConnection(connectionOptions: MysqlConnectionOptions): Promise<Connection> {
|
|
|
+ private async initPostgres(connectionOptions: PostgresConnectionOptions, options: TestServerOptions) {
|
|
|
+ const filename = this.getCallerFilename(2);
|
|
|
+ const dbName = this.getDbNameFromFilename(filename);
|
|
|
+ (connectionOptions as any).database = dbName;
|
|
|
+ (connectionOptions as any).synchronize = true;
|
|
|
+ const client = await this.getPostgresConnection(connectionOptions);
|
|
|
+ await client.query(`DROP DATABASE IF EXISTS ${dbName}`);
|
|
|
+ await client.query(`CREATE DATABASE ${dbName}`);
|
|
|
+ await this.populateInitialData(this.vendureConfig, options);
|
|
|
+ await client.end();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async getMysqlConnection(
|
|
|
+ connectionOptions: MysqlConnectionOptions,
|
|
|
+ ): Promise<import('mysql').Connection> {
|
|
|
const { createConnection } = await import('mysql');
|
|
|
const conn = createConnection({
|
|
|
host: connectionOptions.host,
|
|
|
@@ -137,6 +152,21 @@ export class TestServer {
|
|
|
return conn;
|
|
|
}
|
|
|
|
|
|
+ private async getPostgresConnection(
|
|
|
+ connectionOptions: PostgresConnectionOptions,
|
|
|
+ ): Promise<import('pg').Client> {
|
|
|
+ const { Client } = require('pg');
|
|
|
+ const client = new Client({
|
|
|
+ host: connectionOptions.host,
|
|
|
+ port: connectionOptions.port,
|
|
|
+ user: connectionOptions.username,
|
|
|
+ password: connectionOptions.password,
|
|
|
+ database: 'postgres',
|
|
|
+ });
|
|
|
+ await client.connect();
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
private getDbFilePath(dataDir: string) {
|
|
|
// tslint:disable-next-line:no-non-null-assertion
|
|
|
const testFilePath = this.getCallerFilename(3);
|
|
|
@@ -145,6 +175,10 @@ export class TestServer {
|
|
|
return dbFilePath;
|
|
|
}
|
|
|
|
|
|
+ private getDbNameFromFilename(filename: string): string {
|
|
|
+ return 'e2e_' + path.basename(filename).replace(/[^a-z0-9_]/gi, '_');
|
|
|
+ }
|
|
|
+
|
|
|
private getCallerFilename(depth: number): string {
|
|
|
let pst: ErrorConstructor['prepareStackTrace'];
|
|
|
let stack: any;
|