Skip to content

Commit

Permalink
implement SSCCE
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Mar 25, 2024
1 parent 97871ef commit a029235
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 57 deletions.
41 changes: 0 additions & 41 deletions src/sscce-sequelize-6.ts

This file was deleted.

56 changes: 40 additions & 16 deletions src/sscce-sequelize-7.ts
Original file line number Diff line number Diff line change
@@ -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']);
Expand All @@ -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<To, number>;
declare getTo: BelongsToManyGetAssociationsMixin<To>
}

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({
Expand All @@ -20,24 +33,35 @@ export async function run() {
// For less clutter in the SSCCE
timestamps: false,
},
models: [From, To, Through]
});

class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
declare id: CreationOptional<number>;
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);
}

0 comments on commit a029235

Please sign in to comment.