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

feat: impl dal #192

Merged
merged 1 commit into from
Mar 21, 2024
Merged

feat: impl dal #192

merged 1 commit into from
Mar 21, 2024

Conversation

killagu
Copy link
Contributor

@killagu killagu commented Mar 11, 2024

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

Summary by CodeRabbit

  • New Features
    • Introduced database abstraction layer (DAL) functionality, including decorators for columns, tables, indexes, and data source qualification.
    • Added extensive enumeration support for database-related properties such as column formats, types, compression types, and more.
    • Launched new classes for modeling database columns, indexes, and tables.
    • Provided utilities for managing column, index, and table information in DAL entities.
    • Enhanced DAL documentation with usage guidance and reference to additional resources.
  • Documentation
    • Updated documentation to include guidance on using new DAL decorators and runtime features.
  • Tests
    • Expanded test coverage to include new DAL functionalities and decorators.
  • Chores
    • Updated .gitignore to exclude .node files.

plugin/dal/README.md Outdated Show resolved Hide resolved
@killagu killagu changed the title WIP: feat: impl dal feat: impl dal Mar 18, 2024
Copy link

coderabbitai bot commented Mar 20, 2024

Note

Reviews Paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

This update introduces enhancements across different components, focusing on database abstraction layer (DAL) functionality. It adds new decorators, utilities, models, and types for managing database schema elements like tables, columns, and indexes. The changes also include guidance for using the @eggjs/dal-decorator and improvements to module identification and management through the moduleName property. This comprehensive update aims to streamline database interactions and schema definition in Egg.js applications.

Changes

Files Summary
.gitignore Added .node entry.
core/core-decorator/.../EggMultiInstancePrototypeInfo.ts Added moduleName property.
core/core-decorator/test/decorators.test.ts Added moduleName property in function call.
core/dal-decorator/README.md, core/dal-runtime/README.md Provided usage guidance, referring users to @eggjs/tegg-dal-plugin.
core/dal-decorator/index.ts, core/dal-runtime/index.ts Introduced exports for DAL functionality.
core/dal-decorator/src/decorator/..., core/dal-runtime/src/... Introduced decorator functions, classes, and utilities for DAL management.
core/dal-decorator/src/enum/..., core/dal-runtime/test/... Added enums for database properties and introduced tests for DAL components.
core/metadata/..., plugin/tegg/lib/AppLoadUnit.ts Enhanced module identification with moduleName property.
plugin/dal/... Provided guidance and configurations for DAL in Egg.js applications, including SQL scripts.

🐰✨
In the realm of code where data flows free,
A rabbit hopped in, changes to see.
With decorators, enums, and models so bright,
It crafted a schema, pure delight.
"To manage the data," it said with glee,
"Is simpler now, as you can see!"
🌟📚

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-tests 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 tests 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 tests.
    • @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 a review. This is useful when automatic reviews are disabled for the repository.
  • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@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.

Review Status

Actionable comments generated: 31

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 01d9061 and e0ea1b7.
Files ignored due to path filters (21)
  • core/dal-decorator/package.json is excluded by: !**/*.json
  • core/dal-decorator/test/fixtures/modules/dal/package.json is excluded by: !**/*.json
  • core/dal-decorator/tsconfig.json is excluded by: !**/*.json
  • core/dal-decorator/tsconfig.pub.json is excluded by: !**/*.json
  • core/dal-runtime/package.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/dal/dal/structure/Foo.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/dal/package.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/Foo.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/MultiPrimaryKey.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/generate_codes/package.json is excluded by: !**/*.json
  • core/dal-runtime/tsconfig.json is excluded by: !**/*.json
  • core/dal-runtime/tsconfig.pub.json is excluded by: !**/*.json
  • core/tegg/package.json is excluded by: !**/*.json
  • plugin/dal/package.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/config/module.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/structure/Foo.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml is excluded by: !**/*.yml
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/package.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/package.json is excluded by: !**/*.json
  • plugin/dal/tsconfig.json is excluded by: !**/*.json
  • plugin/dal/tsconfig.pub.json is excluded by: !**/*.json
Files selected for processing (88)
  • .gitignore (1 hunks)
  • core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts (1 hunks)
  • core/core-decorator/test/decorators.test.ts (1 hunks)
  • core/dal-decorator/README.md (1 hunks)
  • core/dal-decorator/index.ts (1 hunks)
  • core/dal-decorator/src/decorator/Column.ts (1 hunks)
  • core/dal-decorator/src/decorator/DataSourceQualifier.ts (1 hunks)
  • core/dal-decorator/src/decorator/Index.ts (1 hunks)
  • core/dal-decorator/src/decorator/Table.ts (1 hunks)
  • core/dal-decorator/src/enum/ColumnFormat.ts (1 hunks)
  • core/dal-decorator/src/enum/ColumnType.ts (1 hunks)
  • core/dal-decorator/src/enum/CompressionType.ts (1 hunks)
  • core/dal-decorator/src/enum/IndexStoreType.ts (1 hunks)
  • core/dal-decorator/src/enum/IndexType.ts (1 hunks)
  • core/dal-decorator/src/enum/InsertMethod.ts (1 hunks)
  • core/dal-decorator/src/enum/RowFormat.ts (1 hunks)
  • core/dal-decorator/src/model/ColumnModel.ts (1 hunks)
  • core/dal-decorator/src/model/IndexModel.ts (1 hunks)
  • core/dal-decorator/src/model/TableModel.ts (1 hunks)
  • core/dal-decorator/src/type/ColumnTsType.ts (1 hunks)
  • core/dal-decorator/src/type/DateSource.ts (1 hunks)
  • core/dal-decorator/src/type/MySql.ts (1 hunks)
  • core/dal-decorator/src/type/Spatial.ts (1 hunks)
  • core/dal-decorator/src/type/SqlMap.ts (1 hunks)
  • core/dal-decorator/src/util/ColumnInfoUtil.ts (1 hunks)
  • core/dal-decorator/src/util/IndexInfoUtil.ts (1 hunks)
  • core/dal-decorator/src/util/TableInfoUtil.ts (1 hunks)
  • core/dal-decorator/test/fixtures/modules/dal/Foo.ts (1 hunks)
  • core/dal-decorator/test/index.test.ts (1 hunks)
  • core/dal-runtime/README.md (1 hunks)
  • core/dal-runtime/index.ts (1 hunks)
  • core/dal-runtime/src/BaseSqlMap.ts (1 hunks)
  • core/dal-runtime/src/CodeGenerator.ts (1 hunks)
  • core/dal-runtime/src/DataSource.ts (1 hunks)
  • core/dal-runtime/src/MySqlDataSource.ts (1 hunks)
  • core/dal-runtime/src/NunjucksConverter.ts (1 hunks)
  • core/dal-runtime/src/NunjucksUtil.ts (1 hunks)
  • core/dal-runtime/src/SqlGenerator.ts (1 hunks)
  • core/dal-runtime/src/SqlMapLoader.ts (1 hunks)
  • core/dal-runtime/src/SqlUtil.ts (1 hunks)
  • core/dal-runtime/src/TableModelInstanceBuilder.ts (1 hunks)
  • core/dal-runtime/src/TableSqlMap.ts (1 hunks)
  • core/dal-runtime/src/TemplateUtil.ts (1 hunks)
  • core/dal-runtime/src/templates/base_dao.njk (1 hunks)
  • core/dal-runtime/src/templates/dao.njk (1 hunks)
  • core/dal-runtime/src/templates/extension.njk (1 hunks)
  • core/dal-runtime/test/CodeGenerator.test.ts (1 hunks)
  • core/dal-runtime/test/DAO.test.ts (1 hunks)
  • core/dal-runtime/test/DataSource.test.ts (1 hunks)
  • core/dal-runtime/test/SqlGenerator.test.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/Foo.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/dao/FooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/dao/base/BaseFooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/structure/Foo.sql (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/Foo.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/MultiPrimaryKey.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/FooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/MultiPrimaryKeyDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseFooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseMultiPrimaryKeyDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/FooExtension.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/MultiPrimaryKeyExtension.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/Foo.sql (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/MultiPrimaryKey.sql (1 hunks)
  • core/metadata/src/factory/EggPrototypeCreatorFactory.ts (1 hunks)
  • core/metadata/src/impl/ModuleLoadUnit.ts (5 hunks)
  • core/metadata/src/model/AppGraph.ts (4 hunks)
  • core/metadata/test/ModuleGraph.test.ts (1 hunks)
  • core/tegg/dal.ts (1 hunks)
  • plugin/dal/README.md (1 hunks)
  • plugin/dal/app.ts (1 hunks)
  • plugin/dal/lib/DalModuleLoadUnitHook.ts (1 hunks)
  • plugin/dal/lib/DalTableEggPrototypeHook.ts (1 hunks)
  • plugin/dal/lib/DataSource.ts (1 hunks)
  • plugin/dal/lib/MysqlDataSourceManager.ts (1 hunks)
  • plugin/dal/lib/SqlMapManager.ts (1 hunks)
  • plugin/dal/lib/TableModelManager.ts (1 hunks)
  • plugin/dal/test/dal.test.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/config/config.default.js (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/config/plugin.js (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/Foo.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/FooDAO.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/base/BaseFooDAO.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/extension/FooExtension.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/structure/Foo.sql (1 hunks)
  • plugin/dal/typings/index.d.ts (1 hunks)
  • plugin/tegg/lib/AppLoadUnit.ts (1 hunks)
Files skipped from review due to trivial changes (13)
  • .gitignore
  • core/dal-decorator/src/enum/ColumnFormat.ts
  • core/dal-decorator/src/enum/ColumnType.ts
  • core/dal-decorator/src/enum/CompressionType.ts
  • core/dal-decorator/src/enum/IndexStoreType.ts
  • core/dal-decorator/src/enum/IndexType.ts
  • core/dal-decorator/src/enum/InsertMethod.ts
  • core/dal-decorator/src/enum/RowFormat.ts
  • core/dal-decorator/src/type/MySql.ts
  • core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/MultiPrimaryKeyExtension.ts
  • core/tegg/dal.ts
  • plugin/dal/typings/index.d.ts
Additional comments: 98
core/dal-decorator/README.md (1)
  • 1-5: The README directs users to another document for usage instructions. While this is clear, consider adding a brief overview or example usage of @eggjs/dal-decorator here for better standalone comprehension.
core/dal-runtime/README.md (1)
  • 1-5: The README directs users to another document for usage instructions. While this is clear, consider adding a brief overview or example usage of @eggjs/dal-runtime here for better standalone comprehension.
core/dal-runtime/index.ts (1)
  • 1-6: The exports in index.ts are correctly defined and cover key components of the dal-runtime package. LGTM!
plugin/dal/test/fixtures/apps/dal-app/config/config.default.js (1)
  • 1-13: The default configuration settings in config.default.js are correctly defined and follow standard practices for an Egg.js application. LGTM!
plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/extension/FooExtension.ts (1)
  • 1-8: The SQL mapping in FooExtension.ts is correctly defined and uses parameterized queries, which is a good practice for preventing SQL injection. LGTM!
plugin/dal/test/fixtures/apps/dal-app/config/plugin.js (1)
  • 1-16: The plugin configurations in plugin.js are correctly defined and follow standard practices for enabling Egg.js plugins. LGTM!
core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/FooDAO.ts (1)
  • 1-16: Skipping this file as it's a duplicate of the previously reviewed FooDAO.ts.
core/dal-decorator/test/fixtures/modules/dal/Foo.ts (1)
  • 1-22: The usage of DAL decorators (@Table, @Index, and @Column) in the Foo class is correctly implemented and follows best practices for defining database entities in the context of the Egg.js framework.
core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/MultiPrimaryKeyDAO.ts (1)
  • 8-8: The @date tag in the class documentation is left empty. Consider completing this with the relevant date or removing the tag if it's not necessary.
core/metadata/test/ModuleGraph.test.ts (1)
  • 10-10: The addition of an extra argument 'foo' to the ModuleGraph constructor call should be verified against the ModuleGraph class definition to ensure it's correctly handled and expected.
core/dal-decorator/src/decorator/DataSourceQualifier.ts (1)
  • 1-11: The DataSourceQualifier decorator function is correctly implemented, following best practices for dependency injection in the Egg.js framework. It properly uses the QualifierUtil to associate a data source with a property.
core/dal-decorator/src/type/SqlMap.ts (1)
  • 1-23: The SqlType enum and SqlMap interfaces are correctly defined, providing a comprehensive framework for managing different types of SQL operations within the Egg.js framework. This design facilitates clear and structured handling of SQL queries and blocks.
plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/FooDAO.ts (2)
  • 12-15: The class definition and its annotation with @SingletonProto are correctly implemented.
  • 16-20: The findByName method implementation looks good and follows best practices for executing SQL queries through the data source.
core/dal-decorator/src/type/DateSource.ts (1)
  • 1-15: The interfaces PaginateData and DataSource are well-defined, covering essential operations for executing SQL queries and managing pagination. Good job on ensuring comprehensive method signatures.
core/dal-decorator/src/util/IndexInfoUtil.ts (1)
  • 6-15: The utility class IndexInfoUtil and its methods addIndex and getIndexList are correctly implemented, making good use of MetadataUtil for metadata operations. This is a solid approach for managing index information.
core/dal-runtime/src/templates/dao.njk (1)
  • 1-18: The Nunjucks template for generating DAO classes is well-structured, making good use of placeholders and conditional rendering. The generated class structure, including documentation and extension of a base DAO class, follows best practices.
core/dal-decorator/src/decorator/Index.ts (1)
  • 6-21: The IndexParams interface and the Index decorator are well-defined and implemented. The interface covers various aspects of index configuration comprehensively, and the decorator correctly utilizes IndexInfoUtil for managing index information.
core/dal-runtime/test/fixtures/modules/generate_codes/MultiPrimaryKey.ts (1)
  • 7-38: The class MultiPrimaryKey and its use of decorators for defining table and column properties, including primary keys and a unique key, are correctly implemented. This approach enhances readability and maintainability of database schema definitions.
plugin/dal/lib/SqlMapManager.ts (1)
  • 3-28: The singleton class SqlMapManager is well-designed, correctly implementing the singleton pattern and providing clear methods for managing SQL maps. This design ensures efficient and centralized management of SQL maps across the application.
plugin/dal/lib/TableModelManager.ts (1)
  • 3-28: The singleton class TableModelManager is well-designed, correctly implementing the singleton pattern and providing clear methods for managing table models. This design ensures efficient and centralized management of table models across the application.
core/dal-decorator/index.ts (1)
  • 1-24: Ensure all exported entities are used within the DAL feature and are documented appropriately. This will aid developers in understanding and utilizing the DAL capabilities effectively.
core/dal-decorator/src/type/ColumnTsType.ts (1)
  • 1-42: Verify the accuracy and completeness of the ColumnTsType definitions against the SQL column types supported by the target database(s). Consider adding or refining types as necessary to ensure comprehensive coverage.
core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts (1)
  • 12-12: The addition of the moduleName property to MultiInstancePrototypeGetObjectsContext is clear and enhances the context for object retrieval in multi-instance scenarios. Ensure that any relevant documentation is updated to reflect this new property.
plugin/dal/lib/DalModuleLoadUnitHook.ts (1)
  • 10-12: The constructor implementation correctly handles the injection of module configurations. This is a good practice for dependency management.
core/dal-decorator/src/util/ColumnInfoUtil.ts (1)
  • 9-27: The utility methods for managing column information and types are correctly implemented and follow best practices for metadata management in TypeScript.
plugin/dal/app.ts (2)
  • 18-21: The configWillLoad method correctly sets up and registers DAL-related lifecycle hooks, integrating DAL functionality into the application lifecycle effectively.
  • 24-34: The beforeClose method correctly handles the teardown of DAL-related lifecycle hooks and managers, ensuring resources are gracefully released during application shutdown.
core/dal-decorator/src/type/Spatial.ts (1)
  • 3-51: The spatial data type definitions and utility methods are correctly implemented, providing a clear and effective way to work with spatial data within the DAL layer.
core/dal-decorator/src/decorator/Table.ts (1)
  • 31-43: The TableParams interface and Table decorator function are correctly implemented, providing a comprehensive and effective way to define table metadata within the DAL layer.
core/dal-decorator/test/index.test.ts (1)
  • 6-58: The test cases for the DAL decorator functionality are correctly implemented, covering the basic functionality and verifying the expected outcomes effectively.
plugin/dal/lib/MysqlDataSourceManager.ts (4)
  • 15-21: The get method for retrieving data sources is correctly implemented, using a two-level map for efficient indexing and retrieval.
  • 23-42: The createDataSource method correctly implements logic for reusing data source configurations when possible, addressing previous concerns about connection reuse across modules. This is an important improvement for resource management.
  • 44-46: The clear method correctly handles the cleanup of data source indices and resources, ensuring efficient resource management.
  • 48-60: The createDataSourceKey utility method is correctly implemented, providing a reliable way to generate unique keys for data source configurations based on their properties.
core/dal-decorator/src/model/IndexModel.ts (1)
  • 42-45: The method buildIndexName generates index names based on keys and type. Ensure that the generated names adhere to any database-specific limitations on index name lengths or character sets.
core/dal-runtime/test/fixtures/modules/dal/dal/structure/Foo.sql (1)
  • 1-44: The SQL script correctly defines a table with a wide range of column types and two indexes. Ensure that all column types and index configurations are relevant to the tests they are intended to support. Additionally, verify that the unique and fulltext indexes (uk_name_col1 and idx_col1) are adequately tested in the corresponding test cases.
core/metadata/src/factory/EggPrototypeCreatorFactory.ts (1)
  • 23-23: Including the moduleName in the getMultiInstanceProperty call is a significant change that aligns with supporting multi-instance objects in Egg.js. Ensure that all downstream usages of this method and the multiInstanceProtoInfo object properly handle the moduleName property.
core/dal-runtime/src/NunjucksUtil.ts (2)
  • 11-16: The custom escapeSQL function provides SQL value escaping. Ensure that this escaping mechanism is thoroughly tested, especially for edge cases, to prevent SQL injection vulnerabilities.
  • 34-64: The _compile function includes a significant modification to the Nunjucks template compilation process. Ensure that this custom compilation process is compatible with all Nunjucks features used in your templates and does not introduce unexpected behavior.
plugin/tegg/lib/AppLoadUnit.ts (1)
  • 45-45: The addition of the moduleName property to the getInitType method call seems appropriate for enhanced module identification or handling. Ensure that this new property is consistently used and properly handled across all relevant parts of the application.
core/dal-runtime/test/SqlGenerator.test.ts (1)
  • 6-56: The test case for the SqlGenerator class is comprehensive, covering a wide range of column types and SQL features. However, consider a more flexible approach to validating the generated SQL, such as parsing and comparing SQL structures, to make the test less fragile to changes in the SQL generation logic or formatting.
core/dal-runtime/src/TableSqlMap.ts (1)
  • 13-107: The TableSqlMap class is well-structured and provides a clear interface for managing SQL templates and generating SQL strings using Nunjucks templates. Ensure that SQL injection vulnerabilities are properly addressed when using template-based SQL generation. Consider adding comments or documentation regarding SQL injection prevention measures.
core/dal-runtime/src/TemplateUtil.ts (1)
  • 14-100: The utility functions in TemplateUtil.ts are well-implemented and serve their purpose for converting various data types to SQL-compatible strings. Ensure that user-supplied data is properly sanitized or escaped to prevent SQL injection vulnerabilities. Consider adding comments or documentation regarding the handling of user-supplied data.
core/dal-runtime/src/DataSource.ts (1)
  • 14-92: The DataSource class is well-structured and provides a clear interface for executing SQL queries and managing database interactions. Ensure that SQL queries are properly sanitized to prevent SQL injection vulnerabilities. Consider adding comments or documentation regarding SQL query sanitization and SQL injection prevention measures.
core/dal-runtime/src/NunjucksConverter.ts (1)
  • 1-111: The conversion methods in NunjucksConverter.ts are a proactive approach to preventing SQL injection vulnerabilities in template-generated SQL. Ensure that all potential injection vectors are covered and that the conversion logic does not introduce new vulnerabilities. Consider thorough testing and review to validate the security of the conversion logic.
plugin/dal/test/dal.test.ts (1)
  • 7-118: The test case in dal.test.ts is comprehensive, covering CRUD operations on the Foo model. Consider enhancing it with more assertions to validate the state of the database after each operation, ensuring the test case fully verifies the expected behavior.
core/core-decorator/test/decorators.test.ts (1)
  • 121-121: The addition of the moduleName property to the getMultiInstanceProperty function call in the test case seems appropriate for enhanced module identification or handling. Ensure that this new property is consistently used and properly handled across all relevant parts of the test cases.
plugin/dal/lib/DataSource.ts (1)
  • 71-111: The DataSourceDelegate class methods execute, executeScalar, executeRaw, executeRawScalar, paginate, and count directly delegate calls to the dataSource member without adding any additional logic. This delegation could be simplified or documented to clarify the necessity of these wrapper methods, ensuring maintainability and understanding of the codebase.
core/dal-runtime/src/templates/base_dao.njk (1)
  • 1-150: The Nunjucks template for generating base DAO classes uses several macros to generate repetitive logic. While this approach is generally good for code generation, ensure that the generated code follows best practices, such as avoiding unnecessary comments (// empty-line) and ensuring type safety and clarity in the generated methods. Additionally, consider adding more comments within the template to explain the purpose of each macro and section for better maintainability.
core/dal-decorator/src/model/TableModel.ts (1)
  • 15-148: The TableModel class provides a structured way to define and work with database tables. Ensure that all properties and methods are well-documented, especially those that might not be immediately clear to other developers, such as autoExtendSize, avgRowLength, and keyBlockSize. Adding detailed comments can improve the maintainability and usability of this class.
plugin/dal/test/fixtures/apps/dal-app/modules/dal/Foo.ts (3)
  • 13-18: The @Table annotation is well-defined, including thoughtful considerations for character encoding with utf8mb4. This setup supports a wide range of Unicode characters, which is a good practice.
  • 19-29: The @Index annotations are appropriately used, with a UNIQUE index on 'name' and 'col1', and a FULLTEXT index on 'col1'. Including comments for indexes is a good practice for maintainability.
  • 31-291: The @Column annotations are comprehensive and well-defined, covering a wide range of data types and specifying properties such as length, unsigned, zeroFill, characterSet, and collate where applicable. This thorough approach enhances the clarity and maintainability of the database schema.
core/dal-runtime/src/SqlUtil.ts (1)
  • 19-185: The minify method in the SqlUtil class uses a state machine approach to effectively parse and minify SQL strings, handling various contexts such as comments, block comments with hints, and quoted strings. To improve readability and maintainability, consider adding comments explaining the purpose of each state and transitions between them.
core/dal-runtime/test/fixtures/modules/dal/Foo.ts (2)
  • 13-31: The usage of @Table and @Index decorators along with the @Column decorator for defining database table structure, columns, and indexes is correctly implemented.
  • 49-317: The class properties are well-defined with appropriate usage of @Column decorator to specify column types and attributes, covering a wide range of SQL data types.
core/dal-runtime/test/fixtures/modules/generate_codes/Foo.ts (1)
  • 13-317: The usage of @Table, @Index, and @Column decorators for defining database table structure, columns, and indexes, as well as the definition of class properties, are correctly implemented, identical to the previous file reviewed.
core/dal-decorator/src/decorator/Column.ts (2)
  • 262-272: The implementation of the Column decorator is correctly handled, ensuring proper registration of column information on the target class and asserting the property key is a string.
  • 7-260: The structured definition of interfaces for various SQL column types ensures type safety and clarity, facilitating easy extension and maintenance. The separation into base interfaces for numeric types demonstrates good practice for code reuse.
core/metadata/src/impl/ModuleLoadUnit.ts (2)
  • 35-42: The addition of the moduleName parameter to the ProtoNode constructor and its usage in the prototype initialization process is correctly implemented, enhancing module identification.
  • 69-75: The handling of the moduleName parameter in the ModuleGraph constructor and its consistent usage throughout the class, including when creating ProtoNode instances, is correctly implemented, facilitating precise module management.

Also applies to: 109-112

core/metadata/src/model/AppGraph.ts (1)
  • 58-64: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [61-70]

The usage of moduleName in the ClazzMap class to enhance the handling of multi-instance properties and access levels based on the module name is correctly implemented, improving dependency resolution and access control.

Also applies to: 89-89

core/dal-runtime/src/BaseSqlMap.ts (6)
  • 6-10: The GenerateSqlMap interface is well-defined and adheres to TypeScript best practices. It clearly specifies the structure for SQL map generation, which is crucial for the DAL's operation.
  • 21-24: The method generateAllColumns cleverly uses Nunjucks templating to dynamically generate SQL columns, including a conditional for counting. This approach is flexible and maintainable.
  • 27-53: The generateFindByPrimary method correctly handles the case where a table might not have a primary key and logs a warning. It also generates SQL for selecting by primary key, supporting both single and composite keys.
  • 190-245: The generateUpdate method correctly generates an SQL update statement, including handling of the gmtModified field to automatically update the timestamp. This is a good practice for maintaining data integrity.
  • 248-262: The generateDelete method properly generates an SQL delete statement based on the primary key. It also correctly handles the case where a table might not have a primary key by logging a warning.
  • 265-321: The load method aggregates all generated SQL maps into a single record, making it easy to access and use these maps throughout the application. This approach enhances modularity and maintainability.
plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/base/BaseFooDAO.ts (3)
  • 26-230: The insert method in BaseFooDAO is well-structured and handles the insertion of various data types correctly. It uses a pattern of checking for undefined values before adding them to the data object, which is a good practice for data integrity.
  • 233-442: The update method follows a similar pattern to the insert method, ensuring that only defined fields are included in the update operation. This approach minimizes the risk of unintentionally overwriting data.
  • 457-493: The find methods (findByCol1, findOneByCol1, findByUkNameCol1, findOneByUkNameCol1, findById, findByPrimary) are correctly implemented, providing various ways to query the Foo entity. It's good practice to have both list and scalar return types for flexibility.
core/dal-runtime/test/fixtures/modules/dal/dal/dao/base/BaseFooDAO.ts (3)
  • 24-228: The insert method in this BaseFooDAO class is consistent with the implementation reviewed in the plugin directory. It correctly handles the insertion of various data types and checks for undefined values.
  • 231-440: The update method also mirrors the implementation in the plugin directory, ensuring that only defined fields are included in the update operation. This consistency across test fixtures and actual implementation is crucial for reliable testing.
  • 455-491: The find methods in this class are consistent with those in the plugin directory, providing various ways to query the Foo entity. This consistency is important for ensuring that test fixtures accurately reflect the actual implementation.
core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseFooDAO.ts (5)
  • 6-6: Consider using a utility type from a standard library or TypeScript itself for Optional, if available, to reduce code redundancy and improve maintainability.
  • 10-10: The comment about the file being auto-generated is crucial. Ensure that there's a mechanism in place to regenerate this file when the underlying model or requirements change, to keep the DAO layer up-to-date.
  • 24-228: The insert method constructs a data object dynamically based on the properties of raw. While this approach is flexible, it's essential to ensure that the executeRawScalar method properly sanitizes inputs to prevent SQL injection vulnerabilities.
  • 231-440: Similar to the insert method, the update method dynamically constructs an update payload. Ensure that the executeRawScalar method used for executing the update operation is secure against SQL injection by properly escaping or binding parameters.
  • 455-491: The methods for finding records (findByCol1, findOneByCol1, findByUkNameCol1, findOneByUkNameCol1, findById, findByPrimary) demonstrate a good use of abstraction for common database operations. Ensure that these methods are adequately tested, especially for edge cases and error handling.
core/dal-runtime/src/SqlGenerator.ts (3)
  • 16-71: The generateColumn method constructs SQL column definitions dynamically. Ensure that inputs, such as column names and comments, are properly sanitized to prevent SQL injection vulnerabilities.
  • 73-253: The generateColumnType method handles various column types and their specific attributes. Ensure that the method correctly handles all edge cases, especially for types with optional parameters like length, precision, and character set.
  • 255-301: The generateIndex method constructs SQL index definitions. Ensure that index names and comments are properly sanitized to prevent SQL injection vulnerabilities.
plugin/dal/README.md (15)
  • 3-3: The introduction provides a brief overview of the plugin's purpose but could benefit from a more detailed explanation of the DAL concept and how this plugin enhances data access layer development in Egg.js applications.
  • 5-14: The installation instructions are clear and straightforward. However, it would be helpful to include a brief description of what each package does for newcomers to understand why they need to install these specific packages.
  • 16-25: The preparation step for enabling experimental decorators in TypeScript is crucial for using this plugin. It's well-explained, but linking to TypeScript documentation for more context on "experimentalDecorators" could enhance understanding.
  • 27-40: Configuration instructions are concise. It might be beneficial to explain the purpose of enabling these plugins in the config/plugin.js file, especially for developers new to the Egg.js ecosystem.
  • 44-57: The section on configuring data sources through module.yml is clear. Including an example of how to reference these data sources in code (beyond the @Table annotation) would provide a more complete picture.
  • 59-90: The example usage of @Table, @Index, and @Column decorators is informative. It would be beneficial to include a brief explanation of each decorator's purpose and how they interact with each other to define a table structure.
  • 92-118: The detailed parameters for the @Table decorator are well-documented. A small example demonstrating the use of some of these parameters (e.g., autoIncrement, compression) in context would make this section more practical.
  • 120-137: Similarly, for the @Index decorator, the documentation is thorough. Including an example showcasing the use of optional parameters like name and storeType could provide clearer guidance on their usage.
  • 139-158: The @Column decorator's parameters are well-explained. An example illustrating the use of parameters like canNull, autoIncrement, or uniqueKey would enhance understanding.
  • 160-207: The enumeration of supported column types is comprehensive. A brief note on choosing the appropriate column type based on the data being stored could help developers make informed decisions.
  • 209-240: The section on using ColumnTsType for uncertain type mappings is helpful. Clarifying when it's best to use ColumnTsType over direct ColumnType enums could guide developers in best practices.
  • 492-511: The directory structure generated by egg-bin dal gen is clearly outlined. It would be useful to include a brief description of typical use cases or scenarios where developers might need to modify or extend the generated files.
  • 513-528: The DAO injection example is straightforward. Expanding on how to handle more complex queries or transactions within the repository class could provide valuable insights for advanced use cases.
  • 530-562: The custom SQL definition and usage instructions are clear. A note on best practices for managing complex SQL queries within extensions, such as organizing or modularizing SQL statements, could be beneficial.
  • 564-581: The DataSource interface documentation is concise. Including examples of common use cases for each method (e.g., paginate, count) would make this section more actionable.

core/dal-runtime/src/templates/extension.njk Outdated Show resolved Hide resolved
core/dal-runtime/src/BaseSqlMap.ts Show resolved Hide resolved
core/dal-runtime/src/BaseSqlMap.ts Outdated Show resolved Hide resolved
Comment on lines +21 to +51
constructor(params: {
columnName: string;
propertyName: string;
type: ColumnTypeParams;
canNull: boolean;
default?: string;
comment?: string;
visible?: boolean;
autoIncrement?: boolean;
uniqueKey?: boolean;
primaryKey?: boolean;
collate?: string;
columnFormat?: ColumnFormat;
engineAttribute?: string;
secondaryEngineAttribute?: string;
}) {
this.columnName = params.columnName;
this.propertyName = params.propertyName;
this.type = params.type;
this.canNull = params.canNull;
this.default = params.default;
this.comment = params.comment;
this.visible = params.visible;
this.autoIncrement = params.autoIncrement;
this.uniqueKey = params.uniqueKey;
this.primaryKey = params.primaryKey;
this.collate = params.collate;
this.columnFormat = params.columnFormat;
this.engineAttribute = params.engineAttribute;
this.secondaryEngineAttribute = params.secondaryEngineAttribute;
}
Copy link

Choose a reason for hiding this comment

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

The constructor of ColumnModel directly assigns values from the params object to its properties. Consider validating the inputs, especially for critical properties like columnName, propertyName, and type, to ensure they meet expected formats or constraints.

Comment on lines +53 to +76
static build(property: string, type: ColumnTypeParams, params?: ColumnParams) {
const columnName = params?.name ?? snakecase(property);
// TODO can null default should be false
let canNull = params?.canNull ?? true;
if (params?.primaryKey) {
canNull = false;
}
return new ColumnModel({
columnName,
propertyName: property,
type,
canNull,
default: params?.default,
comment: params?.comment,
visible: params?.visible,
autoIncrement: params?.autoIncrement,
uniqueKey: params?.uniqueKey,
primaryKey: params?.primaryKey,
collate: params?.collate,
columnFormat: params?.columnFormat,
engineAttribute: params?.engineAttribute,
secondaryEngineAttribute: params?.secondaryEngineAttribute,
});
}
Copy link

Choose a reason for hiding this comment

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

In static build, the handling of canNull for primary keys is correct (line 57-58). However, consider adding a comment explaining why primary keys cannot be null, for clarity to future maintainers.

+ // Primary keys cannot be null by definition
  if (params?.primaryKey) {
    canNull = false;
  }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
static build(property: string, type: ColumnTypeParams, params?: ColumnParams) {
const columnName = params?.name ?? snakecase(property);
// TODO can null default should be false
let canNull = params?.canNull ?? true;
if (params?.primaryKey) {
canNull = false;
}
return new ColumnModel({
columnName,
propertyName: property,
type,
canNull,
default: params?.default,
comment: params?.comment,
visible: params?.visible,
autoIncrement: params?.autoIncrement,
uniqueKey: params?.uniqueKey,
primaryKey: params?.primaryKey,
collate: params?.collate,
columnFormat: params?.columnFormat,
engineAttribute: params?.engineAttribute,
secondaryEngineAttribute: params?.secondaryEngineAttribute,
});
}
static build(property: string, type: ColumnTypeParams, params?: ColumnParams) {
const columnName = params?.name ?? snakecase(property);
// TODO can null default should be false
let canNull = params?.canNull ?? true;
// Primary keys cannot be null by definition
if (params?.primaryKey) {
canNull = false;
}
return new ColumnModel({
columnName,
propertyName: property,
type,
canNull,
default: params?.default,
comment: params?.comment,
visible: params?.visible,
autoIncrement: params?.autoIncrement,
uniqueKey: params?.uniqueKey,
primaryKey: params?.primaryKey,
collate: params?.collate,
columnFormat: params?.columnFormat,
engineAttribute: params?.engineAttribute,
secondaryEngineAttribute: params?.secondaryEngineAttribute,
});
}

Comment on lines +81 to +86
public async del(primary: {
id1: ColumnTsType['INT'],
id2: ColumnTsType['INT']
}): Promise<DeleteResult> {
return this.dataSource.executeRawScalar('delete', primary);
}
Copy link

Choose a reason for hiding this comment

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

The del method is a duplicate of the delete method. If this is intentional (e.g., for aliasing purposes), consider adding a comment to clarify. Otherwise, remove the duplicate to avoid confusion and potential maintenance issues.

-  public async del(primary: {
-    id1: ColumnTsType['INT'],
-    id2: ColumnTsType['INT']
-  }): Promise<DeleteResult> {
-    return this.dataSource.executeRawScalar('delete', primary);
-  }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public async del(primary: {
id1: ColumnTsType['INT'],
id2: ColumnTsType['INT']
}): Promise<DeleteResult> {
return this.dataSource.executeRawScalar('delete', primary);
}

Comment on lines +19 to +27
function _replaceCodeWithSQLFeature(source) {
const funcs = [
'convertNormalVariableCode', // 普通变量
'convertTernaryCode', // 三目运算
'convertNestedObjectCode', // 对象中的变量,如 `user.id`
'convertValueInsideFor', // for 中的值需要转义
];

return funcs.reduce((source, func) => NunjucksConverter[func](source), source);
}
Copy link

Choose a reason for hiding this comment

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

The _replaceCodeWithSQLFeature function modifies Nunjucks code to support SQL escaping. This approach is innovative but could introduce maintenance challenges. Consider documenting the specific Nunjucks patterns this function targets and any limitations or edge cases.

+ /**
+  * Modifies Nunjucks code to support SQL escaping by targeting specific patterns.
+  * Targets:
+  * - Normal variables
+  * - Ternary operations
+  * - Nested object variables
+  * - Values inside loops
+  * Note: Document any limitations or edge cases.
+  */
function _replaceCodeWithSQLFeature(source) {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
function _replaceCodeWithSQLFeature(source) {
const funcs = [
'convertNormalVariableCode', // 普通变量
'convertTernaryCode', // 三目运算
'convertNestedObjectCode', // 对象中的变量,如 `user.id`
'convertValueInsideFor', // for 中的值需要转义
];
return funcs.reduce((source, func) => NunjucksConverter[func](source), source);
}
/**
* Modifies Nunjucks code to support SQL escaping by targeting specific patterns.
* Targets:
* - Normal variables
* - Ternary operations
* - Nested object variables
* - Values inside loops
* Note: Document any limitations or edge cases.
*/
function _replaceCodeWithSQLFeature(source) {
const funcs = [
'convertNormalVariableCode', // 普通变量
'convertTernaryCode', // 三目运算
'convertNestedObjectCode', // 对象中的变量,如 `user.id`
'convertValueInsideFor', // for 中的值需要转义
];
return funcs.reduce((source, func) => NunjucksConverter[func](source), source);
}

@killagu killagu force-pushed the feat/dal branch 2 times, most recently from 8f0ddf0 to 367c2cf Compare March 20, 2024 15:50
Copy link

@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.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 01d9061 and 35d410d.
Files ignored due to path filters (21)
  • core/dal-decorator/package.json is excluded by: !**/*.json
  • core/dal-decorator/test/fixtures/modules/dal/package.json is excluded by: !**/*.json
  • core/dal-decorator/tsconfig.json is excluded by: !**/*.json
  • core/dal-decorator/tsconfig.pub.json is excluded by: !**/*.json
  • core/dal-runtime/package.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/dal/dal/structure/Foo.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/dal/package.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/Foo.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/MultiPrimaryKey.json is excluded by: !**/*.json
  • core/dal-runtime/test/fixtures/modules/generate_codes/package.json is excluded by: !**/*.json
  • core/dal-runtime/tsconfig.json is excluded by: !**/*.json
  • core/dal-runtime/tsconfig.pub.json is excluded by: !**/*.json
  • core/tegg/package.json is excluded by: !**/*.json
  • plugin/dal/package.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/config/module.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/structure/Foo.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml is excluded by: !**/*.yml
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/package.json is excluded by: !**/*.json
  • plugin/dal/test/fixtures/apps/dal-app/package.json is excluded by: !**/*.json
  • plugin/dal/tsconfig.json is excluded by: !**/*.json
  • plugin/dal/tsconfig.pub.json is excluded by: !**/*.json
Files selected for processing (89)
  • .github/workflows/nodejs.yml (3 hunks)
  • .gitignore (1 hunks)
  • core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts (1 hunks)
  • core/core-decorator/test/decorators.test.ts (1 hunks)
  • core/dal-decorator/README.md (1 hunks)
  • core/dal-decorator/index.ts (1 hunks)
  • core/dal-decorator/src/decorator/Column.ts (1 hunks)
  • core/dal-decorator/src/decorator/DataSourceQualifier.ts (1 hunks)
  • core/dal-decorator/src/decorator/Index.ts (1 hunks)
  • core/dal-decorator/src/decorator/Table.ts (1 hunks)
  • core/dal-decorator/src/enum/ColumnFormat.ts (1 hunks)
  • core/dal-decorator/src/enum/ColumnType.ts (1 hunks)
  • core/dal-decorator/src/enum/CompressionType.ts (1 hunks)
  • core/dal-decorator/src/enum/IndexStoreType.ts (1 hunks)
  • core/dal-decorator/src/enum/IndexType.ts (1 hunks)
  • core/dal-decorator/src/enum/InsertMethod.ts (1 hunks)
  • core/dal-decorator/src/enum/RowFormat.ts (1 hunks)
  • core/dal-decorator/src/model/ColumnModel.ts (1 hunks)
  • core/dal-decorator/src/model/IndexModel.ts (1 hunks)
  • core/dal-decorator/src/model/TableModel.ts (1 hunks)
  • core/dal-decorator/src/type/ColumnTsType.ts (1 hunks)
  • core/dal-decorator/src/type/DateSource.ts (1 hunks)
  • core/dal-decorator/src/type/MySql.ts (1 hunks)
  • core/dal-decorator/src/type/Spatial.ts (1 hunks)
  • core/dal-decorator/src/type/SqlMap.ts (1 hunks)
  • core/dal-decorator/src/util/ColumnInfoUtil.ts (1 hunks)
  • core/dal-decorator/src/util/IndexInfoUtil.ts (1 hunks)
  • core/dal-decorator/src/util/TableInfoUtil.ts (1 hunks)
  • core/dal-decorator/test/fixtures/modules/dal/Foo.ts (1 hunks)
  • core/dal-decorator/test/index.test.ts (1 hunks)
  • core/dal-runtime/README.md (1 hunks)
  • core/dal-runtime/index.ts (1 hunks)
  • core/dal-runtime/src/BaseSqlMap.ts (1 hunks)
  • core/dal-runtime/src/CodeGenerator.ts (1 hunks)
  • core/dal-runtime/src/DataSource.ts (1 hunks)
  • core/dal-runtime/src/MySqlDataSource.ts (1 hunks)
  • core/dal-runtime/src/NunjucksConverter.ts (1 hunks)
  • core/dal-runtime/src/NunjucksUtil.ts (1 hunks)
  • core/dal-runtime/src/SqlGenerator.ts (1 hunks)
  • core/dal-runtime/src/SqlMapLoader.ts (1 hunks)
  • core/dal-runtime/src/SqlUtil.ts (1 hunks)
  • core/dal-runtime/src/TableModelInstanceBuilder.ts (1 hunks)
  • core/dal-runtime/src/TableSqlMap.ts (1 hunks)
  • core/dal-runtime/src/TemplateUtil.ts (1 hunks)
  • core/dal-runtime/src/templates/base_dao.njk (1 hunks)
  • core/dal-runtime/src/templates/dao.njk (1 hunks)
  • core/dal-runtime/src/templates/extension.njk (1 hunks)
  • core/dal-runtime/test/CodeGenerator.test.ts (1 hunks)
  • core/dal-runtime/test/DAO.test.ts (1 hunks)
  • core/dal-runtime/test/DataSource.test.ts (1 hunks)
  • core/dal-runtime/test/SqlGenerator.test.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/Foo.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/dao/FooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/dao/base/BaseFooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/dal/dal/structure/Foo.sql (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/Foo.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/MultiPrimaryKey.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/FooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/MultiPrimaryKeyDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseFooDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseMultiPrimaryKeyDAO.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/FooExtension.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/MultiPrimaryKeyExtension.ts (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/Foo.sql (1 hunks)
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/MultiPrimaryKey.sql (1 hunks)
  • core/metadata/src/factory/EggPrototypeCreatorFactory.ts (1 hunks)
  • core/metadata/src/impl/ModuleLoadUnit.ts (5 hunks)
  • core/metadata/src/model/AppGraph.ts (4 hunks)
  • core/metadata/test/ModuleGraph.test.ts (1 hunks)
  • core/tegg/dal.ts (1 hunks)
  • plugin/dal/README.md (1 hunks)
  • plugin/dal/app.ts (1 hunks)
  • plugin/dal/lib/DalModuleLoadUnitHook.ts (1 hunks)
  • plugin/dal/lib/DalTableEggPrototypeHook.ts (1 hunks)
  • plugin/dal/lib/DataSource.ts (1 hunks)
  • plugin/dal/lib/MysqlDataSourceManager.ts (1 hunks)
  • plugin/dal/lib/SqlMapManager.ts (1 hunks)
  • plugin/dal/lib/TableModelManager.ts (1 hunks)
  • plugin/dal/test/dal.test.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/config/config.default.js (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/config/plugin.js (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/Foo.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/FooDAO.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/base/BaseFooDAO.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/extension/FooExtension.ts (1 hunks)
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/structure/Foo.sql (1 hunks)
  • plugin/dal/typings/index.d.ts (1 hunks)
  • plugin/tegg/lib/AppLoadUnit.ts (1 hunks)
Files skipped from review as they are similar to previous changes (85)
  • .gitignore
  • core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts
  • core/core-decorator/test/decorators.test.ts
  • core/dal-decorator/index.ts
  • core/dal-decorator/src/decorator/Column.ts
  • core/dal-decorator/src/decorator/DataSourceQualifier.ts
  • core/dal-decorator/src/decorator/Index.ts
  • core/dal-decorator/src/decorator/Table.ts
  • core/dal-decorator/src/enum/ColumnFormat.ts
  • core/dal-decorator/src/enum/ColumnType.ts
  • core/dal-decorator/src/enum/CompressionType.ts
  • core/dal-decorator/src/enum/IndexStoreType.ts
  • core/dal-decorator/src/enum/IndexType.ts
  • core/dal-decorator/src/enum/InsertMethod.ts
  • core/dal-decorator/src/enum/RowFormat.ts
  • core/dal-decorator/src/model/ColumnModel.ts
  • core/dal-decorator/src/model/IndexModel.ts
  • core/dal-decorator/src/type/ColumnTsType.ts
  • core/dal-decorator/src/type/DateSource.ts
  • core/dal-decorator/src/type/MySql.ts
  • core/dal-decorator/src/type/Spatial.ts
  • core/dal-decorator/src/type/SqlMap.ts
  • core/dal-decorator/src/util/ColumnInfoUtil.ts
  • core/dal-decorator/src/util/IndexInfoUtil.ts
  • core/dal-decorator/src/util/TableInfoUtil.ts
  • core/dal-decorator/test/fixtures/modules/dal/Foo.ts
  • core/dal-decorator/test/index.test.ts
  • core/dal-runtime/index.ts
  • core/dal-runtime/src/BaseSqlMap.ts
  • core/dal-runtime/src/CodeGenerator.ts
  • core/dal-runtime/src/DataSource.ts
  • core/dal-runtime/src/MySqlDataSource.ts
  • core/dal-runtime/src/NunjucksConverter.ts
  • core/dal-runtime/src/NunjucksUtil.ts
  • core/dal-runtime/src/SqlGenerator.ts
  • core/dal-runtime/src/SqlMapLoader.ts
  • core/dal-runtime/src/SqlUtil.ts
  • core/dal-runtime/src/TableModelInstanceBuilder.ts
  • core/dal-runtime/src/TableSqlMap.ts
  • core/dal-runtime/src/TemplateUtil.ts
  • core/dal-runtime/src/templates/base_dao.njk
  • core/dal-runtime/src/templates/dao.njk
  • core/dal-runtime/src/templates/extension.njk
  • core/dal-runtime/test/CodeGenerator.test.ts
  • core/dal-runtime/test/DAO.test.ts
  • core/dal-runtime/test/DataSource.test.ts
  • core/dal-runtime/test/SqlGenerator.test.ts
  • core/dal-runtime/test/fixtures/modules/dal/Foo.ts
  • core/dal-runtime/test/fixtures/modules/dal/dal/dao/FooDAO.ts
  • core/dal-runtime/test/fixtures/modules/dal/dal/dao/base/BaseFooDAO.ts
  • core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts
  • core/dal-runtime/test/fixtures/modules/dal/dal/structure/Foo.sql
  • core/dal-runtime/test/fixtures/modules/generate_codes/Foo.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/MultiPrimaryKey.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/FooDAO.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/MultiPrimaryKeyDAO.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseFooDAO.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseMultiPrimaryKeyDAO.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/FooExtension.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/MultiPrimaryKeyExtension.ts
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/Foo.sql
  • core/dal-runtime/test/fixtures/modules/generate_codes/dal/structure/MultiPrimaryKey.sql
  • core/metadata/src/factory/EggPrototypeCreatorFactory.ts
  • core/metadata/src/impl/ModuleLoadUnit.ts
  • core/metadata/src/model/AppGraph.ts
  • core/metadata/test/ModuleGraph.test.ts
  • core/tegg/dal.ts
  • plugin/dal/README.md
  • plugin/dal/app.ts
  • plugin/dal/lib/DalModuleLoadUnitHook.ts
  • plugin/dal/lib/DalTableEggPrototypeHook.ts
  • plugin/dal/lib/DataSource.ts
  • plugin/dal/lib/MysqlDataSourceManager.ts
  • plugin/dal/lib/SqlMapManager.ts
  • plugin/dal/lib/TableModelManager.ts
  • plugin/dal/test/dal.test.ts
  • plugin/dal/test/fixtures/apps/dal-app/config/config.default.js
  • plugin/dal/test/fixtures/apps/dal-app/config/plugin.js
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/Foo.ts
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/FooDAO.ts
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/dao/base/BaseFooDAO.ts
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/extension/FooExtension.ts
  • plugin/dal/test/fixtures/apps/dal-app/modules/dal/dal/structure/Foo.sql
  • plugin/dal/typings/index.d.ts
  • plugin/tegg/lib/AppLoadUnit.ts
Additional comments: 4
core/dal-decorator/README.md (1)
  • 1-5: The README provides clear direction for users to find more detailed information. Ensure the link to @eggjs/tegg-dal-plugin is functional and accessible from the repository.
core/dal-runtime/README.md (1)
  • 1-5: Ensure the link to @eggjs/tegg-dal-plugin is functional and accessible from the repository. Additionally, verify that the content is relevant to the dal-runtime package.
.github/workflows/nodejs.yml (2)
  • 23-23: Updating the node-version matrix to exclude version 14 aligns with Node.js versioning policies. Consider adding a note in the documentation or changelog to inform users about dropping support for Node.js version 14.

Also applies to: 54-54

  • 83-112: The removal of the Windows runner due to GitHub MySQL service limitations is understandable. Ensure that this limitation and its implications are clearly documented for contributors who might want to run tests on Windows environments.

core/dal-runtime/README.md Outdated Show resolved Hide resolved
columns.push(ColumnModel.build(property, columnType, columnParam));
}

// TODO 补充 PRIMARY 主键
Copy link

@coderabbitai coderabbitai bot Mar 20, 2024

Choose a reason for hiding this comment

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

There's a TODO comment about supplementing the PRIMARY key. Ensure this is addressed or tracked in a project management tool to avoid missing this critical functionality.

Would you like assistance in implementing the PRIMARY key supplementation?

Copy link
Member

Choose a reason for hiding this comment

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

@coderabbitai yes please help.

Copy link

Choose a reason for hiding this comment

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

@fengmk2, to address the supplementation of the PRIMARY key in the TableModel class, you can enhance the getPrimary method to ensure it automatically creates a PRIMARY key index if one is not explicitly defined among the indices. Here's a suggested approach:

  1. The getPrimary method already attempts to find a PRIMARY index among the defined indices. If it finds one, it returns it.
  2. If no PRIMARY index is found, it creates one based on columns marked as primary keys. This is a good approach, but ensure that all columns intended to be part of the PRIMARY key are correctly marked with primaryKey: true in their definitions.

Here's a refined implementation snippet for the getPrimary method:

getPrimary(): IndexModel | undefined {
  // Attempt to find an explicitly defined PRIMARY index
  const index = this.indices.find(t => t.type === IndexType.PRIMARY);
  if (index) {
    return index;
  }
  // If no PRIMARY index is found, create one based on columns marked as primary keys
  const primaryColumn = this.columns.filter(t => t.primaryKey === true);
  if (primaryColumn.length > 0) {
    return new IndexModel({
      name: 'PRIMARY',
      type: IndexType.PRIMARY,
      keys: primaryColumn.map(t => ({
        columnName: t.columnName,
        propertyName: t.propertyName,
      })),
    });
  }
  // If no columns are marked as primary keys, consider logging a warning or throwing an error
  console.warn('No PRIMARY key defined for table:', this.name);
  return undefined;
}

This implementation ensures that if a PRIMARY index is not explicitly defined among the indices, the class will attempt to create one based on columns marked as primary keys. If no columns are marked as primary keys, it logs a warning, which you can replace with an error throw if your application's logic requires a PRIMARY key to be defined.

Please review this approach and adjust it according to your specific requirements and coding standards.

Comment on lines 1 to 147
collate?: string;
compression?: CompressionType;
encryption?: boolean;
engine?: string;
engineAttribute?: string;
insertMethod?: InsertMethod;
keyBlockSize?: number;
maxRows?: number;
minRows?: number;
rowFormat?: RowFormat;
secondaryEngineAttribute?: string;
}) {
this.clazz = params.clazz;
this.name = params.name;
this.dataSourceName = params.dataSourceName;
this.columns = params.columns;
this.indices = params.indices;
this.comment = params.comment;
this.autoExtendSize = params.autoExtendSize;
this.autoIncrement = params.autoIncrement;
this.avgRowLength = params.avgRowLength;
this.characterSet = params.characterSet;
this.collate = params.collate;
this.compression = params.compression;
this.encryption = params.encryption;
this.engine = params.engine;
this.engineAttribute = params.engineAttribute;
this.insertMethod = params.insertMethod;
this.keyBlockSize = params.keyBlockSize;
this.maxRows = params.maxRows;
this.minRows = params.minRows;
this.rowFormat = params.rowFormat;
this.secondaryEngineAttribute = params.secondaryEngineAttribute;
}

getPrimary(): IndexModel | undefined {
const index = this.indices.find(t => t.type === IndexType.PRIMARY);
if (index) {
return index;
}
const primaryColumn = this.columns.filter(t => t.primaryKey === true);
return new IndexModel({
name: 'PRIMARY',
type: IndexType.PRIMARY,
keys: primaryColumn.map(t => {
return {
columnName: t.columnName,
propertyName: t.propertyName,
};
}),
});
}

static build<T>(clazz: EggProtoImplClass<T>): TableModel<T> {
const params = TableInfoUtil.getTableParams(clazz as EggProtoImplClass);
const name = params?.name ?? snakecase(pluralize(clazz.name));
const columnInfoMap = ColumnInfoUtil.getColumnInfoMap(clazz as EggProtoImplClass);
const columnTypeMap = ColumnInfoUtil.getColumnTypeMap(clazz as EggProtoImplClass);
// TODO set to default name
const dataSourceName = params?.dataSourceName ?? 'default';
assert(TableInfoUtil.getIsTable(clazz as EggProtoImplClass), `${name} is not Table`);
assert(columnTypeMap, `${name} has no columns`);
const columns: Array<ColumnModel> = [];
const indices: Array<IndexModel> = [];
for (const [ property, columnType ] of columnTypeMap?.entries()) {
const columnParam = columnInfoMap?.get(property);
columns.push(ColumnModel.build(property, columnType, columnParam));
}

// TODO 补充 PRIMARY 主键
const indexList = IndexInfoUtil.getIndexList(clazz as EggProtoImplClass);
for (const index of indexList) {
indices.push(IndexModel.build(index, columns));
}

return new TableModel({
clazz,
name,
columns,
indices,
dataSourceName,

comment: params?.comment,
autoExtendSize: params?.autoExtendSize,
autoIncrement: params?.autoIncrement,
avgRowLength: params?.avgRowLength,
characterSet: params?.characterSet,
collate: params?.collate,
compression: params?.compression,
encryption: params?.encryption,
engine: params?.engine,
engineAttribute: params?.engineAttribute,
insertMethod: params?.insertMethod,
keyBlockSize: params?.keyBlockSize,
maxRows: params?.maxRows,
minRows: params?.minRows,
rowFormat: params?.rowFormat,
secondaryEngineAttribute: params?.secondaryEngineAttribute,
});
}
Copy link

Choose a reason for hiding this comment

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

The TableModel class is well-structured and provides a comprehensive representation of database table metadata. Ensure that all properties and methods are adequately documented, especially for complex or non-obvious functionality.

Consider adding detailed comments or documentation within the code to explain the purpose and usage of each property and method, especially for the build method, which performs significant logic to construct a TableModel instance.

@killagu
Copy link
Contributor Author

killagu commented Mar 21, 2024

@coderabbitai pause

Copy link

codecov bot commented Mar 21, 2024

Codecov Report

Attention: Patch coverage is 86.49068% with 174 lines in your changes are missing coverage. Please review.

Project coverage is 92.20%. Comparing base (01d9061) to head (ef6fc91).

Files Patch % Lines
core/dal-runtime/src/SqlUtil.ts 45.45% 72 Missing ⚠️
core/dal-runtime/src/SqlGenerator.ts 82.04% 44 Missing ⚠️
core/dal-runtime/src/BaseSqlMap.ts 84.00% 16 Missing ⚠️
core/dal-runtime/src/DataSource.ts 65.11% 15 Missing ⚠️
core/dal-decorator/src/type/Spatial.ts 55.00% 9 Missing ⚠️
plugin/dal/lib/DataSource.ts 85.45% 8 Missing ⚠️
core/dal-decorator/src/util/TableInfoUtil.ts 87.50% 2 Missing ⚠️
core/dal-runtime/src/SqlMapLoader.ts 88.23% 2 Missing ⚠️
core/dal-runtime/src/TemplateUtil.ts 96.36% 2 Missing ⚠️
plugin/dal/lib/DalModuleLoadUnitHook.ts 85.71% 2 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #192      +/-   ##
==========================================
- Coverage   94.02%   92.20%   -1.82%     
==========================================
  Files         236      276      +40     
  Lines        5036     6478    +1442     
  Branches      545      969     +424     
==========================================
+ Hits         4735     5973    +1238     
- Misses        301      505     +204     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fengmk2
Copy link
Member

fengmk2 commented Mar 21, 2024

+1

@fengmk2 fengmk2 merged commit 1c7d145 into master Mar 21, 2024
11 of 13 checks passed
@fengmk2 fengmk2 deleted the feat/dal branch March 21, 2024 11:49
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.

2 participants