npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

denmark-dawa-replicate

v1.0.0

Published

Replicate the DAWA database.

Downloads

3

Readme

#denmark-dawa-replicate Build Status

Replicate the DAWA database.

Installation

npm install denmark-dawa-replicate

Example

This module just provided the necessary logic for fetching the data. The actual insert, update and delete logic is a bit more complicated, a full postgresql example exists inside the example/ directory.

Documentation

const replicate = require('denmark-dawa-replicate')

DAWA (Danmarks Adressers Web API) is a service provided by the danish government, which exposes multiply APIs for getting address related information. The service supports replication of its tables, this module helps getting that data and keeping it up to date.

First create a new DAWAReplicate object.

const replicator = new DAWAReplicate(version);

If there is no data in the database, set version to 0 or just don't specify it. The replicator object is a Readable object stream, where each item has the following structure.

replicator.on('data', function (item) {
	item = {
		table: String // A tableName, e.g. postnumre,
		operation: String // Can be [insert, update, delete],
		data: Object // Object containing all properties for each table row
	}
});

To get all possible table names and associated information, see the denmark-dawa-schema module.

Using a parallel writeable stream

Since there is a lot of data, it is recommended to implement a Writeable stream that supports parallel writes. This can be done by implementing the Writeable._writev method, that takes a chunk of items.

function InsertStream() {
  stream.Writable.call(this, {
		objectMode: true,
		highWaterMark: 16 // Controls the max amount of parallel writes
	});
  this.db = new DBAbstraction();
}
util.inherits(InsertStream, stream.Writable);

InsertStream.prototype._write = function (item, encoding, callback) {
  this.db[item.operation](item.table, item.data, callback);
};

InsertStream.prototype._writev = function (chunks, callback) {
  const self = this;
  async.each(chunks, function (item, done) {
    self._write(item.chunk, item.encoding, done);
  }, callback);
};

You then just pipe the replicator as usual.

replicator.pipe(new InsertStream());

replicator.update([version], callback)

Updates the current version to version. If no version is specified the module will fetch the latest version for you. After the new version has been determined, all new data (events) from DAWA will be piped to the replicator object.

If a newer version exists a new-version event will be emitted with the new version number. As each table starts being fetched the update-table will emit, with the corresponding table name.

The callback is called when all data has been fetched. Before this you are not allowed to execute .update again.

// Standard synchronization pattern
(function recursive() {
	replicate.update(function (err, newVersion) {
		if (err) throw err;
		console.log('updated to version ' + newVersion);
		setTimeout(recursive, 1000 * 60 * 60 * 24);
	});
})();

replicator.getLatestVersion(callback)

In case you want to be more in control, this method just fetches the latest version number, without any side effects.

replicator.getLatestVersion(function (err, remoteVersion) {
	if (err) throw err;

	console.log('remote now has version ' + remoteVersion);
});

Source

DAWA documents the replication protocol at http://dawa.aws.dk/replikeringdok.