2023-08-16 20:54:39 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { noop } from 'lodash';
|
2024-07-22 18:16:33 +00:00
|
|
|
import SQL from '@signalapp/better-sqlite3';
|
2023-08-16 20:54:39 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
import type { ReadableDB, WritableDB } from '../../sql/Interface';
|
2023-08-16 20:54:39 +00:00
|
|
|
import { SCHEMA_VERSIONS } from '../../sql/migrations';
|
|
|
|
import { consoleLogger } from '../../util/consoleLogger';
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
export function createDB(): WritableDB {
|
|
|
|
return new SQL(':memory:') as WritableDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateToVersion(db: WritableDB, version: number): void {
|
2023-08-16 20:54:39 +00:00
|
|
|
const startVersion = db.pragma('user_version', { simple: true });
|
|
|
|
|
|
|
|
const silentLogger = {
|
|
|
|
...consoleLogger,
|
|
|
|
info: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const run of SCHEMA_VERSIONS) {
|
|
|
|
run(startVersion, db, silentLogger);
|
|
|
|
|
|
|
|
const currentVersion = db.pragma('user_version', { simple: true });
|
|
|
|
|
|
|
|
if (currentVersion === version) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Migration to ${version} not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
type TableRows = ReadonlyArray<
|
|
|
|
Record<string, string | number | null | Record<string, unknown>>
|
|
|
|
>;
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
export function insertData(
|
|
|
|
db: WritableDB,
|
|
|
|
table: string,
|
|
|
|
rows: TableRows
|
|
|
|
): void {
|
2023-08-16 20:54:39 +00:00
|
|
|
for (const row of rows) {
|
|
|
|
db.prepare(
|
|
|
|
`
|
|
|
|
INSERT INTO ${table} (${Object.keys(row).join(', ')})
|
|
|
|
VALUES (${Object.values(row)
|
|
|
|
.map(() => '?')
|
|
|
|
.join(', ')});
|
|
|
|
`
|
|
|
|
).run(
|
|
|
|
Object.values(row).map(v => {
|
|
|
|
if (v != null && typeof v === 'object') {
|
|
|
|
return JSON.stringify(v);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
export function getTableData(db: ReadableDB, table: string): TableRows {
|
2023-08-16 20:54:39 +00:00
|
|
|
return db
|
|
|
|
.prepare(`SELECT * FROM ${table}`)
|
|
|
|
.all()
|
|
|
|
.map((row: Record<string, string | number | null>) => {
|
|
|
|
const result: Record<
|
|
|
|
string,
|
|
|
|
string | number | null | Record<string, unknown>
|
|
|
|
> = {};
|
|
|
|
for (const [key, value] of Object.entries(row)) {
|
|
|
|
if (value == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
throw new Error('skip');
|
|
|
|
}
|
|
|
|
result[key] = JSON.parse(value) as Record<string, unknown>;
|
|
|
|
} catch {
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|