Line data Source code
1 : import type { SessionMigrationStep } from '../session_migration.js';
2 :
3 : /// Rename the `sidb` key of the `params` and the `paramsDraft` of stored connections to `dbcom`.
4 : /// Returns the connections unchanged if none of them has the key, sessions and the app manifest share the shape.
5 1 : export function renameSidbConnections(connections: unknown[]): unknown[] {
6 1 : const renamed = (params: unknown) => {
7 1 : if (params == null || typeof params !== 'object' || !('sidb' in params)) return params;
8 1 : const { sidb, ...rest } = params as Record<string, unknown>;
9 1 : return { ...rest, dbcom: sidb };
10 1 : };
11 1 : return connections.map(connection => {
12 1 : if (connection == null || typeof connection !== 'object') return connection;
13 1 : const entry = connection as Record<string, unknown>;
14 1 : return {
15 1 : ...entry,
16 1 : ...('params' in entry ? { params: renamed(entry.params) } : {}),
17 1 : ...('paramsDraft' in entry ? { paramsDraft: renamed(entry.paramsDraft) } : {}),
18 1 : };
19 1 : });
20 1 : }
21 :
22 : /// The Database.com connector was called SIDB.
23 : ///
24 : /// Up to version 9 the connections of its sessions keep their params under the key `sidb`, the step moves them to
25 : /// the key `dbcom`.
26 1 : export const dbcomConnector: SessionMigrationStep = {
27 1 : version: 10,
28 1 : description: 'Rename the connection params of the SIDB connector to dbcom',
29 1 : async migrate(folder) {
30 1 : const text = await folder.readText('dashql-session.json');
31 1 : if (text == null) return;
32 1 : const data = JSON.parse(text) as Record<string, unknown>;
33 1 : if (!Array.isArray(data.connections)) return;
34 1 : data.connections = renameSidbConnections(data.connections);
35 1 : await folder.writeText('dashql-session.json', JSON.stringify(data, null, 2));
36 1 : },
37 1 : };
|