Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add types for realm #278

Merged
merged 5 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
});
});
});
});
cyjake marked this conversation as resolved.
Show resolved Hide resolved
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to port it to egg-orm

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>,
cyjake marked this conversation as resolved.
Show resolved Hide resolved
): 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