npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tianhuil/ts-mongo

v2.5.1

Published

[![Node.js CI Lint](https://github.com/tianhuil/ts-mongo/actions/workflows/test.yaml/badge.svg)](https://github.com/tianhuil/ts-mongo/actions/workflows/test.yaml)

Downloads

179

Readme

TS Mongo

Node.js CI Lint

Strongly-typed Mongo native driver.

Getting Started

To install, run one of the following

npm install @tianhuil/ts-mongo
yarn add @tianhuil/ts-mongo
pnpm add @tianhuil/ts-mongo

Philosophy

Mongodb's node-native driver and mongoose both provide poor typescript support. They need to support every query and update that is allowed by the spec, which is difficult to encapsulate in typescript. The result is promiscuous typesafety that allows even unsafe queries and input values to pass typecheck.

We re-type the node-native driver to provide uptight type-safety. We choose to have type-safety disallow queries that are hard to type or which we deem poor practice. While there are plenty of valid mongo queries our type checking disallows, our aim is to minimize bad queries that pass type checking.

Getting Started

To create a type-safe drop-in replacement

import { mkTsCollection } from '@tianhuil/ts-mongo'

const collection = mkTsCollection<TSchema>(db, 'collection-name')
const result = await collection.findOne(...) // now with better type safety
const unsafeResult = await collection.unsafe.findOne(...) // with old types

Example

Assume you have a collection type of

{ admin: { name: string; email: string }[] }

The mongo native driver allows queries of the form

{ `admin.2.name`: 'Joe' }

to select the second value in the array admin. In the native driver, this is accomplished via type template-literal typing

{ [x: `admin.${number}.name`]?: string }

Unfortunately, this is overly promiscuous and allows any field, i.e. this query type checks:

{ 'not-a-field': 'bad query!' }

By default, we only allow you to select the 0'th element. This solves the problem template literal problem.

  • As a concession, Filter takes an optional second argument (defaults to 0), which can be made number for those seeking the original promiscuous behavior.
  • In general, we believe it's not typesafe to select arbitrarily into an array so allowing an arbitrary number is probably not great programming practice.

Converter

Many middleware functions are handled by the converter, which is like a type-safe middleware that can transform the type of the collection. For example, convertToTimeCollection implements createdAt and updatedAt fields on a collection. See convertReadWriteCollection.

How to fix bugs in TsMongo

When using the code, you may notice mistakes in TsMongo's typing behavior, use the corresponding assert file (which is like unit testing for types) and add an assert case to cover your newly-discovered bug or new feature. For example:

  1. A bug in .sort will be in the type TsSort.
  2. Build a test case in the assert file sort.assert.ts.
  3. Then investigate the original file sort.ts and hack things until you pass testing. Red underlines appear in VSCode to tell you if the typing doesn't work.

Development

To use a local version of ts-mongo you are developing while building another app (let's call it foo), navigate to foo and run the following command (assume the two folders are siblings)

cd ./foo
npm link ../ts-mongo
npm install # install and @tianhuil/ts-mongo will point to local version at ../ts-mongo

When you

Release

To release, run the following to check (preferably from the main branch):

# commit all your changes
pnpm i
npm run build # runs checks on prebuild and builds package (must use npm)
pnpm np --no-tests --any-branch --preview # make sure this is what you want
pnpm np --no-tests # deploy