Giter Club home page Giter Club logo

mongoose-uuid's Introduction

Mongoose UUID Data type

========================

NPM Package Build Status Coverage Status

Why

MongoDB supports storing UUID v1 and v4 in Binary format. Why not take advantage of that when using UUIDs?

What does it do?

This will add an additional UUID type to mongoose. When used instead of String, UUIDs will be stored in Binary format, which takes about half as much space.

This also makes it easy for you to continue to work with UUIDs as strings in your application. It automatically casts Strings to Binary, and when read a document from the database, and you access a property directly, you get the value back as a String.

New: Query population now works correctly! Updated with dependency on mongoose 5

How to use

var uuidv4 = require('uuid/v4');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Will add the UUID type to the Mongoose Schema types
require('mongoose-uuid2')(mongoose);
var UUID = mongoose.Types.UUID;

var ProductSchema = Schema({
  _id: { type: UUID, default: uuidv4 },
  name: String
}, { id: false });

var PhotoSchema = Schema({
  _id: { type: UUID, default: uuidv4 },
  filename: String,
  product: { type: UUID, ref: 'Product' }
}, { id: false });

ProductSchema.set('toObject', {getters: true});
ProductSchema.set('toJSON', {getters: true});

var Product = mongoose.model('Product', ProductSchema);

PhotoSchema.set('toObject', {getters: true});
PhotoSchema.set('toJSON', {getters: true});

var Photo = mongoose.model('Photo', PhotoSchema);

Example

> var product = new Product({ _id: '7c401d91-3852-4818-985d-7e7b79f771c3' });
> console.log(product);
{ _id:
   { _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 7c 40 1d 91 38 52 48 18 98 5d 7e 7b 79 f7 71 c3> } }

  console.log(product.toObject());
  { _id: "7c401d91-3852-4818-985d-7e7b79f771c3" }

> product._id = '48c53f87-21f6-4dee-92f2-f241f942285d';
> console.log(product);
{ _id:
   { _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 48 c5 3f 87 21 f6 4d ee 92 f2 f2 41 f9 42 28 5d> } }

> console.log(product._id);
48c53f87-21f6-4dee-92f2-f241f942285d

mongoose-uuid's People

Contributors

niahmiah avatar jubeiam avatar christiankuhn avatar teslitsky avatar maxkueng avatar thxmike avatar rubenbupe avatar savepointsam avatar simonmeusel 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.