bookshelf: Model.fetchAll not working

When I am trying to all all object via Bookshelf then it is returning incorrect result.

var uuid = require('node-uuid');
var Bookshelf = require('bookshelf');
var Knex = require('knex');
var bookshelf = Bookshelf(Knex({}));
var schema = bookshelf.knex.schema;

schema.createTable('binary_uuid_test', function(table) {
  table.binary('uuid');
  table.string('name');
})

it('loads objects properly', function() {
  // These are uuid.v1()
  var uuids = ['d96d40d1-fcc9-11e5-9b8d-87b1687276b9', 'd96d40d2-fcc9-11e5-9b8d-87b1687276b9'];
  var s_uuids = ['d96d40d1-fcc9-11e5-9b8d-87b1687276b9', 'd96d40d2-fcc9-11e5-9b8d-87b1687276b9'];

  var SubSite = bookshelf.Model.extend({
    idAttribute: 'uuid',
    tableName: 'binary_uuid_test',

    initialize: function() {
      this.on('saving', this._generateId);
    },

    _generateId: function (model, attrs, options) {
      if (model.isNew()) {
        model.set(model.idAttribute, buildBuffer(uuids.pop()));
      }
    }
  });

  function buildBuffer(string) {
    return (new Buffer(uuid.parse(string)));
  }

  var subsite = new SubSite({ name: 'Kuldeep Aggarwal' }),
      subsite1 = new SubSite({ name: 'Kd Aggarwal' });

  return Promise.all([subsite1.save(), subsite.save()])
    .then(function() {
      return SubSite.fetchAll()
        .then(function(collection) {
          expect(_.map(collection.models, function(model) {
            return uuid.unparse(model.id);
          })).to.eq(s_uuids);
        });
    });
});

This test is getting failed.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

format is called after object is initialized from DB -> Model Object and parse is called when object is saved to DB. right?

Other way around.

parse: columns -> attributes format: attributes -> columns