Skip to content

Commit

Permalink
Add date time with time zone type
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
joshbwlng committed Nov 12, 2024
1 parent 7593253 commit ea7d1e3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as Color from './types/color';
import * as ConceptType from './types/concept-type';
import * as Date from './types/date';
import * as DateTime from './types/date-time';
import * as DateTimeTZ from './types/date-time-tz';
import * as File from './types/file';
import * as ForeignKey from './types/foreign-key';
import * as Hashed from './types/hashed';
Expand All @@ -36,6 +37,7 @@ export interface Types {
ConceptType: ConceptType.Types;
Date: Date.Types;
'Date Time': DateTime.Types;
'Date Time TZ': DateTimeTZ.Types;
File: File.Types;
ForeignKey: ForeignKey.Types;
Hashed: Hashed.Types;
Expand All @@ -60,6 +62,7 @@ export default {
Color,
ConceptType,
'Date Time': DateTime,
'Date Time TZ': DateTimeTZ,
Date,
File,
ForeignKey,
Expand Down
48 changes: 48 additions & 0 deletions src/types/date-time-tz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type {
CurrentTimestampNode,
LessThanNode,
} from '@balena/abstract-sql-compiler';
import * as TypeUtils from '../type-utils';

export const types = {
postgres: 'TIMESTAMPTZ',
mysql: 'TIMESTAMP',
websql: 'INTEGER',
odata: {
name: 'Edm.DateTimeOffset',
},
};

export type Types = TypeUtils.TsTypes<string, string | number | Date>;
type DbWriteType = Date;

export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => {
if (data == null) {
return data;
}
let date: Date;
if (data instanceof Date) {
date = data;
} else if (typeof data === 'string' || typeof data === 'number') {
date = new Date(data);
} else {
throw new Error('Fetched date time is not valid: ' + typeof data);
}
return date.toISOString();
};

export const nativeFactTypes: TypeUtils.NativeFactTypes = {
'Date Time TZ': {
...TypeUtils.nativeFactTypeTemplates.equality,
'is before': (from, to): LessThanNode => ['LessThan', from, to],
},
};

export const nativeNames: TypeUtils.NativeNames = {
'Current Time': ['CurrentTimestamp'] satisfies CurrentTimestampNode,
};

export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.date;
17 changes: 17 additions & 0 deletions test/Date Time TZ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as helpers from './helpers';

helpers.describe('Date Time TZ', function (test) {
const now = new Date();
describe('fetchProcessing', function () {
test.fetch(now, now.toISOString());
test.fetch(now.toString(), new Date(now.toString()).toISOString());
test.fetch(now.getTime(), new Date(now.getTime()).toISOString());
test.fetch(null, null);
});

describe('validate', function () {
test.validate(now, true, now);
test.validate(now.getTime(), true, now);
test.validate(now.toString(), true, now);
});
});

0 comments on commit ea7d1e3

Please sign in to comment.