Add TypeScript Generics to Datastore for Type-Safe Document Operations #85
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR refactors the Datastore class in react-native-local-mongodb to support TypeScript generics, providing type safety for document operations. The key changes include:
The Datastore class is now generic, allowing users to specify the document type (T) when instantiating a datastore. This ensures that document operations such as insert, find, update, and remove are type-safe and adhere to the structure defined by the user. Default type for the generic T is set to MongoDocument, maintaining backward compatibility for cases where a document type is not explicitly provided. Updated methods like insertAsync, findAsync, updateAsync, removeAsync, etc. to use the generic T instead of the default MongoDocument, improving type inference and code completion in TypeScript. Benefits: This change enforces type safety for documents stored in the datastore, reducing potential runtime errors. It provides better code completion and static analysis, helping developers catch type errors earlier during development. The update maintains backward compatibility by defaulting to MongoDocument if no type is specified.
How to Test: Instantiate a datastore with a specific type for documents (for example, Workout). Perform document operations like insertAsync, findAsync, and updateAsync to verify that TypeScript enforces the correct structure for the documents. Ensure that existing usages of the Datastore class without generics still work as expected.
No breaking changes expected, and existing functionality remains intact with improved type safety.