Git Product home page Git Product logo

flakeid53's Introduction

Flake ID 53

Simple module that aims to provide snowflake alike ID generator that fits into 53bit number as specificed on IEEE_754.

As being limited to 53bit number, there is some limitations:

  • Timespan is limited to 28 years for given epoch
  • Machine ID is limited from 0..9
  • Sequence is limited to 000..999

Format:

<Timestamp><Worker ID><Sequence>

Example:

8470564087028

  • 847056408 - timespan
  • 7 - worker ID
  • 028 - sequence number

Installation

npm install flakeid53

Usage

Initialize

Options

epoch: Number Time to start the epoch of the snowflake generation, it will be used as substract for given current time, to produce the first 12 digits.

workerId: Number 0-9 range machine or worker ID. It is usefull in case of usage on distributed systems.

// Use as node module

const flakeId = require("flakeid53")({
    epoch: +new Date("2021-03-03"),
    workerId: 2,
});
// Use as ES module

import createFlakeID53 from "flakeid53";

const flakeId = createFlakeID53({
    epoch: +new Date("2021-03-03"),
    workerId: 2,
});
// Use in Deno

import createFlakeID53 from "https://cdn.jsdelivr.net/npm/flakeid53/index.js"

const flakeId = createFlakeID53({
    epoch: +new Date("2021-03-03"),
    workerId: 2,
});

Generate ID

nextId() returns a promise that resolve to next ID number.

await flakeId.nextId();

// Outputs: 8470564087028

Parse given ID produced by generator

flakeId.parse(8470564087028);

// Outputs: { time: 2021-03-12T19:17:36.408Z, workerId: 7, sequence: 28 }

Why this module?

In my specific case, using snowflake ID or some other 64bit alternative is not an option.

flakeid53's People

Contributors

dimitrov-adrian avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.