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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@datacapy/id

v0.1.0

Published

Datacapy id: object id type and helpers

Downloads

192

Readme

@datacapy/id

Short, time-ordered string IDs for relational databases.

@datacapy/id generates a 12-byte value (the same size as a MongoDB ObjectId) and encodes it in Base62, so the ID is 15 alphanumeric characters instead of 24 hex characters. It has no runtime dependencies.

import genUniqueId from "@datacapy/id";

genUniqueId(); // 'ecrDWdCQCiXSrf6'

Why

  • Compact. 15 characters against 24 for hex, and against 36 for a UUID.
  • URL and index friendly. Only 0-9, A-Z and a-z. No hyphens, no escaping.
  • Roughly time-ordered. New IDs sort after older ones, which keeps B-tree primary keys append-mostly.
  • Lossless conversion to and from a 24-character hex ObjectId-style string, so data can move between the two forms.

Requirements

  • BigInt support (ES2020).
  • globalThis.crypto.getRandomValues: Node 19 or later, or any current browser.

Usage

import genUniqueId, { uniqueIdToBsonId, bsonIdToUniqueId } from "@datacapy/id";

const id = genUniqueId(); // 'ecrDWdCQCiXSrf6'

uniqueIdToBsonId("ecrDWdCQCiXSrf6"); // '01a0c50600995e64661d1978'
bsonIdToUniqueId("01a0c50600995e64661d1978"); // 'ecrDWdCQCiXSrf6'

The default export is genUniqueId, so import { genUniqueId } from '@datacapy/id' is equivalent.

API

| Export | Description | | -------------------------------- | ------------------------------------------------------------------------------ | | genUniqueId() (default export) | New Base62 ID | | genBsonId() | New 24-character lowercase hex string, before Base62 encoding | | uniqueIdToBsonId(id) | Base62 ID to 24-character hex string | | bsonIdToUniqueId(hex) | 24-character hex string to Base62 ID | | base62Encode(n: bigint) | Encode a non-negative BigInt | | base62Decode(s) | Decode a Base62 string to a BigInt. Throws on a character outside the alphabet | | base62EncodeBsonId(hex) | Hex string to Base62, same as bsonIdToUniqueId | | base62DecodeBsonId(s) | Base62 to a 24-character hex string, same as uniqueIdToBsonId | | hexToBigInt(hex) | Hex string to BigInt | | bigIntToHex(n, byteLength) | BigInt to hex, left-padded to byteLength bytes |

Encoding a BigInt

The alphabet is 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz, in that order.

import { base62Encode, base62Decode } from "@datacapy/id";

base62Encode(0n); // '0'
base62Encode(61n); // 'z'
base62Encode(62n); // '10'
base62Decode("10"); // 62n
base62Decode("a-b"); // throws: Invalid character '-' in base62 string

ID structure

An ID is 12 bytes, encoded as Base62:

| Bytes | Content | | ----- | ---------------------------------------------------------------------- | | 0-5 | Creation time, milliseconds since the Unix epoch (48 bits, big-endian) | | 6-11 | 48 random bits from crypto.getRandomValues |

Consequences:

  • Length. IDs are 15 characters today. They become 16 characters in July 2056.
  • Ordering. The alphabet is in ASCII order and the timestamp is in the high bits, so IDs of equal length sort lexicographically by creation time. IDs generated within the same millisecond sort in random order relative to each other. There is no counter, so ordering within a millisecond is not guaranteed.
  • Uniqueness. Two IDs collide only if they share a millisecond and all 48 random bits. That is negligible for ordinary write rates, but the library does not check for it. Keep a unique index on the column.
  • Not an ObjectId. The size and hex representation match, but the layout does not. An ObjectId uses a 4-byte seconds timestamp, 5 random bytes and a 3-byte counter. Do not pass these IDs to code that parses the ObjectId fields.
  • Not secret. The timestamp is readable from the ID. Do not use it as a token or a link that must be hard to guess.

Related packages

  • @datacapy/schema uses it to default a String field named _id.
  • @datacapy/migrate uses it to generate the IDs of its metadata-table rows.