npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-binary-uuid

v1.0.1

Published

UUID schema type for mongoose

Downloads

27

Readme

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