Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date time with time zone type #112

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading