React-starter-kit: How to store data?

Created on 5 Dec 2015  路  6Comments  路  Source: kriasoft/react-starter-kit

Hello,

I'm trying to learn React tool and I would like to develop a simple website.

I will wish to create a very simple CRUD: create, read, update and delete an article (text only). I managed to do so with Express and Mongoose but I don't know how with react starter kit.

I installed mongoose:

npm install --save mongoose

And I added in server.js

mongoose.connect('mongodb://localhost/react-starter-kit');
var db = mongoose.connection;
db.on('error', function () {
  throw new Error('unable to connect to database');
});

I created a "models" folder and I created "articleModel.js"

var mongoose = require('mongoose');

var schema = new mongoose.Schema({
  title : String,
  content : String
});

module.exports = articleModel = mongoose.model('articleModel', schema);

How to add a new article then display all articles in the database?

I recently discovered React, thank you for your help.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

Currently only the authentication logic contains db access code - src/core/passport. PostgreSQL seems to be very popular choice of databases, especially since it does also have document storage similar to MongoDB. All user accoutns have exactly the same schema - email, password hash, registration date, the list of roles. That kind of data might be better stored in SQL tables I believe, where transactions are properly handled and data integrity is inforced out of the box.

In additional to Postgres, I would really like to add Azure SQL Database, MongoDB, and SQLite versions of src/core/passport, so that RSK users may pick the appropritate implementation for their solution stack.

All 6 comments

So far, I have an example with native MongoDb driver:

https://medium.com/@tarkus/how-to-open-database-connections-in-a-node-js-express-app-e14b2de5d1f8

Bump...

@koistya your example helps but is missing some things? Namely, how can we implement a mongo schema with your example?

@ArdentKid if you really need a schema, then mongoose is a way to go, I believe it should work similarly to the native MongoDb driver in terms of opening a connection, connection pooling..

Why not use Mongo in GraphQL?

Here is the nice tutorial from Kadira on GraphQL - they have a section "Using real data source" at https://learngraphql.com/basics/using-a-real-data-source. It's really showing a nice example on how to hook up MongoDB in GraphQL, using promised-mongo (https://www.npmjs.com/package/promised-mongo).

Maybe this help!

Currently only the authentication logic contains db access code - src/core/passport. PostgreSQL seems to be very popular choice of databases, especially since it does also have document storage similar to MongoDB. All user accoutns have exactly the same schema - email, password hash, registration date, the list of roles. That kind of data might be better stored in SQL tables I believe, where transactions are properly handled and data integrity is inforced out of the box.

In additional to Postgres, I would really like to add Azure SQL Database, MongoDB, and SQLite versions of src/core/passport, so that RSK users may pick the appropritate implementation for their solution stack.

Hi @AliceRossa, I also have same situation and I'm new with React and Redux. Could you please let me know how you integrated MongoDB with this react starter kit?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nguyenbathanh picture nguyenbathanh  路  4Comments

buildbreakdo picture buildbreakdo  路  3Comments

piglovesyou picture piglovesyou  路  3Comments

artkynet picture artkynet  路  4Comments

scazzy picture scazzy  路  3Comments