Move datasource to own file for typeorm cli
This commit is contained in:
parent
a5b337b99a
commit
8aa355dee7
@ -14,7 +14,8 @@
|
|||||||
"generate:rights": "node scripts/rights.js",
|
"generate:rights": "node scripts/rights.js",
|
||||||
"generate:schema": "node scripts/schema.js",
|
"generate:schema": "node scripts/schema.js",
|
||||||
"generate:client": "node scripts/client.js",
|
"generate:client": "node scripts/client.js",
|
||||||
"generate:changelog": "node scripts/changelog.js"
|
"generate:changelog": "node scripts/changelog.js",
|
||||||
|
"generate:migration": "node node_modules/typeorm/cli.js migration:generate -d dist/util/util/Datasource.js"
|
||||||
},
|
},
|
||||||
"main": "dist/bundle/index.js",
|
"main": "dist/bundle/index.js",
|
||||||
"types": "src/bundle/index.ts",
|
"types": "src/bundle/index.ts",
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
import path from "path";
|
|
||||||
import "reflect-metadata";
|
import "reflect-metadata";
|
||||||
import { DataSource } from "typeorm";
|
import { DataSource } from "typeorm";
|
||||||
import { yellow, green, red } from "picocolors";
|
import { yellow, green, red } from "picocolors";
|
||||||
|
import { DataSourceOptions, DatabaseType } from "./Datasource";
|
||||||
|
|
||||||
// UUID extension option is only supported with postgres
|
// UUID extension option is only supported with postgres
|
||||||
// We want to generate all id's with Snowflakes that's why we have our own BaseEntity class
|
// We want to generate all id's with Snowflakes that's why we have our own BaseEntity class
|
||||||
|
|
||||||
var dbConnection: DataSource | undefined;
|
var dbConnection: DataSource | undefined;
|
||||||
let dbConnectionString =
|
|
||||||
process.env.DATABASE || path.join(process.cwd(), "database.db");
|
|
||||||
|
|
||||||
// Gets the existing database connection
|
// Gets the existing database connection
|
||||||
export function getDatabase(): DataSource | null {
|
export function getDatabase(): DataSource | null {
|
||||||
@ -21,36 +20,9 @@ export function getDatabase(): DataSource | null {
|
|||||||
export async function initDatabase(): Promise<DataSource> {
|
export async function initDatabase(): Promise<DataSource> {
|
||||||
if (dbConnection) return dbConnection;
|
if (dbConnection) return dbConnection;
|
||||||
|
|
||||||
const type = dbConnectionString.includes("://")
|
console.log(`[Database] ${yellow(`connecting to ${DatabaseType} db`)}`);
|
||||||
? dbConnectionString.split(":")[0]?.replace("+srv", "")
|
|
||||||
: "sqlite";
|
|
||||||
const isSqlite = type.includes("sqlite");
|
|
||||||
|
|
||||||
console.log(`[Database] ${yellow(`connecting to ${type} db`)}`);
|
dbConnection = await DataSourceOptions.initialize();
|
||||||
if (isSqlite) {
|
|
||||||
console.log(
|
|
||||||
`[Database] ${red(
|
|
||||||
`You are running sqlite! Please keep in mind that we recommend setting up a dedicated database!`,
|
|
||||||
)}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const dataSource = new DataSource({
|
|
||||||
//@ts-ignore type 'string' is not 'mysql' | 'sqlite' | 'mariadb' | etc etc
|
|
||||||
type,
|
|
||||||
charset: "utf8mb4",
|
|
||||||
url: isSqlite ? undefined : dbConnectionString,
|
|
||||||
database: isSqlite ? dbConnectionString : undefined,
|
|
||||||
entities: ["dist/util/entities/*.js"],
|
|
||||||
synchronize: false,
|
|
||||||
logging: false,
|
|
||||||
bigNumberStrings: false,
|
|
||||||
supportBigNumbers: true,
|
|
||||||
name: "default",
|
|
||||||
migrations: ["dist/util/migrations/*.js"],
|
|
||||||
});
|
|
||||||
|
|
||||||
dbConnection = await dataSource.initialize();
|
|
||||||
|
|
||||||
await dbConnection.runMigrations();
|
await dbConnection.runMigrations();
|
||||||
console.log(`[Database] ${green("connected")}`);
|
console.log(`[Database] ${green("connected")}`);
|
||||||
|
42
src/util/util/Datasource.ts
Normal file
42
src/util/util/Datasource.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { config } from "dotenv"
|
||||||
|
import path from "path";
|
||||||
|
import { DataSource } from "typeorm";
|
||||||
|
import { red } from "picocolors";
|
||||||
|
|
||||||
|
// For typeorm cli
|
||||||
|
if (!process.env) {
|
||||||
|
config();
|
||||||
|
}
|
||||||
|
|
||||||
|
let dbConnectionString =
|
||||||
|
process.env.DATABASE || path.join(process.cwd(), "database.db");
|
||||||
|
|
||||||
|
const type = dbConnectionString.includes("://")
|
||||||
|
? dbConnectionString.split(":")[0]?.replace("+srv", "")
|
||||||
|
: "sqlite";
|
||||||
|
const isSqlite = type.includes("sqlite");
|
||||||
|
|
||||||
|
if (isSqlite) {
|
||||||
|
console.log(
|
||||||
|
`[Database] ${red(
|
||||||
|
`You are running sqlite! Please keep in mind that we recommend setting up a dedicated database!`,
|
||||||
|
)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataSource = new DataSource({
|
||||||
|
//@ts-ignore type 'string' is not 'mysql' | 'sqlite' | 'mariadb' | etc etc
|
||||||
|
type,
|
||||||
|
charset: "utf8mb4",
|
||||||
|
url: isSqlite ? undefined : dbConnectionString,
|
||||||
|
database: isSqlite ? dbConnectionString : undefined,
|
||||||
|
entities: ["dist/util/entities/*.js"],
|
||||||
|
synchronize: false,
|
||||||
|
logging: false,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
supportBigNumbers: true,
|
||||||
|
name: "default",
|
||||||
|
migrations: ["dist/util/migrations/*.js"],
|
||||||
|
});
|
||||||
|
|
||||||
|
export { dataSource as DataSourceOptions, type as DatabaseType };
|
Loading…
x
Reference in New Issue
Block a user