Add TypeScript Generics to Datastore for Type-Safe Document Operations#9
Merged
Conversation
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.
xh-lin
added a commit
that referenced
this pull request
Mar 8, 2026
* Set up repo for dev (#3) * gitignore package-lock.json * nvm use node ver 14 * vscode settings tab size 2 * fix persistence.test.js not picked up by jest * fix persistence.test.js * format on save * Bump async in the npm_and_yarn group across 1 directory (#2) Bumps the npm_and_yarn group with 1 update in the / directory: [async](https://github.com/caolan/async). Updates `async` from 2.6.2 to 2.6.4 - [Release notes](https://github.com/caolan/async/releases) - [Changelog](https://github.com/caolan/async/blob/v2.6.4/CHANGELOG.md) - [Commits](caolan/async@v2.6.2...v2.6.4) --- updated-dependencies: - dependency-name: async dependency-version: 2.6.4 dependency-type: direct:production dependency-group: npm_and_yarn ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * run test pre commit * fix warnings in tsconfig.json * Configure Dependabot for npm updates Updated package ecosystem to 'npm' and added commit message prefix. * remove util * replace binary-search-tree with @seald-io/binary-search-tree * bump mocha version to latest 11.7.5 to fix vulnerabilities * package.json add node 14 * package.json "test-db" script use npx mocha * Add TypeScript Generics to Datastore for Type-Safe Document Operations (#9) * Add TypeScript Generics to Datastore for Type-Safe Document Operations 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. * updateAsync return number instead of document --------- Co-authored-by: Marcel Friedrich <marcel@smartwod.app> * index.d.ts format * index.d.ts improving typing * improve pre-commit script * no longer need to use node 14 * fix vulnerability with "ip" from "react-native" * single quote * bump packages and remove unused package sinon * correct the type signatures in index.d.ts * use type signatures in persistence.test.js * MongoDocument add _id property * MongoDocument _id optional * update typing - callback result can be undefined if error. findOne result can be null if not found * add .npmignore * fix tests and move config/ to test/fixtures/ * upgrade packages events and underscore * version 4.0.1 * contributors add xh-lin --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Marcel Friedrich <marcel@smartwod.app>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
antoniopresto#85