• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cartesi / rollups-explorer / 11943333351

20 Nov 2024 11:09PM UTC coverage: 86.535% (-0.07%) from 86.605%
11943333351

push

github

brunomenezes
chore(ci): Set the Preview endpoint when available in the CI settings.

1276 of 1529 branches covered (83.45%)

Branch coverage included in aggregate %.

9077 of 10435 relevant lines covered (86.99%)

44.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/apps/web/src/providers/connectionConfig/indexedDbRepository.ts
1
"use client";
×
2
import Dexie, { Table } from "dexie";
×
3
import { Connection, Repository } from "./types";
4
import localRepository, { namespace, networkId } from "./localRepository";
×
5
import { Address } from "viem";
6

7
export interface ConnectionItem extends Connection {
8
    network: string;
9
}
10

11
/**
12
 * Implements the Repository interface providing a persistent storage.
13
 * It uses the IndexedDb underneath.
14
 */
15
class IndexedDbRepository extends Dexie implements Repository {
×
16
    // Notify the typing system that 'connections' is added by dexie when declaring the stores()
17
    connections!: Table<ConnectionItem>;
×
18

19
    constructor() {
×
20
        super(namespace);
×
21
        // Create first version of the connections store with address as primary key and indexed props
22
        this.version(1).stores({
×
23
            connections: "address, url, timestamp, network",
×
24
        });
×
25

26
        // When the db is ready, initialize data for it
27
        this.connections.db.on(
×
28
            "ready",
×
29
            () =>
×
30
                new Promise((resolve) => {
×
31
                    this.initialize().then(resolve);
×
32
                }),
×
33
        );
×
34
    }
×
35

36
    private async initialize() {
×
37
        // Get existing connections from indexedDbRepository
38
        const indexedDbConnections = await this.connections
×
39
            .where("network")
×
40
            .equals(networkId)
×
41
            .toArray();
×
42

43
        // Get existing connections from localRepository
44
        const localRepositoryConnections = await localRepository.list();
×
45

46
        // Check if data from localRepository should be migrated to indexedDbRepository
47
        if (
×
48
            indexedDbConnections.length === 0 &&
×
49
            localRepositoryConnections.length > 0
×
50
        ) {
×
51
            const connections = localRepositoryConnections.map(
×
52
                (connection) => ({
×
53
                    ...connection,
×
54
                    network: networkId,
×
55
                }),
×
56
            ) as ConnectionItem[];
×
57

58
            // Save augmented connections
59
            await this.connections.bulkPut(connections);
×
60

61
            // Remove connections from localRepository to keep a single source of truth
62
            await Promise.all(
×
63
                connections.map((c) => localRepository.remove(c.address)),
×
64
            );
×
65
        }
×
66
    }
×
67

68
    private formatConnection(connection: ConnectionItem) {
×
69
        return {
×
70
            address: connection.address,
×
71
            url: connection.url,
×
72
            timestamp: connection.timestamp,
×
73
        } as Connection;
×
74
    }
×
75

76
    async add(conn: Connection) {
×
77
        const connectionItem: ConnectionItem = {
×
78
            ...conn,
×
79
            timestamp: Date.now(),
×
80
            network: networkId,
×
81
        };
×
82

83
        return this.connections.add(connectionItem);
×
84
    }
×
85

86
    async has(addr: Address) {
×
87
        const connection = await this.connections
×
88
            .where("network")
×
89
            .equals(networkId)
×
90
            .and((connection) => connection.address === addr)
×
91
            .first();
×
92

93
        return Boolean(connection);
×
94
    }
×
95

96
    async remove(addr: Address) {
×
97
        await this.connections
×
98
            .where("network")
×
99
            .equals(networkId)
×
100
            .and((connection) => connection.address === addr)
×
101
            .delete();
×
102

103
        return true;
×
104
    }
×
105

106
    async get(addr: Address) {
×
107
        const connection = (await this.connections
×
108
            .where("network")
×
109
            .equals(networkId)
×
110
            .and((connection) => connection.address === addr)
×
111
            .first()) as ConnectionItem;
×
112

113
        return this.formatConnection(connection);
×
114
    }
×
115

116
    async list() {
×
117
        const connections = await this.connections
×
118
            .where("network")
×
119
            .equals(networkId)
×
120
            .toArray();
×
121

122
        return connections.map(this.formatConnection);
×
123
    }
×
124
}
×
125

126
export default IndexedDbRepository;
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc