Skip to content

Commit

Permalink
package updated
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 18, 2023
1 parent ed2b3d3 commit f1f653c
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 44 deletions.
4 changes: 2 additions & 2 deletions JavaScript/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
10 changes: 5 additions & 5 deletions JavaScript/db/diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions JavaScript/db/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
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
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) {
Expand Down
7 changes: 3 additions & 4 deletions TypeScript/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
10 changes: 5 additions & 5 deletions TypeScript/db/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions TypeScript/db/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,10 +8,6 @@ type ExtendedProtocol = IDatabase<IExtensions> & IExtensions;

// pg-promise initialization options:
const initOptions: IInitOptions<IExtensions> = {

// 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) {
Expand Down
4 changes: 2 additions & 2 deletions TypeScript/db/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions TypeScript/db/repos/products.ts
Original file line number Diff line number Diff line change
@@ -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';

/*
Expand Down Expand Up @@ -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<Product> {
add(values: { userId: number, name: string }): Promise<IProduct> {
return this.db.one(sql.add, {
userId: +values.userId,
productName: values.name
Expand All @@ -58,15 +58,15 @@ export class ProductsRepository {
}

// Tries to find a user product from user id + product name;
find(values: { userId: number, name: string }): Promise<Product | null> {
find(values: { userId: number, name: string }): Promise<IProduct | null> {
return this.db.oneOrNone(sql.find, {
userId: +values.userId,
productName: values.name
});
}

// Returns all product records;
all(): Promise<Product[]> {
all(): Promise<IProduct[]> {
return this.db.any('SELECT * FROM products');
}

Expand Down
10 changes: 5 additions & 5 deletions TypeScript/db/repos/users.ts
Original file line number Diff line number Diff line change
@@ -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';

/*
Expand Down Expand Up @@ -49,7 +49,7 @@ export class UsersRepository {
}

// Adds a new user, and returns the new object;
add(name: string): Promise<User> {
add(name: string): Promise<IUser> {
return this.db.one(sql.add, name);
}

Expand All @@ -59,17 +59,17 @@ export class UsersRepository {
}

// Tries to find a user from id;
findById(id: number): Promise<User | null> {
findById(id: number): Promise<IUser | null> {
return this.db.oneOrNone('SELECT * FROM users WHERE id = $1', +id);
}

// Tries to find a user from name;
findByName(name: string): Promise<User | null> {
findByName(name: string): Promise<IUser | null> {
return this.db.oneOrNone('SELECT * FROM users WHERE name = $1', name);
}

// Returns all user records;
all(): Promise<User[]> {
all(): Promise<IUser[]> {
return this.db.any('SELECT * FROM users');
}

Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}

0 comments on commit f1f653c

Please sign in to comment.