Skip to content

Commit c6e8006

Browse files
postgress general analytics pagination
1 parent afcb573 commit c6e8006

File tree

10 files changed

+406
-160
lines changed

10 files changed

+406
-160
lines changed

modules/module-mongodb-storage/src/storage/MongoReportStorage.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { storage } from '@powersync/service-core';
22
import { event_types } from '@powersync/service-types';
33
import { PowerSyncMongo } from './implementation/db.js';
44
import { logger } from '@powersync/lib-services-framework';
5-
import { createPaginatedMongoQuery } from '../utils/util.js';
5+
import { createPaginatedConnectionQuery } from '../utils/util.js';
66

77
export class MongoReportStorage implements storage.ReportStorage {
88
public readonly db: PowerSyncMongo;
@@ -46,18 +46,23 @@ export class MongoReportStorage implements storage.ReportStorage {
4646

4747
async getClientConnections(
4848
data: event_types.ClientConnectionsRequest
49-
): Promise<event_types.PaginatedResponse<event_types.ClientConnection>>{
50-
const { cursor, date_range } = data;
49+
): Promise<event_types.PaginatedResponse<event_types.ClientConnection>> {
50+
const { cursor, date_range } = data;
5151
const limit = data?.limit || 100;
5252

5353
const connected_at = date_range ? { connected_at: { $lte: date_range.end, $gte: date_range.start } } : undefined;
5454
const user_id = data.user_id ? { user_id: data.user_id } : undefined;
5555
const client_id = data.client_id ? { client_id: data.client_id } : undefined;
56-
return await createPaginatedMongoQuery({
57-
...client_id,
58-
...user_id,
59-
...connected_at,
60-
},this.db.connection_report_events, limit, cursor) as event_types.PaginatedResponse<event_types.ClientConnection>
56+
return (await createPaginatedConnectionQuery(
57+
{
58+
...client_id,
59+
...user_id,
60+
...connected_at
61+
},
62+
this.db.connection_report_events,
63+
limit,
64+
cursor
65+
)) as event_types.PaginatedResponse<event_types.ClientConnection>;
6166
}
6267

6368
async reportClientConnection(data: event_types.ClientConnectionBucketData): Promise<void> {

modules/module-mongodb-storage/src/utils/util.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,38 +115,49 @@ export function setSessionSnapshotTime(session: mongo.ClientSession, time: bson.
115115
}
116116
}
117117

118-
export const createPaginatedMongoQuery = async <T extends mongo.Document>( query: mongo.Filter<T>, collection: mongo.Collection<T>,limit: number, cursor?: string) =>{
119-
120-
const createQuery = (cursor?: string)=>{
118+
export const createPaginatedConnectionQuery = async <T extends mongo.Document>(
119+
query: mongo.Filter<T>,
120+
collection: mongo.Collection<T>,
121+
limit: number,
122+
cursor?: string
123+
) => {
124+
const createQuery = (cursor?: string) => {
121125
if (!cursor) {
122126
return query;
123127
}
124128
return {
125129
$and: [
126130
query,
127131
{
128-
_id: {
129-
$lt: new bson.ObjectId(cursor)
132+
/** We are using the connected at date as the cursor so that the functionality works the same on Postgres implementation
133+
* The id field in postgres is an uuid, this will work similarly to the ObjectId in Mongodb
134+
* */
135+
connected_at: {
136+
$lt: new Date(cursor)
130137
}
131138
}
132139
]
133-
} as mongo.Filter<T>
134-
}
135-
140+
} as mongo.Filter<T>;
141+
};
142+
136143
/** cursor.count() deprecated */
137144
const total = await collection.countDocuments(query);
138145

139-
const findCursor = collection.find(createQuery(cursor), { sort: {
140-
_id: -1
141-
}})
146+
const findCursor = collection.find(createQuery(cursor), {
147+
sort: {
148+
/** We are sorting by connected at date descending to match cursor Postgres implementation */
149+
connected_at: -1
150+
}
151+
});
142152

143153
const items = await findCursor.limit(limit).toArray();
144154
const count = items.length;
145155
return {
146156
items,
147157
total,
148158
count,
149-
cursor: count === limit ? items[items.length - 1]._id.toHexString() : undefined,
150-
more: count < total,
151-
}
152-
}
159+
/** Setting the cursor to the connected at date of the last item in the list */
160+
cursor: count === limit ? items[items.length - 1].connected_at.toISOString() : undefined,
161+
more: count < total
162+
};
163+
};

modules/module-mongodb-storage/test/src/__snapshots__/connection-report-storage.test.ts.snap

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -233,26 +233,32 @@ exports[`Report storage tests > Should show paginated response of all connection
233233

234234
exports[`Report storage tests > Should show paginated response of all connections with a limit 1`] = `
235235
{
236-
"count": 3,
236+
"count": 4,
237237
"cursor": "<removed-for-snapshot>",
238238
"items": [
239239
{
240-
"client_id": "client_expired",
241-
"sdk": "powersync-js/1.23.7",
242-
"user_agent": "powersync-js/1.23.0 powersync-web Firefox/141 linux",
243-
"user_id": "user_expired",
240+
"client_id": "client_one",
241+
"sdk": "powersync-dart/1.6.4",
242+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
243+
"user_id": "user_one",
244244
},
245245
{
246-
"client_id": "client_month",
247-
"sdk": "powersync-js/1.23.6",
248-
"user_agent": "powersync-js/1.23.0 powersync-web Firefox/141 linux",
249-
"user_id": "user_month",
246+
"client_id": "client_four",
247+
"sdk": "powersync-js/1.21.4",
248+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
249+
"user_id": "user_four",
250250
},
251251
{
252-
"client_id": "client_one",
253-
"sdk": "powersync-js/1.24.5",
254-
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
255-
"user_id": "user_week",
252+
"client_id": "",
253+
"sdk": "unknown",
254+
"user_agent": "Dart (flutter-web) Chrome/128 android",
255+
"user_id": "user_one",
256+
},
257+
{
258+
"client_id": "client_two",
259+
"sdk": "powersync-js/1.21.1",
260+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
261+
"user_id": "user_two",
256262
},
257263
],
258264
"more": true,
@@ -262,26 +268,32 @@ exports[`Report storage tests > Should show paginated response of all connection
262268

263269
exports[`Report storage tests > Should show paginated response of all connections with a limit 2`] = `
264270
{
265-
"count": 5,
271+
"count": 4,
266272
"cursor": undefined,
267273
"items": [
268274
{
269-
"client_id": "client_expired",
270-
"sdk": "powersync-js/1.23.7",
271-
"user_agent": "powersync-js/1.23.0 powersync-web Firefox/141 linux",
272-
"user_id": "user_expired",
275+
"client_id": "client_one",
276+
"sdk": "powersync-dart/1.6.4",
277+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
278+
"user_id": "user_one",
273279
},
274280
{
275-
"client_id": "client_month",
276-
"sdk": "powersync-js/1.23.6",
277-
"user_agent": "powersync-js/1.23.0 powersync-web Firefox/141 linux",
278-
"user_id": "user_month",
281+
"client_id": "client_four",
282+
"sdk": "powersync-js/1.21.4",
283+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
284+
"user_id": "user_four",
279285
},
280286
{
281-
"client_id": "client_one",
282-
"sdk": "powersync-js/1.24.5",
283-
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
284-
"user_id": "user_week",
287+
"client_id": "",
288+
"sdk": "unknown",
289+
"user_agent": "Dart (flutter-web) Chrome/128 android",
290+
"user_id": "user_one",
291+
},
292+
{
293+
"client_id": "client_two",
294+
"sdk": "powersync-js/1.21.1",
295+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
296+
"user_id": "user_two",
285297
},
286298
],
287299
"more": true,
@@ -294,18 +306,18 @@ exports[`Report storage tests > Should show paginated response of connections of
294306
"count": 2,
295307
"cursor": undefined,
296308
"items": [
297-
{
298-
"client_id": "",
299-
"sdk": "unknown",
300-
"user_agent": "Dart (flutter-web) Chrome/128 android",
301-
"user_id": "user_one",
302-
},
303309
{
304310
"client_id": "client_one",
305311
"sdk": "powersync-dart/1.6.4",
306312
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
307313
"user_id": "user_one",
308314
},
315+
{
316+
"client_id": "",
317+
"sdk": "unknown",
318+
"user_agent": "Dart (flutter-web) Chrome/128 android",
319+
"user_id": "user_one",
320+
},
309321
],
310322
"more": false,
311323
"total": 2,
@@ -319,14 +331,8 @@ exports[`Report storage tests > Should show paginated response of connections ov
319331
"items": [
320332
{
321333
"client_id": "client_one",
322-
"sdk": "powersync-js/1.24.5",
323-
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
324-
"user_id": "user_week",
325-
},
326-
{
327-
"client_id": "",
328-
"sdk": "unknown",
329-
"user_agent": "Dart (flutter-web) Chrome/128 android",
334+
"sdk": "powersync-dart/1.6.4",
335+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
330336
"user_id": "user_one",
331337
},
332338
{
@@ -335,24 +341,30 @@ exports[`Report storage tests > Should show paginated response of connections ov
335341
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
336342
"user_id": "user_four",
337343
},
338-
{
339-
"client_id": "client_three",
340-
"sdk": "powersync-js/1.21.2",
341-
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
342-
"user_id": "user_three",
343-
},
344344
{
345345
"client_id": "client_two",
346346
"sdk": "powersync-js/1.21.1",
347347
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
348348
"user_id": "user_two",
349349
},
350350
{
351-
"client_id": "client_one",
352-
"sdk": "powersync-dart/1.6.4",
353-
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
351+
"client_id": "",
352+
"sdk": "unknown",
353+
"user_agent": "Dart (flutter-web) Chrome/128 android",
354354
"user_id": "user_one",
355355
},
356+
{
357+
"client_id": "client_three",
358+
"sdk": "powersync-js/1.21.2",
359+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
360+
"user_id": "user_three",
361+
},
362+
{
363+
"client_id": "client_one",
364+
"sdk": "powersync-js/1.24.5",
365+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
366+
"user_id": "user_week",
367+
},
356368
],
357369
"more": false,
358370
"total": 6,

modules/module-postgres-storage/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
},
1818
"exports": {
1919
".": {
20+
"types": "./dist/@types/index.d.ts",
2021
"import": "./dist/index.js",
2122
"require": "./dist/index.js",
22-
"default": "./dist/index.js",
23-
"types": "./dist/@types/index.d.ts"
23+
"default": "./dist/index.js"
2424
},
2525
"./types": {
26+
"types": "./dist/@types/index.d.ts",
2627
"import": "./dist/types/types.js",
2728
"require": "./dist/types/types.js",
28-
"default": "./dist/types/types.js",
29-
"types": "./dist/@types/index.d.ts"
29+
"default": "./dist/types/types.js"
3030
}
3131
},
3232
"dependencies": {

0 commit comments

Comments
 (0)