scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Multiple references to the same object in an array only get loaded once in `.loadMany()` #5

Closed scottwrobinson closed 9 years ago

scottwrobinson commented 9 years ago

This issue happens when an object has an array of references and there are multiple references to the same object in the array.

In the code below, we give the user two references to the same Eye, but when we load the user there is only one reference there.

class Eye extends Document {
    constructor() {
        super('eyes');
        this.color = String;
    }
}

class User extends Document {
    constructor() {
        super('users');
        this.eyes = [Eye];
    }
}

var user = User.create();
var eye = Eye.create({color: 'blue'});

eye.save().then(function(e) {
    user.eyes.push(eye, eye);
    return user.save();
}).then(function(u) {
    return User.loadMany({});
}).then(function(users) {
    expect(users[0].eyes).to.have.length(2);    // FAILS
});