Commit 3235498e authored by Dian Fay's avatar Dian Fay
Browse files

feat: automatically deep insert when targeting a compound Readable

parent 47b72070
Loading
Loading
Loading
Loading
+1 −1
Changes for lib/statement/insert.js: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ const Insert = function (source, record, options = {}) {
  this.columns = _.intersection(fields, this.source.columnNames);
  this.junctions = _.difference(fields, this.source.columnNames);

  if (options.deepInsert && this.junctions.length) {
  if ((this.source.loader === 'join' || options.deepInsert) && this.junctions.length) {
    if (this.records.length > 1) {
      throw new Error('Multi-table or deep insert is only supported for single records.');
    }
+28 −0
Changes for test/readable/join.js: 28 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -687,6 +687,34 @@ describe('join', function () {
        });
      });
    });

    it('deep inserts', async function () {
      const join = await db.alpha.join({
        beta: {
          type: 'INNER',
          on: {alpha_id: 'id'}
        }
      });

      const result = await join.insert({
        val: 'newer and improveder',
        beta: [{alpha_id: undefined, val: 'asdf'}]
      });

      assert.deepEqual(result, {id: result.id, val: 'newer and improveder'});

      const inserted = await join.find(result.id);

      assert.deepEqual(inserted, [{
        id: result.id,
        val: 'newer and improveder',
        beta: [{
          id: 6,
          alpha_id: result.id,
          val: 'asdf'
        }]
      }]);
    });
  });

  describe('updates', function () {