diff --git a/src/index.ts b/src/index.ts index 733b22f..0f2c750 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,31 @@ import * as WebResource from './types/web-resource'; export { WebResource as WebResourceType } from './types/web-resource'; +export type Types = { + 'Big Integer': BigInteger.Types; + 'Big Serial': BigSerial.Types; + // eslint-disable-next-line id-denylist + Boolean: Boolean.Types; + 'Case Insensitive Text': CaseInsensitiveText.Types; + Color: Color.Types; + ConceptType: ConceptType.Types; + Date: Date.Types; + 'Date Time': DateTime.Types; + File: File.Types; + ForeignKey: ForeignKey.Types; + Hashed: Hashed.Types; + Integer: Integer.Types; + Interval: Interval.Types; + JSON: JSON.Types; + Real: Real.Types; + Serial: Serial.Types; + SHA: SHA.Types; + 'Short Text': ShortText.Types; + Text: Text.Types; + Time: Time.Types; + WebResource: WebResource.Types; +}; + export default { 'Big Integer': BigInteger, 'Big Serial': BigSerial, @@ -50,6 +75,8 @@ export default { Time, WebResource, } satisfies { + [fieldType in keyof Types]: SbvrType; +} & { Hashed: SbvrType & { compare: (str: string, hash: string) => Promise; }; @@ -57,5 +84,4 @@ export default { validateSync: (value: string) => string; compare: (str: string, hash: string) => Promise; }; - [fieldType: string]: SbvrType; }; diff --git a/src/type-utils.ts b/src/type-utils.ts index d51b9bf..471b86c 100644 --- a/src/type-utils.ts +++ b/src/type-utils.ts @@ -16,6 +16,10 @@ export interface SbvrType { fetchProcessing?: FetchProcessing; validate: Validate; } +export interface TsTypes { + Read: Read; + Write: Write; +} export type FetchProcessing = (data: unknown) => Read | null | undefined; export interface Validate { diff --git a/src/types/big-integer.ts b/src/types/big-integer.ts index e5c96a3..5d970e5 100644 --- a/src/types/big-integer.ts +++ b/src/types/big-integer.ts @@ -9,7 +9,7 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; export const nativeFactTypes = { @@ -17,5 +17,5 @@ export const nativeFactTypes = { Real: TypeUtils.nativeFactTypeTemplates.comparison, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/big-serial.ts b/src/types/big-serial.ts index a89f38f..fc9ae40 100644 --- a/src/types/big-serial.ts +++ b/src/types/big-serial.ts @@ -23,8 +23,8 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/boolean.ts b/src/types/boolean.ts index 112e80b..67c7f7a 100644 --- a/src/types/boolean.ts +++ b/src/types/boolean.ts @@ -16,15 +16,15 @@ export const types = { }, }; -type ReadType = boolean; -type WriteType = boolean | 0 | 1; +export type Types = TypeUtils.TsTypes; type DbWriteType = boolean; // `BOOLEAN` on sqlite/websql is just an alias for `INTEGER` hence the `=== 1` check -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => - data === true || data === 1; +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => data === true || data === 1; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired((originalValue) => { // We use Number rather than parseInt as it deals with booleans and will return NaN for things like "a1" const value = Number(originalValue); diff --git a/src/types/case-insensitive-text.ts b/src/types/case-insensitive-text.ts index f314053..d3df97a 100644 --- a/src/types/case-insensitive-text.ts +++ b/src/types/case-insensitive-text.ts @@ -9,8 +9,8 @@ export const types = { }, }; -type WriteType = string; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.text(); diff --git a/src/types/color.ts b/src/types/color.ts index 2b58188..dc23998 100644 --- a/src/types/color.ts +++ b/src/types/color.ts @@ -16,13 +16,13 @@ export const types = { }, }; -type ReadType = { +type ColorObj = { r: number; g: number; b: number; a: number; }; -type WriteType = number | ReadType; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; export const nativeProperties = { @@ -46,7 +46,9 @@ export const nativeProperties = { }, }; -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => { if (typeof data !== 'number') { throw new Error('Fetched color is not an integer: ' + typeof data); } @@ -60,7 +62,7 @@ export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { /* eslint-enable no-bitwise */ }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired((value) => { let processedValue: number; if (typeof value !== 'object') { diff --git a/src/types/concept-type.ts b/src/types/concept-type.ts index cb69f45..cab40ca 100644 --- a/src/types/concept-type.ts +++ b/src/types/concept-type.ts @@ -9,7 +9,7 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; export const nativeFactTypes = { @@ -17,5 +17,5 @@ export const nativeFactTypes = { Real: TypeUtils.nativeFactTypeTemplates.comparison, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/date-time.ts b/src/types/date-time.ts index 6410f2d..9193e76 100644 --- a/src/types/date-time.ts +++ b/src/types/date-time.ts @@ -9,11 +9,12 @@ export const types = { }, }; -type ReadType = string; -type WriteType = string | number; +export type Types = TypeUtils.TsTypes; type DbWriteType = Date; -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => { if (data == null) { return data; } @@ -39,5 +40,5 @@ export const nativeNames = { 'Current Time': ['Now'], }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.date; diff --git a/src/types/date.ts b/src/types/date.ts index acef75f..961aaca 100644 --- a/src/types/date.ts +++ b/src/types/date.ts @@ -9,11 +9,12 @@ export const types = { }, }; -type ReadType = string; -type WriteType = string | number; +export type Types = TypeUtils.TsTypes; type DbWriteType = Date; -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => { if (data == null) { return data; } @@ -35,5 +36,5 @@ export const nativeFactTypes = { }, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.date; diff --git a/src/types/file.ts b/src/types/file.ts index 1110434..f4a7616 100644 --- a/src/types/file.ts +++ b/src/types/file.ts @@ -8,10 +8,10 @@ export const types = { }, }; -type WriteType = Buffer | string; +export type Types = TypeUtils.TsTypes; type DbWriteType = Buffer; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired((value) => { if (Buffer.isBuffer(value)) { return value; diff --git a/src/types/foreign-key.ts b/src/types/foreign-key.ts index cb69f45..cab40ca 100644 --- a/src/types/foreign-key.ts +++ b/src/types/foreign-key.ts @@ -9,7 +9,7 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; export const nativeFactTypes = { @@ -17,5 +17,5 @@ export const nativeFactTypes = { Real: TypeUtils.nativeFactTypeTemplates.comparison, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/hashed.ts b/src/types/hashed.ts index a07678b..d106311 100644 --- a/src/types/hashed.ts +++ b/src/types/hashed.ts @@ -18,10 +18,10 @@ export const types = { }, }; -type WriteType = string; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired(async (value) => { if (typeof value !== 'string') { throw new Error('is not a string'); diff --git a/src/types/integer.ts b/src/types/integer.ts index 2f08c51..0dbfabb 100644 --- a/src/types/integer.ts +++ b/src/types/integer.ts @@ -9,7 +9,7 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; export const nativeFactTypes = { @@ -17,5 +17,5 @@ export const nativeFactTypes = { Real: TypeUtils.nativeFactTypeTemplates.comparison, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/interval.ts b/src/types/interval.ts index 0852a5b..e976b08 100644 --- a/src/types/interval.ts +++ b/src/types/interval.ts @@ -9,8 +9,8 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/json.ts b/src/types/json.ts index b8deb7e..9f698f1 100644 --- a/src/types/json.ts +++ b/src/types/json.ts @@ -20,18 +20,22 @@ type JSONable = | { [key: string]: JSONable } | { toJSON(): JSONable }; -type ReadType = { [key: string]: JSON } | JSON[]; -type WriteType = { [key: string]: JSONable } | JSONable[]; +export type Types = TypeUtils.TsTypes< + { [key: string]: JSON } | JSON[], + { [key: string]: JSONable } | JSONable[] +>; type DbWriteType = string; -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => { if (typeof data === 'string') { return JSON.parse(data); } return data; }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired((value) => { // Disallow primitives if (typeof value !== 'object') { diff --git a/src/types/real.ts b/src/types/real.ts index 277cad4..ae308e2 100644 --- a/src/types/real.ts +++ b/src/types/real.ts @@ -9,7 +9,7 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; export const nativeFactTypes = { @@ -17,7 +17,7 @@ export const nativeFactTypes = { Real: TypeUtils.nativeFactTypeTemplates.comparison, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired((value) => { const processedValue = parseFloat(value); if (Number.isNaN(processedValue)) { diff --git a/src/types/serial.ts b/src/types/serial.ts index c596678..a89e262 100644 --- a/src/types/serial.ts +++ b/src/types/serial.ts @@ -23,8 +23,8 @@ export const types = { }, }; -type WriteType = number; +export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.integer; diff --git a/src/types/sha.ts b/src/types/sha.ts index 0fb8d17..e4fc4f5 100644 --- a/src/types/sha.ts +++ b/src/types/sha.ts @@ -30,12 +30,12 @@ export const types = { }, }; -type WriteType = string; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; export const validateSync = sha256; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired((value) => { if (typeof value !== 'string') { throw new Error('is not a string'); diff --git a/src/types/short-text.ts b/src/types/short-text.ts index 8034acb..3a4673d 100644 --- a/src/types/short-text.ts +++ b/src/types/short-text.ts @@ -9,8 +9,8 @@ export const types = { }, }; -type WriteType = string; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.text(255); diff --git a/src/types/text.ts b/src/types/text.ts index 2bfd6fa..0abc572 100644 --- a/src/types/text.ts +++ b/src/types/text.ts @@ -9,7 +9,7 @@ export const types = { }, }; -type WriteType = string; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; export const nativeProperties = { @@ -27,5 +27,5 @@ export const nativeFactTypes = { }, }; -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.text(); diff --git a/src/types/time.ts b/src/types/time.ts index ceac285..13f11eb 100644 --- a/src/types/time.ts +++ b/src/types/time.ts @@ -9,11 +9,12 @@ export const types = { }, }; -type ReadType = string; -type WriteType = number | string; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => { if (data != null) { // We append the date of the epoch so that we can parse this as a valid date. return new Date('Thu, 01 Jan 1970 ' + data).toISOString(); @@ -21,17 +22,15 @@ export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { return data; }; -export const validate: TypeUtils.Validate = (async ( - value, - required, -) => { - const date = await TypeUtils.validate.date(value, required); - if (date == null) { - return date; - } - return date.toLocaleTimeString(); -}) as { - (value: WriteType, required: true): Promise; - (value: WriteType, required: false): Promise; - (value: WriteType, required: boolean): Promise; -}; +export const validate: TypeUtils.Validate = + (async (value, required) => { + const date = await TypeUtils.validate.date(value, required); + if (date == null) { + return date; + } + return date.toLocaleTimeString(); + }) as { + (value: Types['Write'], required: true): Promise; + (value: Types['Write'], required: false): Promise; + (value: Types['Write'], required: boolean): Promise; + }; diff --git a/src/types/web-resource.ts b/src/types/web-resource.ts index 5d74441..6f75ed6 100644 --- a/src/types/web-resource.ts +++ b/src/types/web-resource.ts @@ -32,8 +32,7 @@ export const types = { }, }; -type ReadType = WebResource; -type WriteType = WebResource; +export type Types = TypeUtils.TsTypes; type DbWriteType = string; export const nativeProperties = { @@ -90,7 +89,9 @@ export const nativeProperties = { * @param data string|object * @returns a WebResource parsed from the DB */ -export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { +export const fetchProcessing: TypeUtils.FetchProcessing = ( + data, +) => { let refData: WebResource; if (data === null) { return data; @@ -123,7 +124,7 @@ export const fetchProcessing: TypeUtils.FetchProcessing = (data) => { * Returns a Stringified WebResource that will be persisted on the DB * */ -export const validate: TypeUtils.Validate = +export const validate: TypeUtils.Validate = TypeUtils.validate.checkRequired(async (value: WebResource) => { if (typeof value !== 'object') { throw new Error(`is not an object: ${typeof value}`);