Skip to content

Commit

Permalink
fix: virtual column should not be added to unset in case of custom ge…
Browse files Browse the repository at this point in the history
…tter cannot be serialize
  • Loading branch information
JimmyDaddy committed Aug 28, 2022
1 parent 2ef32ce commit 41b94d6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 21 deletions.
15 changes: 9 additions & 6 deletions src/adapters/sequelize.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
Attributes, Literal, OperatorCondition,
BoneOptions, ResultSet, Raw, Connection,
BoneOptions, ResultSet, Raw,
SetOptions, BeforeHooksType, AfterHooksType,
QueryOptions, OrderOptions,
QueryOptions, OrderOptions, QueryResult
} from '../types/common';
import { AbstractBone } from '../types/bone';
import { Spell } from '../spell';
Expand Down Expand Up @@ -131,13 +131,13 @@ export class SequelizeBone extends AbstractBone {
this: T,
fields: string | Array<string> | { [Property in keyof Extract<InstanceType<T>, Literal>]?: number },
options?: SequelizeConditions<T>
): Spell<T>;
): Spell<T, QueryResult>;

static increment<T extends typeof SequelizeBone>(
this: T,
fields: string | Array<string> | { [Property in keyof Extract<InstanceType<T>, Literal>]?: number },
options?: SequelizeConditions<T>
): Spell<T>;
): Spell<T, QueryResult>;

static max<T extends typeof SequelizeBone>(this: T, filed: string, options?: SequelizeConditions<T>): Promise<Literal>;
static min<T extends typeof SequelizeBone>(this: T, filed: string, options?: SequelizeConditions<T>): Promise<Literal>;
Expand Down Expand Up @@ -168,6 +168,9 @@ export class SequelizeBone extends AbstractBone {
static update<T extends typeof SequelizeBone>(this: T, values: SetOptions<T>, options: BaseSequelizeConditions<T>): Promise<number>;
static bulkUpdate<T extends typeof SequelizeBone>(this: T, values: SetOptions<T>, options: BaseSequelizeConditions<T>): Spell<T, number>;

/**
* An alias of instance constructor. Some legacy code access model name from instance with `this.Model.name`.
*/
get Model(): typeof SequelizeBone;
get dataValues(): { [key: string]: Literal };

Expand All @@ -179,8 +182,8 @@ export class SequelizeBone extends AbstractBone {
previous(key?: string): Literal | Literal[] | { [key: string]: Literal | Literal[] };
isSoftDeleted(): boolean;

increment(field: string | string[] | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone>;
decrement(field: string | string[] | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone>;
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>;
}

Expand Down
4 changes: 2 additions & 2 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,8 +1291,8 @@ class Bone {
}

for (const name in attributes) {
const { columnName } = attributes[name];
if (!(columnName in row)) {
const attribute = attributes[name];
if (!(attribute.columnName in row) && !attribute.virtual) {
instance._getRawUnset().add(name);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ export class PostgresDriver extends AbstractDriver {
export class SqliteDriver extends AbstractDriver {
type: 'sqlite';
dialect: 'sqlite';
}
}
8 changes: 4 additions & 4 deletions src/spell.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collecti
toString(): string;

all: Spell<T, U>;
first: Spell<T, U>;
last: Spell<T, U>;
get(index: number): Spell<T, U>;
first: Spell<T, InstanceType<T>>;
last: Spell<T, InstanceType<T>>;
get(index: number): Spell<T, InstanceType<T>>;

unscoped: Spell<T, U>;
unparanoid: Spell<T, U>;
Expand All @@ -157,4 +157,4 @@ export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collecti
useIndex(...hints: Array<CommonHintsArgs>): Spell<T, U>;
forceIndex(...hints: Array<CommonHintsArgs>): Spell<T, U>;
ignoreIndex(...hints: Array<CommonHintsArgs>): Spell<T, U>;
}
}
6 changes: 3 additions & 3 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ type OrderOptions<T extends typeof AbstractBone> = {

export class Collection<T extends AbstractBone> extends Array<T> {
save(): Promise<void>;
toJSON(): Object[];
toObject(): Object[];
toJSON(): InstanceValues<T>[];
toObject(): InstanceValues<T>[];
}

export type WhereConditions<T extends typeof AbstractBone> = {
Expand All @@ -167,4 +167,4 @@ export type InstanceValues<T> = {
}

export type BeforeHooksType = 'beforeCreate' | 'beforeBulkCreate' | 'beforeUpdate' | 'beforeSave' | 'beforeUpsert' | 'beforeRemove';
export type AfterHooksType = 'afterCreate' | 'afterBulkCreate' | 'afterUpdate' | 'afterSave' | 'afterUpsert' | 'afterRemove';
export type AfterHooksType = 'afterCreate' | 'afterBulkCreate' | 'afterUpdate' | 'afterSave' | 'afterUpsert' | 'afterRemove';
43 changes: 41 additions & 2 deletions test/types/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe('=> Basics (TypeScript)', function() {

@Column()
wordCount: number;

@Column(DataTypes.VIRTUAL)
get isEmptyContent(): boolean {
return this.wordCount <= 0;
}
}

before(async function() {
Expand Down Expand Up @@ -95,13 +100,25 @@ describe('=> Basics (TypeScript)', function() {

describe('=> Integration', function() {
it('bone.toJSON()', async function() {
const post = await Post.create({ title: 'Nephalem' });
const post = await Post.create({ title: 'Nephalem', wordCount: 0 });
assert.equal(post.toJSON().title, 'Nephalem');
// virtual column should not be added to unset
assert.equal(post.toJSON().isEmptyContent, true);
const post1 = await Post.findOne();
assert.equal(post1.toJSON().title, 'Nephalem');
// virtual column should not be added to unset
assert.equal(post1.toJSON().isEmptyContent, true);
});

it('bone.toObject()', async function() {
const post = await Post.create({ title: 'Leah' });
assert.equal(post.toObject().title, 'Leah');
// virtual column should not be added to unset
assert.equal(post.toObject().isEmptyContent, true);
const post1 = await Post.findOne();
assert.equal(post1.toObject().title, 'Leah');
// virtual column should not be added to unset
assert.equal(post1.toObject().isEmptyContent, true);
});
});

Expand Down Expand Up @@ -134,7 +151,7 @@ describe('=> Basics (TypeScript)', function() {

describe('=> Read', function() {
beforeEach(async function() {
const posts = await Post.bulkCreate([
await Post.bulkCreate([
{ title: 'Leah' },
{ title: 'Cain' },
{ title: 'Nephalem' }
Expand Down Expand Up @@ -162,6 +179,28 @@ describe('=> Basics (TypeScript)', function() {
const posts = await Post.where({ title: { $like: '%a%' } }).select('title');
assert.equal(posts.length, 3);
});

it('first', async () => {
const post1 = await Post.find().first;
assert.equal(post1.title, 'Leah');
});

it('last', async () => {
const post1 = await Post.find().last;
assert.equal(post1.title, 'Nephalem');
});

it('get(index)', async () => {
const post1 = await Post.find().get(0);
assert.equal(post1.title, 'Leah');
});

it('all', async () => {
const posts = await Post.find().all;
assert.equal(posts.length, 3);
assert.equal(posts[0].title, 'Leah');
});

});

describe('=> Update', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/types/sequelize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1387,4 +1387,4 @@ describe('=> sequelize (TypeScript)', function() {
Like.removeAttribute('userId');
assert(Like.attributes.userId == null);
});
});
});
3 changes: 1 addition & 2 deletions test/types/spell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,4 @@ describe('=> Spell (TypeScript)', function() {
);
});
})

});
});

0 comments on commit 41b94d6

Please sign in to comment.