π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Save JSON directly
Manav Rathif702a9303123
diff --git a/web/packages/new/photos/services/user-entity/db.ts b/web/packages/new/photos/services/user-entity/db.tsindex be1d4cd276..2e61c7b500 100644--- a/web/packages/new/photos/services/user-entity/db.ts+++ b/web/packages/new/photos/services/user-entity/db.ts@@ -1,17 +1,57 @@-import { getKV, getKVN, setKV } from "@/base/kv";-import { LocalLocationTag, type EntityType, type LocationTag } from ".";+import { getKV, getKVN, getKVS, setKV } from "@/base/kv";+import { z } from "zod";+import { LocalLocationTag, type EntityType } from ".";import { RemoteUserEntityKey } from "./remote";// Our DB footprint ---v-const entityKey = (type: EntityType) => `entity/${type}`;+const entitiesKey = (type: EntityType) => `entity/${type}`;const entityKeyKey = (type: EntityType) => `entity/${type}/key`;const latestUpdatedAtKey = (type: EntityType) => `entity/${type}/time`;// ^----export const saveLocationTags = (tags: LocationTag[]) =>- setKV("locationTags", JSON.stringify(tags));+/**+ * A locally persisted user entity.+ */+export interface LocalUserEntity {+ /**+ * The UUID or nanoid of the entity.+ */+ id: string;+ /**+ * Arbitrary (decrypted) data associated with the entity. The format of this+ * data is specific to each entity type, but it is guaranteed to be JSON+ * serializable.+ */+ data: unknown;+ /**+ * Epoch microseconds denoting when this entity was last changed (created or+ * updated).+ */+ updatedAt: number;+}++const LocalUserEntity = z.object({+ id: z.string(),+ // Retain the data verbatim.+ data: z.object({}).passthrough(),+ updatedAt: z.number(),+});++/**+ * Update the list of locally persisted user entities of the given {@link type}.+ */+export const saveEntities = (type: EntityType, items: LocalUserEntity[]) =>+ setKV(entitiesKey(type), items);++/**+ * Return the list of locally persisted user entities of the given {@link type}.+ */+export const savedEntities = async (+ type: EntityType,+): Promise<LocalUserEntity[]> =>+ LocalUserEntity.array().parse((await getKV(entitiesKey(type))) ?? []);/*** Return all the location tags that are present locally.@@ -20,40 +60,42 @@ export const saveLocationTags = (tags: LocationTag[]) =>*/export const savedLocationTags = async () =>LocalLocationTag.array().parse(- JSON.parse((await getKV("locationTags")) ?? "[]"),+ JSON.parse((await getKVS("locationTags")) ?? "[]"),);+/**+ * Save the {@link entityKey} for the given user entity {@link type} to our+ * local database.+ */+export const saveRemoteUserEntityKey = (+ type: EntityType,+ entityKey: RemoteUserEntityKey,+) => setKV(entityKeyKey(type), entityKey);+/*** Return the locally persisted {@link RemoteUserEntityKey}, if any,- * corresponding the given {@link type}.+ * corresponding the given user entity {@link type}.*/export const savedRemoteUserEntityKey = (type: EntityType,): Promise<RemoteUserEntityKey | undefined> =>getKV(entityKeyKey(type)).then((s) =>- s ? RemoteUserEntityKey.parse(JSON.parse(s)) : undefined,+ s ? RemoteUserEntityKey.parse(s) : undefined,);/**- * Setter for {@link savedRemoteUserEntityKey}.+ * Save the latest `updatedAt` {@link value} for the given user entity+ * {@link type} to our local database.*/-export const saveRemoteUserEntityKey = (- type: EntityType,- entityKey: RemoteUserEntityKey,-) => setKV(entityKeyKey(type), JSON.stringify(entityKey));+export const saveLatestUpdatedAt = (type: EntityType, value: number) =>+ setKV(latestUpdatedAtKey(type), value);/*** Return the locally persisted value for the latest `updatedAt` time for the- * given entity {@link type}.+ * given user entity {@link type}.** This is used to checkpoint diffs, so that we can resume fetching from the* last time we did a fetch.*/export const savedLatestUpdatedAt = (type: EntityType) =>getKVN(latestUpdatedAtKey(type));--/**- * Setter for {@link savedLatestUpdatedAt}.- */-export const saveLatestUpdatedAt = (type: EntityType, value: number) =>- setKV(latestUpdatedAtKey(type), value);