Skip to content

3imed-jaberi/Short-ObjectId

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

86 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Short ObjectId ๐Ÿ—œ


Build Status ย ย  Coverage Status ย ย  NPM version ย ย  License ย ย  Top Language ย ย  Code Size ย ย  Code of Conduct PRs Welcome ย ย 

The idea of this module is make your route easier โšก when you work with mongodb object-ids ๐Ÿ—œ๐Ÿš€ ..

Warning โš ๏ธ: Previous versions are deprecated. For that the first release is 5.0.0-rc ๐Ÿ‘ป.

Installation

# npm ..
$ npm install short-objectid
# yarn ..
$ yarn add short-objectid

API

Caution ๐Ÿšจ: ย  Be careful when you use shortObjectIdCache. All cache hooks are not stable outside the function.

  • shortObjectId โ€” helps you to compress your ObjectId and make it short.
  • shortObjectIdCache โ€” A predictable cache container instance, helps you
    to go and return between the shortObjetId number and the original ObjectId
    and subscribe to each shortObjectId (func) call.

When you pass cache: true inside the options object, you don't need to use
shortObjectIdCache because it works on the background.

Usage

This is a practical example of how to use.

  // you can also use the desctructure way .. 
  const shortObjectId = require ('short-objectid');

  // objectId <required> ..  
  let mongodbObjectId = { "$oid" : "507f191e810c19729de860ea" }; 

  // config object <optional> .. 
  let config = { Timestamp: 57, MachineId: 65, ProcessId: 48, Counter: 47 }; 

  // options object <optional> .. 
  // you can pass one of these options [cache, keepOriginal] ..
  // cache option, informs the shortObjectId function that you 
  // should subscribe to each call and then store all data ..
  let opts = { cache: true }; 
  // Now you have access to 2 hooks (only with cache opts)
  // + getShortObjectId(fullObjectId: string): number
  // + getFullObjectId(shortObjectId: number): string
  const { shortObjectId, getShortObjectId, getFullObjectId } = shortObjectId(mongodbObjectId, config, opts);

  // Result: 
  //  console.log(`*****\n ${shortObjectId} \n*****`);
  //  Treminal
  //  $your_pc_name_with_your_directory
  //  *****
  //  532860
  //  *****

You can play around with it on this sandbox example codesandbox.io/short-objectid using Express JS and Mongoose ๐Ÿ™Œ๐Ÿป.

License


MIT

About

The idea of this project is make your details route easier using mongodb object-ids ๐Ÿš€ ..

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published