Making a chat app that saves to a database

If I made a chat app, how would I save the chats to a database? mongo/replit db preferred.
Could you give me a basic code snippet? Thanks!

const Client = require("@replit/database");
const db = new Client();

function createGroup(name, users) {
	db.get(name).then(function(chat) {
		if (!chat) {
			db.set(name, {
				"name": name,
				"users": [ "user1", "user2" ],
				"messages": [ ]
			});
		}
	});
}

function message(chat, username, message) {
	const sendTime = Date.now();
	db.get(chatName, function(chat) {
		if (chat && chat.users.includes(username)) {
			chat.messages.push({
				"author": username,
				message,
				sendTime
			});
			db.set(chatName, chat);
		}
	});
}
3 Likes

oh my. I never thought of that. Can I invite you to a repl plz?

1 Like

Sure, happy to help :+1:

thx. If it works, I’ll mark as a solution.

@MattDESTROYER is the human form of ChatGPT! :wink:

3 Likes

very true you know :laughing:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.