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: fix Bone.create(values) params type definitions #369

Merged
merged 1 commit into from
Nov 10, 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,
GeneratorReturnType, Values, BoneValues,
} 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
SpellMeta, Spell, ColumnMeta, AttributeMeta, SpellBookFormatResult, Values, BoneValues,
};

export * from './src/decorators';
Expand Down
10 changes: 3 additions & 7 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,
QueryOptions, OrderOptions, QueryResult, Values as CommonValues, BoneColumns, InstanceColumns, BoneValues,
} from '../types/common';
import { AbstractBone } from '../types/abstract_bone';
import { Spell } from '../spell';
Expand Down Expand Up @@ -60,10 +60,6 @@ type ScopeOptions = {
override?: boolean
}

type Values<T extends typeof SequelizeBone> = {
[Property in keyof Extract<InstanceType<T>, Literal>]?: Literal;
}

type aggregators = 'count' | 'COUNT' | 'average' | 'AVERAGE' | 'minimum' | 'MINIMUM' | 'maximum' | 'MAXIMUM' | 'sum' | 'SUM';

export class Collection<T extends SequelizeBone> extends Array<T> {
Expand Down Expand Up @@ -125,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: Values<T>, options?: BoneOptions): InstanceType<T>;
static build<T extends typeof SequelizeBone>(this: T, values: BoneValues<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<Values<T>>, options?: BoneOptions): Array<InstanceType<T>>;
static bulkBuild<T extends typeof SequelizeBone>(this:T, valueSets: Array<BoneValues<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
8 changes: 4 additions & 4 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,
QueryOptions, AttributeMeta, AssociateOptions, Values, Connection, BulkCreateOptions, BoneValues,
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: Values<InstanceType<T>> & Partial<Record<BoneColumns<T>, Literal>>, options?: QueryOptions): Promise<InstanceType<T>>;
static create<T extends typeof AbstractBone>(this: T, values: BoneValues<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: Partial<Record<BoneColumns<T>, Literal>>, options?: QueryOptions): Spell<T, number>;
static upsert<T extends typeof AbstractBone>(this: T, values: BoneValues<T>, options?: QueryOptions): Spell<T, number>;

/**
* Batch INSERT
*/
static bulkCreate<T extends typeof AbstractBone>(this: T, records: Array<Partial<Record<BoneColumns<T>, Literal>>>, options?: BulkCreateOptions): Promise<Array<InstanceType<T>>>;
static bulkCreate<T extends typeof AbstractBone>(this: T, records: Array<BoneValues<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
2 changes: 2 additions & 0 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ 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>>>;

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

Expand Down
8 changes: 4 additions & 4 deletions test/types/querying.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ describe('=> Querying (TypeScript)', function() {
class Post extends Bone {
static table = 'articles'

@Column()
id: bigint;
@Column(BIGINT)
id: number;

@Column()
authorId: bigint
@Column(BIGINT)
authorId: number

@Column()
title: string;
Expand Down