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

Document how to use TypeORM #112

Closed
jackwotherspoon opened this issue May 15, 2023 · 17 comments · Fixed by #255
Closed

Document how to use TypeORM #112

jackwotherspoon opened this issue May 15, 2023 · 17 comments · Fixed by #255
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jackwotherspoon
Copy link
Collaborator

Issue to track interest in support for TypeORM with the Cloud SQL Node.js Connector.

Upvoting this issue 👍 will help us prioritize support.

@jackwotherspoon jackwotherspoon added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p2 Moderately-important priority. Fix may not be included in next release. labels May 15, 2023
@tjhiggins
Copy link

Seems to work in typeorm if you use the extra block:

const connector = new Connector();
const clientOpts = await connector.getOptions({
  instanceConnectionName: '<connection_name>',
  ipType: IpAddressTypes.PRIVATE,
  authType: AuthTypes.IAM,
});

return {
  type: 'postgres',

  username: '<username>',
  database: '<database>',

  extra: {
    ...clientOpts,
    max: 5,
  },

  synchronize: true,
  autoLoadEntities: true,
};

@enocom enocom changed the title Consider support for TypeORM Document how to use TypeORM May 23, 2023
@enocom enocom added the type: docs Improvement to the documentation for an API. label May 23, 2023
@enocom
Copy link
Member

enocom commented May 23, 2023

That's great news. In that case, this is just a docs issue.

@jackwotherspoon
Copy link
Collaborator Author

FYI: TypeORM actually also supports automatic IAM authentication without the need for the use of this Connector library at all. typeorm/typeorm#4724

You can leverage passing a function to the password field in order to access a new access token to be used for IAM authentication.

@chadxz
Copy link

chadxz commented Aug 25, 2023

seems to me the connector option with the configuration shared by @tjhiggins is lower maintenance and better supported.

@enocom
Copy link
Member

enocom commented Aug 25, 2023

Both are feasible options. It depends on a person's appetite for adopting a Connector vs a generic Postgres interface.

@edosrecki
Copy link
Contributor

edosrecki commented Sep 6, 2023

@jackwotherspoon, @chadxz, @enocom, @tjhiggins

typeorm uses mssql driver, whose latest version (v9.3.2) uses tedious@^15.0.1, which does not support connector property. Therefore, following does not work:

new DataSource({
    type: 'mssql',
    host: '0.0.0.0', // dummy due to tedious bug
    username: 'my-user',
    password: 'my-password',
    database: 'my-database',
    extra: {
      options: {
        ...clientOpts,
      },
    },
  })

However, I see that upgrade of tedious in mssql has just been merged (tediousjs/node-mssql@4968b3b). Once this is released, the aforementioned snippet will work.

@edosrecki
Copy link
Contributor

edosrecki commented Sep 6, 2023

Ah, of course, this was released in a new major version of mssql, v10.

However, typeorm officially only supports mssql@v9 (https://github.com/typeorm/typeorm/blob/befe4f9cbf17139bfaba83ae8a00e56b071df334/package.json#L152C23-L152C23).

Before typeorm updates the peerDependency version for mssql, we cannot say that connector works with it.

@edosrecki
Copy link
Contributor

Created PR in typeorm to support mssql@10: typeorm/typeorm#10356

cc: @jackwotherspoon, @chadxz, @enocom, @tjhiggins

@jackwotherspoon
Copy link
Collaborator Author

Created PR in typeorm to support mssql@10: typeorm/typeorm#10356

Wow! Thanks so much @edosrecki we really appreciate the contributions you are making 😄

@edosrecki
Copy link
Contributor

Happy to contribute. I discovered a bug in typeorm tests which blocks this PR from becoming green, so let's see how responsive the team there is to roll this all out, but I guess it might take some time. 😄

@ruyadorno
Copy link
Contributor

That sounds good 👍 we'll keep a close eye on that PR 👀

@weilinzung
Copy link

Seems to work in typeorm if you use the extra block:

const connector = new Connector();
const clientOpts = await connector.getOptions({
  instanceConnectionName: '<connection_name>',
  ipType: IpAddressTypes.PRIVATE,
  authType: AuthTypes.IAM,
});

return {
  type: 'postgres',

  username: '<username>',
  database: '<database>',

  extra: {
    ...clientOpts,
    max: 5,
  },

  synchronize: true,
  autoLoadEntities: true,
};

@tjhiggins Is working for you with Typeorm? We are using mysql2 with Typeorm v2, but not working at all after deploying to Cloud Run.

    extra: {
                ...driverConnectorOptions
              }
"No repository for \"TestForm\" was found. Looks like this entity is not registered in current \"default\" connection?",

@tjhiggins
Copy link

Seems to work in typeorm if you use the extra block:

const connector = new Connector();
const clientOpts = await connector.getOptions({
  instanceConnectionName: '<connection_name>',
  ipType: IpAddressTypes.PRIVATE,
  authType: AuthTypes.IAM,
});

return {
  type: 'postgres',

  username: '<username>',
  database: '<database>',

  extra: {
    ...clientOpts,
    max: 5,
  },

  synchronize: true,
  autoLoadEntities: true,
};

@tjhiggins Is working for you with Typeorm? We are using mysql2 with Typeorm v2, but not working at all after deploying to Cloud Run.

    extra: {
                ...driverConnectorOptions
              }
"No repository for \"TestForm\" was found. Looks like this entity is not registered in current \"default\" connection?",

I can only confirm it worked with postgres. It seems like it's still an issue with MySQL based on:
#112 (comment)

@edosrecki
Copy link
Contributor

edosrecki commented Oct 21, 2023

@weilinzung, @tjhiggins

It works with ALL drivers, however, not officially with SQL Server (mssql library) since typeorm only supports v9, but v10 is required for Connector to work. But, you can force install mssql@10 (to suppress npm errors saying that you are using wrong peer dependency) and it will work. But some people might not want to do that in production.

@winterec
Copy link

Seems to work in typeorm if you use the extra block:

const connector = new Connector();
const clientOpts = await connector.getOptions({
  instanceConnectionName: '<connection_name>',
  ipType: IpAddressTypes.PRIVATE,
  authType: AuthTypes.IAM,
});

return {
  type: 'postgres',

  username: '<username>',
  database: '<database>',

  extra: {
    ...clientOpts,
    max: 5,
  },

  synchronize: true,
  autoLoadEntities: true,
};

This is working for me only when using the pg package. If pg-native is installed then TypeORM will automatically use that instead and this config doesn't work.

ruyadorno added a commit that referenced this issue Nov 22, 2023
Adds TypeORM samples along with a new CI target to run tests against the
provided samples, making sure they're always valid usage.

Fixes: #112
ruyadorno added a commit that referenced this issue Nov 22, 2023
Adds TypeORM samples along with a new CI target to run tests against the
provided samples, making sure they're always valid usage.

Fixes: #112
@enocom
Copy link
Member

enocom commented Nov 22, 2023

@ruyadorno shall we open an issue for pg-native support? Since it uses libpq under the hood, I wouldn't expect the connector to work with it.

ruyadorno added a commit that referenced this issue Nov 23, 2023
Adds TypeORM samples along with a new CI target to run tests against the
provided samples, making sure they're always valid usage.

Fixes: #112
ruyadorno added a commit that referenced this issue Nov 23, 2023
Adds TypeORM samples along with a new CI target to run tests against the
provided samples, making sure they're always valid usage.

Fixes: #112
ruyadorno added a commit that referenced this issue Nov 24, 2023
Adds TypeORM samples along with a new CI target to run tests against the
provided samples, making sure they're always valid usage.

Fixes: #112

Co-authored-by: Jack Wotherspoon <jackwoth@google.com>
@ruyadorno
Copy link
Contributor

@ruyadorno shall we open an issue for pg-native support? Since it uses libpq under the hood, I wouldn't expect the connector to work with it.

it might also be an opportunity to wait and see if the community will ask for it instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants