-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0af13d
commit 850c07f
Showing
19 changed files
with
782 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
common: Object.freeze({hash: require('./hash')}), | ||
db: Object.freeze(require('./db')), | ||
console: Object.freeze(require('./logger.di.container')), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
db('city'); | ||
'use strict'; | ||
const {db} = require('../api.di.container'); | ||
|
||
module.exports = db('city'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
'use strict'; | ||
const { db } = require('../api.di.container'); | ||
const { console } = require('../api.di.container'); | ||
|
||
const country = db('country'); | ||
|
||
({ | ||
async read(id) { | ||
console.log({ db }); | ||
return await country.read(id); | ||
}, | ||
module.exports = { | ||
async read(id) { | ||
console.log({ db }); | ||
return await country.read(id); | ||
}, | ||
|
||
async find(mask) { | ||
const sql = 'SELECT * from country where name like $1'; | ||
return await country.query(sql, [mask]); | ||
}, | ||
}); | ||
async find(mask) { | ||
const sql = 'SELECT * from country where name like $1'; | ||
return await country.query(sql, [mask]); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
({ | ||
async read(id) { | ||
return await db('users').read(id, ['id', 'login']); | ||
}, | ||
'use strict'; | ||
const {db, common} = require('../api.di.container'); | ||
|
||
async create({ login, password }) { | ||
const passwordHash = await common.hash(password); | ||
return await db('users').create({ login, password: passwordHash }); | ||
}, | ||
module.exports = { | ||
async read(id) { | ||
return await db('users').read(id, ['id', 'login']); | ||
}, | ||
|
||
async update(id, { login, password }) { | ||
const passwordHash = await common.hash(password); | ||
return await db('users').update(id, { login, password: passwordHash }); | ||
}, | ||
async create({login, password}) { | ||
const passwordHash = await common.hash(password); | ||
return await db('users').create({login, password: passwordHash}); | ||
}, | ||
|
||
async delete(id) { | ||
return await db('users').delete(id); | ||
}, | ||
async update(id, {login, password}) { | ||
const passwordHash = await common.hash(password); | ||
return await db('users').update(id, {login, password: passwordHash}); | ||
}, | ||
|
||
async find(mask) { | ||
const sql = 'SELECT login from users where login like $1'; | ||
return await db('users').query(sql, [mask]); | ||
}, | ||
}); | ||
async delete(id) { | ||
return await db('users').delete(id); | ||
}, | ||
|
||
async find(mask) { | ||
const sql = 'SELECT login from users where login like $1'; | ||
return await db('users').query(sql, [mask]); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
const receiveArgs = async (req) => { | ||
const buffers = []; | ||
for await (const chunk of req) buffers.push(chunk); | ||
const data = Buffer.concat(buffers).toString(); | ||
return JSON.parse(data); | ||
}; | ||
|
||
module.exports = receiveArgs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = { | ||
apiServer: { | ||
/** | ||
* @type {string} 'http' | 'ws' | ||
*/ | ||
transport: 'http', | ||
port: 3000, | ||
}, | ||
staticServer: { | ||
port: 8000, | ||
}, | ||
crypto: { | ||
saltLenByte: 16, | ||
scryptLenByte: 64, | ||
}, | ||
database: { | ||
host: 'localhost', | ||
port: 5432, | ||
database: 'metatech', | ||
user: 'admin', | ||
password: 'admin', | ||
}, | ||
/** | ||
* @type {string} 'pino' | 'meta' | ||
*/ | ||
logger: 'pino' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,60 @@ | ||
'use strict'; | ||
|
||
const pg = require('pg'); | ||
const config = require('./config'); | ||
|
||
const pool = new pg.Pool({ | ||
host: '127.0.0.1', | ||
port: 5432, | ||
database: 'example', | ||
user: 'marcus', | ||
password: 'marcus', | ||
host: config.database.host, | ||
port: config.database.port, | ||
database: config.database.database, | ||
user: config.database.user, | ||
password: config.database.password, | ||
}); | ||
|
||
module.exports = (table) => ({ | ||
async query(sql, args) { | ||
return await pool.query(sql, args); | ||
}, | ||
async query(sql, args) { | ||
return await pool.query(sql, args); | ||
}, | ||
|
||
async read(id, fields = ['*']) { | ||
const names = fields.join(', '); | ||
const sql = `SELECT ${names} FROM ${table}`; | ||
if (!id) return pool.query(sql); | ||
return await pool.query(`${sql} WHERE id = $1`, [id]); | ||
}, | ||
async read(id, fields = ['*']) { | ||
const names = fields.join(', '); | ||
const sql = `SELECT ${names} FROM ${table}`; | ||
if (!id) return pool.query(sql); | ||
return await pool.query(`${sql} WHERE id = $1`, [id]); | ||
}, | ||
|
||
async create({ ...record }) { | ||
const keys = Object.keys(record); | ||
const nums = new Array(keys.length); | ||
const data = new Array(keys.length); | ||
let i = 0; | ||
for (const key of keys) { | ||
data[i] = record[key]; | ||
nums[i] = `$${++i}`; | ||
} | ||
const fields = '"' + keys.join('", "') + '"'; | ||
const params = nums.join(', '); | ||
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`; | ||
return await pool.query(sql, data); | ||
}, | ||
async create({ ...record }) { | ||
const keys = Object.keys(record); | ||
const nums = new Array(keys.length); | ||
const data = new Array(keys.length); | ||
let i = 0; | ||
for (const key of keys) { | ||
data[i] = record[key]; | ||
nums[i] = `$${++i}`; | ||
} | ||
const fields = '"' + keys.join('", "') + '"'; | ||
const params = nums.join(', '); | ||
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params}) RETURNING id`; | ||
return await pool.query(sql, data); | ||
}, | ||
|
||
async update(id, { ...record }) { | ||
const keys = Object.keys(record); | ||
const updates = new Array(keys.length); | ||
const data = new Array(keys.length); | ||
let i = 0; | ||
for (const key of keys) { | ||
data[i] = record[key]; | ||
updates[i] = `${key} = $${++i}`; | ||
} | ||
const delta = updates.join(', '); | ||
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; | ||
data.push(id); | ||
return await pool.query(sql, data); | ||
}, | ||
async update(id, { ...record }) { | ||
const keys = Object.keys(record); | ||
const updates = new Array(keys.length); | ||
const data = new Array(keys.length); | ||
let i = 0; | ||
for (const key of keys) { | ||
data[i] = record[key]; | ||
updates[i] = `${key} = $${++i}`; | ||
} | ||
const delta = updates.join(', '); | ||
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; | ||
data.push(id); | ||
return await pool.query(sql, data); | ||
}, | ||
|
||
async delete(id) { | ||
const sql = `DELETE FROM ${table} WHERE id = $1`; | ||
return await pool.query(sql, [id]); | ||
}, | ||
}); | ||
async delete(id) { | ||
const sql = `DELETE FROM ${table} WHERE id = $1`; | ||
return await pool.query(sql, [id]); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
'use strict'; | ||
|
||
const crypto = require('node:crypto'); | ||
const scrypt = require('node:util').promisify(crypto.scrypt); | ||
|
||
const hash = (password) => new Promise((resolve, reject) => { | ||
const salt = crypto.randomBytes(16).toString('base64'); | ||
crypto.scrypt(password, salt, 64, (err, result) => { | ||
if (err) reject(err); | ||
resolve(salt + ':' + result.toString('base64')); | ||
}); | ||
}); | ||
const hash = async (password) => { | ||
const salt = crypto.randomBytes(16).toString('base64'); | ||
const result = await scrypt(password, salt, 64); | ||
return salt + ':' + result.toString('base64'); | ||
}; | ||
|
||
module.exports = hash; | ||
module.exports = hash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = Object.freeze(require('./logger')); |
Oops, something went wrong.