Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Meta service #8571

Merged
merged 6 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions packages/nocodb/src/db/CustomKnex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,23 +492,27 @@ const appendWhereCondition = function (

return knexRef;
};
type XcConditionObjVal = {
[key in 'eq' | 'neq' | 'lt' | 'gt' | 'ge' | 'le' | 'like' | 'nlike']:
| string
| number
| any;
};

interface XcXonditionObj {
_or: XcXonditionObj[];
_and: XcXonditionObj[];
_not: XcXonditionObj;

[key: string]:
| XcXonditionObj
| XcXonditionObj[]
| XcConditionObjVal
| XcConditionObjVal[];
type AtLeastOne<T, U = { [K in keyof T]: Pick<T, K> }> = Partial<T> &
U[keyof U];

export type ConditionVal = AtLeastOne<{
eq: string | number | boolean | Date;
neq: string | number | boolean | Date;
lt: number | string | Date;
gt: number | string | Date;
ge: number | string | Date;
le: number | string | Date;
like: string;
nlike: string;
}>;

export interface Condition {
_or?: Condition[];
_and?: Condition[];
_not?: Condition;

[key: string]: ConditionVal | Condition | Condition[];
pranavxc marked this conversation as resolved.
Show resolved Hide resolved
}

declare module 'knex' {
Expand All @@ -527,7 +531,7 @@ declare module 'knex' {
): Knex.QueryBuilder<TRecord, TResult>;

condition<TRecord, TResult>(
conditionObj: XcXonditionObj,
conditionObj: Condition,
columnAliases?: {
[columnAlias: string]: string;
},
Expand All @@ -542,7 +546,7 @@ declare module 'knex' {
): Knex.QueryBuilder<TRecord, TResult>;

conditionGraph<TRecord, TResult>(condition: {
condition: XcXonditionObj;
condition: Condition;
models: { [key: string]: BaseModelSql };
}): Knex.QueryBuilder<TRecord, TResult>;

Expand All @@ -552,6 +556,8 @@ declare module 'knex' {
[columnAlias: string]: string;
},
): Knex.QueryBuilder<TRecord, TResult>;

hasWhere(): boolean;
}
}
}
Expand Down Expand Up @@ -1274,8 +1280,15 @@ function parseNestedConditionv2(obj, qb, pKey?, table?, tableAlias?) {
return qb;
}

// Conditionv2
// extend the knex query builder with a method to check if a where clause exists
knex.QueryBuilder.extend('hasWhere', function () {
// Inspect the _statements array for 'where' clauses
return (
this as unknown as { _statements: { grouping: string }[] }
)._statements.some((statement) => statement.grouping === 'where') as any;
});

// Conditionv2
/**
* Append custom where condition(nested object) to knex query builder
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ export class GlobalExceptionFilter implements ExceptionFilter {
}

// try to extract db error for unknown errors
const dbError = !(exception instanceof NcBaseError)
? extractDBError(exception)
: null;
const dbError =
exception instanceof NcBaseError ? null : extractDBError(exception);

// skip unnecessary error logging
if (
Expand Down
11 changes: 11 additions & 0 deletions packages/nocodb/src/helpers/catchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ export class NotFound extends NcBaseError {}

export class SsoError extends NcBaseError {}

export class MetaError extends NcBaseError {
constructor(param: { message: string; sql: string }) {
super(param.message);
Object.assign(this, param);
}
}

export class ExternalError extends NcBaseError {
constructor(error: Error) {
super(error.message);
Expand Down Expand Up @@ -747,4 +754,8 @@ export class NcError {
`Email domain ${domain} is not allowed for this organization`,
);
}

static metaError(param: { message: string; sql: string }) {
throw new MetaError(param);
}
}
Loading
Loading