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.
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.
And you might also find a bunch of posts about building a chat with Firebase in here also:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With