Skip to content

Commit

Permalink
fix: patch mikro-orm adapter to not issue set names 'utf8'; query
Browse files Browse the repository at this point in the history
It used to work by just intercepting the query but it now builds a bigger
query which includes this part and breaks the parser.
  • Loading branch information
knixeur committed Aug 16, 2023
1 parent 2138d0a commit 4eb6155
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/adapters/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ function replaceQueryArgs$(this: void, sql: string, values: any[]) {


export class Adapters implements LibAdapters {
private _mikroPatched?: boolean;

constructor(private db: IMemoryDb) {
}

Expand Down Expand Up @@ -328,7 +326,7 @@ export class Adapters implements LibAdapters {
async createMikroOrm(mikroOrmOptions: any, queryLatency?: number) {

const { MikroORM } = __non_webpack_require__('@mikro-orm/core');
const { AbstractSqlDriver, PostgreSqlConnection, PostgreSqlPlatform } = __non_webpack_require__('@mikro-orm/postgresql');
const { AbstractSqlDriver, AbstractSqlPlatform, PostgreSqlConnection, PostgreSqlPlatform, PostgreSqlSchemaHelper } = __non_webpack_require__('@mikro-orm/postgresql');
const that = this;

// see https://github.com/mikro-orm/mikro-orm/blob/aa71065d0727920db7da9bfdecdb33e6b8165cb5/packages/postgresql/src/PostgreSqlConnection.ts#L5
Expand All @@ -338,25 +336,31 @@ export class Adapters implements LibAdapters {
}

}

class PgMemPostgreSqlSchemaHelper extends PostgreSqlSchemaHelper {
constructor(platform: typeof AbstractSqlPlatform) {
super(platform);
}
getSchemaBeginning(charset: string): string {
// hack: this query is not supported by pgsql-ast-parser
return super.getSchemaBeginning(charset).replace(`set names 'utf8';`, '');
}
}

class PgMemPostgreSqlPlatform extends PostgreSqlPlatform {
constructor() {
super();
this.schemaHelper = new PgMemPostgreSqlSchemaHelper(this);
}
}

// see https://github.com/mikro-orm/mikro-orm/blob/master/packages/postgresql/src/PostgreSqlDriver.ts
class PgMemDriver extends AbstractSqlDriver<PgMemConnection> {
constructor(config: any) {
super(config, new PostgreSqlPlatform(), PgMemConnection, ['knex', 'pg']);
super(config, new PgMemPostgreSqlPlatform(), PgMemConnection, ['knex', 'pg']);
}
}

// hack: this query is not supported by pgsql-ast-parser
if (!this._mikroPatched) {
this.db.public.interceptQueries(q => {
if (q === `set names 'utf8';`) {
return [];
}
return null;
});
this._mikroPatched = true;
}


const orm = await MikroORM.init({
...mikroOrmOptions,
dbName: 'public',
Expand Down

0 comments on commit 4eb6155

Please sign in to comment.