Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what would be a good database schema for an extremely simple chat in NoSQL?

Tags:

chat

nosql

There's a good existing answer on a basic schema in SQL.

I can understand it, it's very straightforward. We have a user table, a chat table, and a chat_line table (which in a sane world would be called messages).

I'm rather new to NoSQL, and my mind is still used to "normal" SQL schemes, and I'm trying to understand what would be the correct schema for a chat app in NoSQL (like mongo, or whathaveyou).

I'm talking the simplest form, between one user to another, nothing special - no file messages, no pictures, no group chats. Just text.

like image 707
yuvi Avatar asked Oct 23 '18 23:10

yuvi


2 Answers

NoSQL is not a single standard. To quote something out of MongoDB's website

NoSQL databases typically fall into one of four categories:

  • Key-value stores
  • Wide-column stores
  • Document databases
  • Graph databases

I'm a big fan of Firebase, both the realtime database and Cloud Firestore and I would strongly suggest you to look into it if you're already confident that you want to build this with a non-relational database.

For Cloud Firestore you can follow mostly the same advices that you will get when building for MongoDB. It's a schema-less database with typed fields. Very easy to work with.

There are plenty of material to find on building chats with Firebase real time database.

  • https://github.com/firebase/friendlychat-web
  • https://codelabs.developers.google.com/codelabs/firebase-web/#0

And you might also find a bunch of posts about building a chat with Firebase in here also:

  • Modelling a Chat like Application in Firebase
  • Firebase realtime database structure in chat app
  • Structure a NoSQL database for a chat application (using FireBase)
like image 154
Dennis Alund Avatar answered Sep 23 '22 06:09

Dennis Alund


As others have pointed out NoSQL is a generic term that refers to any alternative to traditional relational databases in which data is placed in tables and data schema is carefully designed before the database is built.

You mentioned Mongo in your question... MongoDB is schema-less. What you can do is create your own class that interacts with an instance of a Mongo Database and in that class you define rules that the data needs to adhere to.

If you are using node.js, you can install Mongoose which allows you to interact with database in object oriented style by providing a straight-forward, schema-based solution to model your data.

Here is a very simple example on how you would define a chat schema in Mongoose, it is not meant to be a complete schema, it is just a start which hopefully will get you going in implementing what you need:

var chatSchema = new Schema({
    chatSession: { type: Number, index: true },    
    user: { type: String, default: 'anonymous' },
    chatLineText: { type: String },
    dateTime: { type: Date, default: Date.now },
});

var chatModel = mongoose.model('Chat', chatSchema);
var chatLine1 = new chatModel({
    chatSession: '2133123',
    user: 'someUserName',
    chatLineText: 'Hello yuvi!'
});

chatLine1.save(function (err, chatLine) {
    if (err) console.log(err);
    else console.log('following chatLine was saved:', chatLine);
like image 23
Leo Avatar answered Sep 21 '22 06:09

Leo