diff --git a/apps/template/app.js b/apps/template/app.js index 20174ccf..36e4efdb 100644 --- a/apps/template/app.js +++ b/apps/template/app.js @@ -1,15 +1,17 @@ import { createAppServer, asyncJsonHandler as handler, + createDb, LowDBUserDao, } from "@stanlemon/server-with-auth"; import { v4 as uuid } from "uuid"; -const dao = new LowDBUserDao(); -export const db = dao.getDB(); +export const db = createDb(); +const dao = new LowDBUserDao(db); + db.data.items = db.data.items || []; -const app = createAppServer({ +export const app = createAppServer({ webpack: "http://localhost:8080", secure: ["/api/"], dao, @@ -37,5 +39,3 @@ app.delete( return db.data.items; }) ); - -export default app; diff --git a/apps/template/app.test.js b/apps/template/app.test.js index 0d674f57..2e544828 100644 --- a/apps/template/app.test.js +++ b/apps/template/app.test.js @@ -2,7 +2,7 @@ * @jest-environment node */ import request from "supertest"; -import app, { db } from "./app.js"; +import { app, db } from "./app.js"; describe("/app", () => { afterEach(() => { diff --git a/apps/template/package.json b/apps/template/package.json index e79085c5..ee1de758 100644 --- a/apps/template/package.json +++ b/apps/template/package.json @@ -1,6 +1,6 @@ { "name": "@stanlemon/app-template", - "version": "0.3.0", + "version": "0.3.1", "description": "A template for creating apps using the webdev package.", "author": "Stan Lemon ", "license": "MIT", diff --git a/package-lock.json b/package-lock.json index 456a6918..149e7d24 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,7 +56,7 @@ }, "apps/template": { "name": "@stanlemon/app-template", - "version": "0.3.0", + "version": "0.3.1", "license": "MIT", "dependencies": { "@stanlemon/server-with-auth": "*", @@ -17164,7 +17164,7 @@ }, "packages/server-with-auth": { "name": "@stanlemon/server-with-auth", - "version": "0.2.0", + "version": "0.2.1", "license": "MIT", "dependencies": { "@stanlemon/server": "*", diff --git a/packages/server-with-auth/app.js b/packages/server-with-auth/app.js index f60e3163..b9e12208 100644 --- a/packages/server-with-auth/app.js +++ b/packages/server-with-auth/app.js @@ -1,7 +1,8 @@ import { createAppServer, asyncJsonHandler as handler } from "./src/index.js"; -import LowDBUserDao from "./src/data/lowdb-user-dao.js"; +import LowDBUserDao, { createDb } from "./src/data/lowdb-user-dao.js"; -const dao = new LowDBUserDao(); +const db = createDb(); +const dao = new LowDBUserDao(db); const app = createAppServer({ port: 3003, @@ -24,5 +25,5 @@ app.get( // Secure endpoint app.get( "/api/users", - handler(() => ({ users: dao.getDB().data.users })) + handler(() => ({ users: db.data.users })) ); diff --git a/packages/server-with-auth/package.json b/packages/server-with-auth/package.json index 73415eda..97051d86 100644 --- a/packages/server-with-auth/package.json +++ b/packages/server-with-auth/package.json @@ -1,6 +1,6 @@ { "name": "@stanlemon/server-with-auth", - "version": "0.2.0", + "version": "0.2.1", "description": "A basic express web server setup with authentication baked in.", "author": "Stan Lemon ", "license": "MIT", diff --git a/packages/server-with-auth/src/data/lowdb-user-dao.js b/packages/server-with-auth/src/data/lowdb-user-dao.js index 1e3dcda0..ed1c43fd 100644 --- a/packages/server-with-auth/src/data/lowdb-user-dao.js +++ b/packages/server-with-auth/src/data/lowdb-user-dao.js @@ -4,27 +4,40 @@ import { v4 as uuidv4 } from "uuid"; import bcrypt from "bcryptjs"; import UserDao from "./user-dao.js"; -const DEFAULT_ADAPTER = - process.env.NODE_ENV === "test" - ? new MemorySync() - : new JSONFileSync("./db.json"); +export function createInMemoryDb() { + return new LowSync(new MemorySync(), {}); +} + +export function createJsonFileDb(filename = "./db.json") { + return new LowSync(new JSONFileSync(filename), {}); +} + +/** + * Create a database based on the environment. + * Test environment (NODE_ENV=test) will use {MemorySync}. + * @returns {LowSync} Database + */ +export function createDb() { + return process.env.NODE_ENV === "test" + ? createInMemoryDb() + : createJsonFileDb(); +} export default class LowDBUserDao extends UserDao { #db; - constructor(seeds = [], adapter = DEFAULT_ADAPTER) { + constructor(db = createDb()) { super(); - this.#db = new LowSync(adapter, { users: [] }); - this.#db.read(); - - if (seeds.length > 0) { - seeds.forEach((user) => this.createUser(user)); + if (!(db instanceof LowSync)) { + throw new Error("The db object must be of type LowSync."); } - } - getDB() { - return this.#db; + this.#db = db; + this.#db.read(); + + // Default data, ensure that users is an array if it is not already + this.#db.data.users = db.data.users || []; } /** @inheritdoc */ diff --git a/packages/server-with-auth/src/data/lowdb-user-dao.test.js b/packages/server-with-auth/src/data/lowdb-user-dao.test.js index d973812a..461a2238 100644 --- a/packages/server-with-auth/src/data/lowdb-user-dao.test.js +++ b/packages/server-with-auth/src/data/lowdb-user-dao.test.js @@ -1,8 +1,7 @@ /** * @jest-environment node */ -import { MemorySync } from "lowdb"; -import LowDBUserDao from "./lowdb-user-dao.js"; +import LowDBUserDao, { createInMemoryDb } from "./lowdb-user-dao.js"; describe("lowdb-user-dao", () => { // This is a user that we will reuse in our tests @@ -11,12 +10,15 @@ describe("lowdb-user-dao", () => { password: "password", }; + /** @type {LowSync} */ + let db; /** @type {LowDBUserDao} */ let dao; beforeEach(() => { // Before each test reset our users database - dao = new LowDBUserDao([], new MemorySync()); + db = createInMemoryDb(); + dao = new LowDBUserDao(db); }); it("creates a user", async () => { @@ -97,13 +99,13 @@ describe("lowdb-user-dao", () => { it("deletes a user by id", async () => { const user = await dao.createUser(data); - expect(dao.getDB().data.users).toHaveLength(1); + expect(db.data.users).toHaveLength(1); const deleted = dao.deleteUser(user.id); expect(deleted).toBe(true); expect(dao.getUserById(user.id)).toBeUndefined(); - expect(dao.getDB().data.users).toHaveLength(0); + expect(db.data.users).toHaveLength(0); }); }); diff --git a/packages/server-with-auth/src/index.js b/packages/server-with-auth/src/index.js index 91615ba2..e4655034 100644 --- a/packages/server-with-auth/src/index.js +++ b/packages/server-with-auth/src/index.js @@ -13,4 +13,9 @@ export { default as checkAuth } from "./checkAuth.js"; export { default as createAppServer } from "./createAppServer.js"; export { default as schema } from "./schema/user.js"; export { default as SimpleUsersDao } from "./data/simple-users-dao.js"; -export { default as LowDBUserDao } from "./data/lowdb-user-dao.js"; +export { + default as LowDBUserDao, + createDb, + createInMemoryDb, + createJsonFileDb, +} from "./data/lowdb-user-dao.js"; diff --git a/packages/server-with-auth/src/routes/auth.test.js b/packages/server-with-auth/src/routes/auth.test.js index bcf025e6..98de3a09 100644 --- a/packages/server-with-auth/src/routes/auth.test.js +++ b/packages/server-with-auth/src/routes/auth.test.js @@ -2,14 +2,13 @@ * @jest-environment node */ import request from "supertest"; -import { MemorySync } from "lowdb"; import createAppServer from "../createAppServer"; -import LowDBUserDao from "../data/lowdb-user-dao.js"; +import LowDBUserDao, { createInMemoryDb } from "../data/lowdb-user-dao.js"; // This suppresses a warning we don't need in tests process.env.JWT_SECRET = "SECRET"; -let dao = new LowDBUserDao([], new MemorySync()); +let dao = new LowDBUserDao(createInMemoryDb()); // We want to explicitly test functionality we disable during testing process.env.NODE_ENV = "override";