From 5d1f0ec16ea8ad003054eeb271a5d85bf17512dc Mon Sep 17 00:00:00 2001 From: Kathleen Tuite Date: Fri, 7 Jun 2024 09:40:45 -0700 Subject: [PATCH] Fix bug with insertMany, createdAt, and entity timestamp (#1151) * fix bug with insertMany and createdAt * Fix timestamp typo in entity frame * remove outdated comment --- lib/model/frames/entity.js | 3 +-- lib/util/db.js | 2 +- test/unit/util/db.js | 12 ++++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/model/frames/entity.js b/lib/model/frames/entity.js index 28443611e..c500c3ce0 100644 --- a/lib/model/frames/entity.js +++ b/lib/model/frames/entity.js @@ -12,7 +12,6 @@ const { embedded, fieldTypes, Frame, readable, table } = require('../frame'); const { extractEntity, normalizeUuid, extractLabelFromSubmission, extractBaseVersionFromSubmission } = require('../../data/entity'); -// These Frames don't interact with APIs directly, hence no readable/writable class Entity extends Frame.define( table('entities', 'entity'), 'id', 'uuid', readable, @@ -27,7 +26,7 @@ class Entity extends Frame.define( 'int4', 'int4', 'conflictType', 'timestamptz', - 'timestamptz', 'timestampz', + 'timestamptz', 'timestamptz', ]) ) { get def() { return this.aux.def; } diff --git a/lib/util/db.js b/lib/util/db.js index 2bd29fb35..6e096bcd1 100644 --- a/lib/util/db.js +++ b/lib/util/db.js @@ -169,7 +169,7 @@ const insertMany = (objs) => { if (Type.hasCreatedAt) { columns = sql`"createdAt", ${raw(without(['createdAt'], Type.insertfields).map((s) => `"${s}"`).join(','))}`; rows = objs.map(obj => without(['createdAt'], Type.insertfields).map(_assign(obj))); - columnTypes = remove(indexOf(['createdAt'], Type.insertfields), 1, Type.insertFieldTypes); + columnTypes = remove(indexOf('createdAt', Type.insertfields), 1, Type.insertFieldTypes); selectExp = sql`clock_timestamp(), *`; } else { columns = Type.insertlist; diff --git a/test/unit/util/db.js b/test/unit/util/db.js index 2e97ef9bb..0dcf86df0 100644 --- a/test/unit/util/db.js +++ b/test/unit/util/db.js @@ -337,6 +337,18 @@ returning *`); ]); }); + it('should insert createdAt even if last type is not timestamp', () => { + const U = Frame.define(table('dogs'), 'x', 'createdAt', 'age', fieldTypes(['timestamptz', 'timestamptz', 'int4'])); + const query = insertMany([ new U({ x: new Date('2000-01-01'), age: 14 }), new U({ age: 8 }), new U() ]); + query.sql.should.be.eql(` + INSERT INTO dogs ("createdAt", "x","age") + SELECT clock_timestamp(), * FROM unnest($1::"timestamptz"[], $2::"int4"[]) AS t`); + query.values.should.be.eql([ + ['2000-01-01T00:00:00.000Z', null, null], + [14, 8, null] + ]); + }); + it('should throw fieldTypesNotDefined', () => { const U = Frame.define(table('dogs'), 'x', 'createdAt'); (() => insertMany([ new U({ x: new Date('2000-01-01') }), new U() ]))