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

refactor: Meta service #8571

merged 6 commits into from
May 27, 2024

Conversation

mertmit
Copy link
Member

@mertmit mertmit commented May 24, 2024

Change Summary

  • Add proper type definition for xcCondition argument in metaservice
  • Check query contains valid where condition in update/delete query before execution unless force flag is passed
    • By converting query into string and checking presence of where in SQL query
    • And by checking presence of where statement in knex querybuilder

Change type

  • feat: (new feature for the user, not a new feature for build script)
  • fix: (bug fix for the user, not a fix to a build script)
  • docs: (changes to the documentation)
  • style: (formatting, missing semi colons, etc; no production code change)
  • refactor: (refactoring production code, eg. renaming a variable)
  • test: (adding missing tests, refactoring tests; no production code change)
  • chore: (updating grunt tasks etc; no production code change)

Test/ Verification

Provide summary of changes.

Additional information / screenshots (optional)

Anything for maintainers to be made aware of

Copy link
Contributor

coderabbitai bot commented May 24, 2024

Walkthrough

Walkthrough

The recent updates to the NocoDB project include a comprehensive refactor of the CustomKnex.ts file, which introduces new types and interfaces for condition objects and adds a method to check for 'where' clauses in queries. Additionally, the error handling in global-exception.filter.ts is improved for readability, and new error classes and methods are added in catchError.ts. The meta.service.ts file sees significant updates with new methods and additional parameters for existing methods, enhancing the functionality and robustness of metadata operations.


Changes

File Path Change Summary
.../CustomKnex.ts Refactored condition object definitions and interfaces, introduced ConditionVal type and Condition interface, updated method signatures, and added hasWhere() method.
.../global-exception.filter.ts Improved readability of conditional assignment for dbError in GlobalExceptionFilter class.
.../catchError.ts Introduced MetaError class and added metaError method to NcError class.
.../meta.service.ts Added Condition import, updated multiple method signatures with additional parameters, introduced new methods for metadata operations, and enhanced existing methods for better functionality.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant MetaService
    participant CustomKnex
    participant Database
    
    Client->>MetaService: Call metaGet with parameters
    MetaService->>CustomKnex: Build query with new Condition interface
    CustomKnex->>Database: Execute query
    Database-->>CustomKnex: Return results
    CustomKnex-->>MetaService: Return processed results
    MetaService-->>Client: Send response

    Client->>MetaService: Call metaInsert2 with parameters
    MetaService->>CustomKnex: Build insert query
    MetaService->>NcError: Check for errors
    NcError-->>MetaService: Return error status
    MetaService-->>Client: Send response

Warning

Review ran into problems

Problems (1)
  • Git: Failed to clone repository. Please contact CodeRabbit support.

Tip

Early Access Features
  • gpt-4o model for chat

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to full the review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Outside diff range and nitpick comments (14)
packages/nocodb/src/meta/meta.service.ts (2)

60-67: Ensure proper documentation for the xcCondition parameter.

Consider adding a comment to explain the xcCondition parameter, as it seems to be part of the function's functionality but is commented out in the current implementation.


108-115: Ensure that the ignoreIdGeneration parameter is documented properly.

The parameter ignoreIdGeneration could use a more detailed comment to explain its impact on the function's behavior, especially regarding ID handling.

packages/nocodb/src/db/CustomKnex.ts (12)

Line range hint 72-72: Use strict inequality operator.

- if (str && str != '~not')
+ if (str && str !== '~not')

Line range hint 81-83: Avoid assignments within expressions for clarity.

- if (closingIndex === -1)
-   throw new Error(
-     `${str.substring(0, openIndex + 1).slice(-10)} : Closing bracket not found`,
-   );
+ if (closingIndex === -1) {
+   const errorDetail = str.substring(0, openIndex + 1).slice(-10);
+   throw new Error(`${errorDetail} : Closing bracket not found`);
+ }

Line range hint 83-83: Use strict inequality operator.

- if (closingIndex === -1)
+ if (closingIndex !== -1)

Line range hint 98-98: Use strict inequality operator.

- if (operandStartIndex != -1)
+ if (operandStartIndex !== -1)

Line range hint 134-491: Consider using for...of for better readability and performance in modern JavaScript.

- conditions.forEach((condition) => {
+ for (const condition of conditions) {

Line range hint 173-173: Specify a more explicit type instead of any.

- const matches: any[] = condition.match(
+ const matches: Array<string | null> = condition.match(

Line range hint 227-227: Use strict inequality operator.

- if (matches[1] != '')
+ if (matches[1] !== '')

Line range hint 262-262: Use strict inequality operator.

- if (matches[4] != 'null')
+ if (matches[4] !== 'null')

Line range hint 470-470: Use strict equality operator.

- if (matches[3] == 'like' && clientType === 'pg') {
+ if (matches[3] === 'like' && clientType === 'pg') {

Line range hint 489-489: Prefer template literals over string concatenation for better readability and potential performance benefits.

- alias: `${
-   this._tn[relation.tn] ? this._tn[relation.tn] + '___' : ''
- }${relation.tn}`,
+ alias: `${this._tn[relation.tn] ? `${this._tn[relation.tn]}___` : ''}${relation.tn}`,

Line range hint 118-494: Convert function expressions to arrow functions for consistency and to leverage lexical scoping.

- const appendWhereCondition = function (
+ const appendWhereCondition = (

Line range hint 545-545: Specify a more explicit type instead of any.

- const conditions = toArrayOfConditions(conditionString);
+ const conditions: Condition[] = toArrayOfConditions(conditionString);

Also applies to: 573-573, 583-583

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between d89d9c0 and 5f822c3.
Files selected for processing (4)
  • packages/nocodb/src/db/CustomKnex.ts (5 hunks)
  • packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1 hunks)
  • packages/nocodb/src/helpers/catchError.ts (2 hunks)
  • packages/nocodb/src/meta/meta.service.ts (28 hunks)
Additional Context Used
Biome (76)
packages/nocodb/src/db/CustomKnex.ts (20)

72-72: Use !== instead of !=.
!= is only allowed when comparing against null


81-83: The assignment should not be in an expression.


83-83: Use !== instead of !=.
!= is only allowed when comparing against null


98-98: Use !== instead of !=.
!= is only allowed when comparing against null


134-491: Prefer for...of instead of forEach.


173-173: Unexpected any. Specify a different type.


227-227: Use !== instead of !=.
!= is only allowed when comparing against null


262-262: Use !== instead of !=.
!= is only allowed when comparing against null


470-470: Use === instead of ==.
== is only allowed when comparing against null


489-489: Template literals are preferred over string concatenation.


223-223: This code is unreachable


258-258: This code is unreachable


295-295: This code is unreachable


352-352: This code is unreachable


414-414: This code is unreachable


484-484: This code is unreachable


118-494: This function expression can be turned into an arrow function.


545-545: Unexpected any. Specify a different type.


573-573: Unexpected any. Specify a different type.


583-583: Unexpected any. Specify a different type.

packages/nocodb/src/filters/global-exception/global-exception.filter.ts (16)

24-24: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


76-76: Unexpected any. Specify a different type.


77-77: Unexpected any. Specify a different type.


113-140: This else clause can be omitted because previous branches break early.


118-140: This else clause can be omitted because previous branches break early.


123-140: This else clause can be omitted because previous branches break early.


128-140: This else clause can be omitted because previous branches break early.


132-140: This else clause can be omitted because previous branches break early.


134-140: This else clause can be omitted because previous branches break early.


156-156: Unexpected any. Specify a different type.


156-156: Unexpected any. Specify a different type.


160-160: Unexpected any. Specify a different type.


160-160: Unexpected any. Specify a different type.


1-2: Some named imports are only used as types.


40-40: Reassigning a function parameter is confusing.

packages/nocodb/src/helpers/catchError.ts (20)

22-22: Unexpected any. Specify a different type.


23-23: void is not valid as a constituent in a union type


27-27: Unexpected any. Specify a different type.


72-72: Change to an optional chain.


78-78: Change to an optional chain.


84-84: Change to an optional chain.


89-89: Change to an optional chain.


95-95: Change to an optional chain.


98-99: Change to an optional chain.


108-108: Change to an optional chain.


129-129: Change to an optional chain.


145-145: Change to an optional chain.


162-162: Change to an optional chain.


187-187: Change to an optional chain.


206-206: Change to an optional chain.


265-265: Change to an optional chain.


280-280: Change to an optional chain.


295-295: Change to an optional chain.


310-310: Change to an optional chain.


336-336: Change to an optional chain.

packages/nocodb/src/meta/meta.service.ts (20)

31-31: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


56-56: Unexpected any. Specify a different type.


72-72: Unexpected any. Specify a different type.


75-75: Unexpected any. Specify a different type.


120-120: Unexpected any. Specify a different type.


122-122: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


154-154: Unexpected any. Specify a different type.


162-162: Unexpected any. Specify a different type.


191-191: This variable implicitly has the any type.


313-313: Unexpected any. Specify a different type.


320-320: Unexpected any. Specify a different type.


346-346: Unexpected any. Specify a different type.


347-347: Unexpected any. Specify a different type.


357-357: Unexpected any. Specify a different type.


416-416: Unexpected any. Specify a different type.


460-460: Unexpected any. Specify a different type.


463-463: Unexpected any. Specify a different type.

Additional comments not posted (6)
packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1)

44-46: Refactored conditional assignment of dbError enhances readability without altering functionality.

packages/nocodb/src/helpers/catchError.ts (2)

418-423: Introduced MetaError class correctly extends NcBaseError and includes SQL context, enhancing error specificity for database operations.


758-760: Added metaError static method in NcError class to throw MetaError, improving error handling by including SQL details.

packages/nocodb/src/meta/meta.service.ts (3)

15-15: Consider using a more specific type than any for better type safety.

- import { NcError } from '~/helpers/catchError';
+ import { NcError, SpecificErrorType } from '~/helpers/catchError';
Skipped due to learnings
User: pranavxc
PR: nocodb/nocodb#8369
File: packages/nocodb/src/helpers/catchError.ts:733-737
Timestamp: 2024-04-30T19:46:27.844Z
Learning: The `SsoError` in `catchError.ts` is handled specifically within a global exception handler, differentiating it from other errors managed by `NcBaseErrorv2`.

9-9: Consider using a more specific type than any for better type safety.

- import type { Condition } from '~/db/CustomKnex';
+ import type { Condition, SpecificType } from '~/db/CustomKnex';

Likely invalid or redundant comment.


295-307: Consider adding error handling for database operations.

Ensure that error handling is implemented for the database operations in metaPaginatedList, especially since it involves multiple query executions which might fail.

packages/nocodb/src/meta/meta.service.ts Outdated Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented May 24, 2024

Uffizzi Preview deployment-52223 was deleted.

@o1lab o1lab force-pushed the nc-refactor/meta-service branch from 5f822c3 to 8f51e0a Compare May 24, 2024 13:04
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Outside diff range and nitpick comments (8)
packages/nocodb/src/db/CustomKnex.ts (8)

Line range hint 72-72: Use strict inequality operator.

- if (str && str != '~not')
+ if (str && str !== '~not')

Line range hint 81-83: Avoid assignments within expressions.

- if (closingIndex === -1)
-   throw new Error(
-     `${str.substring(0, openIndex + 1).slice(-10)} : Closing bracket not found`,
-   );
+ if (closingIndex === -1) {
+   const errorStr = str.substring(0, openIndex + 1).slice(-10);
+   throw new Error(`${errorStr} : Closing bracket not found`);
+ }

Line range hint 83-83: Use strict inequality operator.

- if (nextOpenIndex != -1)
+ if (nextOpenIndex !== -1)

- if (operandStartIndex != -1)
+ if (operandStartIndex !== -1)

- if (matches[1] != '')
+ if (matches[1] !== '')

- if (matches[1] != '')
+ if (matches[1] !== '')

Also applies to: 98-98, 227-227, 262-262


Line range hint 134-491: Prefer for...of over forEach for better readability and performance.

- conditions.forEach((condition) => {
+ for (const condition of conditions) {

Line range hint 173-173: Specify a more explicit type instead of any.

- const matches: any[] = condition.match(
+ const matches: Array<string | number> = condition.match(

Also applies to: 545-545, 573-573, 583-583


Line range hint 470-470: Use strict equality operator.

- if (matches[3] == 'like' && clientType === 'pg') {
+ if (matches[3] === 'like' && clientType === 'pg') {

Line range hint 489-489: Prefer template literals over string concatenation.

- `${this._tn[relation.tn] ? this._tn[relation.tn] + '___' : ''}${relation.tn}`
+ `${this._tn[relation.tn] ? `${this._tn[relation.tn]}___` : ''}${relation.tn}`

Line range hint 118-494: Convert function expressions to arrow functions for consistency and to maintain lexical this.

- const appendWhereCondition = function (
+ const appendWhereCondition = (
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 5f822c3 and 8f51e0a.
Files selected for processing (4)
  • packages/nocodb/src/db/CustomKnex.ts (5 hunks)
  • packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1 hunks)
  • packages/nocodb/src/helpers/catchError.ts (2 hunks)
  • packages/nocodb/src/meta/meta.service.ts (28 hunks)
Additional Context Used
Biome (76)
packages/nocodb/src/db/CustomKnex.ts (20)

72-72: Use !== instead of !=.
!= is only allowed when comparing against null


81-83: The assignment should not be in an expression.


83-83: Use !== instead of !=.
!= is only allowed when comparing against null


98-98: Use !== instead of !=.
!= is only allowed when comparing against null


134-491: Prefer for...of instead of forEach.


173-173: Unexpected any. Specify a different type.


227-227: Use !== instead of !=.
!= is only allowed when comparing against null


262-262: Use !== instead of !=.
!= is only allowed when comparing against null


470-470: Use === instead of ==.
== is only allowed when comparing against null


489-489: Template literals are preferred over string concatenation.


223-223: This code is unreachable


258-258: This code is unreachable


295-295: This code is unreachable


352-352: This code is unreachable


414-414: This code is unreachable


484-484: This code is unreachable


118-494: This function expression can be turned into an arrow function.


545-545: Unexpected any. Specify a different type.


573-573: Unexpected any. Specify a different type.


583-583: Unexpected any. Specify a different type.

packages/nocodb/src/filters/global-exception/global-exception.filter.ts (16)

24-24: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


76-76: Unexpected any. Specify a different type.


77-77: Unexpected any. Specify a different type.


113-140: This else clause can be omitted because previous branches break early.


118-140: This else clause can be omitted because previous branches break early.


123-140: This else clause can be omitted because previous branches break early.


128-140: This else clause can be omitted because previous branches break early.


132-140: This else clause can be omitted because previous branches break early.


134-140: This else clause can be omitted because previous branches break early.


156-156: Unexpected any. Specify a different type.


156-156: Unexpected any. Specify a different type.


160-160: Unexpected any. Specify a different type.


160-160: Unexpected any. Specify a different type.


1-2: Some named imports are only used as types.


40-40: Reassigning a function parameter is confusing.

packages/nocodb/src/helpers/catchError.ts (20)

22-22: Unexpected any. Specify a different type.


23-23: void is not valid as a constituent in a union type


27-27: Unexpected any. Specify a different type.


72-72: Change to an optional chain.


78-78: Change to an optional chain.


84-84: Change to an optional chain.


89-89: Change to an optional chain.


95-95: Change to an optional chain.


98-99: Change to an optional chain.


108-108: Change to an optional chain.


129-129: Change to an optional chain.


145-145: Change to an optional chain.


162-162: Change to an optional chain.


187-187: Change to an optional chain.


206-206: Change to an optional chain.


265-265: Change to an optional chain.


280-280: Change to an optional chain.


295-295: Change to an optional chain.


310-310: Change to an optional chain.


336-336: Change to an optional chain.

packages/nocodb/src/meta/meta.service.ts (20)

31-31: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


56-56: Unexpected any. Specify a different type.


72-72: Unexpected any. Specify a different type.


75-75: Unexpected any. Specify a different type.


120-120: Unexpected any. Specify a different type.


122-122: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


154-154: Unexpected any. Specify a different type.


162-162: Unexpected any. Specify a different type.


191-191: This variable implicitly has the any type.


313-313: Unexpected any. Specify a different type.


320-320: Unexpected any. Specify a different type.


346-346: Unexpected any. Specify a different type.


347-347: Unexpected any. Specify a different type.


357-357: Unexpected any. Specify a different type.


416-416: Unexpected any. Specify a different type.


460-460: Unexpected any. Specify a different type.


463-463: Unexpected any. Specify a different type.

Learnings (2)
packages/nocodb/src/helpers/catchError.ts (1)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
packages/nocodb/src/meta/meta.service.ts (2)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
User: pranavxc
PR: nocodb/nocodb#8399
File: packages/nocodb/src/db/BaseModelSqlv2.ts:3317-3327
Timestamp: 2024-05-06T06:35:04.758Z
Learning: Database and other exceptions are captured globally and handled in the system.
Additional comments not posted (9)
packages/nocodb/src/helpers/catchError.ts (2)

418-423: Introduce the MetaError class for specialized error handling.

The MetaError class extends NcBaseError and includes additional properties from the param object. This design allows for more detailed error information, particularly useful for database-related errors.


758-760: Implement the metaError static method to throw MetaError.

This method provides a convenient way to throw MetaError instances, encapsulating both a message and SQL context. This is particularly useful for handling database errors in a more descriptive manner.

packages/nocodb/src/meta/meta.service.ts (5)

60-67: Ensure that the method metaGet correctly handles both id and condition types for idOrCondition.

The method handles both string and object types for idOrCondition, allowing flexibility in querying by ID or condition.


295-307: Ensure that metaPaginatedList handles the xcCondition properly and integrates it into the query.

The method correctly integrates xcCondition into both the list and count queries, ensuring consistent application of conditions.


403-411: Validate the presence of conditions in metaDelete to prevent accidental data loss.

The method correctly checks for the presence of conditions unless the force flag is used, which aligns with best practices for data safety.


9-9: Consider using a more specific type than any for better type safety.

- import type { Condition } from '~/db/CustomKnex';
+ import type { Condition, SpecificType } from '~/db/CustomKnex';

Likely invalid or redundant comment.


15-15: Ensure that NcError is properly imported to handle errors effectively.

Verification successful

The NcError class is correctly defined in packages/nocodb/src/helpers/catchError.ts, confirming that the import statement in meta.service.ts is valid.

  • packages/nocodb/src/helpers/catchError.ts
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that NcError is correctly imported and available in the catchError.ts file.
rg --type typescript "export class NcError" --files-with-matches packages/nocodb/src/helpers/catchError.ts

Length of output: 143



Script:

#!/bin/bash
# Verify that NcError is correctly imported and available in the catchError.ts file.
rg --type ts "export class NcError" --files-with-matches packages/nocodb/src/helpers/catchError.ts

Length of output: 142

packages/nocodb/src/db/CustomKnex.ts (2)

499-515: Introduced new type definitions for ConditionVal and Condition interface.

These changes align with the PR's objective to enhance type safety and readability.


560-560: Added hasWhere() method to check for the presence of a 'where' clause.

This addition is consistent with the PR's description and improves the functionality of the query builder.

@o1lab o1lab force-pushed the nc-refactor/meta-service branch from 8f51e0a to a925975 Compare May 24, 2024 13:22
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Outside diff range and nitpick comments (10)
packages/nocodb/src/meta/meta.service.ts (2)

9-9: Consider using a more specific import for Condition to avoid potential conflicts and improve code clarity.


60-67: Ensure proper documentation for parameters, especially for idOrCondition which can be either a string or an object.

packages/nocodb/src/db/CustomKnex.ts (8)

Line range hint 72-72: Use strict inequality operator for null checks.

- if (str && str != '~not')
+ if (str && str !== '~not')

Line range hint 81-83: Avoid assignments within expressions for clarity and maintainability.

- if (closingIndex === -1)
-   throw new Error(
-     `${str.substring(0, openIndex + 1).slice(-10)} : Closing bracket not found`,
-   );
+ if (closingIndex === -1) {
+   const errorDetail = str.substring(0, openIndex + 1).slice(-10);
+   throw new Error(`${errorDetail} : Closing bracket not found`);
+ }

Line range hint 83-83: Use strict inequality checks to avoid unexpected type coercion.

- if (nextOpenIndex != -1)
+ if (nextOpenIndex !== -1)
- if (closingIndex != -1)
+ if (closingIndex !== -1)
- if (operandStartIndex != -1)
+ if (operandStartIndex !== -1)
- if (matches[1] != '')
+ if (matches[1] !== '')

Also applies to: 98-98, 227-227, 262-262


Line range hint 134-491: Consider using for...of for iterating over arrays for better readability and performance.

- conditions.forEach((condition) => {
+ for (const condition of conditions) {

Line range hint 173-173: Specify a more explicit type than any to enhance type safety and code clarity.

- const matches: any[] = condition.match(
+ const matches: Array<string | null> = condition.match(

Also applies to: 545-545, 573-573, 583-583


Line range hint 470-470: Use strict equality for consistency and to avoid type coercion errors.

- if (matches[3] == 'like' && clientType === 'pg') {
+ if (matches[3] === 'like' && clientType === 'pg') {

Line range hint 489-489: Prefer template literals over string concatenation for better readability and performance.

- `${this._tn[relation.tn] ? this._tn[relation.tn] + '___' : ''}${relation.tn}`
+ `${this._tn[relation.tn] ? `${this._tn[relation.tn]}___` : ''}${relation.tn}`

Line range hint 118-494: Convert function expressions to arrow functions for consistency and to leverage lexical scoping.

- const appendWhereCondition = function (
+ const appendWhereCondition = (
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 8f51e0a and a925975.
Files selected for processing (4)
  • packages/nocodb/src/db/CustomKnex.ts (5 hunks)
  • packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1 hunks)
  • packages/nocodb/src/helpers/catchError.ts (2 hunks)
  • packages/nocodb/src/meta/meta.service.ts (27 hunks)
Additional Context Used
Biome (76)
packages/nocodb/src/db/CustomKnex.ts (20)

72-72: Use !== instead of !=.
!= is only allowed when comparing against null


81-83: The assignment should not be in an expression.


83-83: Use !== instead of !=.
!= is only allowed when comparing against null


98-98: Use !== instead of !=.
!= is only allowed when comparing against null


134-491: Prefer for...of instead of forEach.


173-173: Unexpected any. Specify a different type.


227-227: Use !== instead of !=.
!= is only allowed when comparing against null


262-262: Use !== instead of !=.
!= is only allowed when comparing against null


470-470: Use === instead of ==.
== is only allowed when comparing against null


489-489: Template literals are preferred over string concatenation.


223-223: This code is unreachable


258-258: This code is unreachable


295-295: This code is unreachable


352-352: This code is unreachable


414-414: This code is unreachable


484-484: This code is unreachable


118-494: This function expression can be turned into an arrow function.


545-545: Unexpected any. Specify a different type.


573-573: Unexpected any. Specify a different type.


583-583: Unexpected any. Specify a different type.

packages/nocodb/src/filters/global-exception/global-exception.filter.ts (16)

24-24: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


76-76: Unexpected any. Specify a different type.


77-77: Unexpected any. Specify a different type.


113-140: This else clause can be omitted because previous branches break early.


118-140: This else clause can be omitted because previous branches break early.


123-140: This else clause can be omitted because previous branches break early.


128-140: This else clause can be omitted because previous branches break early.


132-140: This else clause can be omitted because previous branches break early.


134-140: This else clause can be omitted because previous branches break early.


156-156: Unexpected any. Specify a different type.


156-156: Unexpected any. Specify a different type.


160-160: Unexpected any. Specify a different type.


160-160: Unexpected any. Specify a different type.


1-2: Some named imports are only used as types.


40-40: Reassigning a function parameter is confusing.

packages/nocodb/src/helpers/catchError.ts (20)

22-22: Unexpected any. Specify a different type.


23-23: void is not valid as a constituent in a union type


27-27: Unexpected any. Specify a different type.


72-72: Change to an optional chain.


78-78: Change to an optional chain.


84-84: Change to an optional chain.


89-89: Change to an optional chain.


95-95: Change to an optional chain.


98-99: Change to an optional chain.


108-108: Change to an optional chain.


129-129: Change to an optional chain.


145-145: Change to an optional chain.


162-162: Change to an optional chain.


187-187: Change to an optional chain.


206-206: Change to an optional chain.


265-265: Change to an optional chain.


280-280: Change to an optional chain.


295-295: Change to an optional chain.


310-310: Change to an optional chain.


336-336: Change to an optional chain.

packages/nocodb/src/meta/meta.service.ts (20)

31-31: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


56-56: Unexpected any. Specify a different type.


72-72: Unexpected any. Specify a different type.


75-75: Unexpected any. Specify a different type.


120-120: Unexpected any. Specify a different type.


122-122: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


154-154: Unexpected any. Specify a different type.


162-162: Unexpected any. Specify a different type.


248-248: Unexpected any. Specify a different type.


255-255: Unexpected any. Specify a different type.


281-281: Unexpected any. Specify a different type.


282-282: Unexpected any. Specify a different type.


292-292: Unexpected any. Specify a different type.


351-351: Unexpected any. Specify a different type.


395-395: Unexpected any. Specify a different type.


398-398: Unexpected any. Specify a different type.


436-436: Unexpected any. Specify a different type.

Learnings (2)
packages/nocodb/src/helpers/catchError.ts (1)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
packages/nocodb/src/meta/meta.service.ts (2)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
User: pranavxc
PR: nocodb/nocodb#8399
File: packages/nocodb/src/db/BaseModelSqlv2.ts:3317-3327
Timestamp: 2024-05-06T06:35:04.758Z
Learning: Database and other exceptions are captured globally and handled in the system.
Additional comments not posted (5)
packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1)

44-46: Refactor approved. The change enhances readability by making the conditional logic clearer.

packages/nocodb/src/helpers/catchError.ts (1)

Line range hint 418-760: The addition of MetaError class and metaError method enhances error handling capabilities and aligns with the PR objectives.

packages/nocodb/src/meta/meta.service.ts (2)

185-189: Refactor the genNanoid method to use a dictionary for prefixes to improve maintainability.

- switch (target) {
-   case MetaTable.PROJECT:
-     prefix = 'p';
-     break;
-   // other cases
-   default:
-     prefix = 'nc';
-     break;
- }
+ const prefixes = {
+   [MetaTable.PROJECT]: 'p',
+   // other mappings
+   default: 'nc',
+ };
+ prefix = prefixes[target] || prefixes.default;

Likely invalid or redundant comment.


1130-1153: Refactor checkConditionPresent to use modern JavaScript features for string checking.

- const sql = queryBuilder.toString();
- if (queryBuilder.hasWhere() && /\bWHERE\b/i.test(sql)) {
+ if (queryBuilder.hasWhere() && queryBuilder.toString().includes('WHERE')) {

Likely invalid or redundant comment.

packages/nocodb/src/db/CustomKnex.ts (1)

1283-1289: Ensure the method hasWhere is correctly checking for 'where' clauses. Consider adding a comment explaining the logic.

Verification successful

The hasWhere method is correctly implemented and used within the codebase. It is defined in CustomKnex.ts and utilized in meta.service.ts, where it checks for the presence of 'where' clauses in a query builder.

  • packages/nocodb/src/db/CustomKnex.ts: Definition of hasWhere.
  • packages/nocodb/src/meta/meta.service.ts: Usage of hasWhere.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct functionality of the `hasWhere` method.

# Test: Check if the method correctly identifies the presence of 'where' clauses.
rg --type python $'hasWhere'

Length of output: 58



Script:

#!/bin/bash
# Description: Verify the correct functionality of the `hasWhere` method.

# Test: Check if the method correctly identifies the presence of 'where' clauses.
rg --type typescript --type javascript 'hasWhere'

Length of output: 84



Script:

#!/bin/bash
# Description: Verify the correct functionality of the `hasWhere` method.

# Test: Check if the method correctly identifies the presence of 'where' clauses.
rg --type ts --type js 'hasWhere'

Length of output: 438

@o1lab o1lab force-pushed the nc-refactor/meta-service branch 2 times, most recently from b185f10 to 127031e Compare May 24, 2024 15:31
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Outside diff range and nitpick comments (5)
packages/nocodb/src/db/CustomKnex.ts (5)

Line range hint 72-72: Replace != with !== for strict inequality checks.

Using !== instead of != ensures that both the type and value are checked, which can prevent subtle bugs related to type coercion in JavaScript.

- if (closingIndex == -1)
+ if (closingIndex !== -1)

- if (operandStartIndex != -1)
+ if (operandStartIndex !== -1)

- if (matches[1] != '')
+ if (matches[1] !== '')

Also applies to: 83-83, 98-98, 227-227, 262-262


Line range hint 134-491: Consider using for...of instead of forEach for better readability and performance.

Using for...of loops can make the code more readable and potentially offer performance benefits over forEach, especially for synchronous operations within the loop.

- conditions.forEach((condition) => {
+ for (const condition of conditions) {

Line range hint 470-470: Use === for strict equality checks.

Using === instead of == ensures that both the type and value are checked, which is generally safer and prevents unexpected type coercion.

- if (matches[3] == 'like' && clientType === 'pg') {
+ if (matches[3] === 'like' && clientType === 'pg') {

Line range hint 489-489: Prefer template literals over string concatenation.

Template literals provide a more readable and concise way to create strings, especially when incorporating variables.

- alias: `${
-   this._tn[relation.tn] ? this._tn[relation.tn] + '___' : ''
- }${relation.tn}`,
+ alias: `${this._tn[relation.tn] ? `${this._tn[relation.tn]}___` : ''}${relation.tn}`,

Line range hint 118-494: Convert function expressions to arrow functions for consistency and clarity.

Using arrow functions can make the code more consistent and clear, especially in a context where this needs to be bound correctly.

- const appendWhereCondition = function (
+ const appendWhereCondition = (
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between a925975 and 127031e.
Files selected for processing (4)
  • packages/nocodb/src/db/CustomKnex.ts (5 hunks)
  • packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1 hunks)
  • packages/nocodb/src/helpers/catchError.ts (2 hunks)
  • packages/nocodb/src/meta/meta.service.ts (27 hunks)
Additional Context Used
Biome (76)
packages/nocodb/src/db/CustomKnex.ts (20)

72-72: Use !== instead of !=.
!= is only allowed when comparing against null


81-83: The assignment should not be in an expression.


83-83: Use !== instead of !=.
!= is only allowed when comparing against null


98-98: Use !== instead of !=.
!= is only allowed when comparing against null


134-491: Prefer for...of instead of forEach.


173-173: Unexpected any. Specify a different type.


227-227: Use !== instead of !=.
!= is only allowed when comparing against null


262-262: Use !== instead of !=.
!= is only allowed when comparing against null


470-470: Use === instead of ==.
== is only allowed when comparing against null


489-489: Template literals are preferred over string concatenation.


223-223: This code is unreachable


258-258: This code is unreachable


295-295: This code is unreachable


352-352: This code is unreachable


414-414: This code is unreachable


484-484: This code is unreachable


118-494: This function expression can be turned into an arrow function.


545-545: Unexpected any. Specify a different type.


573-573: Unexpected any. Specify a different type.


583-583: Unexpected any. Specify a different type.

packages/nocodb/src/filters/global-exception/global-exception.filter.ts (16)

24-24: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


75-75: Unexpected any. Specify a different type.


76-76: Unexpected any. Specify a different type.


112-139: This else clause can be omitted because previous branches break early.


117-139: This else clause can be omitted because previous branches break early.


122-139: This else clause can be omitted because previous branches break early.


127-139: This else clause can be omitted because previous branches break early.


131-139: This else clause can be omitted because previous branches break early.


133-139: This else clause can be omitted because previous branches break early.


155-155: Unexpected any. Specify a different type.


155-155: Unexpected any. Specify a different type.


159-159: Unexpected any. Specify a different type.


159-159: Unexpected any. Specify a different type.


1-2: Some named imports are only used as types.


40-40: Reassigning a function parameter is confusing.

packages/nocodb/src/helpers/catchError.ts (20)

22-22: Unexpected any. Specify a different type.


23-23: void is not valid as a constituent in a union type


27-27: Unexpected any. Specify a different type.


72-72: Change to an optional chain.


78-78: Change to an optional chain.


84-84: Change to an optional chain.


89-89: Change to an optional chain.


95-95: Change to an optional chain.


98-99: Change to an optional chain.


108-108: Change to an optional chain.


129-129: Change to an optional chain.


145-145: Change to an optional chain.


162-162: Change to an optional chain.


187-187: Change to an optional chain.


206-206: Change to an optional chain.


265-265: Change to an optional chain.


280-280: Change to an optional chain.


295-295: Change to an optional chain.


310-310: Change to an optional chain.


336-336: Change to an optional chain.

packages/nocodb/src/meta/meta.service.ts (20)

31-31: Decorators are not valid here.


29-29: Unexpected any. Specify a different type.


56-56: Unexpected any. Specify a different type.


72-72: Unexpected any. Specify a different type.


75-75: Unexpected any. Specify a different type.


120-120: Unexpected any. Specify a different type.


122-122: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


152-152: Unexpected any. Specify a different type.


154-154: Unexpected any. Specify a different type.


162-162: Unexpected any. Specify a different type.


248-248: Unexpected any. Specify a different type.


255-255: Unexpected any. Specify a different type.


281-281: Unexpected any. Specify a different type.


282-282: Unexpected any. Specify a different type.


292-292: Unexpected any. Specify a different type.


351-351: Unexpected any. Specify a different type.


395-395: Unexpected any. Specify a different type.


398-398: Unexpected any. Specify a different type.


436-436: Unexpected any. Specify a different type.

Learnings (2)
packages/nocodb/src/helpers/catchError.ts (1)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
packages/nocodb/src/meta/meta.service.ts (2)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
User: pranavxc
PR: nocodb/nocodb#8399
File: packages/nocodb/src/db/BaseModelSqlv2.ts:3317-3327
Timestamp: 2024-05-06T06:35:04.758Z
Learning: Database and other exceptions are captured globally and handled in the system.
Additional comments not posted (8)
packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1)

44-45: Refactor the conditional assignment for clarity and maintainability.

This change makes the conditional logic clearer by directly checking if the exception is an instance of NcBaseError and assigning null if true, otherwise extracting the DB error.

packages/nocodb/src/helpers/catchError.ts (2)

418-423: Introduce the MetaError class for handling metadata related errors.

The class correctly extends NcBaseError and includes additional properties for SQL-related information, enhancing error handling capabilities.


758-760: Implement the static method metaError to throw MetaError.

This method provides a standardized way to throw metadata-related errors, ensuring consistency and reusability in error handling.

packages/nocodb/src/meta/meta.service.ts (4)

140-147: Consider handling empty data more efficiently in bulkMetaInsert method.

- if (Array.isArray(data) ? !data.length : !data) {
-   return [];
- }
+ if (!data || (Array.isArray(data) && !data.length)) {
+   return Promise.resolve([]);
+ }

Likely invalid or redundant comment.


185-189: Refactor the genNanoid method to use a dictionary for prefixes to improve maintainability.

- switch (target) {
-   case MetaTable.PROJECT:
-     prefix = 'p';
-     break;
-   // other cases
-   default:
-     prefix = 'nc';
-     break;
- }
+ const prefixes = {
+   [MetaTable.PROJECT]: 'p',
+   // other mappings
+   default: 'nc',
+ };
+ prefix = prefixes[target] || prefixes.default;

Likely invalid or redundant comment.


1126-1149: Refactor checkConditionPresent to use modern JavaScript features for string checking.

- const sql = queryBuilder.toString();
- if (queryBuilder.hasWhere() && /\bWHERE\b/i.test(sql)) {
+ if (queryBuilder.hasWhere() && queryBuilder.toString().includes('WHERE')) {

Likely invalid or redundant comment.


15-15: Ensure that NcError is properly imported and utilized throughout the service.

Verification successful

NcError is properly imported and utilized throughout meta.service.ts.

  • NcError is imported from ~/helpers/catchError.
  • It is used multiple times within the file, indicating proper handling and usage.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that NcError is properly imported and utilized in meta.service.ts

# Test: Search for the usage of NcError in meta.service.ts. Expect: Proper usage and handling.
rg "NcError" --type typescript --glob 'meta.service.ts'

Length of output: 88



Script:

#!/bin/bash
# Description: Verify that NcError is properly imported and utilized in meta.service.ts

# Test: Search for the usage of NcError in meta.service.ts. Expect: Proper usage and handling.
rg "NcError" --type ts --glob 'meta.service.ts'

Length of output: 687

packages/nocodb/src/db/CustomKnex.ts (1)

560-560: Verify the implementation of hasWhere method.

The hasWhere method is crucial for ensuring that queries contain a 'where' clause. It's important to verify that this method is correctly identifying the presence of 'where' clauses in all types of queries handled by the system. Consider adding unit tests that cover various query scenarios to ensure robustness.

packages/nocodb/src/db/CustomKnex.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
@o1lab o1lab force-pushed the nc-refactor/meta-service branch from 127031e to d318d00 Compare May 24, 2024 15:39
@o1lab o1lab force-pushed the nc-refactor/meta-service branch from d318d00 to 8948150 Compare May 27, 2024 15:24
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 127031e and 8948150.
Files selected for processing (4)
  • packages/nocodb/src/db/CustomKnex.ts (5 hunks)
  • packages/nocodb/src/filters/global-exception/global-exception.filter.ts (1 hunks)
  • packages/nocodb/src/helpers/catchError.ts (2 hunks)
  • packages/nocodb/src/meta/meta.service.ts (20 hunks)
Files skipped from review as they are similar to previous changes (3)
  • packages/nocodb/src/db/CustomKnex.ts
  • packages/nocodb/src/filters/global-exception/global-exception.filter.ts
  • packages/nocodb/src/helpers/catchError.ts
Additional Context Used
Learnings (1)
packages/nocodb/src/meta/meta.service.ts (3)
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:866-870
Timestamp: 2024-05-24T12:51:19.505Z
Learning: Errors in the `nocodb` project are handled globally through the GlobalException handler, and database errors are managed separately.
User: pranavxc
PR: nocodb/nocodb#8399
File: packages/nocodb/src/db/BaseModelSqlv2.ts:3317-3327
Timestamp: 2024-05-06T06:35:04.758Z
Learning: Database and other exceptions are captured globally and handled in the system.
User: pranavxc
PR: nocodb/nocodb#8571
File: packages/nocodb/src/meta/meta.service.ts:56-63
Timestamp: 2024-05-24T17:57:08.082Z
Learning: The `metaGet` method in `packages/nocodb/src/meta/meta.service.ts` is deprecated.
Additional comments not posted (4)
packages/nocodb/src/meta/meta.service.ts (4)

9-9: Ensure proper usage of the Condition type.

#!/bin/bash
# Description: Verify that the `Condition` type is used correctly across the project.

# Test: Search for incorrect usage of the `Condition` type. Expect: Proper usage everywhere.
rg --type typescript $'Condition' --glob '*.*'

56-63: The metaGet method is marked as deprecated. Ensure it is not used in new code.

#!/bin/bash
# Description: Verify that the deprecated `metaGet` method is not used in new code.

# Test: Search for usage of the `metaGet` method. Expect: No new occurrences.
rg --type typescript $'metaGet' --glob '*.*'

181-222: Refactor the genNanoid method to use a dictionary for prefixes to improve maintainability.

- switch (target) {
-   case MetaTable.PROJECT:
-     prefix = 'p';
-     break;
-   // other cases
-   default:
-     prefix = 'nc';
-     break;
- }
+ const prefixes = {
+   [MetaTable.PROJECT]: 'p',
+   // other mappings
+   default: 'nc',
+ };
+ prefix = prefixes[target] || prefixes.default;

Likely invalid or redundant comment.


717-740: Refactor checkConditionPresent to use modern JavaScript features for string checking.

- const sql = queryBuilder.toString();
- if (queryBuilder.hasWhere() && /\bWHERE\b/i.test(sql)) {
+ if (queryBuilder.hasWhere() && queryBuilder.toString().includes('WHERE')) {

Likely invalid or redundant comment.

packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
packages/nocodb/src/meta/meta.service.ts Show resolved Hide resolved
@pranavxc pranavxc merged commit fd501ed into develop May 27, 2024
23 checks passed
@pranavxc pranavxc deleted the nc-refactor/meta-service branch May 27, 2024 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants