Page MenuHomeSealhub

storage.ts
No OneTemporary

storage.ts

import { Collection, CollectionItem, FieldTypes, Policies } from "sealious";
import type { P24Session } from ".";
import Uuid from "./uuid-field";
export abstract class P24StorageAdapter {
abstract create(body: Omit<P24Session, "id">): Promise<P24Session>;
abstract getByID(id: string): Promise<P24Session | null>;
abstract set(
id: string,
key: keyof Omit<P24Session, "id">,
value: unknown
): Promise<void>;
}
export class P24SessionsCollection extends Collection {
fields = {
identifier: new Uuid(),
amount: new FieldTypes.Float(),
title: new FieldTypes.Text(),
flow: FieldTypes.Required(new FieldTypes.Text()),
metadata: new FieldTypes.JsonObject(),
status: new FieldTypes.Enum(["created", "paid"]),
};
defaultPolicy = new Policies.Super();
}
export class P24SealiousStorage extends P24StorageAdapter {
constructor(public collection: P24SessionsCollection) {
super();
}
async create(body: Omit<P24Session, "id">): Promise<P24Session> {
const created = await this.collection.create(
new this.collection.app.SuperContext(),
{
identifier: "yes",
...body,
}
);
const id = created.get("identifier");
return { id, ...body };
}
async getByID(id: string): Promise<P24Session | null> {
const session = await this.getSessionByID(id);
if (!session) {
throw new Error("Unknown session id: " + id);
}
return {
id,
amount: session.get("amount") as number,
title: session.get("title") || "Transaction title",
flow: session.get("flow") as string,
metadata: session.get("metadata") as Record<string, unknown>,
status: session.get("status") as "created" | "paid",
};
}
async getSessionByID(
id: string
): Promise<CollectionItem<P24SessionsCollection> | null> {
const {
items: [session],
} = await this.collection
.list(new this.collection.app.SuperContext())
.filter({ identifier: id })
.paginate({ items: 1 })
.fetch();
if (!session) {
return null;
}
return session;
}
async set(
id: string,
key: keyof Omit<P24Session, "id">,
value: unknown
): Promise<void> {
const session = await this.getSessionByID(id);
if (!session) {
throw new Error("Wrong session id: " + id);
}
session.set(key, value);
await session.save(new this.collection.app.SuperContext());
}
}

File Metadata

Mime Type
text/x-java
Expires
Sat, Nov 8, 12:04 (19 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1033948
Default Alt Text
storage.ts (2 KB)

Event Timeline