Rewrote storage to support more entities
This commit is contained in:
parent
94335f04b7
commit
b3ff744f4d
|
|
@ -10,7 +10,7 @@ async function initialFetch(options: { id: string }): Promise<Series | null> {
|
|||
if (!series) {
|
||||
return null;
|
||||
}
|
||||
const stored = storage.write(series);
|
||||
const stored = storage.writeSeries(series);
|
||||
if (!stored) {
|
||||
console.error(`Failed to store series ${options.id}`);
|
||||
return null;
|
||||
|
|
@ -50,7 +50,7 @@ async function updateFetch(existingSeries: Series): Promise<UpdatedSeries | Seri
|
|||
episodes: episodesSortedDescending,
|
||||
};
|
||||
|
||||
const updateSuccessful = await storage.write(updated);
|
||||
const updateSuccessful = await storage.writeSeries(updated);
|
||||
if (!updateSuccessful) {
|
||||
console.log(`Failed to update series ${existingSeries.id}`);
|
||||
return null;
|
||||
|
|
@ -76,7 +76,7 @@ function isSeriesFromStorageNew(
|
|||
}
|
||||
|
||||
async function getSeries(options: { id: string }): Promise<Series | null> {
|
||||
const seriesFromStorage = await storage.read(options);
|
||||
const seriesFromStorage = await storage.readSeries(options);
|
||||
|
||||
/**
|
||||
* We don't have the feed in storage,
|
||||
|
|
|
|||
|
|
@ -4,18 +4,18 @@ import { testUtils } from "./test-utils.ts";
|
|||
|
||||
Deno.test("can store a series", async () => {
|
||||
const series = testUtils.generateSeries();
|
||||
await storage.write(series);
|
||||
await storage.writeSeries(series);
|
||||
|
||||
const readSeries = await storage.read(series);
|
||||
const readSeries = await storage.readSeries(series);
|
||||
|
||||
assertEquals(readSeries, series);
|
||||
});
|
||||
|
||||
Deno.test("can retrieve a series", async () => {
|
||||
const series = testUtils.generateSeries();
|
||||
await storage.write(series);
|
||||
await storage.writeSeries(series);
|
||||
|
||||
const readSeries = await storage.read(series);
|
||||
const readSeries = await storage.readSeries(series);
|
||||
|
||||
assertEquals(readSeries, series);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -18,26 +18,43 @@ export type Series = {
|
|||
episodes: Episode[];
|
||||
};
|
||||
|
||||
export type VippsAgreement = {
|
||||
id: string;
|
||||
agreementId: string;
|
||||
userEmail: string;
|
||||
createdAt: Date;
|
||||
revokedAt: Date | null;
|
||||
};
|
||||
|
||||
const kv = await Deno.openKv();
|
||||
|
||||
function seriesKey(series: { id: string }) {
|
||||
return ["series", series.id];
|
||||
type Identifiable = { id: string };
|
||||
type Collection = "series" | "vipps-agreements";
|
||||
|
||||
function read<T extends Identifiable>(collection: Collection) {
|
||||
return async function (series: Identifiable) {
|
||||
const key = [collection, series.id];
|
||||
const read = await kv.get(key);
|
||||
return read.value as T | null;
|
||||
};
|
||||
}
|
||||
|
||||
async function readSeries(options: { id: string }): Promise<Series | null> {
|
||||
const { id } = options;
|
||||
const key = seriesKey({ id });
|
||||
const read = await kv.get<Series>(key);
|
||||
return read.value;
|
||||
function write<T extends Identifiable>(collection: Collection) {
|
||||
return async function (entity: T) {
|
||||
const key = [collection, entity.id];
|
||||
const stored = await kv.set(key, entity);
|
||||
return stored.ok;
|
||||
};
|
||||
}
|
||||
|
||||
async function writeSeries(series: Series): Promise<boolean> {
|
||||
const key = seriesKey(series);
|
||||
const stored = await kv.set(key, series);
|
||||
return stored.ok;
|
||||
}
|
||||
const readSeries = read<Series>("series");
|
||||
const writeSeries = write<Series>("series");
|
||||
const readVippsAgreement = read<VippsAgreement>("vipps-agreements");
|
||||
const writeVippsAgreement = write<VippsAgreement>("vipps-agreements");
|
||||
|
||||
export const storage = {
|
||||
readSeries,
|
||||
writeSeries,
|
||||
readVippsAgreement,
|
||||
writeVippsAgreement,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue