Skip to content

Commit 7a45a74

Browse files
committed
feat: 🎸 Added DealsDb class
1 parent d22ee74 commit 7a45a74

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

‎src/node/db/deals.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { Hash } from 'viem';
2+
import { Storage } from '../../storage/index.js';
3+
import { DealRecord, PaginationOptions } from '../../shared/types.js';
4+
import { createLogger } from '../../utils/logger.js';
5+
6+
const logger = createLogger('DealsDb');
7+
8+
/**
9+
* Interface defining the properties of a Deal.
10+
*/
11+
// export interface Deal {}
12+
13+
/**
14+
* Interface defining the properties of DealsDb initialization options.
15+
*/
16+
export interface DealsDbOptions {
17+
/** Instance of storage used for persisting the state of the API server */
18+
storage: Storage;
19+
/** Prefix used for the storage key to avoid potential key collisions */
20+
prefix: string;
21+
}
22+
23+
/**
24+
* Class that implements an API to the deals records storage
25+
*
26+
* @export
27+
* @class UsersDb
28+
*/
29+
export class DealsDb {
30+
/** Storage instance for persisting the state of the API server */
31+
storage: Storage;
32+
/** Specific key prefix for the storage key to avoid potential key collisions */
33+
prefix: string;
34+
35+
/**
36+
* Creates an instance of DealsDb.
37+
* Initializes an instance of DealsDb with given options.
38+
*
39+
* @param {UsersDbOptions} options
40+
* @memberof DealsDb
41+
*/
42+
constructor(options: DealsDbOptions) {
43+
const { storage, prefix } = options;
44+
45+
// TODO Validate DealsDbOptions
46+
47+
this.prefix = `${prefix}_api_deals_`;
48+
this.storage = storage;
49+
}
50+
51+
/**
52+
* Adds/Updates the record of the deal in the storage
53+
*
54+
* @param {DealRecord} deal The deal object
55+
* @returns {Promise<void>}
56+
* @memberof UsersDb
57+
*/
58+
async set(deal: DealRecord): Promise<void> {
59+
await this.storage.set<DealRecord>(`${this.prefix}${deal.offer.id}`, deal);
60+
}
61+
62+
/**
63+
* Retrieves the deal from storage.
64+
*
65+
* @param {Hash} offerId The Id (offerId) of the deal to be retrieved
66+
* @returns {Promise<DealRecord>} The deal object associated with the given Id
67+
* @throws Will throw an error if the user is not found
68+
* @memberof UsersDb
69+
*/
70+
async get(offerId: Hash): Promise<DealRecord> {
71+
const deal = await this.storage.get<DealRecord>(`${this.prefix}${offerId}`);
72+
73+
if (!deal) {
74+
throw new Error(`Deal ${offerId} not found`);
75+
}
76+
77+
return deal;
78+
}
79+
80+
/**
81+
* Retrieves all the deals from storage.
82+
*
83+
* @param {PaginationOptions} [pagination] Pagination options
84+
* @returns {Promise<DealRecord[]>} Deals records
85+
* @memberof DealsDb
86+
*/
87+
async getAll(pagination?: PaginationOptions): Promise<DealRecord[]> {
88+
return new Promise((resolve, reject) => {
89+
try {
90+
pagination = pagination ?? {
91+
start: 0,
92+
skip: 10,
93+
};
94+
let cursor = 0;
95+
const from = pagination.start >= 0 ? pagination.start : 0;
96+
const to = from + pagination.skip ?? 0;
97+
const records: DealRecord[] = [];
98+
99+
for (const record of this.storage.entries<DealRecord>()) {
100+
if (to > 0 && cursor >= from && cursor < to) {
101+
records.push(record[1]);
102+
}
103+
104+
cursor++;
105+
}
106+
107+
resolve(records);
108+
} catch (error) {
109+
logger.error('getAll', error);
110+
reject(error);
111+
}
112+
});
113+
}
114+
}

0 commit comments

Comments
 (0)