metamigo-db: build, fmt, lint
This commit is contained in:
parent
2ffb15b1f9
commit
2a1ced5383
17 changed files with 160 additions and 125 deletions
|
|
@ -1 +1,8 @@
|
|||
require("../.eslintrc.js");
|
||||
require('eslint-config-link/patch/modern-module-resolution');
|
||||
module.exports = {
|
||||
extends: [
|
||||
"eslint-config-link/profile/node",
|
||||
"eslint-config-link/profile/typescript"
|
||||
],
|
||||
parserOptions: { tsconfigRootDir: __dirname }
|
||||
};
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
import { IAppConfig } from "config";
|
||||
import camelcaseKeys from "camelcase-keys";
|
||||
import PgSimplifyInflectorPlugin from "@graphile-contrib/pg-simplify-inflector";
|
||||
// import PgManyToManyPlugin from "@graphile-contrib/pg-many-to-many";
|
||||
import * as ConnectionFilterPlugin from "postgraphile-plugin-connection-filter";
|
||||
import type { PostGraphileCoreOptions } from "postgraphile-core";
|
||||
|
||||
import {
|
||||
UserRecordRepository,
|
||||
AccountRecordRepository,
|
||||
SessionRecordRepository,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
import {
|
||||
SettingRecordRepository,
|
||||
VoiceProviderRecordRepository,
|
||||
VoiceLineRecordRepository,
|
||||
WebhookRecordRepository,
|
||||
WhatsappBotRecordRepository,
|
||||
WhatsappMessageRecordRepository,
|
||||
WhatsappAttachmentRecordRepository,
|
||||
SignalBotRecordRepository,
|
||||
} from "./records";
|
||||
|
||||
import type { IInitOptions, IDatabase } from "pg-promise";
|
||||
|
||||
export interface IRepositories {
|
||||
users: UserRecordRepository;
|
||||
sessions: SessionRecordRepository;
|
||||
accounts: AccountRecordRepository;
|
||||
settings: SettingRecordRepository;
|
||||
voiceLines: VoiceLineRecordRepository;
|
||||
voiceProviders: VoiceProviderRecordRepository;
|
||||
webhooks: WebhookRecordRepository;
|
||||
whatsappBots: WhatsappBotRecordRepository;
|
||||
whatsappMessages: WhatsappMessageRecordRepository;
|
||||
whatsappAttachments: WhatsappAttachmentRecordRepository;
|
||||
signalBots: SignalBotRecordRepository;
|
||||
}
|
||||
|
||||
export type AppDatabase = IDatabase<IRepositories> & IRepositories;
|
||||
|
||||
export const dbInitOptions = (
|
||||
_config: IAppConfig
|
||||
): IInitOptions<IRepositories> => {
|
||||
return {
|
||||
noWarnings: true,
|
||||
receive(data, result) {
|
||||
if (result) result.rows = camelcaseKeys(data);
|
||||
},
|
||||
|
||||
// Extending the database protocol with our custom repositories;
|
||||
// API: http://vitaly-t.github.io/pg-promise/global.html#event:extend
|
||||
extend(obj: any, _dc) { // AppDatase was obj type
|
||||
// Database Context (_dc) is mainly needed for extending multiple databases with different access API.
|
||||
|
||||
// NOTE:
|
||||
// This event occurs for every task and transaction being executed (which could be every request!)
|
||||
// so it should be as fast as possible. Do not use 'require()' or do any other heavy lifting.
|
||||
obj.users = new UserRecordRepository(obj);
|
||||
obj.sessions = new SessionRecordRepository(obj);
|
||||
obj.accounts = new AccountRecordRepository(obj);
|
||||
obj.settings = new SettingRecordRepository(obj);
|
||||
obj.voiceLines = new VoiceLineRecordRepository(obj);
|
||||
obj.voiceProviders = new VoiceProviderRecordRepository(obj);
|
||||
obj.webhooks = new WebhookRecordRepository(obj);
|
||||
obj.whatsappBots = new WhatsappBotRecordRepository(obj);
|
||||
obj.whatsappMessages = new WhatsappMessageRecordRepository(obj);
|
||||
obj.whatsappAttachments = new WhatsappAttachmentRecordRepository(obj);
|
||||
obj.signalBots = new SignalBotRecordRepository(obj);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const getPostGraphileOptions = (): PostGraphileCoreOptions => {
|
||||
return {
|
||||
ignoreRBAC: false,
|
||||
dynamicJson: true,
|
||||
ignoreIndexes: false,
|
||||
appendPlugins: [
|
||||
PgSimplifyInflectorPlugin,
|
||||
// PgManyToManyPlugin,
|
||||
ConnectionFilterPlugin as any,
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
export * from "./helpers";
|
||||
export * from "./records";
|
||||
|
|
@ -25,15 +25,11 @@
|
|||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"build-test": "tsc -p tsconfig.json",
|
||||
"doc:html": "typedoc src/ --exclude '**/*.test.ts' --exclude '**/*.spec.ts' --name $npm_package_name --readme README.md --target es2019 --mode file --out build/docs",
|
||||
"doc": "yarn run doc:html",
|
||||
"doc": "typedoc src/ --exclude '**/*.test.ts' --exclude '**/*.spec.ts' --name $npm_package_name --readme README.md --target es2019 --mode file --out build/docs",
|
||||
"fix:lint": "eslint src --ext .ts --fix",
|
||||
"fix:prettier": "prettier \"src/**/*.ts\" --write",
|
||||
"worker": "NODE_ENV=development yarn cli worker",
|
||||
"lint:lint": "eslint src --ext .ts",
|
||||
"lint:prettier": "prettier \"src/**/*.ts\" --list-different",
|
||||
"lint": "npm run lint:lint && npm run lint:prettier",
|
||||
"watch:build": "tsc -p tsconfig.json -w"
|
||||
"fmt": "prettier \"src/**/*.ts\" --write",
|
||||
"lint": "eslint src --ext .ts && prettier \"src/**/*.ts\" --list-different",
|
||||
"watch:build": "tsc -p tsconfig.json -w",
|
||||
"worker": "NODE_ENV=development yarn cli worker"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import process from "process";
|
||||
import { existsSync } from "fs";
|
||||
import { exec } from "child_process";
|
||||
import type { IAppConfig } from "config";
|
||||
import process from "node:process";
|
||||
import { existsSync } from "node:fs";
|
||||
import { exec } from "node:child_process";
|
||||
import type { IAppConfig } from "@digiresilience/metamigo-config";
|
||||
|
||||
/**
|
||||
* We use graphile-migrate for managing database migrations.
|
||||
89
packages/metamigo-db/src/index.ts
Normal file
89
packages/metamigo-db/src/index.ts
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import type { IAppConfig } from "@digiresilience/metamigo-config";
|
||||
import camelcaseKeys from "camelcase-keys";
|
||||
import PgSimplifyInflectorPlugin from "@graphile-contrib/pg-simplify-inflector";
|
||||
// import PgManyToManyPlugin from "@graphile-contrib/pg-many-to-many";
|
||||
import * as ConnectionFilterPlugin from "postgraphile-plugin-connection-filter";
|
||||
import type { PostGraphileCoreOptions } from "postgraphile-core";
|
||||
|
||||
import {
|
||||
UserRecordRepository,
|
||||
AccountRecordRepository,
|
||||
SessionRecordRepository,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
import {
|
||||
SettingRecordRepository,
|
||||
VoiceProviderRecordRepository,
|
||||
VoiceLineRecordRepository,
|
||||
WebhookRecordRepository,
|
||||
WhatsappBotRecordRepository,
|
||||
WhatsappMessageRecordRepository,
|
||||
WhatsappAttachmentRecordRepository,
|
||||
SignalBotRecordRepository,
|
||||
} from "./records";
|
||||
|
||||
import type { IInitOptions, IDatabase } from "pg-promise";
|
||||
|
||||
export interface IRepositories {
|
||||
users: UserRecordRepository;
|
||||
sessions: SessionRecordRepository;
|
||||
accounts: AccountRecordRepository;
|
||||
settings: SettingRecordRepository;
|
||||
voiceLines: VoiceLineRecordRepository;
|
||||
voiceProviders: VoiceProviderRecordRepository;
|
||||
webhooks: WebhookRecordRepository;
|
||||
whatsappBots: WhatsappBotRecordRepository;
|
||||
whatsappMessages: WhatsappMessageRecordRepository;
|
||||
whatsappAttachments: WhatsappAttachmentRecordRepository;
|
||||
signalBots: SignalBotRecordRepository;
|
||||
}
|
||||
|
||||
export type AppDatabase = IDatabase<IRepositories> & IRepositories;
|
||||
|
||||
export const dbInitOptions = (
|
||||
_config: IAppConfig
|
||||
): IInitOptions<IRepositories> => ({
|
||||
noWarnings: true,
|
||||
receive(e) {
|
||||
const { data, result } = e;
|
||||
if (result) result.rows = camelcaseKeys(data);
|
||||
},
|
||||
|
||||
// Extending the database protocol with our custom repositories;
|
||||
// API: http://vitaly-t.github.io/pg-promise/global.html#event:extend
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
extend(obj: any, _dc) {
|
||||
// AppDatase was obj type
|
||||
// Database Context (_dc) is mainly needed for extending multiple databases with different access API.
|
||||
|
||||
// NOTE:
|
||||
// This event occurs for every task and transaction being executed (which could be every request!)
|
||||
// so it should be as fast as possible. Do not use 'require()' or do any other heavy lifting.
|
||||
obj.users = new UserRecordRepository(obj);
|
||||
obj.sessions = new SessionRecordRepository(obj);
|
||||
obj.accounts = new AccountRecordRepository(obj);
|
||||
obj.settings = new SettingRecordRepository(obj);
|
||||
obj.voiceLines = new VoiceLineRecordRepository(obj);
|
||||
obj.voiceProviders = new VoiceProviderRecordRepository(obj);
|
||||
obj.webhooks = new WebhookRecordRepository(obj);
|
||||
obj.whatsappBots = new WhatsappBotRecordRepository(obj);
|
||||
obj.whatsappMessages = new WhatsappMessageRecordRepository(obj);
|
||||
obj.whatsappAttachments = new WhatsappAttachmentRecordRepository(obj);
|
||||
obj.signalBots = new SignalBotRecordRepository(obj);
|
||||
},
|
||||
});
|
||||
|
||||
export const getPostGraphileOptions = (): PostGraphileCoreOptions => ({
|
||||
ignoreRBAC: false,
|
||||
dynamicJson: true,
|
||||
ignoreIndexes: false,
|
||||
appendPlugins: [
|
||||
PgSimplifyInflectorPlugin,
|
||||
// PgManyToManyPlugin,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
ConnectionFilterPlugin as any,
|
||||
],
|
||||
});
|
||||
|
||||
export * from "./helpers";
|
||||
export * from "./records";
|
||||
|
|
@ -1,5 +1,10 @@
|
|||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-unused-vars,@typescript-eslint/no-explicit-any,prefer-destructuring */
|
||||
import { RepositoryBase, recordInfo, UUID, Flavor } from "@digiresilience/metamigo-common";
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-explicit-any,prefer-destructuring */
|
||||
import {
|
||||
RepositoryBase,
|
||||
recordInfo,
|
||||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
export type SettingId = Flavor<UUID, "Setting Id">;
|
||||
|
||||
|
|
@ -71,12 +76,12 @@ export const SettingsService = (
|
|||
repo: SettingRecordRepository
|
||||
): ISettingsService => ({
|
||||
name: "settingService",
|
||||
lookup: async <T>(settingInfo: SettingInfo<T>): Promise<T> => {
|
||||
async lookup<T>(settingInfo: SettingInfo<T>): Promise<T> {
|
||||
const s = await repo.findByName<T>(settingInfo.name);
|
||||
return s.value;
|
||||
},
|
||||
|
||||
save: async <T>(settingInfo: SettingInfo<T>, value: T): Promise<T> => {
|
||||
async save<T>(settingInfo: SettingInfo<T>, value: T): Promise<T> {
|
||||
const s = await repo.upsert(settingInfo.name, value);
|
||||
return s.value;
|
||||
},
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
import { RepositoryBase, recordInfo, UUID, Flavor } from "@digiresilience/metamigo-common";
|
||||
import {
|
||||
RepositoryBase,
|
||||
recordInfo,
|
||||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
export type SignalBotId = Flavor<UUID, "Signal Bot Id">;
|
||||
|
||||
|
|
@ -4,7 +4,7 @@ import {
|
|||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
import type { } from "pg-promise";
|
||||
import type {} from "pg-promise";
|
||||
|
||||
export type VoiceLineId = Flavor<UUID, "VoiceLine Id">;
|
||||
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
import { RepositoryBase, recordInfo, UUID, Flavor } from "@digiresilience/metamigo-common";
|
||||
import {
|
||||
RepositoryBase,
|
||||
recordInfo,
|
||||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
/*
|
||||
* VoiceProvider
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
import { RepositoryBase, recordInfo, UUID, Flavor } from "@digiresilience/metamigo-common";
|
||||
import {
|
||||
RepositoryBase,
|
||||
recordInfo,
|
||||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
export type WhatsappAttachmentId = Flavor<UUID, "Whatsapp Attachment Id">;
|
||||
|
||||
|
|
@ -21,4 +26,4 @@ export const WhatsappAttachmentRecord = recordInfo<
|
|||
|
||||
export class WhatsappAttachmentRecordRepository extends RepositoryBase(
|
||||
WhatsappAttachmentRecord
|
||||
) { }
|
||||
) {}
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
import { RepositoryBase, recordInfo, UUID, Flavor } from "@digiresilience/metamigo-common";
|
||||
import {
|
||||
RepositoryBase,
|
||||
recordInfo,
|
||||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
export type WhatsappBotId = Flavor<UUID, "Whatsapp Bot Id">;
|
||||
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
import { RepositoryBase, recordInfo, UUID, Flavor } from "@digiresilience/metamigo-common";
|
||||
import {
|
||||
RepositoryBase,
|
||||
recordInfo,
|
||||
UUID,
|
||||
Flavor,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
|
||||
export type WhatsappMessageId = Flavor<UUID, "Whatsapp Message Id">;
|
||||
|
||||
|
|
@ -23,4 +28,4 @@ export const WhatsappMessageRecord = recordInfo<
|
|||
|
||||
export class WhatsappMessageRecordRepository extends RepositoryBase(
|
||||
WhatsappMessageRecord
|
||||
) { }
|
||||
) {}
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"extends": "tsconfig-link",
|
||||
"compilerOptions": {
|
||||
"outDir": "build/main"
|
||||
"incremental": true,
|
||||
"outDir": "build/main",
|
||||
"rootDir": "src",
|
||||
"baseUrl": "./",
|
||||
"types": ["jest", "node"]
|
||||
},
|
||||
"include": ["**/*.ts", "**/.*.ts"],
|
||||
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["node_modules/**"]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue