-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-type: minor
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |