diff --git a/src/adapters/sequelize.d.ts b/src/adapters/sequelize.d.ts index 768cb86d..f592da76 100644 --- a/src/adapters/sequelize.d.ts +++ b/src/adapters/sequelize.d.ts @@ -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'; @@ -21,12 +21,17 @@ interface BaseSequelizeConditions extends QueryO where?: WhereConditions; order?: OrderOptions; limit?: number; - attributes?: string | Raw | Array<[keyof Extract, Literal>] | string | Raw> | [keyof Extract, Literal>]; + attributes?: string | Raw | Array<[keyof Extract>, Literal>] | string | Raw> | [keyof Extract>, Literal>]; offset?: number; } type SequelizeUpdateOptions = BaseSequelizeConditions & { - fields?: string[]; + fields?: Array<[keyof Extract>, Literal>] | string | Raw> | [keyof Extract>, Literal>]; +} + +interface SequelizeInstanceUpdateOptions extends QueryOptions { + attributes?: string | Raw | Array<[keyof Extract, Literal>] | string | Raw> | [keyof Extract, Literal>]; + fields?: Array<[keyof Extract, Literal>] | string | Raw> | [keyof Extract, Literal>]; } interface SequelizeConditions extends BaseSequelizeConditions { @@ -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(this: T, key: Key, value: T[Key]): void; + get(this: T, key?: Key): T[Key]; setDataValue(this: T, key: Key, value: T[Key]): void; getDataValue(this: T): T; getDataValue(this: T, key: Key): T[Key]; @@ -190,6 +195,7 @@ export class SequelizeBone extends AbstractBone { increment(field: string | string[] | { [Property in keyof Extract]?: number }, options?: QueryOptions): Spell; decrement(field: string | string[] | { [Property in keyof Extract]?: number }, options?: QueryOptions): Spell; destroy(options?: SequelizeDestroyOptions): Promise; + update(this: T, changes?: { [key: string]: Literal } | { [Property in keyof Extract]?: Literal }, opts?: SequelizeInstanceUpdateOptions): Promise; } export const sequelize: (Bone: AbstractBone) => typeof SequelizeBone; diff --git a/src/types/abstract_bone.d.ts b/src/types/abstract_bone.d.ts index f59e60f5..68dcca39 100644 --- a/src/types/abstract_bone.d.ts +++ b/src/types/abstract_bone.d.ts @@ -249,7 +249,7 @@ export class AbstractBone { * @example * bone.attributeWas('foo') // => 1 */ - attributeWas(name: string): Literal; + attributeWas>(this: T, key: Key): T[Key]; /** * See if attribute has been changed or not. @@ -316,7 +316,7 @@ export class AbstractBone { * @param changes data changes * @param opts query options */ - update(changes?: { [key: string]: Literal } | { [Property in keyof Extract]: Literal }, opts?: QueryOptions): Promise; + update(changes?: { [key: string]: Literal } | { [Property in keyof Extract]?: Literal }, opts?: QueryOptions): Promise; /** * create instance diff --git a/src/types/common.d.ts b/src/types/common.d.ts index 504daa1e..e51818c1 100644 --- a/src/types/common.d.ts +++ b/src/types/common.d.ts @@ -61,7 +61,7 @@ export interface QueryOptions { hint?: CommonHintsArgs; transaction?: Connection | { connection: Connection - }; + } | null; } export type BulkCreateOptions = QueryOptions & { @@ -175,7 +175,7 @@ export type WhereConditions = { // https://stackoverflow.com/a/68077021/179691 export type PickTypeKeys = ({ [P in keyof Obj]: Obj[P] extends Type ? P : never })[T]; -export type Values = Partial>>; +export type Values = Partial | 'isNewRecord' | 'Model' | 'dataValues'>>; export type BeforeHooksType = 'beforeCreate' | 'beforeBulkCreate' | 'beforeUpdate' | 'beforeSave' | 'beforeUpsert' | 'beforeRemove'; export type AfterHooksType = 'afterCreate' | 'afterBulkCreate' | 'afterUpdate' | 'afterSave' | 'afterUpsert' | 'afterRemove'; diff --git a/test/types/basics.test.ts b/test/types/basics.test.ts index d7529451..861469b7 100644 --- a/test/types/basics.test.ts +++ b/test/types/basics.test.ts @@ -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() { @@ -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() { diff --git a/test/types/sequelize.test.ts b/test/types/sequelize.test.ts index 56fd6157..645ee6fc 100644 --- a/test/types/sequelize.test.ts +++ b/test/types/sequelize.test.ts @@ -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() { @@ -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); + }); });