Skip to content

Commit

Permalink
Rename the create member of LXCatDatabase to setup (#1019)
Browse files Browse the repository at this point in the history
Correctly handle errors in the setup procedure.
  • Loading branch information
daanboer committed May 7, 2024
1 parent 5e1cfe5 commit b9295ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/database/src/cli/setup.ts
Expand Up @@ -11,14 +11,14 @@ const dbName = process.env.ARANGO_DB ?? "lxcat";
const username = process.env.ARANGO_USERNAME ?? "lxcat";
const password = process.env.ARANGO_PASSWORD!;

const db = await LXCatDatabase.create(systemDb(), dbName);
const db = await LXCatDatabase.setup(systemDb(), dbName);

if (db.isErr) {
console.log(db.error.message);
exit();
}

console.log(`Created ${dbName} database.`);
console.log(`Successfully setup the ${dbName} database.`);

// Create the `lxcat` user if it doesn't already exist.
if (!(await systemDb().listUsers()).find((user) => user.user === username)) {
Expand Down
21 changes: 13 additions & 8 deletions packages/database/src/lxcat-database.ts
Expand Up @@ -5,7 +5,7 @@
import { Database } from "arangojs";
import { CreateDatabaseOptions } from "arangojs/database.js";
import { Result } from "true-myth";
import { ok } from "true-myth/result";
import { err, ok } from "true-myth/result";
import {
addOrganization,
addSession,
Expand Down Expand Up @@ -131,19 +131,24 @@ export class LXCatDatabase {
this.db = db;
}

public static async create(
public static async setup(
system: Database,
name: string,
options?: CreateDatabaseOptions,
): Promise<Result<LXCatDatabase, Error>> {
const dbResult = await setupDatabase(system, name, options);
let db: Database;

// if (dbResult.isErr) {
// return err(dbResult.error);
// }
if (await system.database(name).exists()) {
db = system.database(name);
} else {
const dbResult = await setupDatabase(system, name, options);

// The `setupDatabase` function errors when the database already exists.
const db = dbResult.isErr ? system.database(name) : dbResult.value;
if (dbResult.isErr) {
return err(dbResult.error);
}

db = dbResult.value;
}

await setupUserCollections(db);
await setupSharedCollections(db);
Expand Down

0 comments on commit b9295ed

Please sign in to comment.