Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awesome Idea, Need some features #115

Open
thxmike opened this issue Jan 27, 2018 · 1 comment
Open

Awesome Idea, Need some features #115

thxmike opened this issue Jan 27, 2018 · 1 comment

Comments

@thxmike
Copy link

thxmike commented Jan 27, 2018

This is a great idea. I love the "cleanness" of it. However, it lacks a feature which I need.
With the use of mongoosejs, I can override the _id type stored in the database. The reason I do this is to support UUID's as the key and not to have the disk storage overhead (almost %50) of a string.

I basically do the following:

  • set id as false in the schema, to prevent the system from generating one.
  • set the schema _id to use a mongoose type of UUID by importing the mongoose-uuid2 library, along with setting the default (auto-generate) by using a uuid.v4 by importing the library 'node-uuid' to give it a 'sql - identity - like' feel as records are inserted into the database.
  • then I use a virtual of id to get the value out of the database.
  • To prevent the hidden _ values from showing up on out put, I transform the output on a toJSON and toObject methods
  • The type is a binary bson type 4. To retrieve this value using a mongoose find I convert the uuid string to a the binary bson type 4 then do the comparison. I created a static method for this.

As you can imagine this complicates the schema and model to make it hard to read, plus things like ES6 inheritance are next to impossible

Here is an example

const mongoose = require('mongoose');  
const Schema = mongoose.Schema;
require('mongoose-uuid2')(mongoose);
const UUID = mongoose.Types.UUID;
const uuid = require('node-uuid');
const bson = require('bson');

const BaseSchema = new Schema(
    {
        _id: { type: UUID, default: uuid.v4 },
        code: { type: String },
        name: { type: String }
    }, { id: false }
);

class BaseSchemaClass {

    constructor(){
        setup();

    }

    setup() {
        this.set('toJSON', {
            getters: true,
            virtuals: true,
            transform: (doc, ret) => {
                delete ret.__v;
                delete ret._id;
            },
        });
        this.set('toObject', {
            getters: true,
            virtuals: true,
            transform: (doc, ret) => {
                delete ret.__v;
                delete ret._id;
            },
        });
    }

    //virtuals
    get id() {
        return this._id;
    }

    //private
    static convert_uuid_string_to_buffer (uuid_string) {

        var uuid_parse = uuid.parse(uuid_string);
        var id = new db.Types.Buffer(uuid_parse);
        id.subtype(bson.Binary.SUBTYPE_UUID);
        return id.toObject();
    }
}

BaseSchema.loadClass(BaseSchemaClass);
module.exports = BaseSchema;
@scottwrobinson
Copy link
Owner

Hey @thxmike sorry I haven't responded yet. Unfortunately I don't have the time to work on this project at the moment. If you (or anyone you know) would be interested in coming on and helping to maintain it I'd love to have the help. Just let me know. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants