From f1f653ce9e755e63484107fc6b2a4f47625e5480 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 18 Mar 2023 12:51:20 +0000 Subject: [PATCH] package updated --- JavaScript/README.md | 4 ++-- JavaScript/db/diagnostics.js | 10 +++++----- JavaScript/db/index.js | 5 ----- TypeScript/README.md | 7 +++---- TypeScript/db/diagnostics.ts | 10 +++++----- TypeScript/db/index.ts | 5 ----- TypeScript/db/models.ts | 4 ++-- TypeScript/db/repos/products.ts | 8 ++++---- TypeScript/db/repos/users.ts | 10 +++++----- package.json | 12 +++++------- 10 files changed, 31 insertions(+), 44 deletions(-) diff --git a/JavaScript/README.md b/JavaScript/README.md index 3ee2148..08e05e9 100644 --- a/JavaScript/README.md +++ b/JavaScript/README.md @@ -1,10 +1,10 @@ ## JavaScript implementation of pg-promise-demo -This implementation uses ES2017 syntax, and requires Node.JS v10.0.0 or later. +This implementation uses ES2020 syntax, and requires Node.JS v14.0.0 or later. ### Prerequisites -* Node.js v10.0.0 or later +* Node.js v14.0.0 or later ### Installation diff --git a/JavaScript/db/diagnostics.js b/JavaScript/db/diagnostics.js index 04aa54a..b7deaba 100644 --- a/JavaScript/db/diagnostics.js +++ b/JavaScript/db/diagnostics.js @@ -6,8 +6,8 @@ // which may be a little better performing, but lacks all the nice formatting // provided by pg-monitor. -const os = require('os'); -const fs = require('fs'); +const {EOL} = require('os'); +const {appendFileSync} = require('fs'); const monitor = require('pg-monitor'); monitor.setTheme('matrix'); // changing the default theme; @@ -32,13 +32,13 @@ monitor.setLog((msg, info) => { // errors only, or else the file will grow out of proportion in no time. if (info.event === 'error') { - let logText = os.EOL + msg; // line break + next error message; + let logText = EOL + msg; // line break + next error message; if (info.time) { // If it is a new error being reported, // and not an additional error line; - logText = os.EOL + logText; // add another line break in front; + logText = EOL + logText; // add another line break in front; } - fs.appendFileSync(logFile, logText); // add error handling as required; + appendFileSync(logFile, logText); // add error handling as required; } // We absolutely must not let the monitor write anything into the console diff --git a/JavaScript/db/index.js b/JavaScript/db/index.js index a75a89a..a416446 100644 --- a/JavaScript/db/index.js +++ b/JavaScript/db/index.js @@ -1,4 +1,3 @@ -const promise = require('bluebird'); // best promise library today const pgPromise = require('pg-promise'); // pg-promise core library const dbConfig = require('../../db-config.json'); // db connection details const {Diagnostics} = require('./diagnostics'); // optional diagnostics @@ -6,10 +5,6 @@ const {Users, Products} = require('./repos'); // pg-promise initialization options: const initOptions = { - - // Use a custom promise library, instead of the default ES6 Promise: - promiseLib: promise, - // Extending the database protocol with our custom repositories; // API: http://vitaly-t.github.io/pg-promise/global.html#event:extend extend(obj, dc) { diff --git a/TypeScript/README.md b/TypeScript/README.md index a335b05..1c4cadc 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -1,11 +1,10 @@ -## TypeScript 4.x implementation of pg-promise-demo +## TypeScript implementation of pg-promise-demo -The project is configured to transpile into ES2020, and requires Node.js v10.0.0 or later. +The project is configured to transpile into ES2020, and requires Node.js v14.0.0 or later. ### Prerequisites -* Node.js v10.0.0 or later -* TypeScript 4.x, installed globally via command `npm install typescript -g` +* Node.js v14.0.0 or later ### Installation diff --git a/TypeScript/db/diagnostics.ts b/TypeScript/db/diagnostics.ts index f74dbc4..0f3a5cd 100644 --- a/TypeScript/db/diagnostics.ts +++ b/TypeScript/db/diagnostics.ts @@ -6,8 +6,8 @@ // which may be a little better performing, but lacks all the nice formatting // provided by pg-monitor. -import os = require('os'); -import fs = require('fs'); +import {EOL} from 'os'; +import {appendFileSync} from 'fs'; import * as pgMonitor from 'pg-monitor'; import {IInitOptions} from 'pg-promise'; @@ -33,13 +33,13 @@ pgMonitor.setLog((msg, info) => { // errors only, or else the file will grow out of proportion in no time. if (info.event === 'error') { - let logText = os.EOL + msg; // line break + next error message; + let logText = EOL + msg; // line break + next error message; if (info.time) { // If it is a new error being reported, // and not an additional error line; - logText = os.EOL + logText; // add another line break in front; + logText = EOL + logText; // add another line break in front; } - fs.appendFileSync(logFile, logText); // add error handling as required; + appendFileSync(logFile, logText); // add error handling as required; } // We absolutely must not let the monitor write anything into the console diff --git a/TypeScript/db/index.ts b/TypeScript/db/index.ts index a150339..202d3a3 100644 --- a/TypeScript/db/index.ts +++ b/TypeScript/db/index.ts @@ -1,4 +1,3 @@ -import * as promise from 'bluebird'; // best promise library today import * as dbConfig from '../../db-config.json'; // db connection details import * as pgPromise from 'pg-promise'; // pg-promise core library import {Diagnostics} from './diagnostics'; // optional diagnostics @@ -9,10 +8,6 @@ type ExtendedProtocol = IDatabase & IExtensions; // pg-promise initialization options: const initOptions: IInitOptions = { - - // Using a custom promise library, instead of the default ES6 Promise: - promiseLib: promise, - // Extending the database protocol with our custom repositories; // API: http://vitaly-t.github.io/pg-promise/global.html#event:extend extend(obj: ExtendedProtocol, dc: any) { diff --git a/TypeScript/db/models.ts b/TypeScript/db/models.ts index 71192b8..ae7a54b 100644 --- a/TypeScript/db/models.ts +++ b/TypeScript/db/models.ts @@ -5,12 +5,12 @@ For example, schemats: https://github.com/sweetiq/schemats */ -export interface User { +export interface IUser { id: number; name: string; } -export interface Product { +export interface IProduct { id: number; user_id: number; name: string; diff --git a/TypeScript/db/repos/products.ts b/TypeScript/db/repos/products.ts index 0ebb20f..a559026 100644 --- a/TypeScript/db/repos/products.ts +++ b/TypeScript/db/repos/products.ts @@ -1,6 +1,6 @@ import {IDatabase, IMain} from 'pg-promise'; import {IResult} from 'pg-promise/typescript/pg-subset'; -import {Product} from '../models'; +import {IProduct} from '../models'; import {products as sql} from '../sql'; /* @@ -45,7 +45,7 @@ export class ProductsRepository { // Adds a new record and returns the full object; // It is also an example of mapping HTTP requests into query parameters; - add(values: { userId: number, name: string }): Promise { + add(values: { userId: number, name: string }): Promise { return this.db.one(sql.add, { userId: +values.userId, productName: values.name @@ -58,7 +58,7 @@ export class ProductsRepository { } // Tries to find a user product from user id + product name; - find(values: { userId: number, name: string }): Promise { + find(values: { userId: number, name: string }): Promise { return this.db.oneOrNone(sql.find, { userId: +values.userId, productName: values.name @@ -66,7 +66,7 @@ export class ProductsRepository { } // Returns all product records; - all(): Promise { + all(): Promise { return this.db.any('SELECT * FROM products'); } diff --git a/TypeScript/db/repos/users.ts b/TypeScript/db/repos/users.ts index ced9627..c2b827e 100644 --- a/TypeScript/db/repos/users.ts +++ b/TypeScript/db/repos/users.ts @@ -1,6 +1,6 @@ import {IDatabase, IMain} from 'pg-promise'; import {IResult} from 'pg-promise/typescript/pg-subset'; -import {User} from '../models'; +import {IUser} from '../models'; import {users as sql} from '../sql'; /* @@ -49,7 +49,7 @@ export class UsersRepository { } // Adds a new user, and returns the new object; - add(name: string): Promise { + add(name: string): Promise { return this.db.one(sql.add, name); } @@ -59,17 +59,17 @@ export class UsersRepository { } // Tries to find a user from id; - findById(id: number): Promise { + findById(id: number): Promise { return this.db.oneOrNone('SELECT * FROM users WHERE id = $1', +id); } // Tries to find a user from name; - findByName(name: string): Promise { + findByName(name: string): Promise { return this.db.oneOrNone('SELECT * FROM users WHERE name = $1', name); } // Returns all user records; - all(): Promise { + all(): Promise { return this.db.any('SELECT * FROM users'); } diff --git a/package.json b/package.json index 21e2e85..d809065 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-promise-demo", - "version": "2.0.1", + "version": "3.0.0", "description": "Advanced demo of using pg-promise.", "main": "JavaScript/index.js", "homepage": "https://github.com/vitaly-t/pg-promise-demo", @@ -25,15 +25,13 @@ "node": ">=14" }, "dependencies": { - "bluebird": "3.7.2", "express": "4.18.2", "pg-monitor": "2.0.0", - "pg-promise": "11.0.2" + "pg-promise": "11.4.3" }, "devDependencies": { - "@types/bluebird": "3.5.38", - "@types/express": "4.17.15", - "@types/node": "18.11.18", - "typescript": "4.9.4" + "@types/express": "4.17.17", + "@types/node": "18.15.3", + "typescript": "5.0.2" } }