All Projects → lvkdotsh → sunflake

lvkdotsh / sunflake

Licence: other
Zero dependency, lightweight, snowflake generator

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to sunflake

Id Generator
id-generator部署即使用的ID生成器, 支持HTTP、Dubbo、Spring Cloud方式.
Stars: ✭ 112 (+558.82%)
Mutual labels:  snowflake
Sqitch
Sensible database change management
Stars: ✭ 2,320 (+13547.06%)
Mutual labels:  snowflake
DBTestCompare
Application to compare results of two SQL queries
Stars: ✭ 15 (-11.76%)
Mutual labels:  snowflake
Go Katsubushi
ID generator server
Stars: ✭ 118 (+594.12%)
Mutual labels:  snowflake
Snowflakes
❄️ Snowflakes in JavaScript
Stars: ✭ 170 (+900%)
Mutual labels:  snowflake
DiscordLookup
DiscordLookup | Get more out of Discord with Discord Lookup! Snowflake Decoder, Guild List with Stats, Invite Info and more...
Stars: ✭ 86 (+405.88%)
Mutual labels:  snowflake
Snow Stamp
Get the timestamp from a Discord snowflake ❄
Stars: ✭ 95 (+458.82%)
Mutual labels:  snowflake
go-snowflake
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).
Stars: ✭ 206 (+1111.76%)
Mutual labels:  snowflake
Idworker
idworker 是一个基于zookeeper和snowflake算法的分布式ID生成工具,通过zookeeper自动注册机器(最多1024台),无需手动指定workerId和datacenterId
Stars: ✭ 171 (+905.88%)
Mutual labels:  snowflake
Excelerator
This is an Excel Addin for Windows that reads and writes data to Snowflake
Stars: ✭ 53 (+211.76%)
Mutual labels:  snowflake
Did
高性能的ID生成器, 基于rpcx和Memcached协议提供网络服务调用
Stars: ✭ 120 (+605.88%)
Mutual labels:  snowflake
Terraform Provider Snowflake
Terraform provider for managing Snowflake accounts
Stars: ✭ 165 (+870.59%)
Mutual labels:  snowflake
onionfruit
OnionFruit™ Connect - Tor access client with country selection, bridge configuration, pluggable transports and experimental DNS support
Stars: ✭ 150 (+782.35%)
Mutual labels:  snowflake
Snowflake
java edition of [Twitter Snowflake](https://github.com/twitter/snowflake), a network service for generating unique ID numbers at high scale with some simple guarantees.
Stars: ✭ 114 (+570.59%)
Mutual labels:  snowflake
snowflake
a language
Stars: ✭ 16 (-5.88%)
Mutual labels:  snowflake
Gosnowflake
Go Snowflake Driver
Stars: ✭ 108 (+535.29%)
Mutual labels:  snowflake
Fluentmigrator
Fluent migrations framework for .NET
Stars: ✭ 2,636 (+15405.88%)
Mutual labels:  snowflake
laravel-snowflake
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
Stars: ✭ 94 (+452.94%)
Mutual labels:  snowflake
snowflake
Yet another snowflake
Stars: ✭ 22 (+29.41%)
Mutual labels:  snowflake
sno
Compact, sortable and fast unique IDs with embedded metadata.
Stars: ✭ 77 (+352.94%)
Mutual labels:  snowflake

lvksh sunflake

MINIFIED SIZE COVERAGE LANGUAGES DEPENDENCIRES NPM

Zero dependency, lightweight, snowflake generator.

This library follows the Twitter Snowflake ID specification, which is currently what is utilized by platforms such as Discord, Twitter, Instagram and Blizzard just to name a few.

How it works

Sunflake takes a 42 bit unix timestamp, 10 bits of machine id and 12 bits of sequence number. Sunflake generates id in string format (which easily can be casted into a 64 bit bigint in databases), as javascript is limited to 53 bit integer precision.

111111111111111111111111111111111111111111 1111111111 111111111111
64 22 12 0

FIELD BITS DESCRIPTION RETRIEVAL
Timestamp 42 Milliseconds since given Epoch (snowflake >> 22) + epoch
Machine Id 10 (snowflake & 0x3FF000) >> 12
Increment 12 Increments for every id created
in the same timestamp.
snowflake & 0xFFF
  • (snowflake >> 22) + epoch: We right shift with 22 (64 - 42). This grabs the 42 first bits which is the time since the epoch. We then add the epoch to it and we have a unix timestamp in milliseconds.
  • (snowflake & 0x3FF000) >> 12: We start by offset 0x3FF000, which in binary is 0b1111111111000000000000 which is 22 bits long. This extracts the 10 bits after the 12 bits from the right, and then we remove those 12 bits from the right that we don't need. This is our Machine Id.
  • snowflake & 0xFFF: Grabs everything after the offset 0xFFF which is 111111111111 in binary which is 12 bits long. We start at the 12th bit (start of increament).

Installation

Using npm:

npm install sunflake

or if you prefer to use the yarn package manager:

yarn add sunflake

Usage

import { generateSunflake } from 'sunflake';

export const SnowflakeGen = generateSunflake({
    machineId: 1, // Machine id
    epoch: 1640995200000, // January 1st, 2022
});

Now you can use that function easily

const id1 = SnowflakeGen();
const id2 = SnowflakeGen();

Contributors

LICENSE

This package is licensed under the GNU Lesser General Public License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].