Convert DB connection code to TypeScript

This commit is contained in:
Vincent 2024-09-10 15:30:01 +02:00 коммит произвёл Vincent
Родитель 0a23e91e95
Коммит b0d848befd
13 изменённых файлов: 34 добавлений и 38 удалений

Просмотреть файл

@ -1,27 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import knex from "knex";
import knexConfig from "./knexfile.js";
/** @returns {knex.Knex} */
export default function createDbConnection() {
/* c8 ignore start */
if (process.env.NODE_ENV === "development") {
/** @type {import("./knexfile").KnexConfig} knexConfig */
const { client } = knexConfig;
if (!(client in global)) {
// @ts-ignore-next-line: globalThis is a read-only property
global[client] = knex(knexConfig);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
// @ts-ignore-next-line: Implicitly has type any
return global[client];
}
/* c8 ignore stop */
// @ts-ignore-next-line: knexConfig is expected to have @type KnexConfig
return knex(knexConfig);
}

23
src/db/connect.ts Normal file
Просмотреть файл

@ -0,0 +1,23 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import knex from "knex";
import knexConfig from "./knexfile.js";
export default function createDbConnection(): knex.Knex {
/* c8 ignore start */
if (process.env.NODE_ENV === "development") {
const { client } = knexConfig;
if (!(client in global)) {
(global as unknown as Record<string, knex.Knex>)[client] = knex(
knexConfig as knex.Knex.Config,
);
}
return (global as unknown as Record<string, knex.Knex>)[client];
}
/* c8 ignore stop */
return knex(knexConfig as knex.Knex.Config);
}

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { logger } from "../../app/functions/server/logging";
import { AttributionRow } from "knex/types/tables";

Просмотреть файл

@ -7,7 +7,7 @@ import {
formatDataClassesArray,
HibpGetBreachesResponse,
} from "../../utils/hibp";
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
const knex = createDbConnection();

Просмотреть файл

@ -4,7 +4,7 @@
import { v4 as uuidv4 } from "uuid";
import type { Profile } from "next-auth";
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { subscribeHash } from "../../utils/hibp";
import { getSha1 } from "../../utils/fxa";
import { updateFxAData } from "./subscribers";

Просмотреть файл

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { BreachRow } from "knex/types/tables";
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
const knex = createDbConnection();

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { logger } from "../../app/functions/server/logging";
import { FeatureFlagRow } from "knex/types/tables";

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { CreateProfileRequest } from "../../app/functions/server/onerep.js";
import { parseIso8601Datetime } from "../../utils/parse";

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { logger } from "../../app/functions/server/logging";
import {

Просмотреть файл

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { StatsRow } from "knex/types/tables";
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
const knex = createDbConnection();
export { knex as knexStats };

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { logger } from "../../app/functions/server/logging";
const knex = createDbConnection();

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import { logger } from "../../app/functions/server/logging";
import { captureException } from "@sentry/node";

Просмотреть файл

@ -4,7 +4,7 @@
import type { Profile } from "next-auth";
import type { EmailAddressRow, SubscriberRow } from "knex/types/tables";
import createDbConnection from "../connect.js";
import createDbConnection from "../connect";
import AppConstants from "../../appConstants.js";
import { SerializedSubscriber } from "../../next-auth.js";
import { getFeatureFlagData } from "./featureFlags";