I am trying to learn how to work with KrakenJs and use mysql for my database.
but I have not been able to find anything on line , this is what i have come up with so far
I have created a folder called lib and I want to have my connection file db.js to the database
'use strict';
var mysql = require('mysql');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('site', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
},
});
module.exports = sequelize;
first of all I am not sure what needs to go my main index.js file config option part ...
'use strict';
var express = require('express');
var kraken = require('kraken-js');
var db = require('./lib/db');
var options, app;
/*
* Create and configure application. Also exports application instance for use by tests.
* See https://github.com/krakenjs/kraken-js#options for additional configuration options.
*/
options = {
onconfig: function (config, next) {
/*
* Add any additional config setup or overrides here. `config` is an initialized
* `confit` (https://github.com/krakenjs/confit/) configuration object.
*/
next(null, config);
}
};
How to set up my model , this is just a simple model , I just wanna get the idea of how to use it ...
'use strict';
var db = require('../lib/db');
module.exports = function User() {
var User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
};
and Lastly how to use it my controller
'use strict';
var User = require('../models/users');
module.exports = function (router) {
// var model = new User();
router.get('/', function (req, res) {
User.findOne().then(function (user) {
res.render('index', user);
});
});
};
Related
What I'm trying to do:
I have a small Node app makes a request to an API to get data about the stock market and then saves the data to a Mongo DB (already made and working), which I would like to run constantly. Next I need to build an API which allows my other servers/apps to get that data being (not made yet).
I think Express is a good choice although I know there are other options. I am getting this code from another API that I built for a MEAN stack app and have been looking around to troubleshoot.
My main issue is that most tutorials show how to just build a CRUD API for the MEAN, MERN, and other stacks. I'm not really sure how to structure this system or Node apps can run independently of one another in the way that I am describing.
Currently I am not getting any errors when I run the app but I am not able to get anything when I go to my endpoints.
When I first started I thought that the standalone Node app (request and writing data part of this) could live within the same file as the Express part, but now I don't think that would work and have split those into separate files. I have seen PM2 and other ways of making the node app into a background service?
Not totally sure, some clarification on that, as well as some help with why my express API endpoints won't respond with data from the database. Here is my code.
Standalone app:
const request = require('request');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const d = new Date();
let day = d.getDay()
let hour = d.getHours();
const stockMomentSchema = new Schema({
symbol: String,
price: Number,
size: Number,
time: {type: Date, default: Date.now}
});
const StockMoment = mongoose.model('stockMoment', stockMomentSchema, 'stockPriceData');
mongoose.connect('mongodb://localhost:27017/stock-test')
.then(() => {
console.log('connected');
})
.catch(err => {
console.error(err);
});
function makeRequest() {
if(day <= 1 || day >= 5) {
if(hour >= 10 || hour <= 15) {
getMarketData();
}
}
}
function getMarketData() {
request({
url: 'https://api.iextrading.com/1.0/tops/last',
json: true
}, (err, res, body) => {
if(err) {
return console.error(err);
}
for(let i = 0; i < body.length; i++) {
const stockMoment = new StockMoment({
symbol: body[i].symbol,
price: body[i].price,
size: body[i].size,
time: body[i].time,
});
stockMoment.save((err) => {
if(err) return handleError(err);
console.log('Saved!', i);
});
console.log(body[i]);
}
});
}
makeRequest();
//setInterval(makeRequest, 5000);
server.js:
const express = require('express');
const path = require('path'); // Not sure if this is needed
const http = require('http');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const api = require('./api');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cors());
//app.use(express.static(path.join(__dirname, 'dist'))); // Not sure if this is needed
app.use('/api', api);
app.get('*', function(req, res) {
res.status(404);
});
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
server.listen('3000', function() {
console.log('--------------------');
console.log('Server running');
console.log('You can view the server at http://localhost:3000');
console.log('If you are running an Angular app on while using this server please use http://localhost:4200');
console.log('--------------------');
});
stock model:
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const StockSchema = mongoose.Schema({
username: { type: String, unique: true, required: true },
password: { type: String, required: true },
email: {type: String, required: true},
phoneNumber: {type: String, required: true},
company: {type: String, required: true},
about: {type: String, required: true},
userType: {type: String, required: true}
});
StockSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Stock', StockSchema);
api.js:
const express = require('express');
const router = express.Router();
const Stock = require('./stock.js');
router.get('/', function(req, res, err) {
console.log('Hello world');
});
router.get('/stocks/:id', function(req, res, err) {
console.log('Hello world');
const stockData = {
_id: false,
symbol: true,
price: true,
size: true,
time: true
}
Stock.findById(req.params.id, stockData, function(err, stock) {
if(err) {
res.send('No stocks found');
console.log(err);
} else {
res.json(stock);
}
});
});
module.exports = router;
Edit:
Added mongoose.connect to server.js but it still doesn't work
mongoose.connect('mongodb://localhost:27017/stock-test')
.then((res) => {
console.log('connected');
})
.catch(err => {
console.error(err);
});
Edit: Here's a gist with the working changes https://gist.github.com/drGrove/3e6699ded09f282e528a661fb41439e1
Your api is pulling in a schema that has no information associated with it.
You should break out the StockMomentData, require it from you api.js and use that in your get request by id instead of your current stock model.
Since your mongoose connection is shared between the two "applications" you can pull that out into a db.js and just require that file on load for each project (But that can always be done at a later time).
Just note that you do need a connection in either your Model file or your api.js file so that mongoose actually connects to the mongo database
Initial Answer:
Your API server is missing a connection to Mongo via mongoose. You'll have to have a connection call just like what you have in your "standalone app".
I want to preface this by saying I have read several posts here regarding this issue.
I have a node/express/mongo app with the following:
app.js:
var express = require('express')
var bodyParser = require('body-parser')
var cors = require('cors')
var morgan = require('morgan')
var mongoose = require('mongoose')
var passport = require('passport')
var app = express()
// MongoDB Setup
var configDB = require('./config/database.js')
mongoose.connect(configDB.url)
app.use(morgan('combined'))
app.use(bodyParser.json())
// Check security with this
app.use(cors())
// load our routes and pass in our app and fully configured passport
require('./routes')(app)
app.listen(process.env.PORT || 8081)
console.log('We are up and running, captain.')
routes.js
const AuthenticationController = require('./controllers/AuthenticationController')
module.exports = (app) => {
app.post('/register', AuthenticationController.register)
}
My mongo schema file Account.js:
const mongoose = require('mongoose')
const bcrypt = require('bcrypt-nodejs')
const Schema = mongoose.Schema
var accountSchema = new Schema({
email: String,
password: String,
likesPerDay: { type: Number, min: 0, max: 250 },
followPerDay: { type: Number, min: 0, max: 250 },
unfollowPerDay: { type: Number, min: 0, max: 250 },
commentsPerDay: { type: Number, min: 0, max: 250 },
comment: String,
hashtags: [String]
})
// methods ======================
// generating a hash. We hash password within user model, before it saves to DB.
accountSchema.methods.generateHash = function (password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null)
}
// checking if password is valid
accountSchema.methods.validPassword = function (password) {
return bcrypt.compareSync(password, this.local.password)
}
// create the model for users and expose it to our app
module.exports = mongoose.model('Account', accountSchema)
And finally my controller file AuthenticationController.js
const Account = require('../models/Account.js')
// var bodyParser = require('body-parser')
module.exports = {
register (req, res) {
Account.findOne({email: req.body.id}, function (err, account) {
if (err) {
console.log('Could not regster user')
throw err
}
if (account) {
console.log('account already exists')
} else {
Account.insertOne({email: req.body.email, password: req.body.password}, function (err, res) {
if (err) {
console.log('could not insert')
throw err
}
console.log('inserted account')
Account.close()
})
}
})
}
}
I am getting an error in my AuthenticationController file when I call Account.insertOne function.
I get the error that
TypeError: Account.insertOne is not a function
Now several of the posts here on stack have advised that I make sure that I am exporting the model from my model class, which I am doing, and that would fix this issue. Its weird because the findOne method seems to be fine, but when I call the insertOne i get an issue.
Am I missing something here?
A Mongoose model doesn't have an insertOne method. Use the create method instead:
Account.create({email: req.body.email, password: req.body.password}, function (err, doc) {
The Mongoose docs show how to create documents:
Either via Account.create():
Account.create({email: req.body.email, password: req.body.password}, function (err, res) {
// ...
})
Or by instantiating and save()ing the account:
new Account({email: req.body.email, password: req.body.password}).save(function (err, res) {
// ...
})
edit
as of mongoose documentation, try using
Account.create({ ...params ... }, function (err, small) {
if (err) return handleError(err);
// saved!
})
insertOne command is not available in mongoose directly as mentioned in Mongoose Documentation. If you want to use insertOne command then you need to use bulk command in order to send this command to MongoDB server. Something like below. I hope this works.
Account.bulkWrite([
{
insertOne: {
document: {email: req.body.email, password: req.body.password}
}
}
}]
I'm new in node.
I'm trying to add Sequelize in my simple application with cosign.
config/db.js
var Sequelize = require('sequelize');
var sequelize = new Sequelize('test', 'root', '', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
module.exports = function () {
return sequelize
}
model/user.js
var Sequelize = require('sequelize');
module.exports = function(application, req, res){
var User = sequelize.define('user', {
username: {
type: Sequelize.STRING,
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
User.create({ username: 'fnord'})
.then(function() {
console.log('criado!');
})
}
config/server.js
...
consign()
.include('app/routes')
.then('config/db.js')
.then('app/models')
.then('app/controllers')
.into(app);
module.exports = app;
I'm getting the error sequelize is not definedĀ“ onvar User = sequelize.define('user', {`
What I'm doing wrong?
Create an index.js file inside your moldes folder like this:
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var sequelize = new Sequelize(global.config.dbConfig.name, global.config.dbConfig.user, global.config.dbConfig.password, {
host: global.config.dbConfig.host,
port: global.config.dbConfig.port,
pool: false
});
var db = {};
fs.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
module.exports = db;
and in your user.js do something like this:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: {
type: DataTypes.STRING
},
{
freezeTableName: true // Model tableName will be the same as the model name
}
});
return User;
}
http://docs.sequelizejs.com/en/1.7.0/articles/express/
You should require sequelize instance into user model
config/db.js
module.exports = sequelize;
model/user.js
var Sequelize = require('sequelize');
var sequelize = require('../config/db.js'); //sequelize instance
module.exports = function(application, req, res){
var User = sequelize.define('user', {
...
The Sequelize-CLI is a very useful tool for projects that use Sequelize. When you download it
npm install -g sequelize-cli
You can then run
sequelize init
The above command will go and write out a few folders for you including a models folder that has the index file that Ricardo created above. This gives some really cool environment configuration as well. Within the new models folder, you can create a new file with your object with the syntax...
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: {
type: DataTypes.STRING
},
{
freezeTableName: true // Model tableName will be the same as the model name
}
});
return User;
}
While I do like this as a tool. It is key here to notice that Sequelize will go and look for the first argument to the define() method. So we could just write
module.exports = function(sequelize, DataType){
return sequelize.define("User", {
username: {
type: DataTypes.STRING
},
{
freezeTableName: true // Model tableName will be the same as the model name
}
});
I'm trying to implementing caminteJs on nodejs application, after define database schema on separated file i can't use that and i get this error:
TypeError: models.channels.create is not a constructor
i created models folder and in that i have some database schema which wrapped into separated file as
user.js //contains user table schema
news.js //contains news table schema
into index.js which its into models folder i have:
var users = require( './user' );
var news = require( './news' );
module.exports = {
users:users,
news:news
};
user.js file content:
var config = require('../config');
module.exports = function(schema){
var users = config.define('users', {
user_name: { type: schema.String, limit: 30 },
...
created_at: { type: schema.Date },
updated_at: { type: schema.Date }
});
return users;
};
config.js file content:
var caminte = require( 'caminte' ),
Schema = caminte.Schema,
config = {
driver: "mysql",
host: "localhost",
port: "3306",
username: "root",
password: "",
database: "test",
pool: true
},
schema = new Schema( config.driver, config );
module.exports = {
caminte: caminte,
Schema: Schema,
config: config,
schema: schema
}
and then my server.js to use them:
var socket = require( 'socket.io' ),
...
config = require( './config' ),
models = require( './models' );
io.on( 'connection', function (socket) {
socket.on( "new_user", function (data, device) {
const channels = new models.users.create(function(err){
user_name: 'Peter'
});
console.log( channels );
} );
} );
Depending on what you want and how your schema looks like but for creation try to use:
const user = models.users.create(function(err){
console.log('Error happened', err);
});
I'm following a tutorial which uses sequelize with an express project. Here's the user.js model:
// in models/User.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('User', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
}, {
instanceMethods: {
countTasks: function() {
// how to implement this method ?
}
}
});
};
Then he uses User in various of ways, for example:
var user = User.build({ first_name: 'John', last_name: 'Doe' });
I understand the code in general, but I don't understand completely why module.exports gets a function with two parameters (sequelize, DataTypes). I haven't seen it initialized anywhere in the code. How is it working then?
If you are following this guide you will see in models/index.js that all model definitions are looped through and passed to seqelize.import().
You will find that this line of code within sequelize.import calls the model's module function and passes a reference to sequelize and DataTypes to the model.
In the tutorial you referenced, the author uses a similar method within models/index.js
> Edit (7/14/2015)
Since the link does not work and I could not find it on their current site, I copied the code from their site using The Wayback Machine. I also updated the second link to point to the 2.0 docs instead of master.
models/index.js
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/../config/config.json')[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;