本文整理汇总了TypeScript中mongodb.Cursor类的典型用法代码示例。如果您正苦于以下问题:TypeScript Cursor类的具体用法?TypeScript Cursor怎么用?TypeScript Cursor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cursor类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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,
};
}
示例2: close
close() {
if (this.cursor) {
this.cursor.close();
}
return Promise.resolve();
}
示例3: next
next(): Promise<any> {
return this.cursor.next().then((value) => {
this._result = value;
this.key = value && value._id;
return this.key;
});
}
示例4: toArray
toArray(): Promise<Model[]> {
return this.cursor.toArray();
}
示例5: count
count(applyLimit: boolean = false) {
return this.cursor.count(applyLimit);
}
示例6: limit
limit(newLimit: number): Promise<this> {
this.cursor.limit(newLimit);
return Promise.resolve(this);
}
示例7: advance
advance(count: number): void {
this.cursor.skip(count);
}