From 2641c0de8a4637201e45eb6f2dad913638e0fe27 Mon Sep 17 00:00:00 2001 From: RohovAlex <47148269+RohovAlex@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:14:47 +0300 Subject: [PATCH 1/4] added async to methods in api and await in return for async methods --- JavaScript/2-controller/main.js | 12 ++++++------ JavaScript/3-crud/db.js | 16 ++++++++-------- JavaScript/4-service/db.js | 16 ++++++++-------- JavaScript/4-service/user.js | 12 ++++++------ JavaScript/5-agnostic/db.js | 16 ++++++++-------- JavaScript/5-agnostic/user.js | 12 ++++++------ JavaScript/6-ws/db.js | 16 ++++++++-------- JavaScript/6-ws/user.js | 16 ++++++++-------- JavaScript/7-fs/api/user.js | 16 ++++++++-------- JavaScript/7-fs/db.js | 16 ++++++++-------- JavaScript/8-vm/api/country.js | 8 ++++---- JavaScript/8-vm/api/user.js | 16 ++++++++-------- JavaScript/8-vm/db.js | 16 ++++++++-------- JavaScript/9-logger/api/country.js | 8 ++++---- JavaScript/9-logger/api/user.js | 16 ++++++++-------- JavaScript/9-logger/db.js | 16 ++++++++-------- 16 files changed, 114 insertions(+), 114 deletions(-) diff --git a/JavaScript/2-controller/main.js b/JavaScript/2-controller/main.js index 86d4d78..06b083a 100644 --- a/JavaScript/2-controller/main.js +++ b/JavaScript/2-controller/main.js @@ -17,27 +17,27 @@ const pool = new pg.Pool({ const routing = { user: { - get(id) { + async get(id) { if (!id) return pool.query('SELECT id, login FROM users'); const sql = 'SELECT id, login FROM users WHERE id = $1'; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, async post({ login, password }) { const sql = 'INSERT INTO users (login, password) VALUES ($1, $2)'; const passwordHash = await hash(password); - return pool.query(sql, [login, passwordHash]); + return await pool.query(sql, [login, passwordHash]); }, async put(id, { login, password }) { const sql = 'UPDATE users SET login = $1, password = $2 WHERE id = $3'; const passwordHash = await hash(password); - return pool.query(sql, [login, passwordHash, id]); + return await pool.query(sql, [login, passwordHash, id]); }, - delete(id) { + async delete(id) { const sql = 'DELETE FROM users WHERE id = $1'; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }, }; diff --git a/JavaScript/3-crud/db.js b/JavaScript/3-crud/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/3-crud/db.js +++ b/JavaScript/3-crud/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); diff --git a/JavaScript/4-service/db.js b/JavaScript/4-service/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/4-service/db.js +++ b/JavaScript/4-service/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); diff --git a/JavaScript/4-service/user.js b/JavaScript/4-service/user.js index d5e77d4..6a4ac3e 100644 --- a/JavaScript/4-service/user.js +++ b/JavaScript/4-service/user.js @@ -6,21 +6,21 @@ const hash = require('./hash.js'); const users = db('users'); module.exports = { - read(id) { - return users.read(id, ['id', 'login']); + async read(id) { + return await users.read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await hash(password); - return users.create({ login, password: passwordHash }); + return await users.create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await hash(password); - return users.update(id, { login, password: passwordHash }); + return await users.update(id, { login, password: passwordHash }); }, - delete(id) { - return users.delete(id); + async delete(id) { + return await users.delete(id); }, }; diff --git a/JavaScript/5-agnostic/db.js b/JavaScript/5-agnostic/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/5-agnostic/db.js +++ b/JavaScript/5-agnostic/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); diff --git a/JavaScript/5-agnostic/user.js b/JavaScript/5-agnostic/user.js index d5e77d4..6a4ac3e 100644 --- a/JavaScript/5-agnostic/user.js +++ b/JavaScript/5-agnostic/user.js @@ -6,21 +6,21 @@ const hash = require('./hash.js'); const users = db('users'); module.exports = { - read(id) { - return users.read(id, ['id', 'login']); + async read(id) { + return await users.read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await hash(password); - return users.create({ login, password: passwordHash }); + return await users.create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await hash(password); - return users.update(id, { login, password: passwordHash }); + return await users.update(id, { login, password: passwordHash }); }, - delete(id) { - return users.delete(id); + async delete(id) { + return await users.delete(id); }, }; diff --git a/JavaScript/6-ws/db.js b/JavaScript/6-ws/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/6-ws/db.js +++ b/JavaScript/6-ws/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); diff --git a/JavaScript/6-ws/user.js b/JavaScript/6-ws/user.js index 812786c..557fd76 100644 --- a/JavaScript/6-ws/user.js +++ b/JavaScript/6-ws/user.js @@ -6,26 +6,26 @@ const hash = require('./hash.js'); const users = db('users'); module.exports = { - read(id) { - return users.read(id, ['id', 'login']); + async read(id) { + return await users.read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await hash(password); - return users.create({ login, password: passwordHash }); + return await users.create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await hash(password); - return users.update(id, { login, password: passwordHash }); + return await users.update(id, { login, password: passwordHash }); }, - delete(id) { - return users.delete(id); + async delete(id) { + return await users.delete(id); }, - find(mask) { + async find(mask) { const sql = 'SELECT login from users where login like $1'; - return users.query(sql, [mask]); + return await users.query(sql, [mask]); }, }; diff --git a/JavaScript/7-fs/api/user.js b/JavaScript/7-fs/api/user.js index 8078cc3..6e69499 100644 --- a/JavaScript/7-fs/api/user.js +++ b/JavaScript/7-fs/api/user.js @@ -6,26 +6,26 @@ const hash = require('../hash.js'); const users = db('users'); module.exports = { - read(id) { - return users.read(id, ['id', 'login']); + async read(id) { + return await users.read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await hash(password); - return users.create({ login, password: passwordHash }); + return await users.create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await hash(password); - return users.update(id, { login, password: passwordHash }); + return await users.update(id, { login, password: passwordHash }); }, - delete(id) { - return users.delete(id); + async delete(id) { + return await users.delete(id); }, - find(mask) { + async find(mask) { const sql = 'SELECT login from users where login like $1'; - return users.query(sql, [mask]); + return await users.query(sql, [mask]); }, }; diff --git a/JavaScript/7-fs/db.js b/JavaScript/7-fs/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/7-fs/db.js +++ b/JavaScript/7-fs/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); diff --git a/JavaScript/8-vm/api/country.js b/JavaScript/8-vm/api/country.js index d431ade..f1d91ca 100644 --- a/JavaScript/8-vm/api/country.js +++ b/JavaScript/8-vm/api/country.js @@ -1,13 +1,13 @@ const country = db('country'); ({ - read(id) { + async read(id) { console.log({ db }); - return country.read(id); + return await country.read(id); }, - find(mask) { + async find(mask) { const sql = 'SELECT * from country where name like $1'; - return country.query(sql, [mask]); + return await country.query(sql, [mask]); }, }); diff --git a/JavaScript/8-vm/api/user.js b/JavaScript/8-vm/api/user.js index cc7791e..1f69dca 100644 --- a/JavaScript/8-vm/api/user.js +++ b/JavaScript/8-vm/api/user.js @@ -1,24 +1,24 @@ ({ - read(id) { - return db('users').read(id, ['id', 'login']); + async read(id) { + return await db('users').read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await common.hash(password); - return db('users').create({ login, password: passwordHash }); + return await db('users').create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await common.hash(password); - return db('users').update(id, { login, password: passwordHash }); + return await db('users').update(id, { login, password: passwordHash }); }, - delete(id) { - return db('users').delete(id); + async delete(id) { + return await db('users').delete(id); }, - find(mask) { + async find(mask) { const sql = 'SELECT login from users where login like $1'; - return db('users').query(sql, [mask]); + return await db('users').query(sql, [mask]); }, }); diff --git a/JavaScript/8-vm/db.js b/JavaScript/8-vm/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/8-vm/db.js +++ b/JavaScript/8-vm/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); diff --git a/JavaScript/9-logger/api/country.js b/JavaScript/9-logger/api/country.js index d431ade..f1d91ca 100644 --- a/JavaScript/9-logger/api/country.js +++ b/JavaScript/9-logger/api/country.js @@ -1,13 +1,13 @@ const country = db('country'); ({ - read(id) { + async read(id) { console.log({ db }); - return country.read(id); + return await country.read(id); }, - find(mask) { + async find(mask) { const sql = 'SELECT * from country where name like $1'; - return country.query(sql, [mask]); + return await country.query(sql, [mask]); }, }); diff --git a/JavaScript/9-logger/api/user.js b/JavaScript/9-logger/api/user.js index cc7791e..1f69dca 100644 --- a/JavaScript/9-logger/api/user.js +++ b/JavaScript/9-logger/api/user.js @@ -1,24 +1,24 @@ ({ - read(id) { - return db('users').read(id, ['id', 'login']); + async read(id) { + return await db('users').read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await common.hash(password); - return db('users').create({ login, password: passwordHash }); + return await db('users').create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await common.hash(password); - return db('users').update(id, { login, password: passwordHash }); + return await db('users').update(id, { login, password: passwordHash }); }, - delete(id) { - return db('users').delete(id); + async delete(id) { + return await db('users').delete(id); }, - find(mask) { + async find(mask) { const sql = 'SELECT login from users where login like $1'; - return db('users').query(sql, [mask]); + return await db('users').query(sql, [mask]); }, }); diff --git a/JavaScript/9-logger/db.js b/JavaScript/9-logger/db.js index 1605d15..afc5d04 100644 --- a/JavaScript/9-logger/db.js +++ b/JavaScript/9-logger/db.js @@ -11,15 +11,15 @@ const pool = new pg.Pool({ }); module.exports = (table) => ({ - query(sql, args) { - return pool.query(sql, args); + async query(sql, args) { + return await pool.query(sql, args); }, - read(id, fields = ['*']) { + async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return await pool.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -34,7 +34,7 @@ module.exports = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await pool.query(sql, data); }, async update(id, { ...record }) { @@ -49,11 +49,11 @@ module.exports = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await pool.query(sql, data); }, - delete(id) { + async delete(id) { const sql = `DELETE FROM ${table} WHERE id = $1`; - return pool.query(sql, [id]); + return await pool.query(sql, [id]); }, }); From eb568901e46c3600829a77abdf6d27e74d85f5c3 Mon Sep 17 00:00:00 2001 From: RohovAlex <47148269+RohovAlex@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:03:13 +0300 Subject: [PATCH 2/4] changes in a-config example --- JavaScript/a-config/api/country.js | 4 ++-- JavaScript/a-config/api/user.js | 10 +++++----- JavaScript/a-config/db.js | 14 +++++++------- JavaScript/a-config/http.js | 2 +- JavaScript/a-config/ws.js | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/JavaScript/a-config/api/country.js b/JavaScript/a-config/api/country.js index f99ac87..f1d91ca 100644 --- a/JavaScript/a-config/api/country.js +++ b/JavaScript/a-config/api/country.js @@ -3,11 +3,11 @@ const country = db('country'); ({ async read(id) { console.log({ db }); - return country.read(id); + return await country.read(id); }, async find(mask) { const sql = 'SELECT * from country where name like $1'; - return country.query(sql, [mask]); + return await country.query(sql, [mask]); }, }); diff --git a/JavaScript/a-config/api/user.js b/JavaScript/a-config/api/user.js index 0faaeb6..1f69dca 100644 --- a/JavaScript/a-config/api/user.js +++ b/JavaScript/a-config/api/user.js @@ -1,24 +1,24 @@ ({ async read(id) { - return db('users').read(id, ['id', 'login']); + return await db('users').read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await common.hash(password); - return db('users').create({ login, password: passwordHash }); + return await db('users').create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await common.hash(password); - return db('users').update(id, { login, password: passwordHash }); + return await db('users').update(id, { login, password: passwordHash }); }, async delete(id) { - return db('users').delete(id); + return await db('users').delete(id); }, async find(mask) { const sql = 'SELECT login from users where login like $1'; - return db('users').query(sql, [mask]); + return await db('users').query(sql, [mask]); }, }); diff --git a/JavaScript/a-config/db.js b/JavaScript/a-config/db.js index 5e26018..8674f26 100644 --- a/JavaScript/a-config/db.js +++ b/JavaScript/a-config/db.js @@ -5,14 +5,14 @@ const pg = require('pg'); const crud = (pool) => (table) => ({ async query(sql, args) { const result = await pool.query(sql, args); - return result.rows; + return await result.rows; }, async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; - if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + if (!id) return await this.query(sql); + return await this.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -27,7 +27,7 @@ const crud = (pool) => (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await this.query(sql, data); }, async update(id, { ...record }) { @@ -42,12 +42,12 @@ const crud = (pool) => (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await this.query(sql, data); }, async delete(id) { - const sql = 'DELETE FROM ${table} WHERE id = $1'; - return pool.query(sql, [id]); + const sql = `DELETE FROM ${table} WHERE id = $1`; + return await this.query(sql, [id]); }, }); diff --git a/JavaScript/a-config/http.js b/JavaScript/a-config/http.js index 9f3f87f..52a57ce 100644 --- a/JavaScript/a-config/http.js +++ b/JavaScript/a-config/http.js @@ -24,7 +24,7 @@ module.exports = (routing, port) => { if (signature.includes('{')) args.push(await receiveArgs(req)); console.log(`${socket.remoteAddress} ${method} ${url}`); const result = await handler(...args); - res.end(JSON.stringify(result.rows)); + res.end(JSON.stringify(result)); }).listen(port); console.log(`API on port ${port}`); diff --git a/JavaScript/a-config/ws.js b/JavaScript/a-config/ws.js index ad9cd74..69c972b 100644 --- a/JavaScript/a-config/ws.js +++ b/JavaScript/a-config/ws.js @@ -26,7 +26,7 @@ module.exports = (routing, port) => { console.log(`${ip} ${name}.${method}(${parameters})`); try { const result = await handler(...args); - connection.send(JSON.stringify(result.rows), { binary: false }); + connection.send(JSON.stringify(result), { binary: false }); } catch (err) { console.error(err); connection.send('"Server error"', { binary: false }); From 1ac8749c912b32b1a1e031e3df35bc3f3c9687e1 Mon Sep 17 00:00:00 2001 From: RohovAlex <47148269+RohovAlex@users.noreply.github.com> Date: Fri, 4 Aug 2023 18:36:40 +0300 Subject: [PATCH 3/4] changes in b-transport example --- JavaScript/b-transport/api/country.js | 4 ++-- JavaScript/b-transport/api/talks.js | 2 +- JavaScript/b-transport/api/user.js | 10 +++++----- JavaScript/b-transport/db.js | 14 +++++++------- JavaScript/b-transport/transport/http.js | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/JavaScript/b-transport/api/country.js b/JavaScript/b-transport/api/country.js index f99ac87..f1d91ca 100644 --- a/JavaScript/b-transport/api/country.js +++ b/JavaScript/b-transport/api/country.js @@ -3,11 +3,11 @@ const country = db('country'); ({ async read(id) { console.log({ db }); - return country.read(id); + return await country.read(id); }, async find(mask) { const sql = 'SELECT * from country where name like $1'; - return country.query(sql, [mask]); + return await country.query(sql, [mask]); }, }); diff --git a/JavaScript/b-transport/api/talks.js b/JavaScript/b-transport/api/talks.js index d910c12..9a994ff 100644 --- a/JavaScript/b-transport/api/talks.js +++ b/JavaScript/b-transport/api/talks.js @@ -1,6 +1,6 @@ ({ async say(message) { console.log({ message }); - return { status: 'ok' }; + return await { status: 'ok' }; }, }); diff --git a/JavaScript/b-transport/api/user.js b/JavaScript/b-transport/api/user.js index 0faaeb6..1f69dca 100644 --- a/JavaScript/b-transport/api/user.js +++ b/JavaScript/b-transport/api/user.js @@ -1,24 +1,24 @@ ({ async read(id) { - return db('users').read(id, ['id', 'login']); + return await db('users').read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await common.hash(password); - return db('users').create({ login, password: passwordHash }); + return await db('users').create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await common.hash(password); - return db('users').update(id, { login, password: passwordHash }); + return await db('users').update(id, { login, password: passwordHash }); }, async delete(id) { - return db('users').delete(id); + return await db('users').delete(id); }, async find(mask) { const sql = 'SELECT login from users where login like $1'; - return db('users').query(sql, [mask]); + return await db('users').query(sql, [mask]); }, }); diff --git a/JavaScript/b-transport/db.js b/JavaScript/b-transport/db.js index 5e26018..8674f26 100644 --- a/JavaScript/b-transport/db.js +++ b/JavaScript/b-transport/db.js @@ -5,14 +5,14 @@ const pg = require('pg'); const crud = (pool) => (table) => ({ async query(sql, args) { const result = await pool.query(sql, args); - return result.rows; + return await result.rows; }, async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; - if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + if (!id) return await this.query(sql); + return await this.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -27,7 +27,7 @@ const crud = (pool) => (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await this.query(sql, data); }, async update(id, { ...record }) { @@ -42,12 +42,12 @@ const crud = (pool) => (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await this.query(sql, data); }, async delete(id) { - const sql = 'DELETE FROM ${table} WHERE id = $1'; - return pool.query(sql, [id]); + const sql = `DELETE FROM ${table} WHERE id = $1`; + return await this.query(sql, [id]); }, }); diff --git a/JavaScript/b-transport/transport/http.js b/JavaScript/b-transport/transport/http.js index e12b373..6367dd3 100644 --- a/JavaScript/b-transport/transport/http.js +++ b/JavaScript/b-transport/transport/http.js @@ -32,7 +32,7 @@ module.exports = (routing, port, console) => { if (!handler) return void res.end('"Not found"'); const { args } = await receiveArgs(req); console.log(`${socket.remoteAddress} ${method} ${url}`); - const result = await handler(args); + const result = await handler(...args); res.end(JSON.stringify(result)); }).listen(port); From 9dce5faf0379388e9ae42bd4ac57a8f9d0f8ba33 Mon Sep 17 00:00:00 2001 From: RohovAlex <47148269+RohovAlex@users.noreply.github.com> Date: Fri, 4 Aug 2023 20:49:25 +0300 Subject: [PATCH 4/4] changes in c-commonjs example --- JavaScript/c-commonjs/api/country.js | 4 ++-- JavaScript/c-commonjs/api/talks.js | 2 +- JavaScript/c-commonjs/api/user.js | 10 +++++----- JavaScript/c-commonjs/db.js | 14 +++++++------- JavaScript/c-commonjs/transport/http.js | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/JavaScript/c-commonjs/api/country.js b/JavaScript/c-commonjs/api/country.js index 4afc641..bfd3eff 100644 --- a/JavaScript/c-commonjs/api/country.js +++ b/JavaScript/c-commonjs/api/country.js @@ -6,11 +6,11 @@ const country = db.crud('country'); module.exports = { async read(id) { console.log({ db }); - return country.read(id); + return await country.read(id); }, async find(mask) { const sql = 'SELECT * from country where name like $1'; - return country.query(sql, [mask]); + return await country.query(sql, [mask]); }, }; diff --git a/JavaScript/c-commonjs/api/talks.js b/JavaScript/c-commonjs/api/talks.js index 04378d2..4081d8e 100644 --- a/JavaScript/c-commonjs/api/talks.js +++ b/JavaScript/c-commonjs/api/talks.js @@ -3,6 +3,6 @@ module.exports = { async say(message) { console.log({ message }); - return { status: 'ok' }; + return await { status: 'ok' }; }, }; diff --git a/JavaScript/c-commonjs/api/user.js b/JavaScript/c-commonjs/api/user.js index b2e65b7..57843c4 100644 --- a/JavaScript/c-commonjs/api/user.js +++ b/JavaScript/c-commonjs/api/user.js @@ -6,25 +6,25 @@ const users = db.crud('users'); module.exports = { async read(id) { - return users.read(id, ['id', 'login']); + return await users.read(id, ['id', 'login']); }, async create({ login, password }) { const passwordHash = await common.hash(password); - return users.create({ login, password: passwordHash }); + return await users.create({ login, password: passwordHash }); }, async update(id, { login, password }) { const passwordHash = await common.hash(password); - return users.update(id, { login, password: passwordHash }); + return await users.update(id, { login, password: passwordHash }); }, async delete(id) { - return users.delete(id); + return await users.delete(id); }, async find(mask) { const sql = 'SELECT login from users where login like $1'; - return users.query(sql, [mask]); + return await users.query(sql, [mask]); }, }; diff --git a/JavaScript/c-commonjs/db.js b/JavaScript/c-commonjs/db.js index 45a4a52..108a125 100644 --- a/JavaScript/c-commonjs/db.js +++ b/JavaScript/c-commonjs/db.js @@ -11,14 +11,14 @@ const init = (options) => { const crud = (table) => ({ async query(sql, args) { const result = await pool.query(sql, args); - return result.rows; + return await result.rows; }, async read(id, fields = ['*']) { const names = fields.join(', '); const sql = `SELECT ${names} FROM ${table}`; - if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + if (!id) return this.query(sql); + return await this.query(`${sql} WHERE id = $1`, [id]); }, async create({ ...record }) { @@ -33,7 +33,7 @@ const crud = (table) => ({ const fields = '"' + keys.join('", "') + '"'; const params = nums.join(', '); const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; - return pool.query(sql, data); + return await this.query(sql, data); }, async update(id, { ...record }) { @@ -48,12 +48,12 @@ const crud = (table) => ({ const delta = updates.join(', '); const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; data.push(id); - return pool.query(sql, data); + return await this.query(sql, data); }, async delete(id) { - const sql = 'DELETE FROM ${table} WHERE id = $1'; - return pool.query(sql, [id]); + const sql = `DELETE FROM ${table} WHERE id = $1`; + return await this.query(sql, [id]); }, }); diff --git a/JavaScript/c-commonjs/transport/http.js b/JavaScript/c-commonjs/transport/http.js index e12b373..6367dd3 100644 --- a/JavaScript/c-commonjs/transport/http.js +++ b/JavaScript/c-commonjs/transport/http.js @@ -32,7 +32,7 @@ module.exports = (routing, port, console) => { if (!handler) return void res.end('"Not found"'); const { args } = await receiveArgs(req); console.log(`${socket.remoteAddress} ${method} ${url}`); - const result = await handler(args); + const result = await handler(...args); res.end(JSON.stringify(result)); }).listen(port);