All Projects → cdimascio → uuid-mongodb

cdimascio / uuid-mongodb

Licence: MIT license
📇 Generates and parses MongoDB BSON UUIDs

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to uuid-mongodb

Nest User Auth
A starter build for a back end which implements managing users with MongoDB, Mongoose, NestJS, Passport-JWT, and GraphQL.
Stars: ✭ 145 (+54.26%)
Mutual labels:  mongo, mongoose
flatbson
Recursively flatten a Go struct using its BSON tags
Stars: ✭ 54 (-42.55%)
Mutual labels:  mongo, bson
Mern Passport
A boilerplate example of using passport.js for authenticating a MERN application
Stars: ✭ 214 (+127.66%)
Mutual labels:  mongo, mongoose
Mongoose Fuzzy Searching
Mongoose Fuzzy Searching Plugin
Stars: ✭ 94 (+0%)
Mutual labels:  mongo, mongoose
express-mongo-jwt-boilerplate
Express Mongo JsonWebToken boilerplate
Stars: ✭ 100 (+6.38%)
Mutual labels:  mongo, mongoose
Node Typescript Mongodb
node js typescript mongodb express generator yo
Stars: ✭ 96 (+2.13%)
Mutual labels:  mongo, mongoose
ark.db
Small and fast JSON database for Node and browser. 😋
Stars: ✭ 65 (-30.85%)
Mutual labels:  mongo, mongoose
Mean Angular4 Chat App
MEAN stack with Angular 4 Chat App
Stars: ✭ 41 (-56.38%)
Mutual labels:  mongo, mongoose
koa-session-mongoose
Mongoose store for Koa sessions
Stars: ✭ 29 (-69.15%)
Mutual labels:  mongo, mongoose
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (+123.4%)
Mutual labels:  mongo, mongoose
Lua Mongo
MongoDB Driver for Lua
Stars: ✭ 81 (-13.83%)
Mutual labels:  mongo, bson
nason
🗜 Ultra tiny serializer / encoder with plugin-support. Useful to build binary files containing images, strings, numbers and more!
Stars: ✭ 30 (-68.09%)
Mutual labels:  binary, bson
Avocado
Strongly-typed MongoDB driver for Rust
Stars: ✭ 70 (-25.53%)
Mutual labels:  mongo, bson
Intro Mongo Db
[Course] Introduction to MongoDB code
Stars: ✭ 114 (+21.28%)
Mutual labels:  mongo, mongoose
Mean Stack Angular6 Crud Example
MEAN Stack Angular 6 CRUD Web Application
Stars: ✭ 69 (-26.6%)
Mutual labels:  mongo, mongoose
nestjs-api-mongoose
Collection example apps with NestJS and Typeorm, Sequelize, Mongodb, PostgreSQL, MySQL, GraphQL, Mercurius, etc. for the NestJS community 😻
Stars: ✭ 153 (+62.77%)
Mutual labels:  mongo, mongoose
Spruce
A social networking platform made using Node.js and MongoDB
Stars: ✭ 399 (+324.47%)
Mutual labels:  mongo, mongoose
Kov Blog
A blog platform built with koa,vue and mongoose. 使用 koa ,vue 和 mongo 搭建的博客页面和支持markdown语法的博客编写平台,自动保存草稿。博客地址:https://chuckliu.me
Stars: ✭ 635 (+575.53%)
Mutual labels:  mongo, mongoose
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (-42.55%)
Mutual labels:  mongo, mongoose
mern-stack-crud
MERN stack (MongoDB, Express, React and Node.js) create read update and delete (CRUD) web application example
Stars: ✭ 142 (+51.06%)
Mutual labels:  mongo, mongoose

uuid-mongodb

Codacy Badge All Contributors

Generates and parses BSON UUIDs for use with MongoDB. BSON UUIDs provide better performance than their string counterparts.

Inspired by @srcagency's mongo-uuid

Install

npm install uuid-mongodb

Usage

const MUUID = require('uuid-mongodb');

# Create a v1 binary UUID
const mUUID1 = MUUID.v1();

# Create a v4 binary UUID
const mUUID4 = MUUID.v4();

# Print a string representation of a binary UUID
mUUID1.toString()

# Create a binary UUID from a valid uuid string
const mUUID2 = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459')

# Create a binary UUID from a MongoDb Binary
# This is useful to get MUUIDs helpful toString() method
const mUUID3 = MUUID.from(/** MongoDb Binary of SUBTYPE_UUID */)

Formatting

UUIDs may be formatted using the following options:

Format Description Example
N 32 digits 00000000000000000000000000000000
D 32 digits separated by hyphens 00000000-0000-0000-0000-000000000000
B 32 digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P 32 digits separated by hyphens, enclosed in parentheses (00000000-0000-0000-0000-000000000000)

example:

const mUUID4 = MUUID.v4();
mUUID1.toString(); // equivalent to `D` separated by hyphens
mUUID1.toString('P'); // enclosed in parens, separated by hypens
mUUID1.toString('B'); // enclosed in braces, separated by hyphens
mUUID1.toString('N'); // 32 digits

Modes

uuid-mongodb offers two modes:

  • canonical (default) - A string format that emphasizes type preservation at the expense of readability and interoperability.
  • relaxed - A string format that emphasizes readability and interoperability at the expense of type preservation.

The mode is set globally as such:

const mUUID = MUUID.mode('relaxed'); // use relaxed mode

Mode only impacts how JSON.stringify(...) represents a UUID:

e.g. JSON.stringy(mUUID.v1()) outputs the following:

"DEol4JenEeqVKusA+dzMMA==" // when in 'canonical' mode
"1ac34980-97a7-11ea-8bab-b5327b548666" // when in 'relaxed' mode

Examples

Query using binary UUIDs

const uuid = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459');
return collection.then(c =>
  c.findOne({
    _id: uuid,
  })
);

Work with binary UUIDs returned in query results

return collection
  .then(c => c.findOne({ _id: uuid }))
  .then(doc => {
    const uuid = MUUID.from(doc._id).toString();
    // do stuff
  });

Examples (with source code)

Native Node MongoDB Driver example

  • examples/ex1-mongodb.js

    snippet:

     const insertResult = await collection.insertOne({
       _id: MUUID.v1(),
       name: 'carmine',
     });

Mongoose example

  • examples/ex2-mongoose.js

    snippet:

     const kittySchema = new mongoose.Schema({
       _id: {
         type: 'object',
         value: { type: 'Buffer' },
         default: () => MUUID.v1(),
       },
       title: String,
     });
  • examples/ex3-mongoose.js

    snippet:

     // Define a simple schema
     const kittySchema = new mongoose.Schema({
       _id: {
         type: 'object',
         value: { type: 'Buffer' },
         default: () => MUUID.v1(),
       },
       title: String,
     });
     
     // no need for auto getter for _id will add a virtual later
     kittySchema.set('id', false);
     
     // virtual getter for custom _id
     kittySchema
       .virtual('id')
       .get(function() {
         return MUUID.from(this._id).toString();
       })
       .set(function(val) {
         this._id = MUUID.from(val);
       });
  • examples/ex4-mongoose.js

    const uuid = MUUID.v4();

    // save record and wait for it to commit
    await new Data({ uuid }).save();

    // retrieve the record
    const result = await Data.findOne({ uuid });

Notes

Currently supports UUID v1 and v4

Contributors

Thanks goes to these wonderful people (emoji key):

Carmine DiMascio
Carmine DiMascio

💻
Benjamin Dobell
Benjamin Dobell

💻
David Pfeffer
David Pfeffer

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT

Buy Me A Coffee

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