Skip to content

Commit

Permalink
update all
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 16, 2022
1 parent 8a076eb commit 963738c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 55 deletions.
6 changes: 3 additions & 3 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 v8.0.0 or later.
This implementation uses ES2017 syntax, and requires Node.JS v10.0.0 or later.

### Prerequisites

* Node.js v8.0.0 or later
* Node.js v10.0.0 or later

### Installation

Expand All @@ -15,6 +15,6 @@ This implementation uses ES2017 syntax, and requires Node.JS v8.0.0 or later.
### Starting

* Navigate into folder `JavaScript`, and run the HTTP service with `node index.js` command.
* Proceed with the [steps on the main page].
* Proceed with [the steps on the main page].

[steps on the main page]:https://github.com/vitaly-t/pg-promise-demo
16 changes: 8 additions & 8 deletions JavaScript/db/repos/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,49 @@ class ProductsRepository {
}

// Creates the table;
async create() {
create() {
return this.db.none(sql.create);
}

// Drops the table;
async drop() {
drop() {
return this.db.none(sql.drop);
}

// Removes all records from the table;
async empty() {
empty() {
return this.db.none(sql.empty);
}

// Adds a new record and returns the full object;
// It is also an example of mapping HTTP requests into query parameters;
async add(values) {
add(values) {
return this.db.one(sql.add, {
userId: +values.userId,
productName: values.name
});
}

// Tries to delete a product by id, and returns the number of records deleted;
async remove(id) {
remove(id) {
return this.db.result('DELETE FROM products WHERE id = $1', +id, r => r.rowCount);
}

// Tries to find a user product from user id + product name;
async find(values) {
find(values) {
return this.db.oneOrNone(sql.find, {
userId: +values.userId,
productName: values.name
});
}

// Returns all product records;
async all() {
all() {
return this.db.any('SELECT * FROM products');
}

// Returns the total number of products;
async total() {
total() {
return this.db.one('SELECT count(*) FROM products', [], a => +a.count);
}
}
Expand Down
20 changes: 10 additions & 10 deletions JavaScript/db/repos/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,52 @@ class UsersRepository {
}

// Creates the table;
async create() {
create() {
return this.db.none(sql.create);
}

// Initializes the table with some user records, and return their id-s;
async init() {
init() {
return this.db.map(sql.init, [], row => row.id);
}

// Drops the table;
async drop() {
drop() {
return this.db.none(sql.drop);
}

// Removes all records from the table;
async empty() {
empty() {
return this.db.none(sql.empty);
}

// Adds a new user, and returns the new object;
async add(name) {
add(name) {
return this.db.one(sql.add, name);
}

// Tries to delete a user by id, and returns the number of records deleted;
async remove(id) {
remove(id) {
return this.db.result('DELETE FROM users WHERE id = $1', +id, r => r.rowCount);
}

// Tries to find a user from id;
async findById(id) {
findById(id) {
return this.db.oneOrNone('SELECT * FROM users WHERE id = $1', +id);
}

// Tries to find a user from name;
async findByName(name) {
findByName(name) {
return this.db.oneOrNone('SELECT * FROM users WHERE name = $1', name);
}

// Returns all user records;
async all() {
all() {
return this.db.any('SELECT * FROM users');
}

// Returns the total number of users;
async total() {
total() {
return this.db.one('SELECT count(*) FROM users', [], a => +a.count);
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The demo focuses on the following:
The demo includes two separate implementations, with identical functionality:

* [ES7 JavaScript implementation](https://github.com/vitaly-t/pg-promise-demo/tree/master/JavaScript)
* [TypeScript 3.x implementation](https://github.com/vitaly-t/pg-promise-demo/tree/master/TypeScript)
* [TypeScript 4.x implementation](https://github.com/vitaly-t/pg-promise-demo/tree/master/TypeScript)

Each uses a basic HTTP service to let you quickly test db calls in a browser. Do not however reuse
any of the HTTP-service code, it is over-simplified, for the test, not for you to copy. The demo focus
Expand Down
8 changes: 4 additions & 4 deletions TypeScript/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## TypeScript 3.x implementation of pg-promise-demo
## TypeScript 4.x implementation of pg-promise-demo

The project is configured to transpile into ES2017, and requires Node.js v8.0.0 or later.
The project is configured to transpile into ES2020, and requires Node.js v10.0.0 or later.

### Prerequisites

* Node.js v8.0.0 or later
* TypeScript 3.x, installed globally via command `npm install typescript -g`
* Node.js v10.0.0 or later
* TypeScript 4.x, installed globally via command `npm install typescript -g`

### Installation

Expand Down
16 changes: 8 additions & 8 deletions TypeScript/db/repos/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,49 @@ export class ProductsRepository {
}

// Creates the table;
async create(): Promise<null> {
create(): Promise<null> {
return this.db.none(sql.create);
}

// Drops the table;
async drop(): Promise<null> {
drop(): Promise<null> {
return this.db.none(sql.drop);
}

// Removes all records from the table;
async empty(): Promise<null> {
empty(): Promise<null> {
return this.db.none(sql.empty);
}

// Adds a new record and returns the full object;
// It is also an example of mapping HTTP requests into query parameters;
async add(values: { userId: number, name: string }): Promise<Product> {
add(values: { userId: number, name: string }): Promise<Product> {
return this.db.one(sql.add, {
userId: +values.userId,
productName: values.name
});
}

// Tries to delete a product by id, and returns the number of records deleted;
async remove(id: number): Promise<number> {
remove(id: number): Promise<number> {
return this.db.result('DELETE FROM products WHERE id = $1', +id, (r: IResult) => r.rowCount);
}

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

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

// Returns the total number of products;
async total(): Promise<number> {
total(): Promise<number> {
return this.db.one('SELECT count(*) FROM products', [], (data: { count: string }) => +data.count);
}
}
20 changes: 10 additions & 10 deletions TypeScript/db/repos/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,52 @@ export class UsersRepository {
}

// Creates the table;
async create(): Promise<null> {
create(): Promise<null> {
return this.db.none(sql.create);
}

// Initializes the table with some user records, and return their id-s;
async init(): Promise<number[]> {
init(): Promise<number[]> {
return this.db.map(sql.init, [], (row: { id: number }) => row.id);
}

// Drops the table;
async drop(): Promise<null> {
drop(): Promise<null> {
return this.db.none(sql.drop);
}

// Removes all records from the table;
async empty(): Promise<null> {
empty(): Promise<null> {
return this.db.none(sql.empty);
}

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

// Tries to delete a user by id, and returns the number of records deleted;
async remove(id: number): Promise<number> {
remove(id: number): Promise<number> {
return this.db.result('DELETE FROM users WHERE id = $1', +id, (r: IResult) => r.rowCount);
}

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

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

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

// Returns the total number of users;
async total(): Promise<number> {
total(): Promise<number> {
return this.db.one('SELECT count(*) FROM users', [], (a: { count: string }) => +a.count);
}
}
2 changes: 1 addition & 1 deletion TypeScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function GET(url: string, handler: (req: any) => any) {
success: true,
data
});
} catch (error) {
} catch (error: any) {
res.json({
success: false,
error: error.message || error
Expand Down
2 changes: 1 addition & 1 deletion TypeScript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"target": "es2020",
"strict": true,
"resolveJsonModule": true
},
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise-demo",
"version": "1.5.2",
"version": "1.5.3",
"description": "Advanced demo of using pg-promise.",
"main": "JavaScript/index.js",
"homepage": "https://github.com/vitaly-t/pg-promise-demo",
Expand All @@ -22,18 +22,18 @@
},
"license": "MIT",
"engines": {
"node": ">=8.0.0"
"node": ">=10.0.0"
},
"dependencies": {
"bluebird": "3.7.2",
"express": "4.17.1",
"pg-monitor": "1.4.1",
"pg-promise": "10.11.0"
"express": "4.18.2",
"pg-monitor": "1.5.0",
"pg-promise": "10.14.1"
},
"devDependencies": {
"@types/bluebird": "3.5.36",
"@types/express": "4.17.13",
"@types/node": "16.10.2",
"typescript": "4.4.3"
"@types/bluebird": "3.5.37",
"@types/express": "4.17.14",
"@types/node": "18.11.9",
"typescript": "4.9.3"
}
}

0 comments on commit 963738c

Please sign in to comment.