From a029235f064290f558045c481bb84bcdb1a346f6 Mon Sep 17 00:00:00 2001 From: Alyx Date: Mon, 25 Mar 2024 10:54:36 +0100 Subject: [PATCH] implement SSCCE --- src/sscce-sequelize-6.ts | 41 ----------------------------- src/sscce-sequelize-7.ts | 56 ++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 57 deletions(-) delete mode 100644 src/sscce-sequelize-6.ts diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts deleted file mode 100644 index 132a311d5..000000000 --- a/src/sscce-sequelize-6.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { DataTypes, Model } from 'sequelize'; -import { createSequelize6Instance } from '../dev/create-sequelize-instance'; -import { expect } 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']); - -// You can delete this file if you don't want your SSCCE to be tested against Sequelize 6 - -// Your SSCCE goes inside this function. -export async function run() { - // This function should be used instead of `new Sequelize()`. - // It applies the config for your SSCCE to work on CI. - const sequelize = createSequelize6Instance({ - logQueryParameters: true, - benchmark: true, - define: { - // For less clutter in the SSCCE - timestamps: false, - }, - }); - - class Foo extends Model {} - - Foo.init({ - name: DataTypes.TEXT, - }, { - sequelize, - modelName: 'Foo', - }); - - // You can use sinon and chai assertions directly in your SSCCE. - const spy = sinon.spy(); - sequelize.afterBulkSync(() => spy()); - await sequelize.sync({ force: true }); - expect(spy).to.have.been.called; - - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); -} diff --git a/src/sscce-sequelize-7.ts b/src/sscce-sequelize-7.ts index 603cb219c..d6f130a91 100644 --- a/src/sscce-sequelize-7.ts +++ b/src/sscce-sequelize-7.ts @@ -1,8 +1,9 @@ -import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core'; -import { Attribute, NotNull } from '@sequelize/core/decorators-legacy'; +import { BelongsToMany } from '@sequelize/core/decorators-legacy'; +import { BelongsToManyAddAssociationMixin, DataTypes, Model } from '@sequelize/core'; import { createSequelize7Instance } from '../dev/create-sequelize-instance'; import { expect } from 'chai'; import sinon from 'sinon'; +import { BelongsToManyGetAssociationsMixin } from 'sequelize'; // 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']); @@ -11,6 +12,18 @@ export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', // Your SSCCE goes inside this function. export async function run() { + class From extends Model { + @BelongsToMany(() => To, { through: () => Through}) + declare to?: To[]; + + declare addTo: BelongsToManyAddAssociationMixin; + declare getTo: BelongsToManyGetAssociationsMixin + } + + class To extends Model {} + + class Through extends Model {} + // This function should be used instead of `new Sequelize()`. // It applies the config for your SSCCE to work on CI. const sequelize = createSequelize7Instance({ @@ -20,24 +33,35 @@ export async function run() { // For less clutter in the SSCCE timestamps: false, }, + models: [From, To, Through] }); - class Foo extends Model, InferCreationAttributes> { - declare id: CreationOptional; + await sequelize.sync(); - @Attribute(DataTypes.TEXT) - @NotNull - declare name: string; - } + const [from, to] = await Promise.all([ + From.create(), + To.create() + ]); + + await from.addTo(to); + + const eagerLoadedTo = (await From.findOne({ + include: [To], + rejectOnEmpty: true + })).to!; + + const lazyLoadedTo = await (await From.findOne({ + rejectOnEmpty: true + })).getTo(); - sequelize.addModels([Foo]); + console.log(eagerLoadedTo); + console.dir(lazyLoadedTo); - // You can use sinon and chai assertions directly in your SSCCE. - const spy = sinon.spy(); - sequelize.afterBulkSync(() => spy()); - await sequelize.sync({ force: true }); - expect(spy).to.have.been.called; + // The through model is loaded as the model name for eager-loaded associations + expect('Through' in eagerLoadedTo); + expect(!('fromTo' in eagerLoadedTo)); - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); + // But as the name of the association + expect(!('Through' in lazyLoadedTo)); + expect('fromTo' in lazyLoadedTo); }