scottwrobinson / camo

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

Inconsistent document when creating and loading #50

Open michaeljota opened 8 years ago

michaeljota commented 8 years ago

When you create a new document, it gets return with all the reference as objects, but when it's loaded, only loades the ids.

Creating:

Room {
  _schema:
   { _id: { type: [Function: String] },
     title: { type: [Function: String], required: true },
     owner: { type: [Object], required: true },
     creationDate: { type: [Function: Date], default: 1455393132301 },
     clients: { type: [Object] },
     image: { type: [Function: Image] },
     classroom: { type: [Object] } },
  _id: 'WnvnnT8PEDNxVMVD',
  title: 'asdas',
  owner:
   User {
     _schema: { _id: [Object], user: [Object], role: [Object] },
     _id: 'ZLTyRwTJDx6S5gdn',
     user: 'asdasd',
     role: 'user' },
  creationDate: Sat Feb 13 2016 15:22:12 GMT-0430 (Hora estándar de Venezuela),
  clients:
   [ User {
       _schema: [Object],
       _id: 'ZLTyRwTJDx6S5gdn',
       user: 'asdasd',
       role: 'user' } ],
  image: undefined,
  classroom:
   Classroom {
     _schema:
      { _id: [Object],
        className: [Object],
        date: [Object],
        whiteboards: [Object] },
     _id: 'LJiplNc7Wz2u4lm5',
     className: 'asdas',
     date: Sat Feb 13 2016 15:22:12 GMT-0430 (Hora estándar de Venezuela),
     whiteboards: [ [Object] ] } }

Loading:

Room {
  _schema:
   { _id: { type: [Function: String] },
     title: { type: [Function: String], required: true },
     owner: { type: [Object], required: true },
     creationDate: { type: [Function: Date], default: 1455396264869 },
     clients: { type: [Object] },
     image: { type: [Function: Image] },
     classroom: { type: [Object] } },
  _id: 'nRF8AXEVaYSAggjW',
  title: 'sdf',
  owner:
   User {
     _schema: { _id: [Object], user: [Object], role: [Object] },
     _id: 'HtRw1URfSc23KEeA',
     user: 'asdas',
     role: 'user' },
  creationDate: Sat Feb 13 2016 16:14:08 GMT-0430 (Hora estándar de Venezuela),
  clients:
   [ User {
       _schema: [Object],
       _id: 'HtRw1URfSc23KEeA',
       user: 'asdas',
       role: 'user' } ],
  image:
   Image {
     _schema: { data: [Object], contentType: [Object] },
     data: undefined,
     contentType: undefined },
  classroom:
   Classroom {
     _schema:
      { _id: [Object],
        className: [Object],
        date: [Object],
        whiteboards: [Object] },
     _id: 'KumWJNLXNl6JEVEC',
     className: 'sdf',
     date: Sat Feb 13 2016 16:14:08 GMT-0430 (Hora estándar de Venezuela),
     whiteboards: [ 'DtCIuOKLRj9gdcz9' ] } }

Well, they both have the classroom, not just the reference, but the whiteboards (being a Array of Whiteboard documents) only load the id.

Is this the expected behavior? If it is, I'm sorry. I'm new with Document-oriented databases.

Thanks!

scottwrobinson commented 8 years ago

This might be a bug, just need some more info.

Are you loading these two objects using the same query, options, etc? The more info the better. Or, if you have a bare-bones example of the problem in code, that would be best.

Thanks!

michaeljota commented 8 years ago

Hi! Sorry to being late answering. I'm not loading the first object, I'm creating it.

Creating:

    var r = Room.create(req.body);
    User.loadOne({_id: req.user._id})
        .then((user) => {
            r.owner = user;
            r.clients.push(user);
            var c = Classroom.create ({className: r.title});
            var w = Whiteboard.create();
            w.save()
                .then((whiteboard) => {
                    c.whiteboards.push(w);
                    c.save()
                        .then((classroom) => {
                            r.classroom = c;
                            r.save()
                                .then((room) => {
                                    successHandler (res, room);
                                })
                                .catch((err) => {
                                    errorHandler (res, err);
                                });
                        })
                        .catch((err) => {
                            errorHandler (res, err);
                        });
                })
                .catch((err) => {
                    errorHandler (res, err);
                })
        })
        .catch((err) => {
            errorHandler (res, err);
        });

Loading:

    Room.loadOne({_id: req.params.roomId})
        .then((room) => {
            successHandler (res, room);
        })
        .catch((err) => {
            errorHandler (res, err);
        });

Again, if this is the expected behavior, I'm sorry. I know now the load of the others documents should be on my own without Camo, but maybe this is something Camo should do. Idk.

scottwrobinson commented 8 years ago

Looking at the object data from your first post, I think it's a bug. I should be able to look in to this further this weekend.

Thanks for posting the issue and your code!

devdebonair commented 8 years ago

Any word on this issue yet?

michaeljota commented 8 years ago

@devdebonair I think not. A workarround can be to load the other objects inside the load. I know it's not ideal, but, it will work.