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

assai

v1.1.1

Published

A simple mongodb wrapper to make mongo even easier to work with.

Downloads

63

Readme

This is a small package to improve some things that you, as a developer, have to deal with when working with mongo, like:

Example

import { createMongoCollection } from "assai"

const collection = await createMongoCollection()
const docs = await collection.find({}, {limit: 10})
/**
 * [{id: "507f1f77bcf86cd799439011", name: "Mario"}, ...]
 */

_id

Ever wanted to use just "id" in your collections instead of "_id"?

This package does just that!

Every data that enters the database can, optionally, have an "id". In this case, before sending the data to the native mongodb driver, the object will rename this property to "_id", which mongodb understands. This operation will be applied to insertOne and insertMany methods.

Also, the methods updateOne, updateMany, deleteOne, deleteMany, findOne and find will also rename the field "id" to "_id".

ObjectId

Another thing that is related to "_id" fields are the ObjectIds.

The issue is that your application can before unnecessarily verbose. To fix that, the package will automatically convert all objectId strings into a ObjectId under the hood and all objectIds that will come from your collection, will be converted to strings.

await collection.insertOne({
    name: "Matteo",
    groupId: "507f1f77bcf86cd799439011" // This will be stored as an ObjectId
})

Every time you need a new id, you can call the generateNewId method:

import { mongo } from 'assai'

const myStringId = mongo.generateNewId()

One example this could be useful is if have an API endpoint that accepts structured data. And you use these values to query the database. Like so:

// Client code
const response = await axios.post('/posts', {
    userId: "507f1f77bcf86cd799439011"
})

This is fine, but you will have to convert a string into a ObjectId for each field value that is an objectId in your collection. Even though this is easy to do, you could forget somewhere.

Instead of carrying this risk, you can use the object as-is and the conversion will be made automatically.

projection

The projection from the native mongodb driver is fine as it is. But there is one thing that is annoying: it can cause your program to fail.

To be honest, this behavior makes some sense because this usually comes from a mistake the developer made. But this is not always the case and it goes against how mongodb and javascript in general behave: they avoid throwing an exception when possible.

For that reason, you won't see this error while using this package:

Cannot do exclusion on field '...' in inclusion projection

Making projections like that valid:

{
    "name": true,
    "createdAt": false
}

Connection String

A default environment variable is assumed: DATABASE_URL.

Which makes it easier to start a connection:

const database = await createMongoCollection('myCollection')

This will read the value from process.env.DATABASE_URL.

You can still pass a custom connection string:

const database = await createMongoCollection('myCollection', {
    connectionString: 'my connection string',
})

Client Instance

If you ever worked with serverless, you will notice that you shouldn't open and close a connection everytime your function runs. You need to cache it. The package does this caching for you by default.

You could also do this for simplicity, so instead of:

// db.js
let cachedClient = null
export async function getDb () {
    if (cachedClient == null) {
        const client = await MongoClient.connect('...')
        cachedClient = client
    }
    return cachedClient.db()
}

// router.js
import { getDb } from 'db.js'

router.post('/', (req, res) => {
    const db = await getDb()
    const col = db.collection('myCollection')
    await col.insertOne({
        name: req.body.name,
    })
})

You can simply write:

router.post('/', (req, res) => {
    const col = createMongoCollection('myCollection')
    // Here the connection is opened only if it is not opened already.
    // Further calls to this route won't open a connection.
    await col.insertOne({
        name: req.body.name,
    })
    // ...
})

Remarks

This project can contain bugs and should not be used in production applications.

If you do find a bug, please report it.