@powersync/service-module-postgres-storage 0.10.8 → 0.10.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/@types/migrations/scripts/1756282360128-connection-reporting.d.ts +3 -0
- package/dist/@types/storage/PostgresReportStorage.d.ts +24 -0
- package/dist/@types/storage/PostgresStorageProvider.d.ts +1 -1
- package/dist/@types/storage/storage-index.d.ts +0 -1
- package/dist/@types/types/models/SdkReporting.d.ts +21 -0
- package/dist/@types/types/models/models-index.d.ts +1 -0
- package/dist/@types/{storage/PostgresTestStorageFactoryGenerator.d.ts → utils/test-utils.d.ts} +5 -3
- package/dist/@types/utils/utils-index.d.ts +1 -0
- package/dist/migrations/scripts/1756282360128-connection-reporting.js +107 -0
- package/dist/migrations/scripts/1756282360128-connection-reporting.js.map +1 -0
- package/dist/storage/PostgresReportStorage.js +238 -0
- package/dist/storage/PostgresReportStorage.js.map +1 -0
- package/dist/storage/PostgresStorageProvider.js +10 -1
- package/dist/storage/PostgresStorageProvider.js.map +1 -1
- package/dist/storage/storage-index.js +0 -1
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/types/models/SdkReporting.js +17 -0
- package/dist/types/models/SdkReporting.js.map +1 -0
- package/dist/types/models/models-index.js +1 -0
- package/dist/types/models/models-index.js.map +1 -1
- package/dist/{storage/PostgresTestStorageFactoryGenerator.js → utils/test-utils.js} +22 -6
- package/dist/utils/test-utils.js.map +1 -0
- package/dist/utils/utils-index.js +1 -0
- package/dist/utils/utils-index.js.map +1 -1
- package/package.json +9 -9
- package/src/migrations/scripts/1756282360128-connection-reporting.ts +41 -0
- package/src/storage/PostgresReportStorage.ts +258 -0
- package/src/storage/PostgresStorageProvider.ts +13 -2
- package/src/storage/storage-index.ts +0 -1
- package/src/types/models/SdkReporting.ts +23 -0
- package/src/types/models/models-index.ts +1 -0
- package/src/{storage/PostgresTestStorageFactoryGenerator.ts → utils/test-utils.ts} +21 -5
- package/src/utils/utils-index.ts +1 -0
- package/test/src/__snapshots__/connection-report-storage.test.ts.snap +215 -0
- package/test/src/connection-report-storage.test.ts +233 -0
- package/test/src/util.ts +3 -6
- package/dist/storage/PostgresTestStorageFactoryGenerator.js.map +0 -1
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@powersync/service-module-postgres-storage",
|
|
3
3
|
"repository": "https://github.com/powersync-ja/powersync-service",
|
|
4
4
|
"types": "dist/@types/index.d.ts",
|
|
5
|
-
"version": "0.10.
|
|
5
|
+
"version": "0.10.9",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"license": "FSL-1.1-ALv2",
|
|
8
8
|
"type": "module",
|
|
@@ -29,17 +29,17 @@
|
|
|
29
29
|
"p-defer": "^4.0.1",
|
|
30
30
|
"ts-codec": "^1.3.0",
|
|
31
31
|
"uuid": "^11.1.0",
|
|
32
|
-
"@powersync/lib-service-postgres": "0.4.
|
|
33
|
-
"@powersync/lib-services-framework": "0.7.
|
|
34
|
-
"@powersync/service-core": "1.
|
|
35
|
-
"@powersync/service-
|
|
36
|
-
"@powersync/service-
|
|
37
|
-
"@powersync/service-
|
|
38
|
-
"@powersync/service-
|
|
32
|
+
"@powersync/lib-service-postgres": "0.4.13",
|
|
33
|
+
"@powersync/lib-services-framework": "0.7.9",
|
|
34
|
+
"@powersync/service-core": "1.16.0",
|
|
35
|
+
"@powersync/service-types": "0.13.1",
|
|
36
|
+
"@powersync/service-jpgwire": "0.21.5",
|
|
37
|
+
"@powersync/service-jsonbig": "0.17.12",
|
|
38
|
+
"@powersync/service-sync-rules": "0.29.6"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"typescript": "^5.7.3",
|
|
42
|
-
"@powersync/service-core-tests": "0.12.
|
|
42
|
+
"@powersync/service-core-tests": "0.12.9"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsc -b",
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { migrations } from '@powersync/service-core';
|
|
2
|
+
import { openMigrationDB } from '../migration-utils.js';
|
|
3
|
+
|
|
4
|
+
export const up: migrations.PowerSyncMigrationFunction = async (context) => {
|
|
5
|
+
const {
|
|
6
|
+
service_context: { configuration }
|
|
7
|
+
} = context;
|
|
8
|
+
await using client = openMigrationDB(configuration.storage);
|
|
9
|
+
await client.transaction(async (db) => {
|
|
10
|
+
await db.sql`
|
|
11
|
+
CREATE TABLE IF NOT EXISTS connection_report_events (
|
|
12
|
+
id TEXT PRIMARY KEY,
|
|
13
|
+
user_agent TEXT NOT NULL,
|
|
14
|
+
client_id TEXT NOT NULL,
|
|
15
|
+
user_id TEXT NOT NULL,
|
|
16
|
+
sdk TEXT NOT NULL,
|
|
17
|
+
jwt_exp TIMESTAMP WITH TIME ZONE,
|
|
18
|
+
connected_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
|
19
|
+
disconnected_at TIMESTAMP WITH TIME ZONE
|
|
20
|
+
)
|
|
21
|
+
`.execute();
|
|
22
|
+
|
|
23
|
+
await db.sql`
|
|
24
|
+
CREATE INDEX IF NOT EXISTS sdk_list_index ON connection_report_events (connected_at, jwt_exp, disconnected_at)
|
|
25
|
+
`.execute();
|
|
26
|
+
|
|
27
|
+
await db.sql`CREATE INDEX IF NOT EXISTS sdk_user_id_index ON connection_report_events (user_id)`.execute();
|
|
28
|
+
|
|
29
|
+
await db.sql`CREATE INDEX IF NOT EXISTS sdk_client_id_index ON connection_report_events (client_id)`.execute();
|
|
30
|
+
|
|
31
|
+
await db.sql`CREATE INDEX IF NOT EXISTS sdk_index ON connection_report_events (sdk)`.execute();
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const down: migrations.PowerSyncMigrationFunction = async (context) => {
|
|
36
|
+
const {
|
|
37
|
+
service_context: { configuration }
|
|
38
|
+
} = context;
|
|
39
|
+
await using client = openMigrationDB(configuration.storage);
|
|
40
|
+
await client.sql`DROP TABLE IF EXISTS connection_report_events`.execute();
|
|
41
|
+
};
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { storage } from '@powersync/service-core';
|
|
2
|
+
import * as pg_wire from '@powersync/service-jpgwire';
|
|
3
|
+
import { event_types } from '@powersync/service-types';
|
|
4
|
+
import { v4 } from 'uuid';
|
|
5
|
+
import * as lib_postgres from '@powersync/lib-service-postgres';
|
|
6
|
+
import { NormalizedPostgresStorageConfig } from '../types/types.js';
|
|
7
|
+
import { SdkReporting, SdkReportingDecoded } from '../types/models/SdkReporting.js';
|
|
8
|
+
import { toInteger } from 'ix/util/tointeger.js';
|
|
9
|
+
import { logger } from '@powersync/lib-services-framework';
|
|
10
|
+
import { getStorageApplicationName } from '../utils/application-name.js';
|
|
11
|
+
import { STORAGE_SCHEMA_NAME } from '../utils/db.js';
|
|
12
|
+
|
|
13
|
+
export type PostgresReportStorageOptions = {
|
|
14
|
+
config: NormalizedPostgresStorageConfig;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export class PostgresReportStorage implements storage.ReportStorage {
|
|
18
|
+
readonly db: lib_postgres.DatabaseClient;
|
|
19
|
+
constructor(protected options: PostgresReportStorageOptions) {
|
|
20
|
+
this.db = new lib_postgres.DatabaseClient({
|
|
21
|
+
config: options.config,
|
|
22
|
+
schema: STORAGE_SCHEMA_NAME,
|
|
23
|
+
applicationName: getStorageApplicationName()
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
this.db.registerListener({
|
|
27
|
+
connectionCreated: async (connection) => this.prepareStatements(connection)
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private parseJsDate(date: Date) {
|
|
32
|
+
const year = date.getFullYear();
|
|
33
|
+
const month = date.getMonth();
|
|
34
|
+
const today = date.getDate();
|
|
35
|
+
const day = date.getDay();
|
|
36
|
+
return {
|
|
37
|
+
year,
|
|
38
|
+
month,
|
|
39
|
+
today,
|
|
40
|
+
day,
|
|
41
|
+
parsedDate: date
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private mapListCurrentConnectionsResponse(
|
|
46
|
+
result: SdkReportingDecoded | null
|
|
47
|
+
): event_types.ClientConnectionReportResponse {
|
|
48
|
+
if (!result) {
|
|
49
|
+
return {
|
|
50
|
+
users: 0,
|
|
51
|
+
sdks: []
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
users: Number(result.users),
|
|
56
|
+
sdks: result.sdks?.data || []
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
private async listConnectionsQuery() {
|
|
60
|
+
return await this.db.sql`
|
|
61
|
+
WITH
|
|
62
|
+
filtered AS (
|
|
63
|
+
SELECT
|
|
64
|
+
*
|
|
65
|
+
FROM
|
|
66
|
+
connection_report_events
|
|
67
|
+
WHERE
|
|
68
|
+
disconnected_at IS NULL
|
|
69
|
+
AND jwt_exp > NOW()
|
|
70
|
+
),
|
|
71
|
+
unique_users AS (
|
|
72
|
+
SELECT
|
|
73
|
+
COUNT(DISTINCT user_id) AS count
|
|
74
|
+
FROM
|
|
75
|
+
filtered
|
|
76
|
+
),
|
|
77
|
+
sdk_versions_array AS (
|
|
78
|
+
SELECT
|
|
79
|
+
sdk,
|
|
80
|
+
COUNT(DISTINCT client_id) AS clients,
|
|
81
|
+
COUNT(DISTINCT user_id) AS users
|
|
82
|
+
FROM
|
|
83
|
+
filtered
|
|
84
|
+
GROUP BY
|
|
85
|
+
sdk
|
|
86
|
+
)
|
|
87
|
+
SELECT
|
|
88
|
+
(
|
|
89
|
+
SELECT
|
|
90
|
+
COALESCE(count, 0)
|
|
91
|
+
FROM
|
|
92
|
+
unique_users
|
|
93
|
+
) AS users,
|
|
94
|
+
(
|
|
95
|
+
SELECT
|
|
96
|
+
JSON_AGG(ROW_TO_JSON(s))
|
|
97
|
+
FROM
|
|
98
|
+
sdk_versions_array s
|
|
99
|
+
) AS sdks;
|
|
100
|
+
`
|
|
101
|
+
.decoded(SdkReporting)
|
|
102
|
+
.first();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private updateTableFilter() {
|
|
106
|
+
const { year, month, today } = this.parseJsDate(new Date());
|
|
107
|
+
const nextDay = today + 1;
|
|
108
|
+
return {
|
|
109
|
+
gte: new Date(year, month, today).toISOString(),
|
|
110
|
+
lt: new Date(year, month, nextDay).toISOString()
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async reportClientConnection(data: event_types.ClientConnectionBucketData): Promise<void> {
|
|
115
|
+
const { sdk, connected_at, user_id, user_agent, jwt_exp, client_id } = data;
|
|
116
|
+
const connectIsoString = connected_at.toISOString();
|
|
117
|
+
const jwtExpIsoString = jwt_exp.toISOString();
|
|
118
|
+
const { gte, lt } = this.updateTableFilter();
|
|
119
|
+
const uuid = v4();
|
|
120
|
+
const result = await this.db.sql`
|
|
121
|
+
UPDATE connection_report_events
|
|
122
|
+
SET
|
|
123
|
+
connected_at = ${{ type: 1184, value: connectIsoString }},
|
|
124
|
+
sdk = ${{ type: 'varchar', value: sdk }},
|
|
125
|
+
user_agent = ${{ type: 'varchar', value: user_agent }},
|
|
126
|
+
jwt_exp = ${{ type: 1184, value: jwtExpIsoString }},
|
|
127
|
+
disconnected_at = NULL
|
|
128
|
+
WHERE
|
|
129
|
+
user_id = ${{ type: 'varchar', value: user_id }}
|
|
130
|
+
AND client_id = ${{ type: 'varchar', value: client_id }}
|
|
131
|
+
AND connected_at >= ${{ type: 1184, value: gte }}
|
|
132
|
+
AND connected_at < ${{ type: 1184, value: lt }};
|
|
133
|
+
`.execute();
|
|
134
|
+
if (result.results[1].status === 'UPDATE 0') {
|
|
135
|
+
await this.db.sql`
|
|
136
|
+
INSERT INTO
|
|
137
|
+
connection_report_events (
|
|
138
|
+
user_id,
|
|
139
|
+
client_id,
|
|
140
|
+
connected_at,
|
|
141
|
+
sdk,
|
|
142
|
+
user_agent,
|
|
143
|
+
jwt_exp,
|
|
144
|
+
id
|
|
145
|
+
)
|
|
146
|
+
VALUES
|
|
147
|
+
(
|
|
148
|
+
${{ type: 'varchar', value: user_id }},
|
|
149
|
+
${{ type: 'varchar', value: client_id }},
|
|
150
|
+
${{ type: 1184, value: connectIsoString }},
|
|
151
|
+
${{ type: 'varchar', value: sdk }},
|
|
152
|
+
${{ type: 'varchar', value: user_agent }},
|
|
153
|
+
${{ type: 1184, value: jwtExpIsoString }},
|
|
154
|
+
${{ type: 'varchar', value: uuid }}
|
|
155
|
+
)
|
|
156
|
+
`.execute();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async reportClientDisconnection(data: event_types.ClientDisconnectionEventData): Promise<void> {
|
|
160
|
+
const { user_id, client_id, disconnected_at, connected_at } = data;
|
|
161
|
+
const disconnectIsoString = disconnected_at.toISOString();
|
|
162
|
+
const connectIsoString = connected_at.toISOString();
|
|
163
|
+
await this.db.sql`
|
|
164
|
+
UPDATE connection_report_events
|
|
165
|
+
SET
|
|
166
|
+
disconnected_at = ${{ type: 1184, value: disconnectIsoString }},
|
|
167
|
+
jwt_exp = NULL
|
|
168
|
+
WHERE
|
|
169
|
+
user_id = ${{ type: 'varchar', value: user_id }}
|
|
170
|
+
AND client_id = ${{ type: 'varchar', value: client_id }}
|
|
171
|
+
AND connected_at = ${{ type: 1184, value: connectIsoString }}
|
|
172
|
+
`.execute();
|
|
173
|
+
}
|
|
174
|
+
async getConnectedClients(): Promise<event_types.ClientConnectionReportResponse> {
|
|
175
|
+
const result = await this.listConnectionsQuery();
|
|
176
|
+
return this.mapListCurrentConnectionsResponse(result);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async getClientConnectionReports(
|
|
180
|
+
data: event_types.ClientConnectionReportRequest
|
|
181
|
+
): Promise<event_types.ClientConnectionReportResponse> {
|
|
182
|
+
const { start, end } = data;
|
|
183
|
+
const result = await this.db.sql`
|
|
184
|
+
WITH
|
|
185
|
+
filtered AS (
|
|
186
|
+
SELECT
|
|
187
|
+
*
|
|
188
|
+
FROM
|
|
189
|
+
connection_report_events
|
|
190
|
+
WHERE
|
|
191
|
+
connected_at >= ${{ type: 1184, value: start.toISOString() }}
|
|
192
|
+
AND connected_at <= ${{ type: 1184, value: end.toISOString() }}
|
|
193
|
+
),
|
|
194
|
+
unique_users AS (
|
|
195
|
+
SELECT
|
|
196
|
+
COUNT(DISTINCT user_id) AS count
|
|
197
|
+
FROM
|
|
198
|
+
filtered
|
|
199
|
+
),
|
|
200
|
+
sdk_versions_array AS (
|
|
201
|
+
SELECT
|
|
202
|
+
sdk,
|
|
203
|
+
COUNT(DISTINCT client_id) AS clients,
|
|
204
|
+
COUNT(DISTINCT user_id) AS users
|
|
205
|
+
FROM
|
|
206
|
+
filtered
|
|
207
|
+
GROUP BY
|
|
208
|
+
sdk
|
|
209
|
+
)
|
|
210
|
+
SELECT
|
|
211
|
+
(
|
|
212
|
+
SELECT
|
|
213
|
+
COALESCE(count, 0)
|
|
214
|
+
FROM
|
|
215
|
+
unique_users
|
|
216
|
+
) AS users,
|
|
217
|
+
(
|
|
218
|
+
SELECT
|
|
219
|
+
JSON_AGG(ROW_TO_JSON(s))
|
|
220
|
+
FROM
|
|
221
|
+
sdk_versions_array s
|
|
222
|
+
) AS sdks;
|
|
223
|
+
`
|
|
224
|
+
.decoded(SdkReporting)
|
|
225
|
+
.first();
|
|
226
|
+
return this.mapListCurrentConnectionsResponse(result);
|
|
227
|
+
}
|
|
228
|
+
async deleteOldConnectionData(data: event_types.DeleteOldConnectionData): Promise<void> {
|
|
229
|
+
const { date } = data;
|
|
230
|
+
const result = await this.db.sql`
|
|
231
|
+
DELETE FROM connection_report_events
|
|
232
|
+
WHERE
|
|
233
|
+
connected_at < ${{ type: 1184, value: date.toISOString() }}
|
|
234
|
+
AND (
|
|
235
|
+
disconnected_at IS NOT NULL
|
|
236
|
+
OR (
|
|
237
|
+
jwt_exp < NOW()
|
|
238
|
+
AND disconnected_at IS NULL
|
|
239
|
+
)
|
|
240
|
+
);
|
|
241
|
+
`.execute();
|
|
242
|
+
const deletedRows = toInteger(result.results[1].status.split(' ')[1] || '0');
|
|
243
|
+
if (deletedRows > 0) {
|
|
244
|
+
logger.info(
|
|
245
|
+
`TTL from ${date.toISOString()}: ${deletedRows} PostgresSQL rows have been removed from connection_report_events.`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async [Symbol.asyncDispose]() {
|
|
251
|
+
await this.db[Symbol.asyncDispose]();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async prepareStatements(connection: pg_wire.PgConnection) {
|
|
255
|
+
// It should be possible to prepare statements for some common operations here.
|
|
256
|
+
// This has not been implemented yet.
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -5,8 +5,9 @@ import { storage } from '@powersync/service-core';
|
|
|
5
5
|
import { isPostgresStorageConfig, normalizePostgresStorageConfig, PostgresStorageConfig } from '../types/types.js';
|
|
6
6
|
import { dropTables } from '../utils/db.js';
|
|
7
7
|
import { PostgresBucketStorageFactory } from './PostgresBucketStorageFactory.js';
|
|
8
|
+
import { PostgresReportStorage } from './PostgresReportStorage.js';
|
|
8
9
|
|
|
9
|
-
export class PostgresStorageProvider implements storage.
|
|
10
|
+
export class PostgresStorageProvider implements storage.StorageProvider {
|
|
10
11
|
get type() {
|
|
11
12
|
return lib_postgres.POSTGRES_CONNECTION_TYPE;
|
|
12
13
|
}
|
|
@@ -28,13 +29,23 @@ export class PostgresStorageProvider implements storage.BucketStorageProvider {
|
|
|
28
29
|
config: normalizedConfig,
|
|
29
30
|
slot_name_prefix: options.resolvedConfig.slot_name_prefix
|
|
30
31
|
});
|
|
32
|
+
|
|
33
|
+
const reportStorageFactory = new PostgresReportStorage({
|
|
34
|
+
config: normalizedConfig
|
|
35
|
+
});
|
|
36
|
+
|
|
31
37
|
return {
|
|
38
|
+
reportStorage: reportStorageFactory,
|
|
32
39
|
storage: storageFactory,
|
|
33
|
-
shutDown: async () =>
|
|
40
|
+
shutDown: async () => {
|
|
41
|
+
await storageFactory.db[Symbol.asyncDispose]();
|
|
42
|
+
await reportStorageFactory.db[Symbol.asyncDispose]();
|
|
43
|
+
},
|
|
34
44
|
tearDown: async () => {
|
|
35
45
|
logger.info(`Tearing down Postgres storage: ${normalizedConfig.database}...`);
|
|
36
46
|
await dropTables(storageFactory.db);
|
|
37
47
|
await storageFactory.db[Symbol.asyncDispose]();
|
|
48
|
+
await reportStorageFactory.db[Symbol.asyncDispose]();
|
|
38
49
|
return true;
|
|
39
50
|
}
|
|
40
51
|
} satisfies storage.ActiveStorage;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as t from 'ts-codec';
|
|
2
|
+
import { bigint, jsonb } from '../codecs.js';
|
|
3
|
+
|
|
4
|
+
export const Sdks = t.object({
|
|
5
|
+
sdk: t.string,
|
|
6
|
+
clients: t.number,
|
|
7
|
+
users: t.number
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export type Sdks = t.Encoded<typeof Sdks>;
|
|
11
|
+
|
|
12
|
+
export const SdkReporting = t.object({
|
|
13
|
+
users: bigint,
|
|
14
|
+
sdks: t
|
|
15
|
+
.object({
|
|
16
|
+
data: jsonb<Sdks[]>(t.array(Sdks))
|
|
17
|
+
})
|
|
18
|
+
.optional()
|
|
19
|
+
.or(t.Null)
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export type SdkReporting = t.Encoded<typeof SdkReporting>;
|
|
23
|
+
export type SdkReportingDecoded = t.Decoded<typeof SdkReporting>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { framework, PowerSyncMigrationManager, ServiceContext, TestStorageOptions } from '@powersync/service-core';
|
|
2
2
|
import { PostgresMigrationAgent } from '../migrations/PostgresMigrationAgent.js';
|
|
3
3
|
import { normalizePostgresStorageConfig, PostgresStorageConfigDecoded } from '../types/types.js';
|
|
4
|
-
import {
|
|
4
|
+
import { PostgresReportStorage } from '../storage/PostgresReportStorage.js';
|
|
5
|
+
import { PostgresBucketStorageFactory } from '../storage/PostgresBucketStorageFactory.js';
|
|
5
6
|
|
|
6
7
|
export type PostgresTestStorageOptions = {
|
|
7
8
|
url: string;
|
|
@@ -12,7 +13,7 @@ export type PostgresTestStorageOptions = {
|
|
|
12
13
|
migrationAgent?: (config: PostgresStorageConfigDecoded) => PostgresMigrationAgent;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
export
|
|
16
|
+
export function postgresTestSetup(factoryOptions: PostgresTestStorageOptions) {
|
|
16
17
|
const BASE_CONFIG = {
|
|
17
18
|
type: 'postgresql' as const,
|
|
18
19
|
uri: factoryOptions.url,
|
|
@@ -48,6 +49,21 @@ export const postgresTestSetup = (factoryOptions: PostgresTestStorageOptions) =>
|
|
|
48
49
|
};
|
|
49
50
|
|
|
50
51
|
return {
|
|
52
|
+
reportFactory: async (options?: TestStorageOptions) => {
|
|
53
|
+
try {
|
|
54
|
+
if (!options?.doNotClear) {
|
|
55
|
+
await migrate(framework.migrations.Direction.Up);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return new PostgresReportStorage({
|
|
59
|
+
config: TEST_CONNECTION_OPTIONS
|
|
60
|
+
});
|
|
61
|
+
} catch (ex) {
|
|
62
|
+
// Vitest does not display these errors nicely when using the `await using` syntx
|
|
63
|
+
console.error(ex, ex.cause);
|
|
64
|
+
throw ex;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
51
67
|
factory: async (options?: TestStorageOptions) => {
|
|
52
68
|
try {
|
|
53
69
|
if (!options?.doNotClear) {
|
|
@@ -66,8 +82,8 @@ export const postgresTestSetup = (factoryOptions: PostgresTestStorageOptions) =>
|
|
|
66
82
|
},
|
|
67
83
|
migrate
|
|
68
84
|
};
|
|
69
|
-
}
|
|
85
|
+
}
|
|
70
86
|
|
|
71
|
-
export
|
|
87
|
+
export function postgresTestStorageFactoryGenerator(factoryOptions: PostgresTestStorageOptions) {
|
|
72
88
|
return postgresTestSetup(factoryOptions).factory;
|
|
73
|
-
}
|
|
89
|
+
}
|
package/src/utils/utils-index.ts
CHANGED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`Connection report storage > Should create a connection event if its after a day 1`] = `
|
|
4
|
+
[
|
|
5
|
+
{
|
|
6
|
+
"client_id": "client_week",
|
|
7
|
+
"sdk": "powersync-js/1.24.5",
|
|
8
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
9
|
+
"user_id": "user_week",
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"client_id": "client_week",
|
|
13
|
+
"sdk": "powersync-js/1.24.5",
|
|
14
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
15
|
+
"user_id": "user_week",
|
|
16
|
+
},
|
|
17
|
+
]
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
exports[`Connection report storage > Should delete rows older than specified range 1`] = `
|
|
21
|
+
{
|
|
22
|
+
"sdks": [
|
|
23
|
+
{
|
|
24
|
+
"clients": 1,
|
|
25
|
+
"sdk": "powersync-dart/1.6.4",
|
|
26
|
+
"users": 1,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"clients": 1,
|
|
30
|
+
"sdk": "powersync-js/1.21.1",
|
|
31
|
+
"users": 1,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"clients": 1,
|
|
35
|
+
"sdk": "powersync-js/1.21.2",
|
|
36
|
+
"users": 1,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"clients": 1,
|
|
40
|
+
"sdk": "powersync-js/1.21.4",
|
|
41
|
+
"users": 1,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"clients": 1,
|
|
45
|
+
"sdk": "powersync-js/1.24.5",
|
|
46
|
+
"users": 1,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"clients": 1,
|
|
50
|
+
"sdk": "unknown",
|
|
51
|
+
"users": 1,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
"users": 5,
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
|
|
58
|
+
exports[`Connection report storage > Should update a connection event and make it disconnected 1`] = `
|
|
59
|
+
[
|
|
60
|
+
{
|
|
61
|
+
"client_id": "client_three",
|
|
62
|
+
"sdk": "powersync-js/1.21.2",
|
|
63
|
+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
|
|
64
|
+
"user_id": "user_three",
|
|
65
|
+
},
|
|
66
|
+
]
|
|
67
|
+
`;
|
|
68
|
+
|
|
69
|
+
exports[`Connection report storage > Should update a connection event if its within a day 1`] = `
|
|
70
|
+
[
|
|
71
|
+
{
|
|
72
|
+
"client_id": "client_one",
|
|
73
|
+
"sdk": "powersync-dart/1.6.4",
|
|
74
|
+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
|
|
75
|
+
"user_id": "user_one",
|
|
76
|
+
},
|
|
77
|
+
]
|
|
78
|
+
`;
|
|
79
|
+
|
|
80
|
+
exports[`Report storage tests > Should show connection report data for user over the past day 1`] = `
|
|
81
|
+
{
|
|
82
|
+
"sdks": [
|
|
83
|
+
{
|
|
84
|
+
"clients": 1,
|
|
85
|
+
"sdk": "powersync-dart/1.6.4",
|
|
86
|
+
"users": 1,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"clients": 1,
|
|
90
|
+
"sdk": "powersync-js/1.21.1",
|
|
91
|
+
"users": 1,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"clients": 1,
|
|
95
|
+
"sdk": "powersync-js/1.21.4",
|
|
96
|
+
"users": 1,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
"clients": 1,
|
|
100
|
+
"sdk": "unknown",
|
|
101
|
+
"users": 1,
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
"users": 3,
|
|
105
|
+
}
|
|
106
|
+
`;
|
|
107
|
+
|
|
108
|
+
exports[`Report storage tests > Should show connection report data for user over the past month 1`] = `
|
|
109
|
+
{
|
|
110
|
+
"sdks": [
|
|
111
|
+
{
|
|
112
|
+
"clients": 1,
|
|
113
|
+
"sdk": "powersync-dart/1.6.4",
|
|
114
|
+
"users": 1,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
"clients": 1,
|
|
118
|
+
"sdk": "powersync-js/1.21.1",
|
|
119
|
+
"users": 1,
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"clients": 1,
|
|
123
|
+
"sdk": "powersync-js/1.21.2",
|
|
124
|
+
"users": 1,
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
"clients": 1,
|
|
128
|
+
"sdk": "powersync-js/1.21.4",
|
|
129
|
+
"users": 1,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
"clients": 1,
|
|
133
|
+
"sdk": "powersync-js/1.23.6",
|
|
134
|
+
"users": 1,
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"clients": 1,
|
|
138
|
+
"sdk": "powersync-js/1.23.7",
|
|
139
|
+
"users": 1,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
"clients": 1,
|
|
143
|
+
"sdk": "powersync-js/1.24.5",
|
|
144
|
+
"users": 1,
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
"clients": 1,
|
|
148
|
+
"sdk": "unknown",
|
|
149
|
+
"users": 1,
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
"users": 7,
|
|
153
|
+
}
|
|
154
|
+
`;
|
|
155
|
+
|
|
156
|
+
exports[`Report storage tests > Should show connection report data for user over the past week 1`] = `
|
|
157
|
+
{
|
|
158
|
+
"sdks": [
|
|
159
|
+
{
|
|
160
|
+
"clients": 1,
|
|
161
|
+
"sdk": "powersync-dart/1.6.4",
|
|
162
|
+
"users": 1,
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"clients": 1,
|
|
166
|
+
"sdk": "powersync-js/1.21.1",
|
|
167
|
+
"users": 1,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
"clients": 1,
|
|
171
|
+
"sdk": "powersync-js/1.21.2",
|
|
172
|
+
"users": 1,
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"clients": 1,
|
|
176
|
+
"sdk": "powersync-js/1.21.4",
|
|
177
|
+
"users": 1,
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"clients": 1,
|
|
181
|
+
"sdk": "powersync-js/1.24.5",
|
|
182
|
+
"users": 1,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"clients": 1,
|
|
186
|
+
"sdk": "unknown",
|
|
187
|
+
"users": 1,
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
"users": 5,
|
|
191
|
+
}
|
|
192
|
+
`;
|
|
193
|
+
|
|
194
|
+
exports[`Report storage tests > Should show currently connected users 1`] = `
|
|
195
|
+
{
|
|
196
|
+
"sdks": [
|
|
197
|
+
{
|
|
198
|
+
"clients": 1,
|
|
199
|
+
"sdk": "powersync-dart/1.6.4",
|
|
200
|
+
"users": 1,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
"clients": 1,
|
|
204
|
+
"sdk": "powersync-js/1.21.1",
|
|
205
|
+
"users": 1,
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
"clients": 1,
|
|
209
|
+
"sdk": "unknown",
|
|
210
|
+
"users": 1,
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
"users": 2,
|
|
214
|
+
}
|
|
215
|
+
`;
|