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

fix: ts type definitions #352

Merged
merged 3 commits into from
Sep 26, 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
16 changes: 11 additions & 5 deletions src/adapters/sequelize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Attributes, Literal, OperatorCondition,
BoneOptions, ResultSet, Raw,
SetOptions, BeforeHooksType, AfterHooksType,
QueryOptions, OrderOptions, QueryResult
QueryOptions, OrderOptions, QueryResult, Values as CommonValues,
} from '../types/common';
import { AbstractBone } from '../types/abstract_bone';
import { Spell } from '../spell';
Expand All @@ -21,12 +21,17 @@ interface BaseSequelizeConditions<T extends typeof SequelizeBone> extends QueryO
where?: WhereConditions<T>;
order?: OrderOptions<T>;
limit?: number;
attributes?: string | Raw | Array<[keyof Extract<InstanceType<T>, Literal>] | string | Raw> | [keyof Extract<InstanceType<T>, Literal>];
attributes?: string | Raw | Array<[keyof Extract<CommonValues<InstanceType<T>>, Literal>] | string | Raw> | [keyof Extract<CommonValues<InstanceType<T>>, Literal>];
Copy link
Collaborator

Choose a reason for hiding this comment

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

是否考虑 sequelize 这个文件用 ts 写呀

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

除非 bone 也用 ts 写,单独改这个用处不大,类型提示反而更难处理

offset?: number;
}

type SequelizeUpdateOptions<T extends typeof SequelizeBone> = BaseSequelizeConditions<T> & {
fields?: string[];
fields?: Array<[keyof Extract<CommonValues<InstanceType<T>>, Literal>] | string | Raw> | [keyof Extract<CommonValues<InstanceType<T>>, Literal>];
}

interface SequelizeInstanceUpdateOptions<T extends SequelizeBone> extends QueryOptions {
attributes?: string | Raw | Array<[keyof Extract<CommonValues<T>, Literal>] | string | Raw> | [keyof Extract<CommonValues<T>, Literal>];
fields?: Array<[keyof Extract<CommonValues<T>, Literal>] | string | Raw> | [keyof Extract<CommonValues<T>, Literal>];
}

interface SequelizeConditions<T extends typeof SequelizeBone> extends BaseSequelizeConditions<T> {
Expand Down Expand Up @@ -179,8 +184,8 @@ export class SequelizeBone extends AbstractBone {
get dataValues(): { [key: string]: Literal };

where(): { [key: string]: number | bigint | string };
set(key: string, value: Literal | Literal[]): void;
get(key?: string): Literal | { [key: string]: Literal };
set<T, Key extends keyof T>(this: T, key: Key, value: T[Key]): void;
get<T, Key extends keyof T>(this: T, key?: Key): T[Key];
setDataValue<T, Key extends keyof T>(this: T, key: Key, value: T[Key]): void;
getDataValue<T>(this: T): T;
getDataValue<T, Key extends keyof T>(this: T, key: Key): T[Key];
Expand All @@ -190,6 +195,7 @@ export class SequelizeBone extends AbstractBone {
increment(field: string | string[] | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
decrement(field: string | string[] | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
destroy(options?: SequelizeDestroyOptions): Promise<this| number>;
update<T = this>(this: T, changes?: { [key: string]: Literal } | { [Property in keyof Extract<this, Literal>]?: Literal }, opts?: SequelizeInstanceUpdateOptions<this>): Promise<number>;
}

export const sequelize: (Bone: AbstractBone) => typeof SequelizeBone;
4 changes: 2 additions & 2 deletions src/types/abstract_bone.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class AbstractBone {
* @example
* bone.attributeWas('foo') // => 1
*/
attributeWas(name: string): Literal;
attributeWas<T, Key extends keyof Values<T>>(this: T, key: Key): T[Key];
Copy link
Owner

Choose a reason for hiding this comment

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

👍


/**
* See if attribute has been changed or not.
Expand Down Expand Up @@ -316,7 +316,7 @@ export class AbstractBone {
* @param changes data changes
* @param opts query options
*/
update(changes?: { [key: string]: Literal } | { [Property in keyof Extract<this, Literal>]: Literal }, opts?: QueryOptions): Promise<number>;
update(changes?: { [key: string]: Literal } | { [Property in keyof Extract<this, Literal>]?: Literal }, opts?: QueryOptions): Promise<number>;

/**
* create instance
Expand Down
4 changes: 2 additions & 2 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface QueryOptions {
hint?: CommonHintsArgs;
transaction?: Connection | {
connection: Connection
};
} | null;
}

export type BulkCreateOptions = QueryOptions & {
Expand Down Expand Up @@ -175,7 +175,7 @@ export type WhereConditions<T extends typeof AbstractBone> = {
// https://stackoverflow.com/a/68077021/179691
export type PickTypeKeys<Obj, Type, T extends keyof Obj = keyof Obj> = ({ [P in keyof Obj]: Obj[P] extends Type ? P : never })[T];

export type Values<T> = Partial<Omit<T, PickTypeKeys<T, Function>>>;
export type Values<T> = Partial<Omit<T, PickTypeKeys<T, Function> | 'isNewRecord' | 'Model' | 'dataValues'>>;

export type BeforeHooksType = 'beforeCreate' | 'beforeBulkCreate' | 'beforeUpdate' | 'beforeSave' | 'beforeUpsert' | 'beforeRemove';
export type AfterHooksType = 'afterCreate' | 'afterBulkCreate' | 'afterUpdate' | 'afterSave' | 'afterUpsert' | 'afterRemove';
Expand Down
13 changes: 12 additions & 1 deletion test/types/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ describe('=> Basics (TypeScript)', function() {
assert.equal(post.changed(), false);
assert.equal(post.changed('title'), false);
});

it('bone.attributeWas(name)',async () => {
const post = new Post({
title: 'Yhorm',
});
await post.save();
post.attribute('title', 'Cain');
assert.equal(post.attributeWas('title'), 'Yhorm');
});
});

describe('=> Accessors', function() {
Expand Down Expand Up @@ -240,10 +249,12 @@ describe('=> Basics (TypeScript)', function() {
it('post.update()', async function() {
const post = await Post.create({ title: 'Tyrael' });
assert.equal(post.title, 'Tyrael');
const result = await post.update({ title: 'Stranger' });
let result = await post.update({ title: 'Stranger' });
assert.equal(result, 1);
await post.reload();
assert.equal(post.title, 'Stranger');
result = await post.update({});
assert.equal(result, 0);
});

it('spell.increment()', async function() {
Expand Down
18 changes: 18 additions & 0 deletions test/types/sequelize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,13 @@ describe('=> sequelize (TypeScript)', function() {
assert.equal(result, 1);
await post.reload();
assert.equal(post.title, 'Stranger');
const result1 = await post.update({ title: 'Stranger', content: 'Yhorm' }, {
fields: [ 'content' ],
});
assert.equal(result1, 1);
await post.reload();
assert.equal(post.title, 'Stranger');
assert.equal(post.content, 'Yhorm');
});

it('spell.increment()', async function() {
Expand Down Expand Up @@ -1389,4 +1396,15 @@ describe('=> sequelize (TypeScript)', function() {
Like.removeAttribute('userId');
assert(Like.attributes.userId == null);
});

it('transaction support pass null', async function() {
const post = await Post.create({ title: 'By three they come' }, { transaction: null });
const result = await Post.find({
where: {
title: 'By three they come',
},
transaction: null,
});
assert.equal(result.id, post.id);
});
});