camo icon indicating copy to clipboard operation
camo copied to clipboard

Awesome Idea, Need some features

Open thxmike opened this issue 7 years ago • 1 comments

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;

thxmike avatar Jan 27 '18 12:01 thxmike

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!

scottwrobinson avatar Feb 07 '18 20:02 scottwrobinson