Skip to content

Commit

Permalink
Export typescript read/write types for each sbvr type
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
Page- committed Apr 17, 2024
1 parent de47069 commit 66b9a01
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 71 deletions.
28 changes: 27 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -50,12 +75,13 @@ export default {
Time,
WebResource,
} satisfies {
[fieldType in keyof Types]: SbvrType;
} & {
Hashed: SbvrType & {
compare: (str: string, hash: string) => Promise<boolean>;
};
SHA: SbvrType & {
validateSync: (value: string) => string;
compare: (str: string, hash: string) => Promise<boolean>;
};
[fieldType: string]: SbvrType;
};
4 changes: 4 additions & 0 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface SbvrType<Read = unknown, Write = any, DbWrite = unknown> {
fetchProcessing?: FetchProcessing<Read>;
validate: Validate<Write, DbWrite>;
}
export interface TsTypes<Read, Write> {
Read: Read;
Write: Write;
}

export type FetchProcessing<Read> = (data: unknown) => Read | null | undefined;
export interface Validate<Write, DbWrite> {
Expand Down
4 changes: 2 additions & 2 deletions src/types/big-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
4 changes: 2 additions & 2 deletions src/types/big-serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
10 changes: 5 additions & 5 deletions src/types/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export const types = {
},
};

type ReadType = boolean;
type WriteType = boolean | 0 | 1;
export type Types = TypeUtils.TsTypes<boolean, boolean | 0 | 1>;
type DbWriteType = boolean;

// `BOOLEAN` on sqlite/websql is just an alias for `INTEGER` hence the `=== 1` check
export const fetchProcessing: TypeUtils.FetchProcessing<ReadType> = (data) =>
data === true || data === 1;
export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => data === true || data === 1;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
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);
Expand Down
4 changes: 2 additions & 2 deletions src/types/case-insensitive-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const types = {
},
};

type WriteType = string;
export type Types = TypeUtils.TsTypes<string, string>;
type DbWriteType = string;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.text();
10 changes: 6 additions & 4 deletions src/types/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ColorObj, number | ColorObj>;
type DbWriteType = number;

export const nativeProperties = {
Expand All @@ -46,7 +46,9 @@ export const nativeProperties = {
},
};

export const fetchProcessing: TypeUtils.FetchProcessing<ReadType> = (data) => {
export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => {
if (typeof data !== 'number') {
throw new Error('Fetched color is not an integer: ' + typeof data);
}
Expand All @@ -60,7 +62,7 @@ export const fetchProcessing: TypeUtils.FetchProcessing<ReadType> = (data) => {
/* eslint-enable no-bitwise */
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.checkRequired((value) => {
let processedValue: number;
if (typeof value !== 'object') {
Expand Down
4 changes: 2 additions & 2 deletions src/types/concept-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
9 changes: 5 additions & 4 deletions src/types/date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export const types = {
},
};

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

export const fetchProcessing: TypeUtils.FetchProcessing<ReadType> = (data) => {
export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => {
if (data == null) {
return data;
}
Expand All @@ -39,5 +40,5 @@ export const nativeNames = {
'Current Time': ['Now'],
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.date;
9 changes: 5 additions & 4 deletions src/types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export const types = {
},
};

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

export const fetchProcessing: TypeUtils.FetchProcessing<ReadType> = (data) => {
export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => {
if (data == null) {
return data;
}
Expand All @@ -35,5 +36,5 @@ export const nativeFactTypes = {
},
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.date;
4 changes: 2 additions & 2 deletions src/types/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export const types = {
},
};

type WriteType = Buffer | string;
export type Types = TypeUtils.TsTypes<Buffer, Buffer | string>;
type DbWriteType = Buffer;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.checkRequired((value) => {
if (Buffer.isBuffer(value)) {
return value;
Expand Down
4 changes: 2 additions & 2 deletions src/types/foreign-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
4 changes: 2 additions & 2 deletions src/types/hashed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export const types = {
},
};

type WriteType = string;
export type Types = TypeUtils.TsTypes<string, string>;
type DbWriteType = string;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.checkRequired(async (value) => {
if (typeof value !== 'string') {
throw new Error('is not a string');
Expand Down
4 changes: 2 additions & 2 deletions src/types/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
4 changes: 2 additions & 2 deletions src/types/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
12 changes: 8 additions & 4 deletions src/types/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReadType> = (data) => {
export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => {
if (typeof data === 'string') {
return JSON.parse(data);
}
return data;
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.checkRequired((value) => {
// Disallow primitives
if (typeof value !== 'object') {
Expand Down
4 changes: 2 additions & 2 deletions src/types/real.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.checkRequired((value) => {
const processedValue = parseFloat(value);
if (Number.isNaN(processedValue)) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const types = {
},
};

type WriteType = number;
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
4 changes: 2 additions & 2 deletions src/types/sha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export const types = {
},
};

type WriteType = string;
export type Types = TypeUtils.TsTypes<string, string>;
type DbWriteType = string;

export const validateSync = sha256;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.checkRequired((value) => {
if (typeof value !== 'string') {
throw new Error('is not a string');
Expand Down
4 changes: 2 additions & 2 deletions src/types/short-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const types = {
},
};

type WriteType = string;
export type Types = TypeUtils.TsTypes<string, string>;
type DbWriteType = string;

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.text(255);
4 changes: 2 additions & 2 deletions src/types/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const types = {
},
};

type WriteType = string;
export type Types = TypeUtils.TsTypes<string, string>;
type DbWriteType = string;

export const nativeProperties = {
Expand All @@ -27,5 +27,5 @@ export const nativeFactTypes = {
},
};

export const validate: TypeUtils.Validate<WriteType, DbWriteType> =
export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.text();
Loading

0 comments on commit 66b9a01

Please sign in to comment.