Commit 246bec2b authored by Цой Данил's avatar Цой Данил 💬

Added methods to get last 30 messages + add fixtures

parent fb87f679
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import { User } from './models/User'
import { Message } from './models/Message'
dotenv.config()
mongoose.connect(process.env.MONGO_CLIENT_URL || 'mongodb://localhost/TsoyDanilChatDB')
const db = mongoose.connection
export default db.once('open', async () => {
try{
await db.dropCollection('messages')
await db.dropCollection('users')
} catch(err: unknown){
console.log(`Skipped db drop because of: ${err}`);
}
const [userOne, userTwo, userThree, userFour, userFive] = await User.create(
{
username: 'user1',
password: 'pass'
},
{
username: 'user2',
password: 'pass'
},
{
username: 'user3',
password: 'pass'
},
{
username: 'user4',
password: 'pass'
},
{
username: 'user5',
password: 'pass'
}
)
await Message.create(
{
user: userOne._id,
message: 'mess1'
},
{
user: userOne._id,
message: 'mess2'
},
{
user: userOne._id,
message: 'mess3'
},
{
user: userOne._id,
message: 'mess4'
},
{
user: userOne._id,
message: 'mess5'
},
{
user: userOne._id,
message: 'mess6'
},
{
user: userOne._id,
message: 'mess7'
},
{
user: userOne._id,
message: 'mess8'
},
{
user: userOne._id,
message: 'mess9'
},
{
user: userOne._id,
message: 'mess10'
},
{
user: userOne._id,
message: 'mess11'
},
{
user: userOne._id,
message: 'mess12'
},
{
user: userOne._id,
message: 'mess13'
},
{
user: userOne._id,
message: 'mess14'
},
{
user: userOne._id,
message: 'mess15'
},
{
user: userOne._id,
message: 'mess16'
},
{
user: userOne._id,
message: 'mess17'
},
{
user: userOne._id,
message: 'mess19'
},
{
user: userOne._id,
message: 'mess20'
},
{
user: userOne._id,
message: 'mess21'
},
{
user: userOne._id,
message: 'mess22'
},
{
user: userOne._id,
message: 'mess23'
},
{
user: userOne._id,
message: 'mess24'
},
{
user: userOne._id,
message: 'mess25'
},
{
user: userOne._id,
message: 'mess26'
},
{
user: userOne._id,
message: 'mess27'
},
{
user: userOne._id,
message: 'mess28'
},
{
user: userOne._id,
message: 'mess29'
},
{
user: userOne._id,
message: 'mess30'
},
{
user: userOne._id,
message: 'mess31'
},
{
user: userOne._id,
message: 'mess32'
},
{
user: userOne._id,
message: 'mess33'
},
{
user: userOne._id,
message: 'mess34'
},
{
user: userOne._id,
message: 'mess35'
}
)
db.close()
})
\ No newline at end of file
......@@ -6,6 +6,7 @@ const MessageSchema: Schema = new Schema<IMessage>({
type: Schema.Types.ObjectId,
ref: 'User',
minlength: 1,
trim: true,
required: [true, 'User id has to be provided']
},
message: {
......@@ -14,6 +15,6 @@ const MessageSchema: Schema = new Schema<IMessage>({
minlength: 1,
required: [true, 'Username should exist']
}
})
}, {versionKey: false})
export const Message = mongoose.model<IMessage>('Message', MessageSchema)
\ No newline at end of file
......@@ -87,11 +87,11 @@ export class MongooseDB {
public getMessages = async(): Promise<IResponse<IMessage[] | null>> => {
try{
const data = await Message.find().populate('user').limit(30)
const data = (await Message.find().populate('user').sort({$natural:-1}).limit(30)).reverse()
const response: IResponse<IMessage[] | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Tracks found'
message: 'Messages found'
}
return response
} catch(err: unknown){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment