Dynamically switch dataSource at runtime - nestjs

I would like to know if it's possible to set the database in a connection at runtime (nestjs with TypeOrm and postgres). I'm having a system where there are many tenants and each one has it's own isolated database. The structure of the databases are all exactly the same and i want to use the same entity classes in my code.So after a user logs in, i want to set the database name to be used at runtime.
I cheked few solution working with : Connection, createConnection, getConnectionManager of'typeorm' , which are deprecated , i Wonder if this is possible with DataSource and how can i implement named connection with dataSource (equivalent of getConnection(tenant.name))
ps: i am using '#nestjs/passport' guards
Thanks.

Related

Mongoose Sharing Validation/Pre-Save Methods Across NodeJS Apps

If one Nodejs app connects to a Mongo instance, and that app has defined a User schema with pre-save hooks, validation, etc.
And then another Nodejs app connects to the same database, and tries to register a User schema with different properties.
And then the second app saves a User
What happens?
I'm confused with how two Nodejs apps may communicate to the same database.
For example, it's very easy to see how one might want to have V2 of an api on a separate nodejs app developed by a separate team. But they will plug it into the same database and use the same Schema (or will they?), and I'm confused with how things are shared between the two apps.
Any help clarifying this in best-practices would be appreciated
I believe I've found the answer in the Documentation.
This connection object is then used to create and retrieve models. Models are always scoped to a single connection. docs
And
Models are fancy constructors compiled from our Schema definitions. docs
Which explains that a DB Connection 1's Schema Definitions (pre-save, etc), do not affect DB Connection 2's writes/etc.
Essentially, they are completely independent of validation and everything else. They only need to be OK in their own context.

how to use different database with same table structure in typed dataset xsd

I'm confused how to explain myself well here is my scenario.
I have a database named "database1" and i have used typed dataset in visual studio and have added more than 200 stored procedures in the table adapters.
it is a desktop based application. now i want to deployee the same software in other school same database but i have changed the database name
when i generate new database from query and write all the stored procedures in the database and change the database name in the connection string it doesn't work.
I'd recommend you don't change the database name, or if it's something specific to the first client (like ParisTechnicalCollegeDatabase), change it now to something generic (SchoolManager) so you never have to change it again. There's no specific problem I can think of regards reusing a typed dataSet on a different database: I do it daily on database that aren't clones of each other. Ensure your second server is set up with a user and default schema that is specified in the connection string. The problem will eithe per be faulty connection string or incorrect database setup, not the fault of the dataSet
For more targeted help, post up the error messages that appear when you try to run your app on the new database

How do I use flask Migrate and SQLAlchemy on my own database, and connect to a third party database that I do not wish to migrate?

I have discovered a way to connect to multiple databases with Flask through binds.
Facts leading up to question:
I have defined my own database Models with SQLAlchemy.
I use Flask-Migrate to generate my database
I wish to connect to a third party database and use the SQLAlchemy ORM.
I wish to query the third party database using the SQLAlchemy ORM in a way such as: db.session.query(THIRDPARTYMODEL).filter_by(id=id)
I'm worried, that once I define the new models and use Flask Migrate, that it will try to create a new migration and create the tables I already have.
I wish to use my own database and the third party database at the same time.
How can I connect to the third party database and query it using the SQLAchemy ORM without worrying about Flask Migrate trying to generate new tables on the third party database?
Flask-Migrate does not automatically migrate multiple databases, the default is to only migrate the main database from Flask-SQLAlchemy.
The problem is that if you have additional models created that belong to another database, Alembic will consider them part of that main database, so it will write migrations for your main database that include the entire set of models.
To have things working the way you want, you will need to ensure those other models that do not belong in the main database are not imported when you invoke the manage.py db ... set of commands.
You may also tell Flask-Migrate and Alembic to support migrating multiple databases by using flask db init --multidb when initializing the migrations.
I have the same issue, but I wasn't able to use the accepted include_object solution because that method is called too far into the migration on the non-migrated database. In my case, the user doesn't have write access, so the migration failed as soon as Alembic tried to make the alembic_version table on the remote DB. This happened before include_object was called on any of the tables.
Instead, because I wanted Alembic to completely ignore this database and all its tables, I simply put the read-only database in SQLALCHEMY_BINDS and then removed it in migrations/env.py before it was processed:
# ===Generated code===
bind_names = []
for name, url in current_app.config.get("SQLALCHEMY_BINDS").items():
context.config.set_section_option(name, "sqlalchemy.url", url.replace(
'%', '%%'))
bind_names.append(name)
# ===New code===
### Remove the 'remote' bind, which is a read-only database that we don't
### want to track migration changes on.
if 'remote' in bind_names:
bind_names.remove('remote')

How to change/specify database used during runtime with Sails.js

Sail.js has ORM, I can make queries to databases.
I want to make a frontend that uses AJAX to communicate with the backend. But I want the backend to query different databases based on an id/name given with the ajax query. And it would be good to be able to add/remove those same databases during runtime.
Is this possible with Sails.js? I can only find ways to have a static amount of DBs (declared in conf files).

Can't switch to database other than _system within Foxx

I'm trying to dynamically generate databases/collections on application startup, in case they do not exist yet. This works fine when working with the _system database. The thing is that it seems not to be allowed when trying to switch to other databases. From the docs:
db._useDatabase(name)
Changes the current database to the database specified by name. Note
that the database specified by name must already exist.
Changing the database might be disallowed in some contexts, for
example server-side actions (including Foxx).
Does this mean that Foxx applications can only create collections within the _system database? My manifest file sets the isSystem property to false. What is the meaning of such limitation?
A Foxx is included in one database context and has access to the collections there.
You can install Foxx apps in different databases.
f.E. you can run the following commands in arangosh to install the foxx app "example" in the database "myDB":
db._useDatabase("myDB")
fm.install("example","/example")
your app will than be reachable at (assuming standard configuration):
http://localhost:8529/_db/myDB/example
It is intentionally not possible to access a different database from within a Foxx app.

Resources