scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
557 stars 81 forks source link

Awesome Idea, Need some features #115

Open thxmike opened 6 years ago

thxmike commented 6 years ago

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:

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 commented 6 years ago

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!