import {
RelayPaginateStatics,
RelayPaginateQueryHelper,
alterNodeOnResult,
relayPaginate,
relayPaginatePlugin,
} from "mongoose-relay-paginate";
import { Schema, model, connect, Model, plugin } from "mongoose";
import mongoose from "mongoose";
const url = "mongodb://localhost:32782";
const dbName = "mongo-relay-connection";
plugin(
relayPaginatePlugin({
maxLimit: 100,
})
);
interface User {
_id: mongoose.Types.ObjectId;
myId: number;
name: string;
email: string;
avatar?: string;
}
type UserModel = Model<User, RelayPaginateQueryHelper> & RelayPaginateStatics;
const schema = new Schema<User, UserModel, MyUserMethods>({
myId: Number,
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String,
});
const UserModel = model<User, UserModel>("User", schema);
async function run(): Promise<void> {
const client = await connect(url, {
dbName,
});
const doc = new UserModel({
myId: 1,
name: "Bill",
email: "bill@example.com",
avatar: "https://i.imgur.com/dM7Thhn.png",
});
const doc2 = new UserModel({
myId: 2,
name: "Jill",
email: "jill@example.com",
avatar: "https://i.imgur.com/dM7Thhn.png",
});
const doc3 = new UserModel({
myId: 3,
name: "Phill",
email: "Phill@example.com",
avatar: "https://i.imgur.com/dM7Thhn.png",
});
await doc.save();
await doc2.save();
await doc3.save();
const result = await UserModel.find()
.sort({ name: -1 })
.relayPaginate({
first: 1,
});
console.log(result.nodes);
}