24.1.3 • Published 7 days ago

@cloudize/uuid-mongodb v24.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

uuid-mongodb

npm.io Codacy Badge npm.io

This is package is a fork of the uuid-mongodb package created by Carmine DiMascio, Benjamin Dobell & David Pfeffer with some minor contributions from Erich Kuba of Cloudize Limited. The purpose of the fork is to provide a package that aligns with the release cadence and dependencies of the API Games Framework.

Install

$ npm i @cloudize/uuid-mongodb 

Usage

Please see https://github.com/cdimascio/uuid-mongodb for usage notes.

Authors or Acknowledgments

// 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:

FormatDescriptionExample
N32 digits00000000000000000000000000000000
D32 digits separated by hyphens00000000-0000-0000-0000-000000000000
B32 digits separated by hyphens, enclosed in braces{00000000-0000-0000-0000-000000000000}
P32 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.stringify(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:**
    
    	```javascript
    	const insertResult = await collection.insertOne({
    	  _id: MUUID.v1(),
    	  name: 'carmine',
    	});
    	```

Mongoose example

  • examples/ex2-mongoose.js

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

    	**snippet:**
    
    	```javascript
    	// 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

License

This project is licensed under the MIT License

24.1.3

7 days ago

24.1.2

1 month ago

24.1.1

2 months ago

23.3.5

3 months ago

24.1.0

3 months ago

23.3.3

7 months ago

23.3.2

7 months ago

23.3.4

6 months ago

23.2.4

11 months ago

23.2.3

11 months ago

23.2.2

1 year ago

23.2.1

1 year ago

23.1.5

1 year ago

23.1.4

1 year ago

23.1.3

1 year ago

23.1.0

1 year ago

22.2.4

2 years ago

22.2.3

2 years ago

22.2.2

2 years ago

22.2.0

2 years ago

22.1.17

2 years ago

22.1.14

2 years ago

22.1.13

2 years ago

22.1.12

2 years ago