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

Table prefix and suffix for multi-tenant environment #125

Closed
fourdim opened this issue Mar 14, 2022 · 3 comments · Fixed by #126
Closed

Table prefix and suffix for multi-tenant environment #125

fourdim opened this issue Mar 14, 2022 · 3 comments · Fixed by #126

Comments

@fourdim
Copy link
Contributor

fourdim commented Mar 14, 2022

Feature needed
Hi, there. I've read the wiki and learnt that the solution for jet to manage multi-tenant environment is to use the function:

Artist2 := Artist.FromSchema("chinook2")

As MySQL treats schema the same as database, I would like to have a multi-tenant environment based on the prefix or suffix of tables.

Suggested solution
Modify the generator by adding the functions like:

Artist2 := Artist.WithPrefix("chinook2")
Artist3 := Artist.WithSuffix("chinook2")
func (a {{tableTemplate.TypeName}}) WithPrefix(prefix string) {{tableTemplate.TypeName}} {
	return new{{tableTemplate.TypeName}}(a.SchemaName(), prefix+a.TableName(), a.Alias())
}

Or simply export the function new{{tableTemplate.TypeName}}.

@go-jet
Copy link
Owner

go-jet commented Mar 15, 2022

Hi @fourdim. Yeah, it make sense to add WithPrefix and WithSufix methods.

What's your plan in general?
To have tables like artist, tenat01.artist, tenant02.artist, etc...
Where artist table will be used just as a template for jet generation, and other tables would be ignored by jet generator.

If we change table name, we will also change default aliasing so Artist.WithPrefix("tenat01.") would expect Tenant01Artist model type as destination. So If we want destination to be generic Artist model type, we would need probably to set alias to TableName() inside WithPrefix/Sufix method.

     return new{{tableTemplate.TypeName}}(a.SchemaName(), prefix+a.TableName(), a.TableName())

@fourdim
Copy link
Contributor Author

fourdim commented Mar 15, 2022

What's your plan in general?

My plan is to have artist, tenat01_artist, tenant02_artist, as the . may be treated as an operator in MySQL. Also, artist table would be the template, which was exactly what I was thinking.

If we change table name, we will also change default aliasing so Artist.WithPrefix("tenat01.") would expect Tenant01Artist model type as destination. So If we want destination to be generic Artist model type, we would need probably to set alias to TableName() inside WithPrefix/Sufix method.

It seems that you are right. And I think this plan is good enough for me to solve the multi-tenant problem.

@go-jet
Copy link
Owner

go-jet commented Mar 16, 2022

Do you want to open a PR? Contributions are always welcomed.

@go-jet go-jet added this to the Version 2.8.0 milestone May 14, 2022
@go-jet go-jet mentioned this issue May 17, 2022
@go-jet go-jet closed this as completed in 87cc6c9 May 17, 2022
masterkidan pushed a commit to getoutreach/jet that referenced this issue Dec 2, 2022
* add table prefix and suffix for multi-tenant environment support
This closes go-jet#125.

* Update generator tests

* Bump github.com/lib/pq from 1.7.0 to 1.10.5

Bumps [github.com/lib/pq](https://github.com/lib/pq) from 1.7.0 to 1.10.5.
- [Release notes](https://github.com/lib/pq/releases)
- [Commits](lib/pq@v1.7.0...v1.10.5)

---
updated-dependencies:
- dependency-name: github.com/lib/pq
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* fix -ignore-tables, -ignore-enums and -ignore-views when -dsn is present

* Bump github.com/jackc/pgx/v4 from 4.11.0 to 4.16.0

Bumps [github.com/jackc/pgx/v4](https://github.com/jackc/pgx) from 4.11.0 to 4.16.0.
- [Release notes](https://github.com/jackc/pgx/releases)
- [Changelog](https://github.com/jackc/pgx/blob/master/CHANGELOG.md)
- [Commits](jackc/pgx@v4.11.0...v4.16.0)

---
updated-dependencies:
- dependency-name: github.com/jackc/pgx/v4
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Add support for CockorachDB.

* Add support for EXTRACT time/date function.

* Generator code cleanup.

* Improve doc.

* [Bug] Statement Query and Exec methods can not be used with sql.Conn

* Update dependencies.

* [Bug] DebugSQL panics with libraries that do not implemente Stringer interface.

* Update README.md

* Add multi-tenant table `WithPrefix`, `WithSuffix` tests.

* Add multi-tenant table `WithPrefix`, `WithSuffix` tests.

* mysql: export some conditional functions

COALESCE, NULLIF, GREATEST and LEAST already existed
and were available in the postgres dialect, but not in MySQL.

* add postgres json literal

* Better json func, and tests

* Update badge links.

* Add missing sqlite conditional functions.

* add missing SET method to ColumnTimez interface

it has been already implemented, just missing from interface

* [MySQL] Add NEW alias for the rows to be inserted.

* Go fmt.

* Add omitted reserved word 'right' on postgresql

* [MySQL] Optimizer hints

* [postgres] Add Json literal test.

* Potential security flaw

I am using go-jet as a go library for an application that uses dynamic database credentials and noticed this potential security risk that exposes the whole database credentials (though it is timed, it is still a risk)

* Removed dsn from generator

* fix: Updating query for types to the target namespace

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: fourdim <[email protected]>
Co-authored-by: go-jet <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Nikita Konin <[email protected]>
Co-authored-by: go-jet <[email protected]>
Co-authored-by: Karl Blomster <[email protected]>
Co-authored-by: Vladimír Zahradník <[email protected]>
Co-authored-by: Stefan <[email protected]>
Co-authored-by: S T E P H E N <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants