Skip to content

Commit 3c44ad8

Browse files
committed
feat: add TypeScript definitions for various modules and enhance existing ones
1 parent 4315823 commit 3c44ad8

18 files changed

+2257
-6
lines changed

api/utils/localization.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module api/utils/localization
44
*/
55

6-
/** @type(import('../../types/localization').Locale) */
6+
/** @type {import('../../types/localization').Locale} */
77
var locale = {},
88
fs = require('fs'),
99
path = require('path'),
@@ -91,5 +91,5 @@ locale.getProperties = function(lang, callback) {
9191
}
9292
};
9393

94-
/** @type(import('../../types/localization').Locale) */
94+
/** @type {import('../../types/localization').Locale} */
9595
module.exports = locale;

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"types/pluginManager.d.ts", // Plugin manager type definitions
1616
"types/utils.d.ts", // Utility functions type definitions
1717
"types/batcher.d.ts", // Database batcher type definitions
18+
"types/rights.d.ts", // Rights validation type definitions
19+
"types/events.d.ts", // Events processing type definitions
20+
"types/usage.d.ts", // Usage processing type definitions
1821
"api/utils/localization.js" // Include JS file for type checking
1922
],
2023
"compilerOptions": {
@@ -40,7 +43,6 @@
4043
"forceConsistentCasingInFileNames": true, // Ensure that casing is correct in imports
4144

4245
// Completeness
43-
"skipLibCheck": true, // Skip type checking all .d.ts files
4446
"typeRoots": ["./types", "./node_modules/@types"] // Specify multiple folders that act like './node_modules/@types'
4547
}
4648
}

types/batcher.d.ts

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
// Type definitions for Countly Database Batcher
2+
// Generated from /api/parts/data/batcher.js
3+
4+
import { Db, Collection, BulkWriteOptions } from "mongodb";
5+
6+
/** Global batcher statistics object */
7+
export interface BatcherStats {
8+
key: string;
9+
pid: number;
10+
insert_queued: number;
11+
insert_processing: number;
12+
insert_errored_fallback: number;
13+
insert_errored_no_fallback: number;
14+
insert_errored_no_fallback_last_error: string;
15+
update_queued: number;
16+
update_processing: number;
17+
update_errored_fallback: number;
18+
update_errored_no_fallback: number;
19+
update_errored_no_fallback_last_error: string;
20+
}
21+
22+
/** Options for add operations in WriteBatcher */
23+
export interface AddOptions {
24+
/** Whether to upsert the document if it doesnt exist (default: true) */
25+
upsert?: boolean;
26+
}
27+
28+
/** Configuration for batch processing */
29+
export interface BatchConfig {
30+
/** Batch period in seconds */
31+
batch_period: number;
32+
/** Whether batch processing is enabled */
33+
batch_processing: boolean;
34+
/** Read batch period in seconds */
35+
batch_read_period: number;
36+
/** Read batch TTL in seconds */
37+
batch_read_ttl: number;
38+
/** Whether read batch processing is enabled */
39+
batch_read_processing: boolean;
40+
}
41+
42+
/** Projection object for database queries */
43+
export interface ProjectionObject {
44+
projection?: Record<string, 0 | 1>;
45+
fields?: Record<string, 0 | 1>;
46+
[key: string]: any;
47+
}
48+
49+
/** Keys extracted from projection */
50+
export interface ProjectionKeys {
51+
keys: string[];
52+
have_projection: boolean;
53+
}
54+
55+
/** Cached data structure for ReadBatcher */
56+
export interface CachedData {
57+
query: Record<string, any>;
58+
data?: any;
59+
promise?: Promise<any> | null;
60+
projection: ProjectionObject;
61+
last_used: number;
62+
last_updated: number;
63+
multi: boolean;
64+
}
65+
66+
/** Batch operation for WriteBatcher */
67+
export interface BatchOperation {
68+
id: string;
69+
value: Record<string, any>;
70+
upsert?: boolean;
71+
}
72+
73+
/** Promise resolver for ReadBatcher */
74+
export interface PromiseResolver {
75+
resolve: (value: any) => void;
76+
reject: (error: any) => void;
77+
}
78+
79+
/**
80+
* Class for batching database insert operations
81+
* Collects documents and performs bulk inserts periodically
82+
*/
83+
declare class InsertBatcher {
84+
/** Database connections indexed by name */
85+
private dbs: Record<string, Db>;
86+
/** Queued documents indexed by database and collection */
87+
private data: Record<string, Record<string, any[]>>;
88+
/** Batch period in milliseconds */
89+
private period: number;
90+
/** Whether batch processing is enabled */
91+
private process: boolean;
92+
/** Whether running in shared mode */
93+
private shared: boolean;
94+
95+
/**
96+
* Create batcher instance
97+
* @param db - Primary database object
98+
*/
99+
constructor(db: Db);
100+
101+
/**
102+
* Add another database to batch
103+
* @param name - Name of the database
104+
* @param connection - MongoDB connection to that database
105+
*/
106+
addDb(name: string, connection: Db): void;
107+
108+
/**
109+
* Reloads server configs from plugin manager
110+
*/
111+
private loadConfig(): void;
112+
113+
/**
114+
* Writes data to database for specific collection
115+
* @param db - Name of the database for which to write data
116+
* @param collection - Name of the collection for which to write data
117+
*/
118+
private flush(db: string, collection: string): Promise<void>;
119+
120+
/**
121+
* Run all pending database queries
122+
* @returns Promise that resolves when all flushes complete
123+
*/
124+
flushAll(): Promise<void>;
125+
126+
/**
127+
* Schedule next flush cycle
128+
*/
129+
private schedule(): void;
130+
131+
/**
132+
* Provide a document to insert into collection
133+
* @param collection - Name of the collection where to insert data
134+
* @param doc - One document or array of documents to insert
135+
* @param db - Name of the database for which to write data (default: "countly")
136+
*/
137+
insert(collection: string, doc: Record<string, any> | Record<string, any>[], db?: string): void;
138+
}
139+
140+
/**
141+
* Class for batching database update operations for aggregated data
142+
* Merges multiple operations for the same document
143+
*/
144+
declare class WriteBatcher {
145+
/** Database connections indexed by name */
146+
private dbs: Record<string, Db>;
147+
/** Queued operations indexed by database, collection, and document ID */
148+
private data: Record<string, Record<string, Record<string, BatchOperation>>>;
149+
/** Batch period in milliseconds */
150+
private period: number;
151+
/** Whether batch processing is enabled */
152+
private process: boolean;
153+
/** Whether running in shared mode */
154+
private shared: boolean;
155+
156+
/**
157+
* Create batcher instance
158+
* @param db - Primary database object
159+
*/
160+
constructor(db: Db);
161+
162+
/**
163+
* Add another database to batch
164+
* @param name - Name of the database
165+
* @param connection - MongoDB connection to that database
166+
*/
167+
addDb(name: string, connection: Db): void;
168+
169+
/**
170+
* Reloads server configs from plugin manager
171+
*/
172+
private loadConfig(): void;
173+
174+
/**
175+
* Writes data to database for specific collection
176+
* @param db - Name of the database for which to write data
177+
* @param collection - Name of the collection for which to write data
178+
*/
179+
private flush(db: string, collection: string): Promise<void>;
180+
181+
/**
182+
* Run all pending database queries
183+
* @returns Promise that resolves when all flushes complete
184+
*/
185+
flushAll(): Promise<void>;
186+
187+
/**
188+
* Schedule next flush cycle
189+
*/
190+
private schedule(): void;
191+
192+
/**
193+
* Get operation on document by id (returns reference for modification)
194+
* @param collection - Name of the collection where to update data
195+
* @param id - ID of the document
196+
* @param db - Name of the database for which to write data (default: "countly")
197+
* @returns BulkWrite query for document by reference
198+
*/
199+
get(collection: string, id: string, db?: string): Record<string, any>;
200+
201+
/**
202+
* Provide operation for document id and batcher will merge multiple operations
203+
* @param collection - Name of the collection where to update data
204+
* @param id - ID of the document
205+
* @param operation - MongoDB update operation
206+
* @param db - Name of the database for which to write data (default: "countly")
207+
* @param options - Options for operation (upsert control)
208+
*/
209+
add(collection: string, id: string, operation: Record<string, any>, db?: string, options?: AddOptions): void;
210+
}
211+
212+
/**
213+
* Class for caching database read operations
214+
* Caches results for a configurable TTL period
215+
*/
216+
declare class ReadBatcher {
217+
/** Database connection */
218+
private db: Db;
219+
/** Cached data indexed by collection and cache ID */
220+
private data: Record<string, Record<string, CachedData>>;
221+
/** Promise resolvers for worker processes */
222+
private promises: Record<string, PromiseResolver>;
223+
/** Cache period in milliseconds */
224+
private period: number;
225+
/** Cache TTL in milliseconds */
226+
private ttl: number;
227+
/** Whether read processing is enabled */
228+
private process: boolean;
229+
/** Whether running on master process */
230+
private onMaster: boolean;
231+
232+
/**
233+
* Create batcher instance
234+
* @param db - Database object
235+
*/
236+
constructor(db: Db);
237+
238+
/**
239+
* Reloads server configs from plugin manager
240+
*/
241+
private loadConfig(): void;
242+
243+
/**
244+
* Get data from database
245+
* @param collection - Name of the collection for which to read data
246+
* @param id - ID of cache entry
247+
* @param query - MongoDB query for the document
248+
* @param projection - Which fields to return
249+
* @param multi - True if expecting multiple documents
250+
* @returns Promise resolving to query result
251+
*/
252+
private getData(collection: string, id: string, query: Record<string, any>, projection: ProjectionObject, multi: boolean): Promise<any>;
253+
254+
/**
255+
* Check all cached entries and remove expired ones
256+
*/
257+
private checkAll(): void;
258+
259+
/**
260+
* Schedule next cache cleanup cycle
261+
*/
262+
private schedule(): void;
263+
264+
/**
265+
* Gets list of keys from projection object which are included
266+
* @param projection - Projection object to analyze
267+
* @returns Object with keys list and projection status
268+
*/
269+
private keysFromProjectionObject(projection: ProjectionObject): ProjectionKeys;
270+
271+
/**
272+
* Invalidate specific cache entry
273+
* @param collection - Name of the collection
274+
* @param query - Query for the document
275+
* @param projection - Which fields to return
276+
* @param multi - True if multiple documents
277+
*/
278+
invalidate(collection: string, query: Record<string, any>, projection: ProjectionObject, multi: boolean): void;
279+
280+
/**
281+
* Get data from cache or from database and cache it
282+
* @param collection - Name of the collection
283+
* @param query - Query for the document
284+
* @param projection - Which fields to return
285+
* @param multi - True if multiple documents
286+
* @returns Promise resolving to query result
287+
*/
288+
private get(collection: string, query: Record<string, any>, projection: ProjectionObject, multi: boolean): Promise<any>;
289+
290+
/**
291+
* Get single document from cache or from database and cache it
292+
* @param collection - Name of the collection
293+
* @param query - Query for the document
294+
* @param projection - Which fields to return
295+
* @param callback - Optional callback to get result
296+
* @returns Promise if callback not passed
297+
*/
298+
getOne(collection: string, query: Record<string, any>, projection?: ProjectionObject, callback?: (err: Error | null, result: any) => void): Promise<any>;
299+
getOne(collection: string, query: Record<string, any>, callback: (err: Error | null, result: any) => void): void;
300+
301+
/**
302+
* Get multiple documents from cache or from database and cache it
303+
* @param collection - Name of the collection
304+
* @param query - Query for the documents
305+
* @param projection - Which fields to return
306+
* @param callback - Optional callback to get result
307+
* @returns Promise if callback not passed
308+
*/
309+
getMany(collection: string, query: Record<string, any>, projection?: ProjectionObject, callback?: (err: Error | null, result: any[]) => void): Promise<any[]>;
310+
getMany(collection: string, query: Record<string, any>, callback: (err: Error | null, result: any[]) => void): void;
311+
312+
/**
313+
* Cache data read from database
314+
* @param collection - Name of the collection
315+
* @param id - ID of the cache entry
316+
* @param query - Query for the document
317+
* @param projection - Which fields to return
318+
* @param data - Data from database to cache
319+
* @param multi - True if multiple documents
320+
*/
321+
private cache(collection: string, id: string, query: Record<string, any>, projection: ProjectionObject, data: any, multi: boolean): void;
322+
}
323+
324+
export { WriteBatcher, ReadBatcher, InsertBatcher };

types/common.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface DbMap {
6767
sum: string;
6868
dur: string;
6969
count: string;
70+
paying: string;
7071
}
7172

7273
/** Mapping of common user database properties */
@@ -158,6 +159,10 @@ export interface ValidationArgProperties {
158159
mods?: string;
159160
/** allow multiple values */
160161
multiple?: boolean;
162+
/** array-specific validation options */
163+
array?: any;
164+
/** discriminator for validation */
165+
discriminator?: any;
161166
};
162167
}
163168

@@ -932,8 +937,6 @@ export interface Common {
932937
*/
933938
dbext: DbExt;
934939

935-
DataTable: any;
936-
937940
/**
938941
* Sync license check results to request (and session if present)
939942
*

0 commit comments

Comments
 (0)