GithubHelp home page GithubHelp 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

christiankuhn avatar jubeiam avatar maxkueng avatar niahmiah avatar savepointsam avatar simonmeusel avatar thxmike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mongoose-uuid's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Once you have installed CI on this repository, you’ll need to re-trigger Greenkeeper’s initial Pull Request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Will this work for a model.find

I like the idea of adding this to my schema and replacing the _id.

However, I am at a loss on how would a model.find, model.findbyid or model.findone would work in this case?

I have tried different casts but can't find the right combination.

Requiring package gives me an error.

This line of code: require('mongoose-uuid2')(mongoose);

Gives me this error: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type function.

Not able to run my program.

Problems with .findById() and .Populate() with mongoose 4.5.9

mongo: 3.2.8
mongoose: 4.5.9

I'm trying to use the mongoose-uuid2 package for primary keys in my database and here is my schema:

require('mongoose-uuid2').loadType(mongoose);
const UUID = mongoose.Types.UUID;

...

  let emailSchema = new Schema({
    _id: { type: UUID, default: uuid.v4 },
    email: { type: String, required: true, lowercase: true, trim: true, unique: true },
    confirmed: { type: Date },
    _owner: { type: UUID, ref: 'User' }
  }, {
    safe: true,
    strict: true,
    timestamps: true
  });

  let userSchema = new Schema({
    _id: { type: UUID, default: uuid.v4 },
    first_name: { type: String },
    last_name: { type: String },
    password: { type: String, required: true },
    _primary_email: { type: UUID, ref: 'Email' }
  }, {
    safe: true,
    strict: true,
    timestamps: true
  });

I managed to populate references saving id directly and I can see all correct entries in my database. However, if I do something like this:

models.User.findById(userId).exec().then(function(userModel) {
    console.log(userModel);
});

I can see this output:

{ _id: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 22 12 2d 07 91 ed 4c 4a 8a 41 03 da 59 a9 e1 2c> },
  updatedAt: 2016-08-25T18:23:27.103Z,
  createdAt: 2016-08-25T18:23:24.962Z,
  password: '$2a$10$ZSjEv98fMYsmQ6NKA2YrBetaUSaoNJ2q3Q5mAL8g/j7hHXH0XXG7G',
  first_name: 'Joe',
  last_name: null,
  _primary_email: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer c2 6d 91 b3 29 f1 4c 3c 8f d8 9e 1e 85 41 91 35> },
  __v: 0
}

which is correct. Next, I modify the query to populate _primary_email field like so:

models.User.findById(userId)populate('_primary_email')..exec().then(function(userModel) {
    console.log(userModel);
});

I get this object:

{ _id: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 22 12 2d 07 91 ed 4c 4a 8a 41 03 da 59 a9 e1 2c> },
  updatedAt: 2016-08-25T18:23:27.103Z,
  createdAt: 2016-08-25T18:23:24.962Z,
  password: '$2a$10$ZSjEv98fMYsmQ6NKA2YrBetaUSaoNJ2q3Q5mAL8g/j7hHXH0XXG7G',
  first_name: 'Joe',
  last_name: null,
  __v: 0
}

as you can see, any references to primary email is gone and it bacame undefined.
The similar thing happens if I try to assign object to _primary_email field, so this code does not work either:

models.User.findById(userId).exec().then(function (userModel) {
    if (!userModel) {
      throw new errors.UserNotFoundError();
    }
    models.Email.findById(userModel._primary_email).exec().then(function(emailModel) {
        if (!emailModel) {
          throw new errors.NotFoundError();
        }
        userModel._primary_email = emailModel;
        console.log(props.userModel);
    });
})

I get:

{ _id: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 22 12 2d 07 91 ed 4c 4a 8a 41 03 da 59 a9 e1 2c> },
  updatedAt: 2016-08-25T18:23:27.103Z,
  createdAt: 2016-08-25T18:23:24.962Z,
  password: '$2a$10$ZSjEv98fMYsmQ6NKA2YrBetaUSaoNJ2q3Q5mAL8g/j7hHXH0XXG7G',
  first_name: 'Joe',
  last_name: null,
  _primary_email: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer c2 6d 91 b3 29 f1 4c 3c 8f d8 9e 1e 85 41 91 35> },
  __v: 0
}

So, it is seen from this example the _primary_email filed still showing model _id instead of the actual object.

I tested this logic with ObjectId instead of UUID and it seems work as expected, so I think there may be something special about using UUID as a primary key. Any ideas?

How to get an _id to UUID string after populate ?

Hi,

I don't understand why is not possible to have an UUID String representation after a populate ?

This is the test code that I tried:

it('should work', function(cb) {
      Photo.findOne({_id: '7c401d91-3852-4818-985d-7e7b79f771c2'}).populate('pet').exec(function(err, photo) {
        (photo.pet).should.exist;
        (photo.pet.name).should.equal('Sammy');

        var photoJSON = photo.toJSON();

        cb(err);
      });
    });

And the error:

TypeError: photo.toJSON is not a function
    at /Users/username/Projects/mongoose-uuid/test/index.js:168:33
    at /Users/username/Projects/mongoose-uuid/node_modules/mongoose/lib/model.js:4759:16
    at /Users/username/Projects/mongoose-uuid/node_modules/mongoose/lib/utils.js:263:16
    at _hooks.execPost (node_modules/mongoose/lib/query.js:4074:11)
    at /Users/username/Projects/mongoose-uuid/node_modules/kareem/index.js:135:16
    at processTicksAndRejections (internal/process/next_tick.js:74:9)

Problem with populate in mongoose 4.12.5

mongoose 4.12.5

I'm trying tu use uuids as _id and it works fine.
Only when i use populate some error occurs.
I've installed this packege and runned test with fail result, can someone help?

Test result bellow:

`$ npm run test

[email protected] test C:\Users\someuser\www\mongoose-uuid
mocha

mongoose-uuid
(node:12656) DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection(). See http://mongoosejs.com/docs/connections.html#use-mongo-client
(node:12656) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
√ should cast uuid strings to binary
√ should convert back to text with toObject()
√ should save without errors (537ms)
√ should be found correctly with .find()
√ should be found correctly with .findById()
query population
1) should work
other scenarios
√ should get with no value
2) setting a populated field to uuid string

6 passing (2s)
2 failing

  1. mongoose-uuid query population should work:
    Uncaught TypeError: binary.length is not a function
    at model.getter (C:\Users\someuser\www\mongoose-uuid\index.js:12:20)
    at SchemaUUID.SchemaType.applyGetters (C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\schematype.js:723:22)
    at model.Document.get (C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\document.js:971:18)
    at model.get (C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\services\document\compile.js:137:25)
    at C:\Users\someuser\www\mongoose-uuid\test\index.js:119:15
    at C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\query.js:3061:18
    at newTickHandler (C:\Users\someuser\www\mongoose-uuid\node_modules\mpromise\lib\promise.js:234:18)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

  2. mongoose-uuid other scenarios setting a populated field to uuid string:
    Uncaught TypeError: binary.length is not a function
    at model.getter (C:\Users\someuser\www\mongoose-uuid\index.js:12:20)
    at SchemaUUID.SchemaType.applyGetters (C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\schematype.js:723:22)
    at model.Document.get (C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\document.js:971:18)
    at model.get [as dingy] (C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\services\document\compile.js:137:25)
    at C:\Users\someuser\www\mongoose-uuid\test\index.js:166:20
    at C:\Users\someuser\www\mongoose-uuid\node_modules\mongoose\lib\query.js:3061:18
    at newTickHandler (C:\Users\someuser\www\mongoose-uuid\node_modules\mpromise\lib\promise.js:234:18)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

npm ERR! Windows_NT 10.0.15063
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "test"
npm ERR! node v6.11.0
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] test: mocha
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] test script 'mocha'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the mongoose-uuid2 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! mocha
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs mongoose-uuid2
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls mongoose-uuid2
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\someuser\www\mongoose-uuid\npm-debug.log`

The mongoose-uuid2 node package is confusing

Hi there,

This isn't exactly an issue, but the naming mongoose-uuid2 of the node package seems confusing to me (as if it's specified only to UUID v2). mongoose-uuid seems to be already taken and maybe that's the reason for the current naming.

I'd suggest using something like mongoose-type-uuid. It's a bit verbose, but also clear to readers what's this package about.

Thanks for the makers of this package. It made it a breeze to use UUIDs as document ids.

Good luck,
Emad

Some confusion...

I've completed the installation and base code for a schema using your add-on, however, I'm confused as to the line which begins with "require('...').loadType"

The test code is different than the readme, and either way i'm getting "Cannot find module..." What am I missing?

Problem with find methods with mongoose 5.4.19

Hi everyone, I'm using Mongo 4.0 and those libraries in a NodeJS backend:

"mongoose": "^5.3.0",
"mongoose-auto-increment": "^5.0.1",
"mongoose-paginate": "^5.0.3",
"mongoose-uuid2": "^2.3.0",

With the implemented schemas:

'use strict';
var uuidv4 = require('uuid/v4');
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var mongooseUuid = require('mongoose-uuid2')(mongoose);
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');

var UserSchema = new Schema({
                                _id: { type: mongoose.Types.UUID, default: uuidv4 },
                                username: {type: String, unique: true},
                                datacollect_id: {type: String, unique: true, required: true},
                                password: {type: String},
                                age: {type: Number},
                                gender: {type: String, enum: ["F", "M"]},
                                handedness: {type: String, enum: ["left", "right"]},
                                language: { type:  mongoose.Types.UUID, ref: 'Language'},
                                isTest: {type: Boolean, required: true, default: false},
                                swipeExperience: {type: String, enum: ["never", "first_experience", "regular_user", "expert"]},
                                keyboardExperience: {type: String, enum: ["never", "first_experience", "regular_user", "expert"]},
                                creationDate: {type: Date, default: Date.now, required: true},
                                creationBy: { type:  mongoose.Types.UUID, ref: 'User', required: true },
                                expirationDate: {type: Date},
                                updateDate: {type: Date, required: true},
                                enabled: {type: Boolean, required: true},
                                roles: {
                                    type: [String], enum: ["USER"], required: true
                                },
                                collects: [{
                                    collect: { type:  mongoose.Types.UUID, ref: 'Collect', required: true},
                                    sessions: {type: [{
                                        stepsIndex: {type: Number, required: true},
                                        inks: {type: [{
                                            _id: {type:  mongoose.Types.UUID, ref: 'Ink'}
                                        }]},
                                        inksGoal: {type: Number, required: true},
                                        beginDate: {type: Date, required: true},
                                        endDate: {type: Date},
                                        updateDate: {type: Date, required: true}
                                    }]}
                                }]
                            },
                            {
                                id: false
                            });

UserSchema.plugin(mongoosePaginate);

UserSchema.pre('save', function (next) {
    var user = this;
    if (this.password && (this.isModified('password') || this.isNew)) {
        bcrypt.genSalt(10, function (err, salt) {
            if (err) {
                return next(err);
            }
            bcrypt.hash(user.password, salt, function (err, hash) {
                if (err) {
                    return next(err);
                }
                user.password = hash;
                next();
            });
        });
    } else {
        return next();
    }
});

UserSchema.methods.comparePassword = function (passw, cb) {
    bcrypt.compare(passw, this.password, function (err, isMatch) {
        if (err) {
            return cb(err);
        }
        cb(null, isMatch);
    });
};

mongoose.set('debug', true);

module.exports = mongoose.model('User', UserSchema);

But when my backend trying to do this:

 User.findOne({ username: username }, function(err, user) {...});

the result user is empty. I tried others "find" methods and same result.

this is the mongoose log:

Mongoose: users.findOne({ username: 'admin' }, { projection: {} })

My database is full and nothing happened in MongoDB logs.

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.