Skip to content

Commit 8551715

Browse files
authored
fix: do not error if a timestamp is null in msql (#235)
[fixes #221]
1 parent 4142d81 commit 8551715

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

packages/mysql/src/__tests__/dates.test.mysql.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,54 @@ test('DATE as utc', async () => {
439439
]);
440440
await db.dispose();
441441
});
442+
443+
test('timestamp NULL', async () => {
444+
await db.task(async (db) => {
445+
await db.query(sql`SET time_zone = "+00:00";`);
446+
await db.query(sql`
447+
DROP TABLE IF EXISTS dates_test_timestamp_null;
448+
CREATE TABLE dates_test_timestamp_null (
449+
id INT NOT NULL PRIMARY KEY,
450+
a TIMESTAMP NULL DEFAULT NULL
451+
);
452+
`);
453+
454+
const sampleDate = new Date('2000-06-03T05:40:10.123Z');
455+
await db.query(sql`
456+
INSERT INTO dates_test_timestamp_null (id, a)
457+
VALUES (1, ${sampleDate}),
458+
(2, NULL);
459+
`);
460+
461+
expect(
462+
(
463+
await (
464+
await rawConnection
465+
).query(`SELECT * from dates_test_timestamp_null`)
466+
)[0],
467+
).toEqual([
468+
{
469+
a: '2000-06-03 15:10:10',
470+
id: 1,
471+
},
472+
{
473+
a: null,
474+
id: 2,
475+
},
476+
]);
477+
478+
const result = await db.query(sql`
479+
SELECT * from dates_test_timestamp_null;
480+
`);
481+
expect(result).toEqual([
482+
{
483+
a: new Date('2000-06-03T05:40:10.000Z'),
484+
id: 1,
485+
},
486+
{
487+
a: null,
488+
id: 2,
489+
},
490+
]);
491+
});
492+
});

packages/mysql/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,23 @@ function getDateParser(
300300
};
301301
}
302302
}
303+
303304
function getDateTimeParser(
304305
mode: 'string' | 'date-object',
305306
timeZone: 'local' | 'utc',
306-
): (f: {string(): string}) => any {
307+
): (f: {string(): string; buffer(): Buffer}) => any {
307308
switch (mode) {
308309
case 'string':
309310
return (f) => f.string();
310311
case 'date-object':
311312
return (f) => {
313+
const fBuffer = f.buffer();
314+
if (fBuffer === null || fBuffer.length === 0) {
315+
return null;
316+
}
312317
const match =
313318
/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})\:(\d{2})\:(\d{2})(?:\.(\d+))?$/.exec(
314-
f.string(),
319+
fBuffer.toString(),
315320
);
316321
if (!match) {
317322
throw new Error('Expected yyyy-mm-dd HH:MM:SS');

0 commit comments

Comments
 (0)