metamigo-api: work on making it build
This commit is contained in:
parent
38e68852d9
commit
ef216f7b1c
35 changed files with 407 additions and 322 deletions
|
|
@ -10,7 +10,6 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@hapi/hapi": "^21.3.0",
|
||||
"@hapi/hoek": "^11.0.1",
|
||||
"pg-monitor": "^1.4.1",
|
||||
"pg-promise": "^10.11.0"
|
||||
},
|
||||
|
|
@ -19,8 +18,7 @@
|
|||
"fix:lint": "eslint src --ext .ts --fix",
|
||||
"fmt": "prettier \"src/**/*.ts\" --write",
|
||||
"test": "jest --coverage --forceExit --detectOpenHandles --reporters=default --reporters=jest-junit",
|
||||
"lint": "eslint src --ext .ts",
|
||||
"lint-fmt": "prettier \"src/**/*.ts\" --list-different",
|
||||
"lint": "eslint src --ext .ts && prettier \"src/**/*.ts\" --list-different",
|
||||
"doc": "typedoc src/ --exclude '**/*.test.ts' --exclude '**/*.spec.ts' --name $npm_package_name --readme README.md --target es2019 --mode file --out build/docs",
|
||||
"watch:build": "tsc -p tsconfig.json -w"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import PgPromisePlugin from ".";
|
||||
import { makePlugin } from ".";
|
||||
|
||||
const plugin = makePlugin();
|
||||
|
||||
describe("plugin option validation", () => {
|
||||
let server;
|
||||
|
|
@ -8,7 +10,7 @@ describe("plugin option validation", () => {
|
|||
});
|
||||
|
||||
it("should throw when no connection details defined", async () => {
|
||||
expect(server.register(PgPromisePlugin)).rejects.toThrow();
|
||||
expect(server.register(plugin)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -20,7 +22,7 @@ describe("basic plugin runtime", () => {
|
|||
beforeEach(async () => {
|
||||
server = new Hapi.Server({ port: 0 });
|
||||
await server.register({
|
||||
plugin: PgPromisePlugin,
|
||||
plugin,
|
||||
options: defaultOpts,
|
||||
});
|
||||
await server.start();
|
||||
|
|
@ -63,7 +65,7 @@ describe("plugin runtime", () => {
|
|||
expect.assertions(5);
|
||||
|
||||
await server.register({
|
||||
plugin: PgPromisePlugin,
|
||||
plugin,
|
||||
options: {
|
||||
...defaultOpts,
|
||||
logSql: true,
|
||||
|
|
|
|||
|
|
@ -1,22 +1,12 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import * as Hoek from "@hapi/hoek";
|
||||
import pgPromise from "pg-promise";
|
||||
import * as pgMonitor from "pg-monitor";
|
||||
import type { IConnectionParameters } from "pg-promise/typescript/pg-subset";
|
||||
import type { IMain, IInitOptions } from "pg-promise";
|
||||
|
||||
import { IPGPPluginOptions, ExtendedProtocol } from "./types";
|
||||
import { Plugin } from "@hapi/hapi/lib/types/plugin";
|
||||
|
||||
export * from "./types";
|
||||
const defaultOptions: IPGPPluginOptions<unknown> = {
|
||||
connection: undefined,
|
||||
pgpInit: {},
|
||||
logSql: false,
|
||||
decorateAs: {
|
||||
pgp: "pgp",
|
||||
db: "db",
|
||||
},
|
||||
};
|
||||
|
||||
export const startDiagnostics = <T>(
|
||||
logSql: boolean,
|
||||
|
|
@ -45,59 +35,57 @@ const startDb = async <T>(
|
|||
return db;
|
||||
};
|
||||
|
||||
const register = async <T>(
|
||||
server: Hapi.Server,
|
||||
userOpts?: IPGPPluginOptions<T>
|
||||
): Promise<void> => {
|
||||
const options: IPGPPluginOptions<T> = Hoek.applyToDefaults(
|
||||
defaultOptions,
|
||||
userOpts
|
||||
) as IPGPPluginOptions<T>;
|
||||
export function makePlugin<T>(): Plugin<IPGPPluginOptions<T>, void> {
|
||||
return {
|
||||
version: "1.0.0",
|
||||
name: "pg-promise",
|
||||
async register(
|
||||
server: Hapi.Server,
|
||||
userOpts?: IPGPPluginOptions<T>
|
||||
): Promise<void> {
|
||||
if (userOpts.logSql === undefined) userOpts.logSql = false;
|
||||
if (!userOpts.decorateAs) userOpts.decorateAs = { pgp: "pgp", db: "db" };
|
||||
|
||||
if (!options.connection) {
|
||||
throw new Error(
|
||||
"hapi-pg-promise: connection details are not defined. You must specify a valid connection for the plugin to boot."
|
||||
);
|
||||
}
|
||||
const options = userOpts;
|
||||
|
||||
if ("pgp" in options && "pgpInit" in options) {
|
||||
throw new Error(
|
||||
"hapi-pg-promise: options pgp and pgpInit are mutually exclusive"
|
||||
);
|
||||
}
|
||||
if (!options.connection) {
|
||||
throw new Error(
|
||||
"hapi-pg-promise: connection details are not defined. You must specify a valid connection for the plugin to boot."
|
||||
);
|
||||
}
|
||||
|
||||
let pgp: IMain;
|
||||
if ("pgp" in options) {
|
||||
pgp = options.pgp;
|
||||
} else {
|
||||
pgp = await startPgp(options.pgpInit);
|
||||
startDiagnostics(options.logSql, options.pgpInit);
|
||||
}
|
||||
if ("pgp" in options && "pgpInit" in options) {
|
||||
throw new Error(
|
||||
"hapi-pg-promise: options pgp and pgpInit are mutually exclusive"
|
||||
);
|
||||
}
|
||||
|
||||
const db = await startDb(pgp, options.connection);
|
||||
let pgp: IMain;
|
||||
if ("pgp" in options) {
|
||||
pgp = options.pgp;
|
||||
} else {
|
||||
pgp = await startPgp(options.pgpInit || {});
|
||||
startDiagnostics(options.logSql, options.pgpInit || {});
|
||||
}
|
||||
|
||||
if (options.decorateAs) {
|
||||
if (options.decorateAs) {
|
||||
server.decorate("request", options.decorateAs.pgp, pgp);
|
||||
server.decorate("server", options.decorateAs.pgp, pgp);
|
||||
}
|
||||
const db = await startDb(pgp, options.connection);
|
||||
|
||||
if (options.decorateAs.db) {
|
||||
server.decorate("server", options.decorateAs.db, () => db);
|
||||
server.decorate("request", options.decorateAs.db, () => db);
|
||||
}
|
||||
}
|
||||
if (options.decorateAs) {
|
||||
if (options.decorateAs) {
|
||||
server.decorate("request", options.decorateAs.pgp, pgp);
|
||||
server.decorate("server", options.decorateAs.pgp, pgp);
|
||||
}
|
||||
|
||||
server.ext("onPostStop", async () => {
|
||||
stopDiagnostics();
|
||||
await db.$pool.end();
|
||||
});
|
||||
};
|
||||
if (options.decorateAs.db) {
|
||||
server.decorate("server", options.decorateAs.db, () => db);
|
||||
server.decorate("request", options.decorateAs.db, () => db);
|
||||
}
|
||||
}
|
||||
|
||||
const pgPromisePlugin = {
|
||||
register,
|
||||
name: "pg-promise",
|
||||
version: "0.0.1",
|
||||
};
|
||||
|
||||
export default pgPromisePlugin;
|
||||
server.ext("onPostStop", async () => {
|
||||
stopDiagnostics();
|
||||
await db.$pool.end();
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue