Skip to content

datopian/ckan-client-js

Repository files navigation

CKAN Client: Javascript SDK

contributions welcome build The MIT License

CKAN SDK for interacting with CKAN instances with CKAN v3 style cloud storage. It covers the whole action API as well as convenience methods for uploading files, updating the metastore etc. Tutorial on use at https://tech.datopian.com/ckan-client-guide/. API documentation below.

Frictionless Formats

The client uses Frictionless formats by default for describing dataset and resource objects passed to client methods. Internally we then use the CKAN<=>Frictionless Mapper to convert to CKAN formats before calling the API. Thus, you can use Frictionless Formats by default with the client. (As CKAN moves to Frictionless to default this will gradually become unnecessary).

Install

Node

Prerequisites

Then clone the repo via git:

$ npm install https://github.com/datopian/ckan-client-js.git

or

$ yarn add https://github.com/datopian/ckan-client-js.git

Now you can use the code:

const { Client } = require('ckanClient')
...

Browser

If you want to use it as a script in the browser, then follow the steps below:

$ git clone git@github.com:datopian/ckan-client-js.git
$ cd ckan-client-js
$ npm install
$ npm run build

The last command will create /dist/index.js bundle which can be used in the browser like:

<head>
  <script src="./ckan-client-js/dist/index.js"></script>
  <script>
    // Global ckanClient variable is available here...
    const { Client } = ckanClient;
    ...
  </script>
</head>
<body></body>

Examples

Add a resource to a dataset and save

// pushing some resource to the dataset
dataset.resources.push({
  bytes: 12,
  path: 'https://somecsvonline.com/somecsv.csv',
})

// then saving it, this will return a new dataset with updated fields
const updatedDataset = await client.push(dataset)
console.log(updatedDataset)

Upload resource data and save resource

// to open a file and give a frictionless resource with stream method
// this uses the frictionless js library https://github.com/datopian/data.js
const f11s = require('data.js')

// If it is in Browser you can pass an attached file
const resource = f11s.open('path/to/example.csv')

await client.pushBlob(resource)
// If you are in browser you can also track the progress, in the second argument
// client.pushBlob(resource, (progressEvent) => {
//   let progress = (progressEvent.loaded / progressEvent.total) * 100
//   console.log(progress)
// })

// create a dataset, add resource metadata and save
const dataset = await client.create({
  name: 'dataset-name',
})
dataset.resources.push(resource.descriptor)

const updatedDataset = await client.push(dataset)

API

See API documentation here

Developers

Build

This command will create a bundle in dist

npm run build

Tests

$ npm test

or

$ yarn test

watch the test

$ npm run test:watch

Documentation

We use JSDoc to document the methods we are creating. Those JSDoc comments are being used to generate API docs for README via jsdoc2md.

To generate API markdown docs from JSDoc comments run:

$ npx jsdoc2md example.js

Where example.js is the file where you have implemented new methods. Paste the output in API docs. Make sure you paste only your changes.

Contributing

Please make sure to read the CONTRIBUTING.md Guide before making a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details