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

Compatibility with sequelize v5/v6 #159

Closed
killerfurbel opened this issue Jun 5, 2020 · 3 comments
Closed

Compatibility with sequelize v5/v6 #159

killerfurbel opened this issue Jun 5, 2020 · 3 comments

Comments

@killerfurbel
Copy link

killerfurbel commented Jun 5, 2020

sequelize adapted to the tedious api and is now checking a connection.state versus a connection.STATE.INITIALIZED property. If those match, the .connect() function is called.

See sequelize/sequelize#12182 (comment)

Since the guys from sequelize won't fix that code so it would still work with msnodesqlv8, this module needs to be adapted.

In the first place, the .INITIALIZED enum needs to be defined. Otherwise, this will lead to an uncaught exception. As for the current implementation, the if statement should not return true since the .connect() should not be called sync, but it is called from the constructor after the driver has been detected.

I could add a dummy STATE if this is desired...

@TimelordUK
Copy link
Owner

Sorry I am still not sure what needs to be done to make this work again ?

@killerfurbel
Copy link
Author

The easiest (but dirty) way is to add a line like this to the constructor of Connection in lib/sequelize/connection.js:

this.STATE = {INITIALIZED: 1}

Within sequelize, the following code is executed:

const connection = new this.lib.Connection(connectionConfig);
if (connection.state === connection.STATE.INITIALIZED) {
    connection.connect();
}

This will not throw an error then and will compare undefined with 1 which leads to false. The extra connection.connect() would not be executed. The connect() is not needed/wanted anyway, because it will be done in the constructor after driver detection.


The more comprehensive way would be

  • implementing the whole state-handling the way tedious is doing it.
  • not executing the connect() in the constructor any more, but let sequelize call it
  • since .connect() is called sync directly after the constructor, moving the driver detection to the connect method

@TimelordUK
Copy link
Owner

thanks for letting me know the fix - I went with the simple solution for now

should be fixed in latest version 1.1.7

"dev\js\sql\node_modules\msnodesqlv8\samples\javascript\sequelize.js
Executing (1c5937d5-fbba-4665-b2d9-2bf852cc487c): IF OBJECT_ID('[users]', 'U') IS NOT NULL DROP TABLE [users];
Executing (1c5937d5-fbba-4665-b2d9-2bf852cc487c): IF OBJECT_ID('[users]', 'U') IS NULL CREATE TABLE [users] ([id] INTEGER NOT NULL IDENTITY(1,1) , [username] NVARCHAR(255) NULL, [job] NVARCHAR(255) NULL, [createdAt] DATETIMEOFFSET NOT NULL, [updatedAt] DATETIMEOFFSET NOT NULL, PRIMARY KEY ([id]));
Executing (1c5937d5-fbba-4665-b2d9-2bf852cc487c): EXEC sys.sp_helpindex @objname = N'[users]';
Executing (1c5937d5-fbba-4665-b2d9-2bf852cc487c): INSERT INTO [users] ([username],[job],[createdAt],[updatedAt]) OUTPUT INSERTED.[id],INSERTED.[username],INSERTED.[job],INSERTED.[createdAt],INSERTED.[updatedAt] VALUES (@0,@1,@2,@3);
Executing (ad3decdb-8914-44c4-a58c-eb3908c7f6ad): INSERT INTO [users] ([username],[job],[createdAt],[updatedAt]) OUTPUT INSERTED.[id],INSERTED.[username],INSERTED.[job],INSERTED.[createdAt],INSERTED.[updatedAt] VALUES (@0,@1,@2,@3);
Executing (f2b32239-e8e9-4fb3-a767-beee06d6b49c): INSERT INTO [users] ([username],[job],[createdAt],[updatedAt]) OUTPUT INSERTED.[id],INSERTED.[username],INSERTED.[job],INSERTED.[createdAt],INSERTED.[updatedAt] VALUES (@0,@1,@2,@3);
Executing (f2b32239-e8e9-4fb3-a767-beee06d6b49c): SELECT [id], [username], [job], [createdAt], [updatedAt] FROM [users] AS [user] WHERE [user].[id] = 3;
{
    "id": 3,
    "username": "techno03",
    "job": "Agile Leader",
    "createdAt": "2020-07-05T14:17:54.670Z",
    "updatedAt": "2020-07-05T14:17:54.670Z"
}
Executing (f2b32239-e8e9-4fb3-a767-beee06d6b49c): SELECT [id], [username], [job], [createdAt], [updatedAt] FROM [users] AS [user] WHERE [user].[job] = N'Agile Leader' ORDER BY [user].[id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
{
    "id": 3,
    "username": "techno03",
    "job": "Agile Leader",
    "createdAt": "2020-07-05T14:17:54.670Z",
    "updatedAt": "2020-07-05T14:17:54.670Z"
}
Executing (f2b32239-e8e9-4fb3-a767-beee06d6b49c): SELECT [id], [username], [job], [createdAt], [updatedAt] FROM [users] AS [user];
[
    {
        "id": 1,
        "username": "techno01",
        "job": "Programmer",
        "createdAt": "2020-07-05T14:17:54.669Z",
        "updatedAt": "2020-07-05T14:17:54.669Z"
    },
    {
        "id": 2,
        "username": "techno02",
        "job": "Head Programmer",
        "createdAt": "2020-07-05T14:17:54.670Z",
        "updatedAt": "2020-07-05T14:17:54.670Z"
    },
    {
        "id": 3,
        "username": "techno03",
        "job": "Agile Leader",
        "createdAt": "2020-07-05T14:17:54.670Z",
        "updatedAt": "2020-07-05T14:17:54.670Z"
    }
]
Executing (f2b32239-e8e9-4fb3-a767-beee06d6b49c): SELECT count(*) AS [count] FROM [users] AS [user] WHERE [user].[job] LIKE N'%Programmer';
Executing (1c5937d5-fbba-4665-b2d9-2bf852cc487c): SELECT [id], [username], [job], [createdAt], [updatedAt] FROM [users] AS [user] WHERE [user].[job] LIKE N'%Programmer' ORDER BY [user].[id] OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY;
2
[
  {
    id: 1,
    username: 'techno01',
    job: 'Programmer',
    createdAt: 2020-07-05T14:17:54.669Z { nanosecondsDelta: 0 },
    updatedAt: 2020-07-05T14:17:54.669Z { nanosecondsDelta: 0 }
  },
  {
    id: 2,
    username: 'techno02',
    job: 'Head Programmer',
    createdAt: 2020-07-05T14:17:54.670Z { nanosecondsDelta: 0 },
    updatedAt: 2020-07-05T14:17:54.670Z { nanosecondsDelta: 0 }
  }
]
Executing (1c5937d5-fbba-4665-b2d9-2bf852cc487c): UPDATE [users] SET [job]=@0,[updatedAt]=@1 WHERE [id] = @2
[ 0 ]
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants