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

chore: export Utility Type BoneInstanceValues #370

Merged
merged 2 commits into from
Nov 18, 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
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Connection, QueryOptions,
Raw, ColumnMeta, AttributeMeta,
BeforeHooksType, AfterHooksType, Collection,
GeneratorReturnType, Values, BoneValues,
GeneratorReturnType, Values, BoneCreateValues, BoneInstanceValues,
} from './src/types/common';
import { SpellMeta, Spell, SpellBookFormatResult } from './src/spell';
import Bone from './src/bone';
Expand All @@ -19,7 +19,7 @@ export {
DataTypes, Literal, Validator, Connection,
Hint, IndexHint, HintInterface, INDEX_HINT_SCOPE_TYPE, INDEX_HINT_SCOPE, INDEX_HINT_TYPE,
Bone, Raw, Collection,
SpellMeta, Spell, ColumnMeta, AttributeMeta, SpellBookFormatResult, Values, BoneValues,
SpellMeta, Spell, ColumnMeta, AttributeMeta, SpellBookFormatResult, Values, BoneCreateValues, BoneInstanceValues,
};

export * from './src/decorators';
Expand Down
8 changes: 4 additions & 4 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, Values as CommonValues, BoneColumns, InstanceColumns, BoneValues,
QueryOptions, OrderOptions, QueryResult, Values as CommonValues, BoneColumns, InstanceColumns, BoneCreateValues,
} from '../types/common';
import { AbstractBone } from '../types/abstract_bone';
import { Spell } from '../spell';
Expand Down Expand Up @@ -121,14 +121,14 @@ export class SequelizeBone extends AbstractBone {
static aggregate<T extends typeof SequelizeBone>(this: T, name: BoneColumns<T>, func: aggregators, options?: SequelizeConditions<T>): Spell<T, number>;
static aggregate<T extends typeof SequelizeBone>(this: T, name: Raw | '*', func: aggregators, options?: SequelizeConditions<T>): Spell<T, number>;

static build<T extends typeof SequelizeBone>(this: T, values: BoneValues<T>, options?: BoneOptions): InstanceType<T>;
static build<T extends typeof SequelizeBone>(this: T, values: BoneCreateValues<T>, options?: BoneOptions): InstanceType<T>;

/**
* see https://github.com/sequelize/sequelize/blob/a729c4df41fa3a58fbecaf879265d2fb73d80e5f/src/model.js#L2299
* @param valueSets
* @param options
*/
static bulkBuild<T extends typeof SequelizeBone>(this:T, valueSets: Array<BoneValues<T>>, options?: BoneOptions): Array<InstanceType<T>>;
static bulkBuild<T extends typeof SequelizeBone>(this:T, valueSets: Array<BoneCreateValues<T>>, options?: BoneOptions): Array<InstanceType<T>>;

static count<T extends typeof SequelizeBone>(this: T, name?: BoneColumns<T>): Spell<T, ResultSet<T> | number>;
static count<T extends typeof SequelizeBone>(this: T, name?: Raw | '*'): Spell<T, ResultSet<T> | number>;
Expand Down Expand Up @@ -209,7 +209,7 @@ export class SequelizeBone extends AbstractBone {
increment(field: string | Raw | Array<string | Raw>, options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
decrement(field: InstanceColumns<this> | Array<InstanceColumns<this>> | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
decrement(field: string | Raw | Array<string | Raw> , options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
destroy(options?: SequelizeDestroyOptions): Promise<this| number>;
destroy<T>(this: T, options?: SequelizeDestroyOptions): Promise<T | number>;
update<T = this>(this: T, changes?: { [Property in keyof Extract<this, Literal>]?: Literal }, opts?: SequelizeInstanceUpdateOptions<this>): Promise<number>;
update<T = this>(this: T, changes?: { [key: string]: Literal }, opts?: SequelizeInstanceUpdateOptions<this>): Promise<number>;

Expand Down
13 changes: 6 additions & 7 deletions src/types/abstract_bone.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DataTypes, { AbstractDataType, DataType } from "../data_types";
import {
Pool, Literal, WhereConditions,
Collection, ResultSet, OrderOptions,
QueryOptions, AttributeMeta, AssociateOptions, Values, Connection, BulkCreateOptions, BoneValues,
QueryOptions, AttributeMeta, AssociateOptions, Values, Connection, BulkCreateOptions, BoneCreateValues,
GeneratorReturnType, BoneColumns, InstanceColumns, Raw,
} from './common';
import { AbstractDriver } from '../drivers';
Expand Down Expand Up @@ -127,7 +127,7 @@ export class AbstractBone {
* @example
* Bone.create({ foo: 1, bar: 'baz' })
*/
static create<T extends typeof AbstractBone>(this: T, values: BoneValues<T> & Partial<Record<BoneColumns<T>, Literal>>, options?: QueryOptions): Promise<InstanceType<T>>;
static create<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T> & Partial<Record<BoneColumns<T>, Literal>>, options?: QueryOptions): Promise<InstanceType<T>>;

/**
* INSERT or UPDATE rows
Expand All @@ -136,12 +136,12 @@ export class AbstractBone {
* @param values values
* @param opt query options
*/
static upsert<T extends typeof AbstractBone>(this: T, values: BoneValues<T>, options?: QueryOptions): Spell<T, number>;
static upsert<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T>, options?: QueryOptions): Spell<T, number>;

/**
* Batch INSERT
*/
static bulkCreate<T extends typeof AbstractBone>(this: T, records: Array<BoneValues<T>>, options?: BulkCreateOptions): Promise<Array<InstanceType<T>>>;
static bulkCreate<T extends typeof AbstractBone>(this: T, records: Array<BoneCreateValues<T>>, options?: BulkCreateOptions): Promise<Array<InstanceType<T>>>;

/**
* SELECT all rows. In production, when the table is at large, it is not recommended to access records in this way. To iterate over all records, {@link Bone.batch} shall be considered as the better alternative. For tables with soft delete enabled, which means they've got `deletedAt` attribute, use {@link Bone.unscoped} to discard the default scope.
Expand Down Expand Up @@ -316,9 +316,8 @@ export class AbstractBone {
/**
* Get attribute changes
*/
changes<T, Key extends keyof Values<T>>(this: T, name: Key): Record<InstanceColumns<this>, [ Literal, Literal ]>;
changes<T, Key extends keyof T>(this: T, name: Key): Record<InstanceColumns<this>, [ Literal, Literal ]>;
changes(): Record<InstanceColumns<this>, [ Literal, Literal ]>;
changes<T, Key extends keyof Values<T>>(this: T, name: Key): Record<Key, [ T[Key] | null, T[Key] | null ]>;
changes<T, Key extends keyof Values<T>>(this: T): Record<Key, [ Literal, Literal ]>;

/**
* See if attribute was changed previously or not.
Expand Down
11 changes: 10 additions & 1 deletion src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,19 @@ export type BoneColumns<T extends typeof AbstractBone, Key extends keyof Instanc

export type InstanceColumns<T = typeof AbstractBone, Key extends keyof T = keyof Values<T>> = Key;

export type BoneValues<T extends typeof AbstractBone> = Partial<Values<InstanceType<T>>>;
/**
* Bone.create(values: BoneCreateValues<this>);
*/
export type BoneCreateValues<T extends typeof AbstractBone> = Partial<Values<InstanceType<T>>>;

export type BeforeHooksType = 'beforeCreate' | 'beforeBulkCreate' | 'beforeUpdate' | 'beforeSave' | 'beforeUpsert' | 'beforeRemove';
export type AfterHooksType = 'afterCreate' | 'afterBulkCreate' | 'afterUpdate' | 'afterSave' | 'afterUpsert' | 'afterRemove';

// https://stackoverflow.com/a/67232225/179691
type GeneratorReturnType<T extends Generator> = T extends Generator<any, infer R, any> ? R: never;

/**
* Plain keyMap type object of a Bone's attributes
* BoneInstanceValues<user> = { id: number, name: string }
*/
export type BoneInstanceValues<T extends typeof AbstractBone> = Omit<InstanceType<T>, PickTypeKeys<InstanceType<T>, Function> | 'isNewRecord' | 'Model' | 'dataValues'>;
11 changes: 8 additions & 3 deletions test/types/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ describe('=> Basics (TypeScript)', function() {

it('bone.changes()', async function() {
const post = new Post({ title: 'Cain' });
assert.deepEqual(post.changes(), { title: [ null, 'Cain' ] });
assert.deepEqual(post.changes('title'), { title: [ null, 'Cain' ] });

const changes = post.changes();
assert.deepEqual(changes, { title: [ null, 'Cain' ] });
assert.equal(changes.title?.[0], null);
assert.equal(changes.title[1], 'Cain');
const titleChanges = post.changes('title');
assert.equal(titleChanges.title[0], null);
assert.equal(titleChanges.title[1], 'Cain');
assert.deepEqual(titleChanges, { title: [ null, 'Cain' ] });
await post.create();

assert.ok(post.id);
Expand Down
11 changes: 11 additions & 0 deletions test/types/sequelize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,17 @@ describe('=> sequelize (TypeScript)', function() {
const rowCount = await Post.destroy({ force: true });
assert.equal(rowCount, 2);
});

it('bone.destroy()', async () => {
const post = await Post.create({ title: 'By three they come' });
class PostExtend extends Post {
subType?: string;
}

const p1: PostExtend = await post.destroy() as Post;
p1.subType = 'extend';
assert.equal(p1.id, post.id);
});
})

it('Model.removeAttribute()', async function() {
Expand Down