diff --git a/test/types/realm.test.ts b/test/types/realm.test.ts new file mode 100644 index 00000000..1a5bef78 --- /dev/null +++ b/test/types/realm.test.ts @@ -0,0 +1,71 @@ +import { strict as assert } from 'assert'; +import Realm from '../..'; + +describe('=> Realm (TypeScript)', function () { + let realm: Realm; + before(function() { + realm = new Realm({ + port: process.env.MYSQL_PORT, + database: 'leoric', + subclass: true, + }); + }); + + describe('realm.define(name, attributes, options, descriptors)', async function() { + it('options and descriptors should be optional', async function() { + assert.doesNotThrow(function() { + const { STRING } = realm.DataTypes; + realm.define('User', { name: STRING }); + }); + }); + + it('can customize attributes with descriptors', async function() { + const { STRING } = realm.DataTypes; + realm.define('User', { name: STRING }, {}, { + get name() { + return this.attribute('name').replace(/^([a-z])/, function(m, chr) { + return chr.toUpperCase(); + }); + }, + set name(value) { + if (typeof value !== 'string') throw new Error('unexpected name' + value); + this.attribute('name', value); + } + }); + }); + }); + + describe('realm.sync(options)', async function() { + it('options should be optional', async function() { + assert.doesNotThrow(async () => { + const { STRING } = realm.DataTypes; + realm.define('User', { name: STRING }); + await realm.sync(); + }); + }); + + it('`force` can be passed individually', async function() { + assert.doesNotThrow(async () => { + const { STRING } = realm.DataTypes; + realm.define('User', { name: STRING }); + await realm.sync({ force: true }); + }); + }); + + it('`alter` can be passed individually', async function() { + assert.doesNotThrow(async () => { + const { STRING } = realm.DataTypes; + realm.define('User', { name: STRING }); + await realm.sync({ alter: true }); + }); + }); + + it('`force` and `alter` can be passed together', async function() { + assert.doesNotThrow(async () => { + const { STRING } = realm.DataTypes; + realm.define('User', { name: STRING }); + await realm.sync({ force: true, alter: true }); + }); + }); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 570cb7ca..fc6dd7b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "target": "es2018", "moduleResolution": "Node", "module": "CommonJS", - "experimentalDecorators": true + "experimentalDecorators": true, + "esModuleInterop": true } } diff --git a/types/index.d.ts b/types/index.d.ts index 5b330e3e..e81e4632 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -593,13 +593,15 @@ export class Bone { toObject(): InstanceValues; } -interface ConnectOptions { +export interface ConnectOptions { client?: 'mysql' | 'mysql2' | 'pg' | 'sqlite3' | '@journeyapps/sqlcipher'; dialect?: 'mysql' | 'postgres' | 'sqlite'; host?: string; + port?: number | string; user?: string; database: string; models?: string | (typeof Bone)[]; + subclass?: boolean; } interface InitOptions { @@ -612,6 +614,11 @@ interface InitOptions { }; } +interface SyncOptions { + force?: boolean; + alter?: boolean; +} + type RawSql = { __raw: true, value: string, @@ -626,6 +633,7 @@ interface RawQueryOptions { export default class Realm { Bone: typeof Bone; + DataTypes: typeof DataType; driver: Driver; models: Record; @@ -633,9 +641,9 @@ export default class Realm { define( name: string, - attributes: Record, - options: InitOptions, - descriptors: Record, + attributes: Record | AttributeMeta>, + options?: InitOptions, + descriptors?: Record, ): Bone; raw(sql: string): RawSql; @@ -646,6 +654,8 @@ export default class Realm { transaction(callback: GeneratorFunction): Promise; transaction(callback: (connection: Connection) => Promise): Promise; + + sync(options?: SyncOptions): Promise; } /**