Программа для работы с базой данных postgre
Для установки программы используйте npm:
npm i postre-manager
Подключите модуль postgre-manager и в методе use приложения Express укажите на использование installRouter.
import * as Express from 'express';
const app = Express();
import * as Http from 'http';
const server = new Http.Server(app);
import { InstallRouter, IPostgreConnectionOptions } from 'postgre-manager';
...
app.use('/install/db', InstallRouter(server, "db", <IPostgreConnectionOptions>options, queries));
где,
server: Http.Server,
"db": псевдоним, используется при подключении нескольких InstallRouter,
options: IPostgreConnectionOptions - параметры подключения к базе данных postgre,
queries: Array<namedQuery> - массив запросов создания базы данных.
interface IPostgreConnectionOptions {
host: string,
user?: string, // default process.env.PGUSER || process.env.USER
password?: string, //default process.env.PGPASSWORD
database?: string, // default process.env.PGDATABASE || process.env.USER
templateDatabase?: string, //template database on which creates new database
port?: number, // default process.env.PGPORT
statement_timeout: number, // number of milliseconds before a query will time out default is no timeout
// number of milliseconds to wait before timing out when connecting a new client
// by default this is 0 which means no timeout
connectionTimeoutMillis?: number,
// number of milliseconds a client must sit idle in the pool and not be checked out
// before it is disconnected from the backend and discarded
// default is 10000 (10 seconds) - set to 0 to disable auto-disconnection of idle clients
idleTimeoutMillis?: number,
// maximum number of clients the pool should contain
// by default this is set to 10.
max?: number,
}
const queries: Array<namedQuery> = [
{
name: `Создание базы данных "db"`,
query: `CREATE DATABASE db`
}
]
app.use('/install/products',
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
if (req.isAuthenticated()) {
next();
} else {
res.send('Login failed');
}
},
InstallRouter(server, "productsDB", <IPostgreConnectionOptions>productsDBConnectionOptions, installProductsDBQueries)
);
app.use('/install/users',
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
if (req.isAuthenticated()) {
next();
} else {
res.send('Login failed');
}
},
InstallRouter(server, "usersDB", <IPostgreConnectionOptions>usersDBConnectionOptions, installusersDBQueries)
);