Ghost/ghost/members-csv
Nazar Gargol b8c1aeee35 Added empty string '' to null transform when parsing CSVs
no issue

- When items are parsed from CSV empty values were interpreted as empty strings - ''. Empty strings are always transformed into 'null' values in Ghost's model layer and are much more problematic to validate comparing to plain `null`. Specifically validation was imossible for 'format: date-time' with JSON schema validation through ajv when the value of date property was an empty string
- This behavior resemples one present in Ghost's model layer  - 95880dddeb
- When testing performance overhead for this change did not spot any statistically significant change in performance (tested set was 50K rows)
2020-08-17 17:57:49 +12:00
..
lib Added empty string '' to null transform when parsing CSVs 2020-08-17 17:57:49 +12:00
test Added empty string '' to null transform when parsing CSVs 2020-08-17 17:57:49 +12:00
.eslintrc.js
index.js Fixed module paths 2020-06-19 19:52:21 +12:00
LICENSE
package.json Published new versions 2020-07-08 20:23:05 +12:00
README.md Added package usage examples to README 2020-07-08 20:09:44 +12:00

Members Csv

Install

npm install @tryghost/members-csv --save

or

yarn add @tryghost/members-csv

Usage

There are 2 parts to this package: CSV to JSON serialization and JSON to CSV serialization. The module exposes 2 methods to fullfil these: parse and unparse respectively.

To parse CSV file and convert it to JSON use parse method, e.g.:

const {parse} = require('@tryghost/members-csv');

const mapping = {
    email: 'csv_column_containing_email_data',
    name: 'csv_column_containing_names_data'
}
const membersJSON = await parse(csvFilePath, mapping);

csvFilePath - is a path to the CSV file that has to be processed mapping - optional parameter, it's a hash describing custom mapping for CSV columns to JSON properties

Example mapping for CSV having email under correo_electronico column would look like following:

{
    email: 'correo_electronico'
}

To unparse JSON to CSV compatible with members format use following:

const {unparse} = require('@tryghost/members-csv');

const members = [{
    email: 'email@example.com',
    name: 'Sam Memberino',
    note: 'Early supporter'
}];

const membersCSV = unparse(members);

console.log(membersCSV);
// -> "id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels\r\n,email@example.com,Sam Memberino,Early supporter,,,,,,"

Develop

This is a mono repository, managed with lerna.

Follow the instructions for the top-level repo.

  1. git clone this repo & cd into it as usual
  2. Run yarn to install top-level dependencies.

Run

  • yarn dev

Test

  • yarn lint run just eslint
  • yarn test run lint and tests

Copyright & License

Copyright (c) 2020 Ghost Foundation - Released under the MIT license.