From e16456ee1660a491512c5bf8b8fc90adaeb1da6a Mon Sep 17 00:00:00 2001 From: dengruoqi Date: Sun, 20 Feb 2022 23:39:45 +0800 Subject: [PATCH 1/5] feat: add types for realm --- types/index.d.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 5b330e3e..c1516a1c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -612,6 +612,11 @@ interface InitOptions { }; } +interface SyncOptions { + force?: boolean; + alter?: boolean; +} + type RawSql = { __raw: true, value: string, @@ -634,8 +639,8 @@ export default class Realm { define( name: string, attributes: Record, - options: InitOptions, - descriptors: Record, + options?: InitOptions, + descriptors?: Record, ): Bone; raw(sql: string): RawSql; @@ -646,6 +651,8 @@ export default class Realm { transaction(callback: GeneratorFunction): Promise; transaction(callback: (connection: Connection) => Promise): Promise; + + sync(options?: SyncOptions): Promise; } /** From c7522ddd2b01d71c4b72b3f71211feefdeecc8db Mon Sep 17 00:00:00 2001 From: dengruoqi Date: Mon, 21 Feb 2022 20:34:36 +0800 Subject: [PATCH 2/5] export `ConnectionOptions` --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index c1516a1c..8675c55d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -593,7 +593,7 @@ export class Bone { toObject(): InstanceValues; } -interface ConnectOptions { +export interface ConnectOptions { client?: 'mysql' | 'mysql2' | 'pg' | 'sqlite3' | '@journeyapps/sqlcipher'; dialect?: 'mysql' | 'postgres' | 'sqlite'; host?: string; From 232600c3a3a76171ab78a9d5161a66631e9c374a Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Tue, 22 Feb 2022 15:02:06 +0800 Subject: [PATCH 3/5] test: realm types (#1) --- test/types/realm.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++ types/index.d.ts | 5 ++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/types/realm.test.ts diff --git a/test/types/realm.test.ts b/test/types/realm.test.ts new file mode 100644 index 00000000..a94bdff3 --- /dev/null +++ b/test/types/realm.test.ts @@ -0,0 +1,41 @@ +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() { + + }); +}); diff --git a/types/index.d.ts b/types/index.d.ts index 8675c55d..e81e4632 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -597,9 +597,11 @@ 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 { @@ -631,6 +633,7 @@ interface RawQueryOptions { export default class Realm { Bone: typeof Bone; + DataTypes: typeof DataType; driver: Driver; models: Record; @@ -638,7 +641,7 @@ export default class Realm { define( name: string, - attributes: Record, + attributes: Record | AttributeMeta>, options?: InitOptions, descriptors?: Record, ): Bone; From dd73320755197155f8bb5c62b23a1822ae42b331 Mon Sep 17 00:00:00 2001 From: dengruoqi Date: Tue, 22 Feb 2022 17:56:52 +0800 Subject: [PATCH 4/5] add test cases --- test/start.sh | 2 +- test/types/realm.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/test/start.sh b/test/start.sh index 8743deee..16a251a6 100755 --- a/test/start.sh +++ b/test/start.sh @@ -29,7 +29,7 @@ function integration { ## # definition type tests function dts { - npx tsc + npx tsc --esModuleInterop run "$(ls test/types/*.test.js)"; } diff --git a/test/types/realm.test.ts b/test/types/realm.test.ts index a94bdff3..1a5bef78 100644 --- a/test/types/realm.test.ts +++ b/test/types/realm.test.ts @@ -36,6 +36,36 @@ describe('=> Realm (TypeScript)', function () { }); 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 }); + }); + }); }); }); From d5d3d304156a6c9e6a61ed5dccddd6f84e23f49d Mon Sep 17 00:00:00 2001 From: dengruoqi Date: Tue, 22 Feb 2022 19:21:46 +0800 Subject: [PATCH 5/5] move tsc option to tsconfig.json --- test/start.sh | 2 +- tsconfig.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/start.sh b/test/start.sh index 16a251a6..8743deee 100755 --- a/test/start.sh +++ b/test/start.sh @@ -29,7 +29,7 @@ function integration { ## # definition type tests function dts { - npx tsc --esModuleInterop + npx tsc run "$(ls test/types/*.test.js)"; } 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 } }