MCPcopy Create free account
hub / github.com/DeepNotesApp/DeepNotes / insertPageSnapshot

Function insertPageSnapshot

packages/@deeplib/data/src/snapshots.ts:8–66  ·  view source on GitHub ↗
(input: {
  dataAbstraction: DataAbstraction<typeof dataHashes>;

  pageId: string;
  authorId: string;

  encryptedSymmetricKey: Uint8Array;
  encryptedData: Uint8Array;

  type: PageSnapshotType;

  dtrx: DataTransaction;
})

Source from the content-addressed store, hash-verified

6import type { dataHashes } from './data-hashes';
7
8export async function insertPageSnapshot(input: {
9 dataAbstraction: DataAbstraction<typeof dataHashes>;
10
11 pageId: string;
12 authorId: string;
13
14 encryptedSymmetricKey: Uint8Array;
15 encryptedData: Uint8Array;
16
17 type: PageSnapshotType;
18
19 dtrx: DataTransaction;
20}) {
21 const pageSnapshotInfos: PageSnapshotInfo[] =
22 await input.dataAbstraction.hget('page-snapshots', input.pageId, 'infos');
23
24 const pageSnapshot = await PageSnapshotModel.query(input.dtrx.trx)
25 .insert({
26 page_id: input.pageId,
27 encrypted_symmetric_key: input.encryptedSymmetricKey,
28 encrypted_data: input.encryptedData,
29 author_id: input.authorId,
30 type: input.type,
31 })
32 .returning(['id', 'creation_date']);
33
34 // Add new snapshot
35
36 pageSnapshotInfos.unshift({
37 id: pageSnapshot.id,
38 creationDate: pageSnapshot.creation_date,
39 authorId: pageSnapshot.author_id,
40 type: pageSnapshot.type,
41 });
42
43 // Trim excess snapshots
44
45 const deletedSnapshotIds: string[] = [];
46
47 while (
48 pageSnapshotInfos.length > 10 &&
49 new Date() > addDays(pageSnapshotInfos.at(-1)!.creationDate, 14)
50 ) {
51 deletedSnapshotIds.push(pageSnapshotInfos.pop()!.id);
52 }
53
54 await PageSnapshotModel.query(input.dtrx.trx)
55 .whereIn('id', deletedSnapshotIds)
56 .delete();
57
58 // Update page snapshot infos
59
60 await input.dataAbstraction.hmset(
61 'page-snapshots',
62 input.pageId,
63 { infos: pageSnapshotInfos },
64 { dtrx: input.dtrx },
65 );

Callers 2

saveFunction · 0.90

Calls 5

addDaysFunction · 0.90
insertMethod · 0.80
hgetMethod · 0.45
deleteMethod · 0.45
hmsetMethod · 0.45

Tested by

no test coverage detected