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

fix: Dojo store add entity #349

Merged
merged 3 commits into from
Dec 9, 2024
Merged

Conversation

rsodre
Copy link
Contributor

@rsodre rsodre commented Dec 3, 2024

Introduced changes

zustand Dojo store updateEntity() can add new entities.

Checklist

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Add a dedicated CI job for new examples
  • Performed self-review of the code

Summary by CodeRabbit

  • New Features
    • Enhanced entity management functionality, allowing new entities to be added when entityId does not exist.
  • Bug Fixes
    • Improved logic for updating entities, ensuring clearer handling of both existing and new entities.
  • Tests
    • Expanded test coverage for entity management, including comprehensive checks for getEntities() and getEntitiesByModel.
  • Chores
    • Removed unnecessary console logging in the query conversion function to streamline execution.

Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The pull request includes modifications to the @dojoengine/sdk package, specifically in the package.json, where two dependencies were removed. Additionally, changes were made to the updateEntity method in the Zustand store, streamlining its logic and improving readability. The test suite for the createDojoStore function was also updated to reflect changes in entity management, including the addition of new assertions and a renaming of a test case to align with the updated functionality.

Changes

File Change Summary
packages/sdk/package.json Removed dependencies: @dojoengine/core, @dojoengine/create-burner.
packages/sdk/src/tests/state.test.ts Updated test for updateEntity: renamed test case and added assertions for entity management.
packages/sdk/src/state/zustand.ts Modified updateEntity method: streamlined logic, improved readability, and preserved original functionality.
packages/sdk/src/convertQueryToEntityKeyClauses.ts Removed console logging from createClauseFromWhere function.

Possibly related PRs

Suggested reviewers

  • ponderingdemocritus

🐰 "In the code where entities play,
We've tidied up, come what may.
With tests refined and logic clear,
Our store now sings, oh so dear!
Dependencies lost, but we stand tall,
In the world of code, we’ll not fall!" 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a2dde2d and 090d2d6.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (2)
  • packages/sdk/package.json (0 hunks)
  • packages/sdk/src/convertQueryToEntityKeyClauses.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • packages/sdk/package.json
  • packages/sdk/src/convertQueryToEntityKeyClauses.ts

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using 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 do a full 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 configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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

@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: 0

🧹 Outside diff range and nitpick comments (2)
packages/sdk/src/state/zustand.ts (1)

97-103: Consider using Object.assign with multiple sources.

The nested Object.assign calls can be simplified for better readability.

-                                        ] = Object.assign(
-                                            {},
-                                            mergedModels[
-                                                typedNamespace as keyof typeof mergedModels
-                                            ],
-                                            namespaceModels
-                                        );
+                                        ] = Object.assign({},
+                                            mergedModels[typedNamespace as keyof typeof mergedModels],
+                                            namespaceModels);
packages/sdk/src/__tests__/state.test.ts (1)

107-107: Consider adding edge case tests.

While the basic functionality is well tested, consider adding tests for:

  1. Adding an entity with missing required fields
  2. Adding an entity with invalid model structure
  3. Concurrent updates to the same entity

Also applies to: 128-128, 148-148

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between db5ddfa and a2dde2d.

📒 Files selected for processing (3)
  • packages/sdk/package.json (0 hunks)
  • packages/sdk/src/__tests__/state.test.ts (3 hunks)
  • packages/sdk/src/state/zustand.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/sdk/package.json
🔇 Additional comments (2)
packages/sdk/src/state/zustand.ts (1)

75-116: Verify type safety and required fields validation.

While the implementation correctly handles both updating and adding entities, consider adding validation to ensure:

  1. New entities conform to the expected schema type
  2. Required fields are present when adding new entities
✅ Verification successful

Based on the code analysis, I can now provide a final response:

Type safety is handled through TypeScript's static type system

The implementation is already type-safe through TypeScript's static typing:

  • ParsedEntity<T> type ensures entities conform to the schema type
  • WritableDraft<ParsedEntity<T>> type is used for new entities
  • Type validation for models and namespaces is enforced through TypeScript's generics

Required fields validation is implicitly handled:

  • Entity ID is required through the type system (entity.entityId)
  • Models object is required through the type system (entity.models)
  • Additional validation is not necessary as the store is an internal implementation detail, with data already validated at the SDK level
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for type validation patterns in the codebase
rg -A 3 "type.*validation|schema.*validation|required.*fields"

# Look for similar entity validation patterns
ast-grep --pattern 'if ($entity && $entity.models) {
  $$$
}'

Length of output: 128


Script:

#!/bin/bash
# Let's check for type validation patterns more broadly
rg -A 3 "typeof|instanceof|type.*check"

# Look for entity validation or type checking in the same file
fd "zustand.ts" --exec cat {}

# Check for type definitions related to entities
ast-grep --pattern 'type ParsedEntity'

Length of output: 58614


Script:

#!/bin/bash
# Let's check the ParsedEntity type definition and related type validations
fd "types.ts" --exec cat {}

# Look for schema validation patterns in the codebase
rg -A 3 "schema.*validation|validateSchema|checkSchema"

# Check for any type guards or validation functions
ast-grep --pattern 'function is$_($entity) {
  $$$
}'

Length of output: 10974

packages/sdk/src/__tests__/state.test.ts (1)

Line range hint 134-151: LGTM! Test coverage properly validates the new functionality.

The test case effectively verifies that:

  1. New entities can be added via updateEntity
  2. The entity count increases correctly
  3. The added entity has the expected structure and data

@MartianGreed MartianGreed merged commit 4335585 into dojoengine:main Dec 9, 2024
2 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jan 24, 2025
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