Adding a class wrapper incase using get/put functions multiple times, functions like the PouchDB constructor.

This commit is contained in:
mike12345567 2022-06-23 20:27:20 +01:00
parent b4bed6c0ce
commit 56956dba4f
2 changed files with 24 additions and 9 deletions

View File

@ -1,5 +1,5 @@
require("../../../tests/utilities/TestConfiguration") require("../../../tests/utilities/TestConfiguration")
const { get, put } = require("../writethrough") const { Writethrough } = require("../writethrough")
const { dangerousGetDB } = require("../../db") const { dangerousGetDB } = require("../../db")
const tk = require("timekeeper") const tk = require("timekeeper")
@ -9,20 +9,20 @@ tk.freeze(START_DATE)
const DELAY = 5000 const DELAY = 5000
const db = dangerousGetDB("test") const db = dangerousGetDB("test")
const writethrough = new Writethrough(db)
describe("writethrough", () => { describe("writethrough", () => {
describe("put", () => { describe("put", () => {
let first let first
it("should be able to store, will go to DB", async () => { it("should be able to store, will go to DB", async () => {
const response = await put(db,{ _id: "test", value: 1 }, DELAY) const response = await writethrough.put({ _id: "test", value: 1 }, DELAY)
const output = await db.get(response._id) const output = await db.get(response._id)
first = output first = output
expect(output.value).toBe(1) expect(output.value).toBe(1)
}) })
it("second put shouldn't update DB", async () => { it("second put shouldn't update DB", async () => {
const response = await put(db, { ...first, value: 2 }, DELAY) const response = await writethrough.put({ ...first, value: 2 }, DELAY)
const output = await db.get(response._id) const output = await db.get(response._id)
expect(first._rev).toBe(output._rev) expect(first._rev).toBe(output._rev)
expect(output.value).toBe(1) expect(output.value).toBe(1)
@ -30,7 +30,7 @@ describe("writethrough", () => {
it("should put it again after delay period", async () => { it("should put it again after delay period", async () => {
tk.freeze(START_DATE + DELAY + 1) tk.freeze(START_DATE + DELAY + 1)
const response = await put(db, { ...first, value: 3 }, DELAY) const response = await writethrough.put({ ...first, value: 3 }, DELAY)
const output = await db.get(response._id) const output = await db.get(response._id)
expect(response._rev).not.toBe(first._rev) expect(response._rev).not.toBe(first._rev)
expect(output.value).toBe(3) expect(output.value).toBe(3)
@ -39,7 +39,7 @@ describe("writethrough", () => {
describe("get", () => { describe("get", () => {
it("should be able to retrieve", async () => { it("should be able to retrieve", async () => {
const response = await get(db, "test") const response = await writethrough.get("test")
expect(response.value).toBe(3) expect(response.value).toBe(3)
}) })
}) })

View File

@ -21,11 +21,11 @@ function makeCacheItem(value: any, lastWrite: number | null = null): CacheItem {
return { value, lastWrite: lastWrite || Date.now() } return { value, lastWrite: lastWrite || Date.now() }
} }
exports.put = async ( export async function put(
db: PouchDB.Database, db: PouchDB.Database,
value: any, value: any,
writeRateMs: number = DEFAULT_WRITE_RATE_MS writeRateMs: number = DEFAULT_WRITE_RATE_MS
) => { ) {
const cache = await getCache() const cache = await getCache()
const key = value._id const key = value._id
let cacheItem: CacheItem | undefined = await cache.get(key) let cacheItem: CacheItem | undefined = await cache.get(key)
@ -46,7 +46,7 @@ exports.put = async (
return output return output
} }
exports.get = async (db: PouchDB.Database, id: string): Promise<any> => { export async function get(db: PouchDB.Database, id: string): Promise<any> {
const cache = await getCache() const cache = await getCache()
let cacheItem: CacheItem = await cache.get(id) let cacheItem: CacheItem = await cache.get(id)
if (!cacheItem) { if (!cacheItem) {
@ -56,3 +56,18 @@ exports.get = async (db: PouchDB.Database, id: string): Promise<any> => {
} }
return cacheItem.value return cacheItem.value
} }
export class Writethrough {
db: PouchDB.Database
constructor(db: PouchDB.Database) {
this.db = db
}
async put(value: any, writeRateMs: number = DEFAULT_WRITE_RATE_MS) {
return put(this.db, value, writeRateMs)
}
async get(id: string) {
return get(this.db, id)
}
}