Skip to content

Commit

Permalink
changes in c-commonjs example
Browse files Browse the repository at this point in the history
  • Loading branch information
RohovAlex committed Aug 4, 2023
1 parent 1ac8749 commit 9dce5fa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
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

0 comments on commit 9dce5fa

Please sign in to comment.