Skip to content

Commit

Permalink
Merge pull request #210 from cyjake/docs-data-types-dts
Browse files Browse the repository at this point in the history
docs: DataTypes definitions in d.ts
  • Loading branch information
SmartOrange authored Oct 28, 2021
2 parents b4e1510 + cf669f6 commit b580d62
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 17 deletions.
31 changes: 31 additions & 0 deletions test/types/data_types.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
88 changes: 88 additions & 0 deletions types/data_types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
type LENGTH_VARIANTS = 'tiny' | '' | 'medium' | 'long';

interface INVOKABLE<T> {
(length: LENGTH_VARIANTS): T;
(length: number): T;
}

export default class DataType {
toSqlString(): string;

static STRING: typeof STRING & INVOKABLE<STRING>;
static INTEGER: typeof INTEGER & INVOKABLE<INTEGER>;
static BIGINT: typeof BIGINT & INVOKABLE<BIGINT>;
static TEXT: typeof TEXT & INVOKABLE<TEXT>;
static BLOB: typeof BLOB & INVOKABLE<BLOB>;
static JSON: typeof JSON & INVOKABLE<JSON>;
static JSONB: typeof JSONB & INVOKABLE<JSONB>;
static BINARY: typeof BINARY & INVOKABLE<BINARY>;
static VARBINARY: typeof VARBINARY & INVOKABLE<VARBINARY>;
static DATE: typeof DATE & INVOKABLE<DATE>;
static DATEONLY: typeof DATEONLY & INVOKABLE<DATEONLY>;
static BOOLEAN: typeof BOOLEAN & INVOKABLE<BOOLEAN>;
}

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'
}
38 changes: 21 additions & 17 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import DataType from './data_types';

export { DataType as DataTypes };

type DataTypes<T> = {
[Property in keyof T as Exclude<Property, "toSqlString">]: T[Property]
}

interface ExprIdentifier {
type: 'id';
value: string;
Expand Down Expand Up @@ -129,21 +137,15 @@ type InstanceValues<T> = {
[Property in keyof Extract<T, Literal>]?: Extract<T, Literal>[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<DataType>;
}

interface RelateOptions {
Expand Down Expand Up @@ -212,8 +214,10 @@ declare class Collection<T extends Bone> extends Array<T> {
}

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;

Expand Down Expand Up @@ -250,7 +254,7 @@ export class Bone {
/**
* The attribute definitions of the model.
*/
static attributes: { [key: string]: DataType | AttributeMeta };
static attributes: { [key: string]: DataTypes<DataType> | AttributeMeta };

/**
* The schema info of current model.
Expand Down Expand Up @@ -617,7 +621,7 @@ interface RawQueryOptions {
}

export default class Realm {
Bone: Bone;
Bone: typeof Bone;
driver: Driver;
models: Record<string, Bone>;

Expand Down

0 comments on commit b580d62

Please sign in to comment.