I have a database created through Postgresql. I'm attempting to connect to the database via Knex.js.
In my text editor (atom) I have required knex
const knex = require('knex');
Second - I have begun to initialize the library like so:
const postgres = knex ({ client: 'pg', connection: { host: '127.0.0.1', port: '5432', user: 'is myself', password: '', database: 'mediumrare_database' } });
Third - I am attempting to pull data from that db like so:
postgres.select('*').from('mediumrare_database).then(data => {console.log(data)});
Finally, the error message I am receiving is as follows:
Unhandled rejection error: relation "mediumrare_database" does not exist
Your database is named mediumrare_database, and the table you are trying to get datas from is named the same way.
So it seems the problem is that you didn't created any table.
In SQL, your Knex commands would be:
SELECT * FROM mediumrare_database;, which means Return all the datas inside the 'mediumrare_database' table.
You first need to create your database structure (tables to store data) with a CREATE instruction. See https://knexjs.org/#Schema-createTable.
FROM is meant to be used on a table, as you already specified the database to connect to in the connection string.
I can only suggest you learn the basics of SQL before using Knex, which is an SQL Query Builder. You will run into a lot of problems if you don't understand the underlying system and language, because Knex will not teach you that :)
You can check the one of CodeCademy, they always have great ressources.
EDIT: So you created a vinyl_information table (comments on OP). Your Knex command should then be:
postgres
.select('*')
.from('vinyl_information')
.then(data => console.log(data))
I have a Discord server where we help each others, don't hesitate joining it if you need further help on the topic :) https://discord.gg/C2bVzgb
Related
Aim: sync elasticsearch with postgres database
Why: sometimes newtwork or cluster/server break so future updates should be recorded
This article https://qafoo.com/blog/086_how_to_synchronize_a_database_with_elastic_search.html suggests that I should create a separate table updates that will sync elasticsearch's id, allowing to select new data (from database) since the last record (in elasticsearch). So I thought what if I could record elasticsearch's failure and successful connection: if client ponged back successfully (returned a promise), I could launch a function to sync records with my database.
Here's my elasticConnect.js
import elasticsearch from 'elasticsearch'
import syncProcess from './sync'
const client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.ping({
requestTimeout: Infinity,
hello: "elasticsearch!"
})
.then(() => syncProcess) // successful connection
.catch(err => console.error(err))
export default client
This way, I don't even need to worry about running cron job (if question 1 is correct), since I know that cluster is running.
Questions
Will syncProcess run before export default client? I don't want any requests coming in while syncing...
syncProcess should run only once (since it's cached/not exported), no matter how many times I import elasticConnect.js. Correct?
Is there any advantages using the method with updates table, instead of just selecting data from parent/source table?
The articles' comments say "don't use timestamp to compare new data!".Ehhh... why? It should be ok since database is blocking, right?
For 1: As it is you have not warranty that syncProcess will have run by the time the client is exported. Instead you should do something like in this answer and export a promise instead.
For 2: With the solution I linked to in the above question, this would be taken care of.
For 3: An updates table would also catch record deletions, while simply selecting from the DB would not, since you don't know which records have disappeared.
For 4: The second comment after the article you linked to provides the answer (hint: timestamps are not strictly monotonic).
Any way to do a bulk update, create using sequelize and postgres?
I have tried the following:
sequelize.models.Location.bulkCreate(updatedPost.locations,
{ ignoreDuplicates: true }).then(function (locations) {
res.send(updatedPost);
res.end();
});
I got this error:
postgres does not support the 'ignoreDuplicates' option.
INSERT ... ON CONFLICT DO NOTHING/UPDATE is a thing on Postgres now. They have really good documentation. So you could do Sequelize.query() to do it.
But I'm sorry Sequelize still does not support it natively. You have to write a method on your model to do it.
I want to see the SQL commands that are sent to the PostgreSQL server because I need to check if they are correct. In particular, I am interested in the table creation commands.
For instance, ActiveRecord (Ruby) prints its SQL statements to standard output. Is this possible with Node.js/ActionHero.js and Sequelize.js as well?
You can pass a logging option when initializing sequelize, which can either be a function or console.log
var sequelize = new Sequelize('database', 'username', 'password', {
logging: console.log
logging: function (str) {
// do your own logging
}
});
You can also pass a logging option to .sync if you only want to view the table creation queries
sequelize.sync({ logging: console.log })
As stated in the log Error: Please note that find* was refactored and uses only one options object from now on.. For the latest sequelize version (4) if you want to have the result for only one command:
User.findAll({where: {...}, logging: console.log})
If you want to look at the sequelize for one command you can listen to it and attach a function to the print the sql.
Check out this example:
User.find(1).on('sql', console.log).then(function(user) {
// do whatever you want with the user here
You can also take advantage of Sequelize's use of the Debug module, by setting your environment, thus:
DEBUG=sequelize:sql*
before starting your app.
Can someone please stop me from going insane!
I have a MongoDB database which has a simply database that was created and populated via Mongoose, this works great I can perform finds woth no problems at all.
I went into the Mongo console and created a new database with just use newDB and the performed a simple insert, I inserted several records and they appeared fine within Mongo. I can find on them and so all the Mongo operations but when I try to perform a find on this database Mongoose returns a null???
I have noticed that the database I created in Mongo console does not create the '__v' field which I believe is for Mongoose internal indexing uses, I have created this field in my custom tables but still no joy I just cannot create data from outside of Mongoose and use it within my app??????
I have spent hours looking into this and reading maybe I just missed something but honestly I cannot find a thing on this and many people must hit this every week????
**Sorry here is the code I am running against the database:
exports.adduser = function(req, res){
var mongoose = require("mongoose");
mongoose.connect("localhost/nm", function(err){
if(err)throw(err);
console.log("Connected to MongoDB successfully...")
var schema = mongoose.Schema({
Firstname: String,
Lastname: String,
MiddleInitial: String,
Password: String,
Username: String
});
var auser = mongoose.model("Users", schema);
auser.find({}, function(err, alist){
console.log(">>>>"+alist);
});
});
**
Thanks again!!!!! for your input it is very much appreciated....
try inspecting mongo instance with
show dbs
use <dbName>
in mongo shell to make sure you are using the right database and then
show collections
or alternatively
db.getCollectionNames()
To see if your collections are there or not.
__v is a document version property incremented only for array operations(mongoose 3).
Your connection string might be wrong.
Anyone experiencing this problem and I know there are a good few read the comment by robertklep above it solved my problems very quickly!
DOnt know how the accept a comment sorry!
With MongoDB and Mongoose where do the database files save? I have the below code running on my local node setup and I can't find the database when using the mongo shell. Please help.
# coffescipt:
mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1/testData');
db = mongoose.connection
db.once 'open', ->
console.log 'Connected to MongoDB'
dataSchema = mongoose.Schema {type: String, success: Boolean}
Data = mongoose.model('Data', dataSchema)
talon = new Data {type: 'data', success: false}
talon.save (err, talon) ->
console.log "#{talon.type} Added to DB"
Data.find (err, data) ->
if err
console.error 'No data found :('
else
console.log data
I've looked under all visible databases using show dbs and none of the data Mongoose has submitted is in any of them, however when Mongoose runs Data.find (err, data) -> it displays the full list of inserted documents.
I just want to be able to work with the database through the mongo command line, then use Mongoose to manipulate it for my web app.
thanks for your help.
Apologies, the problem was I had not read enough up on the Mongo Shell. Everything was workign fine.
The key was collections. show collections I was unfamiliar with them up until now. Still learning :)
The data directory used by mongod can be provided on the command line with --dbpath. My guess is your install is using the default location, which varies by OS and distribution but in linux look under /var and /var/lib or on OSX with homebrew look under /usr/local/var/lib.
However, your problem is really not about where the data files live on disk, is getting a consistent connection to the same database server and same database name, which all comes from your connection URL. When you run the mongo shell, try providing a URL like mongo localhost/testData, which should make the code you have above.