Skip to content

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

Merged
xh-lin merged 2 commits into
xh-lin:masterfrom
lecramfriedrich:patch-1
Jan 24, 2026
Merged

Add TypeScript Generics to Datastore for Type-Safe Document Operations#9
xh-lin merged 2 commits into
xh-lin:masterfrom
lecramfriedrich:patch-1

Conversation

@xh-lin

@xh-lin xh-lin commented Jan 24, 2026

Copy link
Copy Markdown
Owner

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 xh-lin changed the title Patch 1 Add TypeScript Generics to Datastore for Type-Safe Document Operations Jan 24, 2026
@xh-lin xh-lin merged commit fb30d62 into xh-lin:master Jan 24, 2026
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>
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