本文整理汇总了TypeScript中mongodb.Cursor.sort方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Cursor.sort方法的具体用法?TypeScript Cursor.sort怎么用?TypeScript Cursor.sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongodb.Cursor
的用法示例。
在下文中一共展示了Cursor.sort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: transformNodeFunction
public async getGraphQLConnectionFromMongoCursor<InputNode, OutputNode>(
mongoCursor: Cursor, // return value of .find() in mongodb
first: number = 10, // how many to fetch
after?: string, // base64 encoded string
transformNodeFunction?: (node: InputNode) => OutputNode,
) {
// default value:
if (!after) {
after = this.createCursor();
}
// decode cursor:
const cursor = this.decodeCursor(after);
// get totalCount and entries for nodes
const [totalCount, entries] = await Promise.all([
mongoCursor.count(),
mongoCursor
.sort(cursor.sort)
.skip(cursor.skip)
.limit(first)
.toArray(),
]);
// get startCursor and endCursor
const startCursor: string = this.createCursor({
skip: cursor.skip,
sort: cursor.sort,
});
const endCursor: string = this.createCursor({
skip: cursor.skip + entries.length,
sort: cursor.sort,
});
// return graphql connection response
return {
edges: entries.map((entry: InputNode, index: number) => {
return {
cursor: this.createCursor({
skip: cursor.skip + index + 1,
sort: cursor.sort,
}),
node: transformNodeFunction(entry),
};
}),
items: entries.map(transformNodeFunction),
pageInfo: {
endCursor,
hasNextPage: cursor.skip + first <= totalCount - 1,
startCursor,
},
totalCount,
};
}