fix typings

This commit is contained in:
Fendy Heryanto
2026-01-08 13:46:28 +00:00
parent 06039686cf
commit 7fee9bf690
5 changed files with 72 additions and 114 deletions

View File

@@ -0,0 +1,48 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export abstract class GenericDBQueryClient implements DBQueryClient {
temporaryTableRaw({
knex,
data,
fields,
alias,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
return knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
}
temporaryTable(param: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
return param.knex.from(this.temporaryTableRaw(param));
}
abstract concat(fields: string[]): string;
abstract simpleCast(field: string, asType: string): string;
}

View File

@@ -1,42 +1,10 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export class MySqlDBQueryClient implements DBQueryClient {
temporaryTable({
knex,
data,
fields,
alias,
asKnexFrom = true,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
const query = knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
return asKnexFrom ? knex.from(query) : query;
}
import { GenericDBQueryClient } from '~/dbQueryClient/generic';
export class MySqlDBQueryClient
extends GenericDBQueryClient
implements DBQueryClient
{
concat(fields: string[]) {
return `CONCAT(${fields.join(', ')})`;
}

View File

@@ -1,42 +1,10 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export class PGDBQueryClient implements DBQueryClient {
temporaryTable({
knex,
data,
fields,
alias,
asKnexFrom = true,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
const query = knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
return asKnexFrom ? knex.from(query) : query;
}
import { GenericDBQueryClient } from '~/dbQueryClient/generic';
export class PGDBQueryClient
extends GenericDBQueryClient
implements DBQueryClient
{
concat(fields: string[]) {
return `CONCAT(${fields.join(', ')})`;
}

View File

@@ -1,42 +1,10 @@
import { arrFlatMap } from 'nocodb-sdk';
import type { DBQueryClient } from '~/dbQueryClient/types';
import type { XKnex } from '~/db/CustomKnex';
export class SqliteDBQueryClient implements DBQueryClient {
temporaryTable({
knex,
data,
fields,
alias,
asKnexFrom = true,
}: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
}) {
const fieldsValuePlaceholder = `(${fields.map(() => '?').join(',')})`;
const valuesPlaceholder = data.map(() => fieldsValuePlaceholder).join(', ');
const fieldsPlaceholder = fields.map(() => '??').join(',');
const query = knex.raw(
`(VALUES ${valuesPlaceholder}) AS ?? (${fieldsPlaceholder})`,
[
...arrFlatMap(
data.map((row) =>
fields.reduce((acc, field) => {
acc.push(row[field]);
return acc;
}, []),
),
),
alias,
...fields,
],
);
return asKnexFrom ? knex.from(query) : query;
}
import { GenericDBQueryClient } from '~/dbQueryClient/generic';
export class SqliteDBQueryClient
extends GenericDBQueryClient
implements DBQueryClient
{
concat(fields: string[]) {
return `${fields.join(' || ')}`;
}

View File

@@ -1,4 +1,4 @@
import type { XKnex } from '~/db/CustomKnex';
import type { Knex, XKnex } from '~/db/CustomKnex';
export interface DBQueryClient {
temporaryTable(payload: {
@@ -6,8 +6,14 @@ export interface DBQueryClient {
fields: string[];
alias: string;
knex: XKnex;
asKnexFrom?: boolean;
});
}): Knex.QueryInterface;
temporaryTableRaw(payload: {
data: Record<string, any>[];
fields: string[];
alias: string;
knex: XKnex;
}): Knex.Raw;
concat(fields: string[]);
simpleCast(field: string, asType: string);