Skip to content

Commit

Permalink
feat: add types for realm (#278)
Browse files Browse the repository at this point in the history
* feat: add types for realm

* export `ConnectionOptions`

* test: realm types (#1)

* add test cases

* move tsc option to tsconfig.json

Co-authored-by: dengruoqi <[email protected]>
Co-authored-by: Chen Yangjian <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2022
1 parent 9b31605 commit a003599
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
71 changes: 71 additions & 0 deletions test/types/realm.test.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
});
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "es2018",
"moduleResolution": "Node",
"module": "CommonJS",
"experimentalDecorators": true
"experimentalDecorators": true,
"esModuleInterop": true
}
}
18 changes: 14 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,15 @@ export class Bone {
toObject(): InstanceValues<this>;
}

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 {
Expand All @@ -612,6 +614,11 @@ interface InitOptions {
};
}

interface SyncOptions {
force?: boolean;
alter?: boolean;
}

type RawSql = {
__raw: true,
value: string,
Expand All @@ -626,16 +633,17 @@ interface RawQueryOptions {

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

constructor(options: ConnectOptions);

define(
name: string,
attributes: Record<string, AttributeMeta>,
options: InitOptions,
descriptors: Record<string, Function>,
attributes: Record<string, DataTypes<DataType> | AttributeMeta>,
options?: InitOptions,
descriptors?: Record<string, Function>,
): Bone;

raw(sql: string): RawSql;
Expand All @@ -646,6 +654,8 @@ export default class Realm {

transaction(callback: GeneratorFunction): Promise<void>;
transaction(callback: (connection: Connection) => Promise<void>): Promise<void>;

sync(options?: SyncOptions): Promise<void>;
}

/**
Expand Down

0 comments on commit a003599

Please sign in to comment.