Skip to content

SSCCE for Sequelize 7 models issue #263

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

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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"cross-env": "^7",
"fs-jetpack": "^4",
"sequelize": "^6",
"@sequelize/core": "^7.0.0-alpha.10",
"@sequelize/core": "^7.0.0-alpha.27",
"sinon": "^13",
"sinon-chai": "^3"
},
Expand Down Expand Up @@ -52,6 +52,6 @@
"lodash": "^4.17.21",
"sqlite3": "^5",
"ts-node": "^10.4.0",
"typescript": "~4.5"
"typescript": "5.0"
}
}
41 changes: 0 additions & 41 deletions src/sscce-sequelize-6.ts

This file was deleted.

79 changes: 60 additions & 19 deletions src/sscce-sequelize-7.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,82 @@
import { DataTypes, Model } from '@sequelize/core';
import { createSequelize7Instance } from '../setup/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
import { DataTypes, Model } from "@sequelize/core";
import { createSequelize7Instance } from "../setup/create-sequelize-instance";
import { Attribute } from "@sequelize/core/decorators-legacy";
import type { Sequelize } from "@sequelize/core";
import { expect } from "chai";

// 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 7
class User extends Model {
@Attribute({
type: DataTypes.STRING,
allowNull: false,
})
declare username: string;

@Attribute({
type: DataTypes.DATE,
allowNull: false,
})
declare birthday: Date;
}

// 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 = createSequelize7Instance({
let sequelize: Sequelize | undefined = createSequelize7Instance({
logQueryParameters: true,
benchmark: true,
define: {
// For less clutter in the SSCCE
timestamps: false,
},
});
sequelize.addModels([User]);
await sequelize.sync({ force: true });

const jane = await User.create({
username: "janedoe",
birthday: new Date(1980, 6, 20),
});

class Foo extends Model {}
console.log("\nJane:", jane.toJSON());

Foo.init({
name: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Foo',
await sequelize.close();
sequelize = undefined;

expect(jane.username).to.equal("janedoe");

const anotherSequelize = createSequelize7Instance({
logQueryParameters: true,
benchmark: true,
define: {
// For less clutter in the SSCCE
timestamps: false,
},
});

// 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;
// Since the previous instance has been closed
// I expect that I can create another instance
// and associate the model to this instance
anotherSequelize.addModels([User]);

const john = await User.create({
username: "johndoe",
birthday: new Date(1990, 6, 20),
});
console.log("John:", john.toJSON());

await anotherSequelize.close();

console.log(await Foo.create({ name: 'TS foo' }));
expect(await Foo.count()).to.equal(1);
expect(john.username).to.equal("johndoe");
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
],
"compilerOptions": {
"target": "es2021",
"module": "CommonJS",
"moduleResolution": "Node",
"module": "Node16",
"moduleResolution": "Node16",
"esModuleInterop": true,
"resolveJsonModule": true,
"experimentalDecorators": true,
"newLine": "lf",
"stripInternal": true,
"strict": true,
Expand Down