mongoose-snowflake-id
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Snowflake ID auto-generating plugin

NPM info

Discord: https://discord.gg/TfG5hep

Usage

Once you have the plugin installed it is very simple to use. Just pass mongoose-snowflake-id to the plugin() function on your schema.

import snowflakeId from "mongoose-snowflake-id";

const BookSchema = new mongoose.Schema({
    title: String,
    genre: String,
    publishDate: Date,
});

BookSchema.plugin(snowflakeId);

This plugin will create a new field with name snowflakeId

Want a field other than snowflakeId or replace _id?

BookSchema.plugin(snowflakeId, {
    field: "_id",
});

Options

BookSchema.plugin(snowflakeId, {
    field: "snowflakeId", // Defaults to snowflakeId
    customEpoch: 1546300800000, // Defaults to the current time. This is UNIX timestamp in ms
    machineId: 112, // A value ranging between 0 - 4095. Defaults to 112
    type: true, // Defaults to true. If set to false, defaults to number
});

Configuration

  • field: string; // Defaults to snowflakeId

UniqueID constructor takes in the following configuration

  • customEpoch: number; // Defaults to the current time. This is UNIX timestamp in ms, EX: 1546300800000 (01-01-2019) , (config for SnowflakeId)
  • machineId: number; // A value ranging between 0 - 4095. Defaults to 112 (config for SnowflakeId)
  • type: Boolean; // Defaults to true. If set to false, defaults to number (type of field)

Note

This note copy from https://github.com/utkarsh-pro/nodejs-snowflake

Snowflake ID auto-generating is a fast and reliable way to generate time sortable 64-bit ids written for distributed systems. The main id generation function is written in C++ using N-API which makes the process of id generation extremely fast. The usage of C++ for id generation also guarantees that the generated number will be of size 64 bits.

Dependencies (2)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i mongoose-snowflake-id

    Weekly Downloads

    1

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    13.5 kB

    Total Files

    8

    Last publish

    Collaborators

    • ds112