Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix db methods #29

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions JavaScript/a-config/api/country.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
});
10 changes: 5 additions & 5 deletions JavaScript/a-config/api/user.js
Original file line number Diff line number Diff line change
@@ -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]);
},
});
14 changes: 7 additions & 7 deletions JavaScript/a-config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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 }) {
Expand All @@ -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]);
},
});

Expand Down
2 changes: 1 addition & 1 deletion JavaScript/a-config/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/a-config/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 2 additions & 2 deletions JavaScript/b-transport/api/country.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
});
2 changes: 1 addition & 1 deletion JavaScript/b-transport/api/talks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
({
async say(message) {
console.log({ message });
return { status: 'ok' };
return await { status: 'ok' };
},
});
10 changes: 5 additions & 5 deletions JavaScript/b-transport/api/user.js
Original file line number Diff line number Diff line change
@@ -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]);
},
});
14 changes: 7 additions & 7 deletions JavaScript/b-transport/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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 }) {
Expand All @@ -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]);
},
});

Expand Down
2 changes: 1 addition & 1 deletion JavaScript/b-transport/transport/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions JavaScript/c-commonjs/api/country.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
};
2 changes: 1 addition & 1 deletion JavaScript/c-commonjs/api/talks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module.exports = {
async say(message) {
console.log({ message });
return { status: 'ok' };
return await { status: 'ok' };
},
};
10 changes: 5 additions & 5 deletions JavaScript/c-commonjs/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
};
14 changes: 7 additions & 7 deletions JavaScript/c-commonjs/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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 }) {
Expand All @@ -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]);
},
});

Expand Down
2 changes: 1 addition & 1 deletion JavaScript/c-commonjs/transport/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down