Skip to content

Commit

Permalink
Fix bug with insertMany, createdAt, and entity timestamp (#1151)
Browse files Browse the repository at this point in the history
* fix bug with insertMany and createdAt

* Fix timestamp typo in entity frame

* remove outdated comment
  • Loading branch information
ktuite authored Jun 7, 2024
1 parent 6eeb5c5 commit 5d1f0ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/model/frames/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,7 +26,7 @@ class Entity extends Frame.define(
'int4', 'int4',
'conflictType',
'timestamptz',
'timestamptz', 'timestampz',
'timestamptz', 'timestamptz',
])
) {
get def() { return this.aux.def; }
Expand Down
2 changes: 1 addition & 1 deletion lib/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() ]))
Expand Down

0 comments on commit 5d1f0ec

Please sign in to comment.