Skip to content

Commit

Permalink
fix: ts type definitions (#352)
Browse files Browse the repository at this point in the history
* fix: ts type definitions

* chore: unit test complete

Co-authored-by: JimmyDaddy <[email protected]>
  • Loading branch information
JimmyDaddy and JimmyDaddy authored Sep 26, 2022
1 parent 2a29e96 commit 6d82dc6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
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>];
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];

/**
* 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);
});
});

0 comments on commit 6d82dc6

Please sign in to comment.