Skip to content

Commit

Permalink
add column type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 22, 2024
1 parent 7e79982 commit 8f68d0a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/connections/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
transformObjectBasedResultFirstRow,
} from './../utils/transformer';
import { SqlConnection } from './sql-base';
import { ColumnDataType } from 'src/query-builder';

export class BigQueryConnection extends SqlConnection {
bigQuery: BigQuery;
Expand Down
9 changes: 9 additions & 0 deletions src/connections/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
transformArrayBasedResult,
} from './../utils/transformer';
import { QueryResult } from '.';
import { ColumnDataType } from 'src/query-builder';

interface MySQLSchemaResult {
SCHEMA_NAME: string;
Expand Down Expand Up @@ -210,6 +211,14 @@ export class MySQLConnection extends SqlConnection {
this.conn = conn;
}

mapDataType(dataType: string): string {
if (dataType === ColumnDataType.STRING) return 'TEXT';
if (dataType === ColumnDataType.NUMBER) return 'INT';
if (dataType === ColumnDataType.ARRAY) return 'JSON';
if (dataType === ColumnDataType.UUID) return 'TEXT';
return super.mapDataType(dataType);
}

async query<T = Record<string, unknown>>(
query: Query
): Promise<QueryResult<T>> {
Expand Down
9 changes: 8 additions & 1 deletion src/connections/postgre/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractDialect } from './../../query-builder';
import { AbstractDialect, ColumnDataType } from './../../query-builder';
import { PostgresDialect } from './../../query-builder/dialects/postgres';
import { Database } from './../../models/database';
import { buildMySQLDatabaseSchmea } from './../mysql';
Expand All @@ -7,6 +7,13 @@ import { SqlConnection } from '../sql-base';
export abstract class PostgreBaseConnection extends SqlConnection {
dialect: AbstractDialect = new PostgresDialect();

mapDataType(dataType: string): string {
if (dataType === ColumnDataType.ID) return 'SERIAL';
if (dataType === ColumnDataType.STRING) return 'TEXT';
if (dataType === ColumnDataType.NUMBER) return 'INTEGER';
return super.mapDataType(dataType);
}

async fetchDatabaseSchema(): Promise<Database> {
// Get the list of schema first
const { data: schemaList } = await this.query<{ schema_name: string }>({
Expand Down
17 changes: 14 additions & 3 deletions src/connections/sql-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Outerbase,
QueryResult,
} from '..';
import { AbstractDialect } from './../query-builder';
import { AbstractDialect, ColumnDataType } from './../query-builder';
import { TableColumn, TableColumnDefinition } from './../models/database';

export abstract class SqlConnection extends Connection {
Expand All @@ -15,6 +15,11 @@ export abstract class SqlConnection extends Connection {
query: Query
): Promise<QueryResult<T>>;

mapDataType(dataType: string): string {
if (dataType === ColumnDataType.ID) return 'INTEGER';
return dataType;
}

async raw(query: string): Promise<QueryResult> {
return await this.query({ query });
}
Expand Down Expand Up @@ -148,7 +153,10 @@ export abstract class SqlConnection extends Connection {
);

for (const column of columns) {
qb.column(column.name, column.definition);
qb.column(column.name, {
...column.definition,
type: this.mapDataType(column.definition.type),
});
}

return this.query(qb.toQuery());
Expand Down Expand Up @@ -217,7 +225,10 @@ export abstract class SqlConnection extends Connection {
.alterTable(
schemaName ? `${schemaName}.${tableName}` : tableName
)
.alterColumn(columnName, defintion)
.alterColumn(columnName, {
...defintion,
type: this.mapDataType(defintion.type),
})
.toQuery()
);
}
Expand Down
1 change: 1 addition & 0 deletions src/query-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Dialect {
}

export enum ColumnDataType {
ID = 'id',
STRING = 'string',
NUMBER = 'number',
BOOLEAN = 'boolean',
Expand Down
35 changes: 18 additions & 17 deletions tests/connections/connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ColumnDataType } from '../../src/query-builder';
import createTestClient from './create-test-connection';
const { client: db, defaultSchema: DEFAULT_SCHEMA } = createTestClient();

// Some drivers are just too slow such as Cloudflare and BigQuery
jest.setTimeout(10000);

beforeAll(async () => {
await db.connect();

Expand Down Expand Up @@ -39,15 +43,15 @@ describe('Database Connection', () => {
[
{
name: 'id',
definition: { type: 'INTEGER', primaryKey: true },
definition: {
type: ColumnDataType.NUMBER,
primaryKey: true,
},
},
{
name: 'name',
definition: {
type:
process.env.CONNECTION_TYPE === 'bigquery'
? 'STRING'
: 'VARCHAR(255)',
type: ColumnDataType.STRING,
},
},
]
Expand All @@ -59,22 +63,22 @@ describe('Database Connection', () => {
[
{
name: 'id',
definition: { type: 'INTEGER', primaryKey: true },
definition: {
type: ColumnDataType.NUMBER,
primaryKey: true,
},
},
{
name: 'name',
definition: {
type:
process.env.CONNECTION_TYPE === 'bigquery'
? 'STRING'
: 'VARCHAR(255)',
type: ColumnDataType.STRING,
},
},
{ name: 'age', definition: { type: 'INTEGER' } },
{ name: 'age', definition: { type: ColumnDataType.NUMBER } },
{
name: 'team_id',
definition: {
type: 'INTEGER',
type: ColumnDataType.NUMBER,
references: {
column: ['id'],
table: 'teams',
Expand Down Expand Up @@ -236,7 +240,7 @@ describe('Database Connection', () => {
includeCounting: true,
});
expect(count).toEqual(2);
});
}, 10000);

test('Select from non-existing table should return error', async () => {
// MongoDB does not show error when selecting from non-existing collection
Expand Down Expand Up @@ -306,10 +310,7 @@ describe('Database Connection', () => {
'persons',
'email',
{
type:
process.env.CONNECTION_TYPE === 'bigquery'
? 'STRING'
: 'VARCHAR(255)',
type: ColumnDataType.STRING,
}
);

Expand Down

0 comments on commit 8f68d0a

Please sign in to comment.