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

sort-bug #285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 10 additions & 12 deletions dev/create-sequelize-instance.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import type { Options as Sequelize6Options } from 'sequelize';
import { Sequelize as Sequelize6 } from 'sequelize';
import type { Options as Sequelize7Options, Sequelize as Sequelize7 } from '@sequelize/core';
import { wrapOptions } from './wrap-options';
import type { Options as Sequelize6Options } from "sequelize";
import { Sequelize as Sequelize6 } from "sequelize";
import type {
Options as Sequelize7Options,
Sequelize as Sequelize7,
} from "@sequelize/core";
import { wrapOptions } from "./wrap-options";

export function createSequelize6Instance(options?: Sequelize6Options): Sequelize6 {
export function createSequelize6Instance(
options?: Sequelize6Options
): Sequelize6 {
return new Sequelize6(wrapOptions(options));
}

export function createSequelize7Instance(options?: Sequelize7Options): Sequelize7 {
// not compatible with node 10
const { Sequelize: Sequelize7Constructor } = require('@sequelize/core');
// @ts-expect-error -- wrapOptions expect sequelize 6.
return new Sequelize7Constructor(wrapOptions(options));
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"type": "commonjs",
"license": "MIT",
"dependencies": {
"@sequelize/core": "alpha",
"chai": "^4",
"chai-as-promised": "^7",
"chai-datetime": "^1",
"chalk": "^4.1.2",
"cross-env": "^7",
"fs-jetpack": "^4",
"pg": "^8.13.1",
"sequelize": "^6",
"@sequelize/core": "alpha",
"sinon": "^13",
"sinon-chai": "^3"
},
Expand Down
156 changes: 139 additions & 17 deletions src/sscce-sequelize-6.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { DataTypes, Model } from 'sequelize';
import { createSequelize6Instance } from '../dev/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
import { DataTypes, Model, fn, col, where, Association } from "sequelize";
import { createSequelize6Instance } from "../dev/create-sequelize-instance";
import { expect, use } from "chai";
import sinon from "sinon";

// if your issue is dialect specific, remove the dialects you don't need to test on.
export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', 'postgres', 'postgres-native']);
export const testingOnDialects = new Set([
"mssql",
"sqlite",
"mysql",
"mariadb",
"postgres",
"postgres-native",
]);

// You can delete this file if you don't want your SSCCE to be tested against Sequelize 6

Expand All @@ -21,21 +28,136 @@ export async function run() {
},
});

class Foo extends Model {}
class User extends Model {
public readonly id!: number;
public name!: string;
static Tags: Association;
}

Foo.init({
name: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Foo',
});
User.init(
{
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: DataTypes.TEXT,
},
{
sequelize,
modelName: "User",
}
);

class Tag extends Model {
public readonly id!: number;
public name!: string;
static Users: Association;
}

Tag.init(
{
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: DataTypes.TEXT,
},
{
sequelize,
modelName: "Tag",
}
);

class UserTag extends Model {
public readonly id!: number;
static User: Association;
static Tag: Association;
}

UserTag.init(
{
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
// userId: {
// type: DataTypes.INTEGER,
// },
// tagId: {
// type: DataTypes.INTEGER,
// },
},
{
sequelize,
modelName: "UserTag",
}
);

UserTag.User = UserTag.hasOne(User);
UserTag.Tag = UserTag.hasOne(Tag);

User.Tags = User.belongsToMany(Tag, { through: UserTag });
Tag.Users = Tag.belongsToMany(User, { through: UserTag });

// You can use sinon and chai assertions directly in your SSCCE.
const spy = sinon.spy();
sequelize.afterBulkSync(() => spy());
// const spy = sinon.spy();
// sequelize.afterBulkSync(() => spy());
await sequelize.sync({ force: true });
expect(spy).to.have.been.called;
// expect(spy).to.have.been.called;

const bob = await User.create({ name: "Bob" });
const joe = await User.create({ name: "Joe" });
const sue = await User.create({ name: "Sue" });
const walt = await User.create({ name: "Walt" });
const ann = await User.create({ name: "Annette" });
const pat = await User.create({ name: "Patricia" });
const contribTag = await Tag.create({ name: "Contributer" });
const adminTag = await Tag.create({ name: "Admin" });
const ownerTag = await Tag.create({ name: "Owner" });

await UserTag.create({
user: bob,
tag: contribTag,
});

await UserTag.create({
user: joe,
tag: contribTag,
});

await UserTag.create({
user: sue,
tag: contribTag,
});

await UserTag.create({
user: walt,
tag: adminTag,
});

await UserTag.create({
user: ann,
tag: adminTag,
});

await UserTag.create({
user: pat,
tag: ownerTag,
});

await UserTag.create({
user: pat,
tag: adminTag,
});

expect(await User.count()).to.equal(6);
expect(await Tag.count()).to.equal(3);
expect(await UserTag.count()).to.equal(7);

const rowsPerPage = 30;
const page = 1;

const users = await User.findAll({
include: [{ association: User.Tags }],
order: [fn("BOOL_OR", where(col("userTags.tagId"), adminTag.id)), "DESC"],
group: ["users.id"],
offset: rowsPerPage * (page - 1),
limit: rowsPerPage,
});

console.log(await Foo.create({ name: 'TS foo' }));
expect(await Foo.count()).to.equal(1);
expect(users.length).to.equal(6);
expect(users[0].name).to.equal("Walt");
expect(users[1].name).to.equal("Annette");
expect(users[2].name).to.equal("Patricia");
}
43 changes: 0 additions & 43 deletions src/sscce-sequelize-7.ts

This file was deleted.

Loading