From 51f44864ebaae9b0e2a021a67881c39dc157ab68 Mon Sep 17 00:00:00 2001 From: Karissa McKelvey Date: Wed, 5 Aug 2015 00:57:06 -0400 Subject: [PATCH] Add compound keys to dat import --- bin/import.js | 11 +++++++---- docs/cli-docs.md | 3 ++- lib/import.js | 14 +++++++++++++- tests/import.js | 19 +++++++++++++++++++ usage/export.txt | 1 + usage/import.txt | 2 ++ 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/bin/import.js b/bin/import.js index 504acc7e..37152cde 100644 --- a/bin/import.js +++ b/bin/import.js @@ -33,6 +33,11 @@ module.exports = { boolean: false, abbr: 'k' }, + { + name: 'keys', + boolean: false, + abbr: 'ks' + }, { name: 'batch', boolean: false, @@ -49,11 +54,9 @@ module.exports = { function handleImport (args) { debug('handleImport', args) - if (args.help || args._.length === 0) { - return usage() - } - + if (args.help || args._.length === 0) return usage() if (!args.dataset) abort(new Error('Error: Must specify dataset (-d)'), args) + if (args.keys) args.keys = args.keys.split(',') openDat(args, function (err, db) { if (err) abort(err, args) diff --git a/docs/cli-docs.md b/docs/cli-docs.md index 2febb394..9a2b8d5b 100644 --- a/docs/cli-docs.md +++ b/docs/cli-docs.md @@ -480,7 +480,8 @@ dat import --dataset= ### Options -- `key`/`k` - specify which field to use as the primary key (false for no key) +- `key`/`k` - specify which column to use as the primary key (defaults to auto-generated keys) +- `keys`/`ks` - comma-separated list of column names to craft a compound key. sorted ascending by default - `message`/`m` - a short description of this import Examples: diff --git a/lib/import.js b/lib/import.js index 653e1f28..f0198d4d 100644 --- a/lib/import.js +++ b/lib/import.js @@ -4,6 +4,8 @@ var through = require('through2') var debug = require('debug')('lib/import') var parseInputStream = require('../lib/util/parse-input-stream.js') +var COMPOUND_KEY_SEPARATOR = '+' + module.exports = function (db, opts) { if (!opts) opts = {} if (!opts.dataset) throw new Error('Error: Must specify dataset (-d)') @@ -11,7 +13,17 @@ module.exports = function (db, opts) { var transform = through.obj(function (obj, enc, next) { debug('heres my obj!', obj) - var key = obj[opts.key] || obj.key || uuid() + + var key + if (opts.keys) { + key = opts.keys.sort().map(function (key) { + return obj[key] || '' + }).join(COMPOUND_KEY_SEPARATOR) + } else { + key = obj[opts.key] || obj.key + } + + if (!key || key === COMPOUND_KEY_SEPARATOR) key = uuid() var doc = {type: 'put', key: key, value: obj} next(null, doc) }) diff --git a/tests/import.js b/tests/import.js index f69836e9..e06a0195 100644 --- a/tests/import.js +++ b/tests/import.js @@ -41,6 +41,25 @@ test('import: dat import json', function (t) { st.end() }) +test('import: dat import json with compound key', function (t) { + var json = path.resolve(__dirname + '/fixtures/all_hour.json') + var st = spawn(t, dat + ' import ' + json + ' --keys=latitude,longitude -d compound', {cwd: dat2}) + st.stdout.empty() + st.stderr.match(/Done importing data/) + st.end() +}) + +test('import: dat keys get integer id', function (t) { + var st = spawn(t, dat + ' keys -d compound', {cwd: dat2}) + st.stdout.match(function (output) { + var keys = output.split('\n') + t.same(keys[0], '33.9233322+-117.9376678') + return true + }) + st.stderr.empty() + st.end() +}) + test('import: dat import json with integer id', function (t) { var json = path.resolve(__dirname + '/fixtures/all_hour.json') var st = spawn(t, dat + ' import ' + json + ' --key=int --dataset=int-id', {cwd: dat2}) diff --git a/usage/export.txt b/usage/export.txt index 59955b26..458863fa 100644 --- a/usage/export.txt +++ b/usage/export.txt @@ -2,4 +2,5 @@ dat export -d (required) # the name of the dataset --limit= # the number of rows to output. default: infinity --format=[ndjson, csv, json] # how to parse the output. default: ndjson + --full # output full metadata with each row, including autogenerated keys diff --git a/usage/import.txt b/usage/import.txt index 7b8537c4..55b64d73 100644 --- a/usage/import.txt +++ b/usage/import.txt @@ -1,3 +1,5 @@ dat import (required) -d (required) # the name of the dataset to import -f [ndjson, csv, json] # how to parse the file. guessed if not supplied. + --key # the primary key to use. defaults to auto-generated keys + --keys # a comma-separated list of column names for a compound key