Merge branch 'main' into aggregation

This commit is contained in:
rijkvanzanten
2021-06-30 10:12:16 -04:00
29 changed files with 252 additions and 615 deletions

View File

@@ -6,8 +6,9 @@ import logger from '../../../logger';
import { getSchema } from '../../../utils/get-schema';
import { RolesService, UsersService, SettingsService } from '../../../services';
import getDatabase, { isInstalled, hasDatabaseConnection } from '../../../database';
import { SchemaOverview } from '../../../types';
export default async function bootstrap(): Promise<void> {
export default async function bootstrap({ skipAdminInit }: { skipAdminInit?: boolean }): Promise<void> {
logger.info('Initializing bootstrap...');
if ((await isDatabaseAvailable()) === false) {
@@ -27,29 +28,12 @@ export default async function bootstrap(): Promise<void> {
const schema = await getSchema();
logger.info('Setting up first admin role...');
const rolesService = new RolesService({ schema });
const role = await rolesService.createOne({ name: 'Admin', admin_access: true });
logger.info('Adding first admin user...');
const usersService = new UsersService({ schema });
let adminEmail = env.ADMIN_EMAIL;
if (!adminEmail) {
logger.info('No admin email provided. Defaulting to "admin@example.com"');
adminEmail = 'admin@example.com';
if (skipAdminInit == null) {
await createDefaultAdmin(schema);
} else {
logger.info('Skipping creation of default Admin user and role...');
}
let adminPassword = env.ADMIN_PASSWORD;
if (!adminPassword) {
adminPassword = nanoid(12);
logger.info(`No admin password provided. Defaulting to "${adminPassword}"`);
}
await usersService.createOne({ email: adminEmail, password: adminPassword, role });
if (env.PROJECT_NAME && typeof env.PROJECT_NAME === 'string' && env.PROJECT_NAME.length > 0) {
const settingsService = new SettingsService({ schema });
await settingsService.upsertSingleton({ project_name: env.PROJECT_NAME });
@@ -78,3 +62,28 @@ async function isDatabaseAvailable() {
return false;
}
async function createDefaultAdmin(schema: SchemaOverview) {
logger.info('Setting up first admin role...');
const rolesService = new RolesService({ schema });
const role = await rolesService.createOne({ name: 'Admin', admin_access: true });
logger.info('Adding first admin user...');
const usersService = new UsersService({ schema });
let adminEmail = env.ADMIN_EMAIL;
if (!adminEmail) {
logger.info('No admin email provided. Defaulting to "admin@example.com"');
adminEmail = 'admin@example.com';
}
let adminPassword = env.ADMIN_PASSWORD;
if (!adminPassword) {
adminPassword = nanoid(12);
logger.info(`No admin password provided. Defaulting to "${adminPassword}"`);
}
await usersService.createOne({ email: adminEmail, password: adminPassword, role });
}

View File

@@ -63,7 +63,11 @@ rolesCommand
program.command('count <collection>').description('Count the amount of items in a given collection').action(count);
program.command('bootstrap').description('Initialize or update the database').action(bootstrap);
program
.command('bootstrap')
.description('Initialize or update the database')
.option('--skipAdminInit', 'Skips the creation of the default Admin Role and User')
.action(bootstrap);
program.parseAsync(process.argv).catch((err) => {
console.error(err);

View File

@@ -190,7 +190,7 @@ export class FilesService extends ItemsService {
* Delete multiple files
*/
async deleteMany(keys: PrimaryKey[], opts?: MutationOptions): Promise<PrimaryKey[]> {
const files = await super.readMany(keys, { fields: ['id', 'storage'] });
const files = await super.readMany(keys, { fields: ['id', 'storage'], limit: -1 });
if (!files) {
throw new ForbiddenException();