Don't initialize database on file require (#6003)

This commit is contained in:
Rijk van Zanten
2021-06-02 11:41:42 -04:00
committed by GitHub
parent 02fc696c53
commit 77e00b7db4
39 changed files with 222 additions and 146 deletions

View File

@@ -4,6 +4,8 @@ import installDatabase from '../../../database/seeds/run';
import env from '../../../env';
import logger from '../../../logger';
import { getSchema } from '../../../utils/get-schema';
import { RolesService, UsersService, SettingsService } from '../../../services';
import getDatabase, { isInstalled, hasDatabaseConnection } from '../../../database';
export default async function bootstrap(): Promise<void> {
logger.info('Initializing bootstrap...');
@@ -13,10 +15,7 @@ export default async function bootstrap(): Promise<void> {
process.exit(1);
}
const { isInstalled, default: database } = require('../../../database');
const { RolesService } = require('../../../services/roles');
const { UsersService } = require('../../../services/users');
const { SettingsService } = require('../../../services/settings');
const database = getDatabase();
if ((await isInstalled()) === false) {
logger.info('Installing Directus system tables...');
@@ -66,8 +65,6 @@ export default async function bootstrap(): Promise<void> {
}
async function isDatabaseAvailable() {
const { hasDatabaseConnection } = require('../../../database');
const tries = 5;
const secondsBetweenTries = 5;

View File

@@ -1,5 +1,7 @@
import getDatabase from '../../../database';
export default async function count(collection: string): Promise<void> {
const database = require('../../../database/index').default;
const database = getDatabase();
if (!collection) {
console.error('Collection is required');

View File

@@ -1,9 +1,9 @@
import { Knex } from 'knex';
import runMigrations from '../../../database/migrations/run';
import installSeeds from '../../../database/seeds/run';
import getDatabase from '../../../database';
export default async function start(): Promise<void> {
const database = require('../../../database/index').default as Knex;
const database = getDatabase();
try {
await installSeeds(database);

View File

@@ -1,7 +1,8 @@
import run from '../../../database/migrations/run';
import getDatabase from '../../../database';
export default async function migrate(direction: 'latest' | 'up' | 'down'): Promise<void> {
const database = require('../../../database').default;
const database = getDatabase();
try {
console.log('✨ Running migrations...');

View File

@@ -1,8 +1,9 @@
import { getSchema } from '../../../utils/get-schema';
import { RolesService } from '../../../services';
import getDatabase from '../../../database';
export default async function rolesCreate({ role: name, admin }: { role: string; admin: boolean }): Promise<void> {
const { default: database } = require('../../../database/index');
const { RolesService } = require('../../../services/roles');
const database = getDatabase();
if (!name) {
console.error('Name is required');

View File

@@ -1,4 +1,6 @@
import { getSchema } from '../../../utils/get-schema';
import { UsersService } from '../../../services';
import getDatabase from '../../../database';
export default async function usersCreate({
email,
@@ -9,8 +11,7 @@ export default async function usersCreate({
password?: string;
role?: string;
}): Promise<void> {
const { default: database } = require('../../../database/index');
const { UsersService } = require('../../../services/users');
const database = getDatabase();
if (!email || !password || !role) {
console.error('Email, password, role are required');

View File

@@ -1,9 +1,10 @@
import argon2 from 'argon2';
import { getSchema } from '../../../utils/get-schema';
import { UsersService } from '../../../services';
import getDatabase from '../../../database';
export default async function usersPasswd({ email, password }: { email?: string; password?: string }): Promise<void> {
const { default: database } = require('../../../database/index');
const { UsersService } = require('../../../services/users');
const database = getDatabase();
if (!email || !password) {
console.error('Email and password are required');