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

Add TypeScript Generics to Datastore for Type-Safe Document Operations #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lecramfriedrich
Copy link

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.

const workoutDb = new Datastore<Workout>();

No breaking changes expected, and existing functionality remains intact with improved type safety.

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.

const workoutDb = new Datastore<Workout>();

No breaking changes expected, and existing functionality remains intact with improved type safety.
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.

1 participant