Skip to content

Commit

Permalink
refactor(indiekit): create mongodb database during bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 24, 2023
1 parent 09303f8 commit 86cf974
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
5 changes: 3 additions & 2 deletions helpers/database/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoMemoryServer } from "mongodb-memory-server";
import { getMongodbConfig } from "@indiekit/indiekit/lib/mongodb.js";
import { getMongodbClient } from "@indiekit/indiekit/lib/mongodb.js";

/**
* Generate access token for testing
Expand All @@ -9,7 +9,8 @@ import { getMongodbConfig } from "@indiekit/indiekit/lib/mongodb.js";
export const testDatabase = async (name) => {
const mongod = await MongoMemoryServer.create();
const mongodbUrl = mongod.getUri();
const database = await getMongodbConfig(mongodbUrl);
const client = await getMongodbClient(mongodbUrl);
const database = client.db("indiekit-test");

return database.collection(name);
};
10 changes: 6 additions & 4 deletions packages/indiekit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getCategories } from "./lib/categories.js";
import { getIndiekitConfig } from "./lib/config.js";
import { getInstalledPlugins } from "./lib/installed-plugins.js";
import { getLocaleCatalog } from "./lib/locale-catalog.js";
import { getMongodbConfig } from "./lib/mongodb.js";
import { getMongodbClient } from "./lib/mongodb.js";
import { getPostTemplate } from "./lib/post-template.js";
import { getPostTypes } from "./lib/post-types.js";

Expand Down Expand Up @@ -53,10 +53,12 @@ export const Indiekit = class {
}

async bootstrap() {
const database = await getMongodbConfig(this.application.mongodbUrl);
const client = await getMongodbClient(this.application.mongodbUrl);

// Setup database
if (client) {
const database = client.db("indiekit");

// Setup databases
if (database) {
this.application.hasDatabase = true;
this.application.cache = new Keyv({
collectionName: "cache",
Expand Down
10 changes: 5 additions & 5 deletions packages/indiekit/lib/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { MongoClient } from "mongodb";

/**
* Connect to MongoDB database
* Connect to MongoDB client
* @param {string} mongodbUrl - MongoDB URL
* @returns {Promise<import('mongodb').Db>} MongoDB database instance
* @returns {Promise<import('mongodb').MongoClient>} MongoDB client
*/
export const getMongodbConfig = async (mongodbUrl) => {
export const getMongodbClient = async (mongodbUrl) => {
if (mongodbUrl) {
try {
const client = new MongoClient(mongodbUrl, {
connectTimeoutMS: 5000,
});
await client.connect();
const database = client.db("indiekit");
return database;

return client;
} catch (error) {
console.warn(error.message);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/indiekit/tests/unit/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from "ava";
import sinon from "sinon";
import { MongoMemoryServer } from "mongodb-memory-server";
import { getMongodbConfig } from "../../lib/mongodb.js";
import { getMongodbClient } from "../../lib/mongodb.js";

test.beforeEach(async (t) => {
const mongod = await MongoMemoryServer.create();
Expand All @@ -13,14 +13,14 @@ test.beforeEach(async (t) => {
});

test("Connects to MongoDB database", async (t) => {
const result = await getMongodbConfig(t.context.url);
const result = await getMongodbClient(t.context.url);

t.is(result.s.namespace.db, "indiekit");
t.is(result.s.namespace.db, "admin");
});

test("Returns false if can’t connect to a MongoDB database", async (t) => {
sinon.stub(console, "warn");
await getMongodbConfig("https://foo.bar");
await getMongodbClient("https://foo.bar");

t.true(
console.warn.calledWith(
Expand Down

0 comments on commit 86cf974

Please sign in to comment.