grab.js
script to pull data from the Northwind OData service.grab.js
script, using Axios's .all()
function which allows us to fire off multiple HTTP requests, and which returns a promise..then(xs => {
return xs
})
odata.nextLink
based paging mechanism that's used to return pages of data where the whole data set is deemed to large to return in one go (there are, for example, 77 products).data
directory, the JSON representing the data resources for the entitysets we want to download, so we can store and serve them locally, rather than run any risk of being (rightly) rate-limited or banned for overuse of the public Northwind service.http.server
module, which is super useful. We use this in the data/
directory to serve up the JSON resources we've previously downloaded. Note that the module name changed between Python versions 2 and 3 (it was SimpleHTTPServer
with Python 2).=> python -mhttp.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000)
grab.js
script, changing the baseurl
to point to this local HTTP server endpoint.nodemon
(which we first saw in episode 0) locally inside this project, and starting it up thus:./node_modules/nodemon/bin/nodemon.js grab.js
.then(xs => xs.reduce((a, x) => a.concat(x.data.value), []))
.then(xs => xs.length)
.then(console.log)
.then(...)
call is doing, with the reduce
function; basically, here's an example, from the Node.js REPL:> stuff
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
> stuff.reduce((a, x) => a.concat(x), [])
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
axios.get(baseurl + '0')
etc) and writing something to generate them instead. In this part, we look again at Python, to briefly explore the difference between range
and xrange
, noting the difference between the two - one is lazy, the other is not. Laziness is a good thing! BTW there is range
and xrange
in Python 2, but Python 3's range
is the equivalent of Python 2's xrange
.range
in JavaScript (I make a brief reference to Gary Bernhardt's excellent and very entertaining talk titled "Wat", on JavaScript), we create a range
function like this:range = x => [...Array(x).keys()]
get()
calls in the all()
call by generating the calls dynamically, making use of our new range
function.$
to get to the end of the line, then a
to start appending from there. I should have simply used A
which does both those things.all()
call looks like this:.all(range(4).map(x => axios.get(baseurl + (x * 20))))
grab
that we can then make calls to later. The function will return a promise.grab
like this:grab('Products')
.then(xs => xs.length)
.then(console.log)
grab
in an Object.keys
that works on the entities configuration map we previously defined, noting that the simple use of console.log
in the map
call, we get what is really passed to the function in a map
call, i.e. three arguments (not just a single argument) - current value, index and array. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Parameter... for details about this.grab
function could be curried, to allow us to remove the reliance that it currently has on the global baseurl
constant. This is a good idea and something we should look at if we cover this code again in the next few sessions. If you're interested in what this means and why it's a good idea, you might want to take a look at episode 12 ("Exploring and understanding parts of @Sisn/cds JS - code & style") where we look at pure functions and partial application in the course of exploring a short JS script.fs
module to start to write the JSON data to output files. When we run this, it causes nodemon
to repeatedly restart, endlessly! This is because we're creating files that it notices, causing it to restart, and recreate the files, causing it to notice those files, restarting, etc. Ad infinitum.nodemon
to ignore files in the data directory, so it looks like this:./node_modules/nodemon/bin/nodemon.js grab.js --ignore data
[object Object]
which is not quite right. This is fixed by adding another process in the dot chain, to stringify the data (the objects) into JSON. Along the way, I write this:.then(x => JSON.stringify(x))
.then(JSON.stringify)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
40 | |
24 | |
16 | |
10 | |
9 | |
8 | |
7 | |
7 | |
7 | |
5 |