Chapter 6: updates
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
const express = require('express')
|
const express = require("express");
|
||||||
const path = require('path')
|
const path = require("path");
|
||||||
const index = require('./routes/index')
|
const index = require("./routes/index");
|
||||||
|
|
||||||
const PORT = process.env.PORT || 3000
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
const app = express()
|
const app = express();
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, 'public')))
|
app.use(express.static(path.join(__dirname, "public")));
|
||||||
app.use('/', index)
|
app.use("/", index);
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server listening on port ${PORT}`)
|
console.log(`Server listening on port ${PORT}`);
|
||||||
})
|
});
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
const {hash} = require('xxhash')
|
|
||||||
const through = require('through2')
|
|
||||||
const eos = require('end-of-stream')
|
|
||||||
const level = require('level')
|
|
||||||
|
|
||||||
const db = level('./data')
|
|
||||||
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
add(params, (err) => {
|
|
||||||
if (err) console.error(err)
|
|
||||||
list(params.author)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author) {
|
|
||||||
list(params.author)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
function add({quote, author}, cb) {
|
|
||||||
const key = author + hash(Buffer.from(quote), 0xDAF1DC)
|
|
||||||
db.put(key, quote, cb)
|
|
||||||
}
|
|
||||||
|
|
||||||
function list (author) {
|
|
||||||
if (!author) db.close()
|
|
||||||
const quotes = db.createValueStream({
|
|
||||||
gte: author,
|
|
||||||
lt: String.fromCharCode(author.charCodeAt(0) + 1)
|
|
||||||
})
|
|
||||||
const format = through((quote, enc, cb) => {
|
|
||||||
cb(null, `${author} ${quote}`)
|
|
||||||
})
|
|
||||||
quotes.pipe(format).pipe(process.stdout)
|
|
||||||
eos(format, () => {
|
|
||||||
db.close()
|
|
||||||
console.log()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "level-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"end-of-stream": "^1.1.0",
|
|
||||||
"level": "^1.6.0",
|
|
||||||
"through2": "^2.0.3",
|
|
||||||
"xxhash": "^0.2.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
const {hash} = require('xxhash')
|
|
||||||
const through = require('through2')
|
|
||||||
const eos = require('end-of-stream')
|
|
||||||
const levelup = require('levelup')
|
|
||||||
const sqldown = require('sqldown')
|
|
||||||
const db = levelup('./data', {db: sqldown})
|
|
||||||
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
add(params, (err) => {
|
|
||||||
if (err) console.error(err)
|
|
||||||
list(params.author)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author) {
|
|
||||||
list(params.author)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
function add({quote, author}, cb) {
|
|
||||||
const key = author + hash(Buffer.from(quote), 0xDAF1DC)
|
|
||||||
db.put(key, quote, cb)
|
|
||||||
}
|
|
||||||
|
|
||||||
function list (author) {
|
|
||||||
if (!author) db.close()
|
|
||||||
const quotes = db.createValueStream({
|
|
||||||
gte: author,
|
|
||||||
lt: String.fromCharCode(author.charCodeAt(0) + 1)
|
|
||||||
})
|
|
||||||
const format = through((quote, enc, cb) => {
|
|
||||||
cb(null, `${author} ${quote}`)
|
|
||||||
})
|
|
||||||
quotes.pipe(format).pipe(process.stdout)
|
|
||||||
eos(format, () => {
|
|
||||||
db.close()
|
|
||||||
console.log()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "level-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"end-of-stream": "^1.1.0",
|
|
||||||
"levelup": "^1.3.3",
|
|
||||||
"sqldown": "^2.1.0",
|
|
||||||
"sqlite": "^2.3.0",
|
|
||||||
"through2": "^2.0.3",
|
|
||||||
"xxhash": "^0.2.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
const {MongoClient} = require('mongodb')
|
|
||||||
const client = new MongoClient()
|
|
||||||
|
|
||||||
client.connect('mongodb://localhost:27017/quotes', ready)
|
|
||||||
|
|
||||||
function ready (err, db) {
|
|
||||||
if (err) throw err
|
|
||||||
const collection = db.collection('quotes')
|
|
||||||
collection.ensureIndex('author', (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
collection.distinct('author', (err, result) => {
|
|
||||||
if (err) throw err
|
|
||||||
console.log(result.join('\n'))
|
|
||||||
db.close()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
const {MongoClient} = require('mongodb')
|
|
||||||
const client = new MongoClient()
|
|
||||||
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
client.connect('mongodb://localhost:27017/quotes', ready)
|
|
||||||
|
|
||||||
function ready (err, db) {
|
|
||||||
if (err) throw err
|
|
||||||
const collection = db.collection('quotes')
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
collection.insert({
|
|
||||||
author: params.author,
|
|
||||||
quote: params.quote
|
|
||||||
}, (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author) {
|
|
||||||
collection.find({
|
|
||||||
author: params.author
|
|
||||||
}).each((err, doc) => {
|
|
||||||
if (err) throw err
|
|
||||||
if (!doc) {
|
|
||||||
db.close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
console.log('%s: %s \n', doc.author, doc.quote)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db.close()
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mongo-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mongodb": "^2.2.16"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
const {MongoClient, ObjectID} = require('mongodb')
|
|
||||||
const client = new MongoClient()
|
|
||||||
const params = {id: process.argv[2]}
|
|
||||||
|
|
||||||
client.connect('mongodb://localhost:27017/quotes', ready)
|
|
||||||
|
|
||||||
function ready (err, db) {
|
|
||||||
if (err) throw err
|
|
||||||
const collection = db.collection('quotes')
|
|
||||||
|
|
||||||
if (!params.id) {
|
|
||||||
showIds(collection, db)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
vote(params.id, db, collection)
|
|
||||||
}
|
|
||||||
|
|
||||||
function showIds (collection, db) {
|
|
||||||
collection.find().each((err, doc) => {
|
|
||||||
if (err) throw err
|
|
||||||
if (doc) {
|
|
||||||
console.log(doc._id, doc.quote)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
db.close()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function vote (id, db, collection) {
|
|
||||||
const query = {
|
|
||||||
_id: ObjectID(id)
|
|
||||||
}
|
|
||||||
const action = {$inc: {votes: 1}}
|
|
||||||
const opts = {safe: true}
|
|
||||||
collection.update(query, action, opts, (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
console.log('1 vote added to %s by %s', params.id)
|
|
||||||
const by = {votes: 1}
|
|
||||||
const max = 10
|
|
||||||
collection.find().sort(by).limit(max).each((err, doc) => {
|
|
||||||
if (err) throw err
|
|
||||||
if (doc) {
|
|
||||||
const votes = doc.votes || 0
|
|
||||||
console.log(`${votes} | ${doc.author}: ${doc.quote.substr(0, 30)}...`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
db.close()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
const mysql = require('mysql')
|
|
||||||
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = mysql.createConnection({
|
|
||||||
user: 'root',
|
|
||||||
//password: 'pw-if-set',
|
|
||||||
//debug: true
|
|
||||||
})
|
|
||||||
|
|
||||||
db.query('CREATE DATABASE quotes')
|
|
||||||
db.query('USE quotes')
|
|
||||||
|
|
||||||
db.query(`
|
|
||||||
CREATE TABLE quotes.quotes (
|
|
||||||
id INT NOT NULL AUTO_INCREMENT,
|
|
||||||
author VARCHAR ( 128 ) NOT NULL,
|
|
||||||
quote TEXT NOT NULL, PRIMARY KEY ( id )
|
|
||||||
)
|
|
||||||
`)
|
|
||||||
|
|
||||||
const ignore = new Set([
|
|
||||||
'ER_DB_CREATE_EXISTS',
|
|
||||||
'ER_TABLE_EXISTS_ERROR'
|
|
||||||
])
|
|
||||||
|
|
||||||
db.on('error', (err) => {
|
|
||||||
if (ignore.has(err.code)) return
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
db.query(`
|
|
||||||
INSERT INTO quotes.quotes (author, quote)
|
|
||||||
VALUES (?, ?);
|
|
||||||
`, [params.author, params.quote])
|
|
||||||
}
|
|
||||||
|
|
||||||
db.end()
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mysql-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mysql": "^2.12.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
const mysql = require('mysql')
|
|
||||||
const db = mysql.createConnection({
|
|
||||||
user: 'root',
|
|
||||||
//password: 'pw-if-set',
|
|
||||||
//debug: true
|
|
||||||
})
|
|
||||||
|
|
||||||
db.query('CREATE DATABASE quotes')
|
|
||||||
db.query('USE quotes')
|
|
||||||
|
|
||||||
db.query(`
|
|
||||||
CREATE TABLE quotes.quotes (
|
|
||||||
id INT NOT NULL AUTO_INCREMENT,
|
|
||||||
author VARCHAR ( 128 ) NOT NULL,
|
|
||||||
quote TEXT NOT NULL, PRIMARY KEY ( id )
|
|
||||||
)
|
|
||||||
`)
|
|
||||||
|
|
||||||
const ignore = new Set([
|
|
||||||
'ER_DB_CREATE_EXISTS',
|
|
||||||
'ER_TABLE_EXISTS_ERROR'
|
|
||||||
])
|
|
||||||
|
|
||||||
db.on('error', (err) => {
|
|
||||||
if (ignore.has(err.code)) return
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
|
|
||||||
db.query(`
|
|
||||||
INSERT INTO quotes.quotes (author, quote)
|
|
||||||
VALUES ("Bjarne Stroustrup", "Proof by analogy is fraud.");
|
|
||||||
`)
|
|
||||||
|
|
||||||
db.end()
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mysql-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mysql": "^2.12.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
const mysql = require('mysql')
|
|
||||||
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = mysql.createConnection({
|
|
||||||
user: 'root',
|
|
||||||
//password: 'pw-if-set',
|
|
||||||
//debug: true
|
|
||||||
})
|
|
||||||
|
|
||||||
db.query('CREATE DATABASE quotes')
|
|
||||||
db.query('USE quotes')
|
|
||||||
|
|
||||||
db.query(`
|
|
||||||
CREATE TABLE quotes.quotes (
|
|
||||||
id INT NOT NULL AUTO_INCREMENT,
|
|
||||||
author VARCHAR ( 128 ) NOT NULL,
|
|
||||||
quote TEXT NOT NULL, PRIMARY KEY ( id )
|
|
||||||
)
|
|
||||||
`)
|
|
||||||
|
|
||||||
const ignore = new Set([
|
|
||||||
'ER_DB_CREATE_EXISTS',
|
|
||||||
'ER_TABLE_EXISTS_ERROR'
|
|
||||||
])
|
|
||||||
|
|
||||||
db.on('error', (err) => {
|
|
||||||
if (ignore.has(err.code)) return
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
db.query(`
|
|
||||||
INSERT INTO quotes.quotes (author, quote)
|
|
||||||
VALUES (?, ?);
|
|
||||||
`, [params.author, params.quote])
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author) {
|
|
||||||
db.query(`
|
|
||||||
SELECT * FROM quotes
|
|
||||||
WHERE author LIKE ${db.escape(params.author)}
|
|
||||||
`).on('result', ({author, quote}) => {
|
|
||||||
console.log(`${author} ${quote}`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
db.end()
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mysql-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mysql": "^2.12.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
const pg = require('pg')
|
|
||||||
const db = new pg.Client()
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
db.connect((err) => {
|
|
||||||
if (err) throw err
|
|
||||||
db.query(`
|
|
||||||
CREATE TABLE IF NOT EXISTS quotes (
|
|
||||||
id SERIAL,
|
|
||||||
author VARCHAR ( 128 ) NOT NULL,
|
|
||||||
quote TEXT NOT NULL, PRIMARY KEY ( id )
|
|
||||||
)
|
|
||||||
`, (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
db.query(`
|
|
||||||
INSERT INTO quotes (author, quote)
|
|
||||||
VALUES ($1, $2);
|
|
||||||
`, [params.author, params.quote], (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
list(db, params)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!params.quote) list(db, params)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
function list (db, params) {
|
|
||||||
if (!params.author) return db.end()
|
|
||||||
db.query(`
|
|
||||||
SELECT * FROM quotes
|
|
||||||
WHERE author LIKE ${db.escapeLiteral(params.author)}
|
|
||||||
`, (err, results) => {
|
|
||||||
if (err) throw err
|
|
||||||
results.rows.forEach(({author, quote}) => {
|
|
||||||
console.log(`${author} ${quote}`)
|
|
||||||
})
|
|
||||||
db.end()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mysql-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mysql": "^2.12.0",
|
|
||||||
"pg": "^6.1.2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
const pg = require('pg').native || require('pg')
|
|
||||||
const db = new pg.Client()
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
db.connect((err) => {
|
|
||||||
if (err) throw err
|
|
||||||
db.query(`
|
|
||||||
CREATE TABLE IF NOT EXISTS quotes (
|
|
||||||
id SERIAL,
|
|
||||||
author VARCHAR ( 128 ) NOT NULL,
|
|
||||||
quote TEXT NOT NULL, PRIMARY KEY ( id )
|
|
||||||
)
|
|
||||||
`, (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
db.query(`
|
|
||||||
INSERT INTO quotes (author, quote)
|
|
||||||
VALUES ($1, $2);
|
|
||||||
`, [params.author, params.quote], (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
list(db, params)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!params.quote) list(db, params)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
function list (db, params) {
|
|
||||||
if (!params.author) return db.end()
|
|
||||||
db.query(`
|
|
||||||
SELECT * FROM quotes
|
|
||||||
WHERE author LIKE $1
|
|
||||||
`, [params.author], (err, results) => {
|
|
||||||
if (err) throw err
|
|
||||||
results.rows.forEach(({author, quote}) => {
|
|
||||||
console.log(`${author} ${quote}`)
|
|
||||||
})
|
|
||||||
db.end()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mysql-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mysql": "^2.12.0",
|
|
||||||
"pg": "^6.1.2"
|
|
||||||
},
|
|
||||||
"optionalDependencies": {
|
|
||||||
"pg-native": "^1.10.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
const pg = require('pg')
|
|
||||||
const db = new pg.Client()
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
db.connect((err) => {
|
|
||||||
if (err) throw err
|
|
||||||
db.query(`
|
|
||||||
CREATE TABLE IF NOT EXISTS quote_docs (
|
|
||||||
id SERIAL,
|
|
||||||
doc jsonb,
|
|
||||||
CONSTRAINT author CHECK (length(doc->>'author') > 0 AND (doc->>'author') IS NOT NULL),
|
|
||||||
CONSTRAINT quote CHECK (length(doc->>'quote') > 0 AND (doc->>'quote') IS NOT NULL)
|
|
||||||
)
|
|
||||||
`, (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
db.query(`
|
|
||||||
INSERT INTO quote_docs (doc)
|
|
||||||
VALUES ($1);
|
|
||||||
`, [params], (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
list(db, params)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!params.quote) list(db, params)
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
function list (db, params) {
|
|
||||||
if (!params.author) return db.end()
|
|
||||||
db.query(`
|
|
||||||
SELECT * FROM quote_docs
|
|
||||||
WHERE doc ->> 'author' LIKE ${db.escapeLiteral(params.author)}
|
|
||||||
`, (err, results) => {
|
|
||||||
if (err) throw err
|
|
||||||
results.rows
|
|
||||||
.map(({doc}) => doc)
|
|
||||||
.forEach(({author, quote}) => {
|
|
||||||
console.log(`${author} ${quote}`)
|
|
||||||
})
|
|
||||||
db.end()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mysql-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"mysql": "^2.12.0",
|
|
||||||
"pg": "^6.1.2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
const uuid = require('uuid')
|
|
||||||
const steed = require('steed')()
|
|
||||||
const redis = require('redis')
|
|
||||||
const client = redis.createClient()
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
add(params)
|
|
||||||
list((err) => {
|
|
||||||
if (err) console.error(err)
|
|
||||||
client.quit()
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author) {
|
|
||||||
list((err) => {
|
|
||||||
if (err) console.error(err)
|
|
||||||
client.quit()
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
client.quit()
|
|
||||||
|
|
||||||
function add ({author, quote}) {
|
|
||||||
const key = `Quotes: ${uuid()}`
|
|
||||||
client.hmset(key, {author, quote})
|
|
||||||
client.sadd(`Author: ${params.author}`, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
function list (cb) {
|
|
||||||
client.smembers(`Author: ${params.author}`, (err, keys) => {
|
|
||||||
if (err) return cb(err)
|
|
||||||
steed.each(keys, (key, next) => {
|
|
||||||
client.hgetall(key, (err, {author, quote}) => {
|
|
||||||
if (err) return next(err)
|
|
||||||
console.log(`${author} ${quote} \n`)
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}, cb)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "redis-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"redis": "^2.7.1",
|
|
||||||
"steed": "^1.1.3",
|
|
||||||
"uuid": "^3.0.1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
const uuid = require('uuid')
|
|
||||||
const steed = require('steed')()
|
|
||||||
const redis = require('redis')
|
|
||||||
const client = redis.createClient(),
|
|
||||||
const params = {
|
|
||||||
author: process.argv[2],
|
|
||||||
quote: process.argv[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author && params.quote) {
|
|
||||||
add(params, (err) => {
|
|
||||||
if (err) throw err
|
|
||||||
list((err) => {
|
|
||||||
if (err) console.error(err)
|
|
||||||
client.quit()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.author) {
|
|
||||||
list((err) => {
|
|
||||||
if (err) console.error(err)
|
|
||||||
client.quit()
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
client.quit()
|
|
||||||
|
|
||||||
|
|
||||||
function add ({author, quote}, cb) {
|
|
||||||
const key = `Quotes: ${uuid()}`
|
|
||||||
client
|
|
||||||
.multi()
|
|
||||||
.hmset(key, {author, quote})
|
|
||||||
.sadd(`Author: ${params.author}`, key)
|
|
||||||
.exec((err, replies) => {
|
|
||||||
if (err) return cb(err)
|
|
||||||
if (replies[0] === "OK") console.log('Added...\n')
|
|
||||||
cb()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function list (cb) {
|
|
||||||
client.smembers(`Author: ${params.author}`, (err, keys) => {
|
|
||||||
if (err) return cb(err)
|
|
||||||
steed.each(keys, (key, next) => {
|
|
||||||
client.hgetall(key, (err, {author, quote}) => {
|
|
||||||
if (err) return next(err)
|
|
||||||
console.log(`${author} ${quote} \n`)
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}, cb)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "redis-app",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"redis": "^2.7.1",
|
|
||||||
"steed": "^1.1.3",
|
|
||||||
"uuid": "^3.0.1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user