Commit b4d4b301 authored by Dian Fay's avatar Dian Fay
Browse files

feat!: open-ended decomposeTo instead of boolean array flag; default to arrays instead of objects

BREAKING CHANGE: resultset decomposition now creates descendants as arrays by default. The 'array' decomposition schema element is no longer recognized, and has been replaced by a 'decomposeTo' element. Set this latter to 'object' to create descendants as objects.
parent 3235498e
Loading
Loading
Loading
Loading
+0 −1
Changes for lib/loader/tables.js: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ exports = module.exports = function (db) {
    columns: ['schema', 'name', 'parent', 'pk', 'columns', 'is_insertable_into'],
    fks: {
      pk: 'fk',
      array: true,
      columns: {
        fk: 'fk',
        fk_dependent_columns: 'dependent_columns',
+2 −3
Changes for lib/readable.js: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -373,7 +373,6 @@ Readable.prototype.join = function (definition) {
  const primaryPk = (definition.pk ? _.castArray(definition.pk) : this.pk).map(this.aliasField, this);
  const decompositionSchema = {
    pk: primaryPk,
    array: true,
    columns: this.columns.reduce((map, c) => {
      map[this.aliasField(c.name)] = c.name;

@@ -427,7 +426,7 @@ Readable.prototype.join = function (definition) {
    const schemaNodeCurrent = schemaNodeParent[alias];

    schemaNodeCurrent.pk = (val.pk ? _.castArray(val.pk) : readable.pk).map(c => `${alias}__${c}`);
    schemaNodeCurrent.array = !val.object;
    schemaNodeCurrent.decomposeTo = val.decomposeTo;
    schemaNodeCurrent.columns = readable.columns.reduce((map, c) => {
      const columnAlias = `${alias}__${c.name}`;

@@ -465,7 +464,7 @@ Readable.prototype.join = function (definition) {
        case 'pk':
        case 'parentReadable':
        case 'parentAlias':
        case 'object':
        case 'decomposeTo':
          return node;
        default:
          // Any other property is a descendant definition node. Attach its
+4 −4
Changes for lib/util/decompose.js: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -80,13 +80,13 @@ exports = module.exports = function (schema, data) {

      Object.keys(objSchema).forEach(function (c) {
        switch (c) {
          case 'pk': case 'columns': case 'array': break;
          case 'pk': case 'columns': case 'decomposeTo': break;
          default: {
            const descendant = build(obj[strid][c] || {}, objSchema[c]);

            if (descendant) {
              obj[strid][c] = descendant;
            } else if (objSchema[c].array) {
            } else if (objSchema[c].decomposeTo !== 'object') {
              // we always want an array if there could be multiple descendants
              obj[strid][c] = [];
            }
@@ -116,12 +116,12 @@ exports = module.exports = function (schema, data) {
          return obj;
        }

        if (obj[k]) {
          // we have to init & pass the accumulator into the *next* recursion
          // since the single option is defined on the child rather than the
          // parent
        const accumulator = v && v.array ? [] : {};
          const accumulator = v && v.decomposeTo === 'object' ? {} : [];

        if (obj[k]) {
          // don't recurse for null descendants
          obj[k] = transform(v, obj[k], accumulator);
        }
+3 −9
Changes for test/readable/decomposition.js: 3 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ describe('decomposing results', function () {
            test_id: 'id',
            name: 'name'
          },
          array: true,
          issues: {
            pk: 'id',
            columns: {
@@ -46,8 +45,7 @@ describe('decomposing results', function () {
              user_id: 'user_id',
              test_id: 'test_id',
              description: 'description'
            },
            array: true
            }
          }
        }
      }
@@ -105,7 +103,6 @@ describe('decomposing results', function () {
            test_id: 'id',
            name: 'name'
          },
          array: true,
          issues: {
            pk: 'id',
            columns: {
@@ -113,8 +110,7 @@ describe('decomposing results', function () {
              user_id: 'user_id',
              test_id: 'test_id',
              description: 'description'
            },
            array: true
            }
          }
        }
      }
@@ -152,7 +148,6 @@ describe('decomposing results', function () {
            test_id: 'id',
            name: 'name'
          },
          array: true,
          issues: {
            pk: 'id',
            columns: {
@@ -160,8 +155,7 @@ describe('decomposing results', function () {
              user_id: 'user_id',
              test_id: 'test_id',
              description: 'description'
            },
            array: true
            }
          }
        }
      },
+2 −2
Changes for test/readable/join.js: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -347,12 +347,12 @@ describe('join', function () {
    });
  });

  it('decomposes objects', function () {
  it('changes the decomposition target type', function () {
    return db.alpha.join({
      beta: {
        type: 'INNER',
        on: {alpha_id: 'id'},
        object: true
        decomposeTo: 'object'
      }
    }).find({
      'alpha.id': 2
Loading