本文整理匯總了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);
}