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

Add reproduction #259

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\setup\\runner.js",
"runtimeExecutable": "npm",
"args": [
"run",
"test:sqlite"
],
// "program": "${workspaceFolder}\\setup\\runner.ts",
"env": {
"LOCAL_SSCCE": "true",
"DIALECT": "sqlite"
Expand Down
51 changes: 47 additions & 4 deletions src/sscce-sequelize-6.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataTypes, Model } from 'sequelize';
import { BelongsToGetAssociationMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model } from 'sequelize';
import { createSequelize6Instance } from '../setup/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
Expand All @@ -21,21 +21,64 @@ export async function run() {
},
});

class Foo extends Model {}
class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
declare id?: string;
declare name: string;
declare BarId: ForeignKey<Bar['id']>;
declare getBar: BelongsToGetAssociationMixin<Bar>;
}
class Bar extends Model<InferAttributes<Bar>, InferCreationAttributes<Bar>> {
declare id?: string;
declare name: string;
}

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

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

Bar.hasMany(Foo);
Foo.belongsTo(Bar);

// 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);
const theBar = await Bar.create({ name: 'TS bar' });
const theFoo = await Foo.create({ name: 'TS foo', BarId: theBar.id });

// All is fine, BarId is correctly filled in the model, due to the create
expect(await theFoo?.getBar()).to.not.be.null;

// If have a function in my code that gives me the attributes and include automatically. But in some cases, I want to be
// manually able to lazy load a specific relationship
const theFreshFoo = await Foo.findOne({
where: {
name: theFoo.get("name")
},
attributes: ["name"]
})
// And when trying to load the relation, the relationship attribute is missing, so I simply get null
expect(await theFreshFoo?.getBar()).to.not.be.null;
}