-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathget-all-climbs.ts
More file actions
53 lines (49 loc) · 1.47 KB
/
get-all-climbs.ts
File metadata and controls
53 lines (49 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { ClimbExtType } from '../../ClimbTypes.js'
import { getClimbModel } from '../../ClimbSchema.js'
import { DEFAULT_CHUNK_SIZE } from './defaults.js'
/**
* SQL equivalent:
*
* SELECT climbs.*, areas.ancestors, areas.pathTokens
* FROM climbs left join areas on areas.metadata.area_id = climbs.metadata.areaRef;
*/
export async function * getAllClimbs (chunkSize: number = DEFAULT_CHUNK_SIZE): AsyncGenerator<ClimbExtType[], void, unknown> {
let pageNum = 0
while (true) {
const page = await getClimbModel()
.aggregate<ClimbExtType>([
{
$lookup: {
from: 'areas', // other collection name
localField: 'metadata.areaRef',
foreignField: 'metadata.area_id',
as: 'area', // clobber array of climb IDs with climb objects
pipeline: [
{
$project: {
// only include specific fields
_id: 0,
ancestors: 1,
pathTokens: 1
}
}
]
}
},
{ $unwind: '$area' }, // Previous stage returns as an array of 1 element. 'unwind' turn it into an object.
{
$replaceWith: {
// Merge area.* with top-level object
$mergeObjects: ['$$ROOT', '$area']
}
}
])
.skip(pageNum * chunkSize)
.limit(chunkSize)
if (page.length === 0) {
return
}
yield page
pageNum += 1
}
}