Giter Club home page Giter Club logo

id's Introduction

@yaupon/id

  • @yaupon/id is a library that provides a simple way to generate unique identifiers.
  • Application provides two exports @yaupon/id/safe and @yaupon/id/fast, with @yaupon/id/safe being the default export.
    • @yaupon/id/safe is a safe implementation of unique identifier generation, which is type-safe by default ( prefixing id with safe generation algorithm) and the fast export is using nanoid to provide a 32-character ID which is there to be used on infrastructure level.
  • There are ready-to-use presets of IDs and aliases to Node.js functions which ensures compatibility with the generic ID type from @yaupon/id library, to avoid type-casting everytime ID is generated outside library.

@yaupon/id are unique to the place used and are not compatible one with another.

class User {
    id: ID
}

class Post {
    id: ID
}

const user = new User()
const post = new Post()

console.log(user.id === post.id) // compilation error

I come down to this idea just because some retarded coworker was trolling me on codebase with functions that take userId which is later used at ORM as productId - no such things possible with this approach.

function updatePost(postId: Post['id']) {
    // ...
}

"But Jay, Post["id"] makes me import Post everywhere where I just want to use ID type!", you say?

Overall recommendation is to use import type { Post } from "post.ts" and use Post["id"] where needed, send approach is creating a new type alias for ID type in your project.

import type { ID } from '@yaupon/id'

type PostID = ID // Or PostID = Post['id']
function updatePost(postId: PostID) {
    // ...
}

However, I find it more readable to use Post["id"] in this case.

"Why I would use this library instead of nanoid or uuid?"'

  • We did not write this library to compete with nanoid or uuid, but to provide a type-safe way to generate unique identifiers.
  • nanoid and uuid are great libraries, but they do not protect against ID misuse.
  • @yaupon/id is a library that provides a simple way to generate unique identifiers, which are unique to the place used and are not compatible one with another.

"But xyz library does the same thing!"

They had bad marketing, tags, description or something elseβ€”at the time I was building library there was only typeid from Jetpack which would provide similar functionality, container this package as fork of typeid.

Integrations

  • [TODO] Ahead-of-time compilation/validation with typia and helper function such as isID to ensure that ID is valid.
  • [TODO] Integration with ORM libraries to ensure that ID is always valid and is not used in wrong context.

id's People

Contributors

keinsell 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.