diff --git a/test/types/data_types.ts b/test/types/data_types.ts new file mode 100644 index 00000000..3412c2b0 --- /dev/null +++ b/test/types/data_types.ts @@ -0,0 +1,31 @@ +import { strict as assert } from 'assert'; +import Realm, { Bone, DataTypes } from '../..'; + +describe('=> Data types (TypeScript)', function() { + const { STRING, TEXT, BLOB } = DataTypes; + + it('Bone.DataTypes', async function() { + assert.equal(Bone.DataTypes, DataTypes); + }); + + it('realm.Bone.DataTypes', async function() { + const realm = new Realm({ + dialect: 'sqlite', + database: '/tmp/leoric.sqlite3', + }); + // should be mixed with dialect overrides + assert.notEqual(realm.Bone.DataTypes, DataTypes); + }) + + it('STRING', async function() { + assert.equal(STRING(255).toSqlString(), 'VARCHAR(255)'); + }); + + it('TEXT', async function() { + assert.equal(TEXT('long').toSqlString(), 'LONGTEXT'); + }); + + it('BLOB', async function() { + assert.equal(BLOB('medium').toSqlString(), 'MEDIUMBLOB'); + }); +}); diff --git a/types/data_types.d.ts b/types/data_types.d.ts new file mode 100644 index 00000000..e8e0a766 --- /dev/null +++ b/types/data_types.d.ts @@ -0,0 +1,88 @@ +type LENGTH_VARIANTS = 'tiny' | '' | 'medium' | 'long'; + +interface INVOKABLE { + (length: LENGTH_VARIANTS): T; + (length: number): T; +} + +export default class DataType { + toSqlString(): string; + + static STRING: typeof STRING & INVOKABLE; + static INTEGER: typeof INTEGER & INVOKABLE; + static BIGINT: typeof BIGINT & INVOKABLE; + static TEXT: typeof TEXT & INVOKABLE; + static BLOB: typeof BLOB & INVOKABLE; + static JSON: typeof JSON & INVOKABLE; + static JSONB: typeof JSONB & INVOKABLE; + static BINARY: typeof BINARY & INVOKABLE; + static VARBINARY: typeof VARBINARY & INVOKABLE; + static DATE: typeof DATE & INVOKABLE; + static DATEONLY: typeof DATEONLY & INVOKABLE; + static BOOLEAN: typeof BOOLEAN & INVOKABLE; +} + +declare class STRING extends DataType { + dataType: 'varchar'; + length: number; + constructor(length: number); +} + +declare class INTEGER extends DataType { + dataType: 'integer' | 'bigint'; + length: number; + constructor(length: number); + get UNSIGNED(): this; + get ZEROFILL(): this; +} + +declare class BIGINT extends INTEGER { + dataType: 'bigint'; +} + +declare class TEXT extends DataType { + dataType: 'text'; + length: LENGTH_VARIANTS; + constructor(length: LENGTH_VARIANTS); +} + +declare class BLOB extends DataType { + dataType: 'blob'; + length: LENGTH_VARIANTS; + constructor(length: LENGTH_VARIANTS) +} + +declare class JSON extends DataType { + dataType: 'text' | 'json'; +} + +declare class JSONB extends JSON { + dataType: 'json'; +} + +declare class BINARY extends DataType { + dataType: 'binary'; + length: number; + constructor(length: number); +} + +declare class VARBINARY extends DataType { + dataType: 'varbinary'; + length: number; + constructor(length: number); +} + +declare class DATE extends DataType { + dataType: 'date'; + precision: number; + timezone: boolean; + constructor(precision: number, timezone?: boolean) +} + +declare class DATEONLY extends DataType { + dataType: 'dateonly'; +} + +declare class BOOLEAN extends DataType { + dataType: 'boolean' +} diff --git a/types/index.d.ts b/types/index.d.ts index 0bfbe9c2..cfb495d2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,3 +1,11 @@ +import DataType from './data_types'; + +export { DataType as DataTypes }; + +type DataTypes = { + [Property in keyof T as Exclude]: T[Property] +} + interface ExprIdentifier { type: 'id'; value: string; @@ -129,21 +137,15 @@ type InstanceValues = { [Property in keyof Extract]?: Extract[Property] } -declare class DataType {} - -export const DataTypes: { - [key in 'STRING' | 'INTEGER' | 'BIGINT' | 'DATE' | 'BOOLEAN' | 'TEXT' | 'BLOB' | 'JSON' | 'JSONB' | 'BINARY' | 'VARBINARY' ]: DataType; -}; - interface AttributeMeta { - column: string; - columnType: string; - allowNull: boolean; - defaultValue: Literal; - primaryKey: boolean; - dataType: string; - jsType: Literal; - type: DataType; + column?: string; + columnType?: string; + allowNull?: boolean; + defaultValue?: Literal; + primaryKey?: boolean; + dataType?: string; + jsType?: Literal; + type: DataTypes; } interface RelateOptions { @@ -212,8 +214,10 @@ declare class Collection extends Array { } export class Bone { + static DataTypes: DataType; + /** - * The connection pool of the specified client, with few `Leoric_` prefixed methods extended to eliminate client differences. + * get the connection pool of the driver */ static pool: Pool; @@ -250,7 +254,7 @@ export class Bone { /** * The attribute definitions of the model. */ - static attributes: { [key: string]: DataType | AttributeMeta }; + static attributes: { [key: string]: DataTypes | AttributeMeta }; /** * The schema info of current model. @@ -617,7 +621,7 @@ interface RawQueryOptions { } export default class Realm { - Bone: Bone; + Bone: typeof Bone; driver: Driver; models: Record;