typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.

Home Page:http://typeorm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating Databases using typeorm

omidnavy opened this issue · comments

Hi
I wanted to create a schema if it does not exists , So is this possible with typeorm ?
e.g is there anyway to make "database" optional in Connection object ? as I Know the mysql driver can handle this connection I'm not sure about others. And is there any way to have "custom queries" like "CREATE DATABASE IF NOT EXISTS" or making a function for this ?

Can u help me modifying this ?

yeah to run a custom query you can use connection.query method:

connection.query("CREATE DATABASE IF NOT EXISTS");

Thanks this can do the job , but are you planning to add this feature (opening connections without specifying database ) so this can be done with a simple function like other functions ?

Yeah I'll think how to fix this issue

You can now open connections without specifying database in SQL Server and MySql. Dynamic database creation also supported. Using multiple databases in a single connection. This feature will be released in 0.1.0-alpha.48

@AlexMesser is it supported for Postgres?

@pleerock @AlexMesser This is currently not supported with Postgres, right?

I am also using Postgres and would like to know. Bump

Any news to get this done for Postgres?

It would be nice if there was something like rails' rails db:setup or rails db:create db:migrate

Postgres support would be so nice.

commented

I haven't managed to get this working with MariaDB. Any progress on this?

commented

Actually, it'd be awesome if the migrations script, instead of failing on ER_BAD_DB_ERROR (non-existent DB) just created DB with that name. It'd automate some infrastructure jobs very nicely.

Any update on the hasDatabase() implementation for postgresql??

+1 could we get a script to create the database?

  • 1 for postgres

Does anyone have a workaround they'd like to share?

@tcd I have created a tiny library to create and drop PostgreSQL databases.
The only dependency of the library is pg which is also required by TypeORM anyway.

See here for detailed doc https://github.com/ivawzh/pg-god#with-typeorm

I'm a bit confused. database is mandatory in order to create a new connection. How would I run connection.query('create database') if I need a database from the beggining. 😕

In case of PostgreSQL you can use default databases for initial bootstrapping: postgres, template0 and template1. That is - simply use them for connection, do your deployment work and then reconnect to the newly created DB.

Granted, there are scenarios when this DBs can be unavailable, but those are unlikely.

commented

add the possibility to create the DB running a migration please

we still don't have a solution.. Ruby on Rails's rake db:create and rake db:drop works perfect almost from the beginning of rails

commented

You can also use https://www.npmjs.com/package/typeorm-extension package, to create or drop the database specified for the connection, for nearly all database driver.
The package also parses the extra parameter charset and characterSet of the extra parameter (ENV: TYPEORM_DRIVER_EXTRA)

import {createDatabase} from "typeorm-extension";

(async () => {
    await createDatabase({ifNotExist: true});
   
    await dropDatabase({ifExist: true});

    process.exit(0);
})();

You can also provide the charset and characterSet as properties for the first parameter to the createDatabase() function.
F.e

  • postgres
createDatabase({ifNotExist: true, characterSet: "UTF8"});
  • mysql
createDatabase({ifNotExist: true, charset: "utf8mb4_general_ci", characterSet: "utf8mb4"});

If you have any questions or concerns feel free to contact me or contribute to the codebase on github https://github.com/Tada5hi/typeorm-extension/issues :)

You can also use https://www.npmjs.com/package/typeorm-extension package, to create or drop the database specified for the connection, for nearly all database driver. The package also parses the extra parameter charset and characterSet of the extra parameter (ENV: TYPEORM_DRIVER_EXTRA)

import {createDatabase} from "typeorm-extension";

(async () => {
    await createDatabase({ifNotExist: true});
   
    await dropDatabase({ifExist: true});

    process.exit(0);
})();

You can also provide the charset and characterSet as properties for the first parameter to the createDatabase() function. F.e

  • postgres
createDatabase({ifNotExist: true, characterSet: "UTF8"});
  • mysql
createDatabase({ifNotExist: true, charset: "utf8mb4_general_ci", characterSet: "utf8mb4"});

If you have any questions or concerns feel free to contact me or contribute to the codebase on github https://github.com/Tada5hi/typeorm-extension/issues :)

Be careful about using this library on production, any unwanted changes in the library can wipe out your database!

Be careful about using this library on production, any unwanted changes in the library can wipe out your database!

Agree, but... you also should avoid using a database user with this kind of power in production... At most, the database users in production should be able to insert/select/update.

commented

Be careful about using this library on production, any unwanted changes in the library can wipe out your database!

This can happen literally with any open source library, even with this one or direct dependencies of it. To be safe check file changes on upgrading to a new version of any library you use.

Agree, but... you also should avoid using a database user with this kind of power in production... At most, the database users in production should be able to insert/select/update.

Definitly got a point there. I'm working in a medical context for online- & federated-learning besides working on open-source projects and we are running our application in a containerized docker infrastructure and don't expose the database server to the public.

Not having this feature in TypeORM, is especially annoying when using docker. With Sequelize you simply execute npx sequelize db:create && npx sequelize db:migrate as part of the deploy/startup routine.

Why was this issue closed? such a nice feature to have for postgres, it would help automate the new environment deployments.