0.21.2 • Published 7 months ago

@fetsorn/csvs-js v0.21.2

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

#+TITLE: csvs-js #+OPTIONS: toc:nil

Create, read, update and delete records in a csvs database, run powerful search queries.

[https://norcivilianlabs.org/csvs]

  • Setup #+begin_src sh

install

npm i @fetsorn/csvs-js

test js functions

yarn test

test wasm functions in the browser with index.html

npx http-server

publish

yarn build npm publish #+end_src

  • Getting started #+begin_src js // functions exported by csvs-js require a callback // that describes how to fetch and write files from a csvs database

function fetchDataMetadir(path) { // fetch file from path }

function writeDataMetadir(path, content) { // write content to file at path }

const callback = { fetch: fetchDataMetadir, write: writeDataMetadir }

// get array of results from query var searchParams = new URLSearchParams() searchParams.set('val1', 'foo') const query1 = await queryMetadir(searchParams, callback, true) console.log(query1) // {"val1": "foo", // "val2": "bar", // "UUID": "b29b9ee3ad01efde7d8694cea4a37844c677ad807b61fa90c44409a21710035c"}

// if object has no uuid // editEntry creates a new record in the database // and returns it let entryBaz = {"val1": "foo", "val2": "baz"} let entryNew1 = await editEntry(entryBaz, callback) console.log(entryNew1) // {"val1": "foo", // "val2": "baz", // "UUID": "33d6e141e92852d5be1930458c1713c2bed09f42d545bf95c0f6d6e271b4077a"}

const query2 = await queryMetadir(searchParams, callback, true) console.log(query2) // {"val1": "foo", // "val2": "bar", // "UUID": "b29b9ee3ad01efde7d8694cea4a37844c677ad807b61fa90c44409a21710035c"}, // {"val1": "foo", // "val2": "baz", // "UUID": "33d6e141e92852d5be1930458c1713c2bed09f42d545bf95c0f6d6e271b4077a"}

// if object has a uuid // editEntry updates the record with matching uuid // and returns it let entryBarbaz = {"val1": "foo", "val2": "barbaz", "UUID": "33d6e141e92852d5be1930458c1713c2bed09f42d545bf95c0f6d6e271b4077a"} let entryNew2 = await editEntry(entryBarbaz, callback) console.log(entryNew) // {"val1": "foo", // "val2": "barbaz", // "UUID": "33d6e141e92852d5be1930458c1713c2bed09f42d545bf95c0f6d6e271b4077a"}

const query3 = await queryMetadir(searchParams, callback, true) console.log(query3) // {"val1": "foo", // "val2": "bar", // "UUID": "b29b9ee3ad01efde7d8694cea4a37844c677ad807b61fa90c44409a21710035c"}, // {"val1": "foo", // "val2": "barbaz", // "UUID": "33d6e141e92852d5be1930458c1713c2bed09f42d545bf95c0f6d6e271b4077a"}

// deleteEntry deletes a record with matching uuid let uuid = "b29b9ee3ad01efde7d8694cea4a37844c677ad807b61fa90c44409a21710035c" await deleteEntry(uuid, callback)

const query4 = await queryMetadir(searchParams, callback, true) console.log(query4) // {"val1": "foo", // "val2": "barbaz", // "UUID": "33d6e141e92852d5be1930458c1713c2bed09f42d545bf95c0f6d6e271b4077a"} #+end_src

[https://github.com/fetsorn/qualia] - Web UI and desktop app

[https://github.com/fetsorn/csvs-sh] - command-line interface

[https://github.com/fetsorn/wasm-grep] - ripgrep compiled to WASM

Getting Started

This documentation describes the library for searching and updating CSVS datasets.

Install from the NPM registry

yarn install @fetsorn/csvs-js

Import the library

import { CSVS } from @fetsorn/csvs-js;

Clone a sample csvs dataset

git clone https://gitlab.com/norcivilian-labs/csvs-template-en

Point the dir variable to the dataset. In NodeJS, that would be the path to a dataset directory.

import fs from 'fs';

const dir = "/path/to/csvs-template-en"

const query = new CSVS({ fs, dir });

And search the records

const records = await query.select(new URLSearchParams());
// [{
//   _: 'datum',
//   UUID: '...',
//   datum: 'value1',
//   actdate: '2001-01-01'
// }]

Next, learn more about csvs integration in the Tutorial and the User Guides.

Tutorial

Let's nodejs query, update, delete

Install from the NPM registry

yarn install @fetsorn/csvs-js

Import the library

import { CSVS } from @fetsorn/csvs-js;

Clone a sample csvs dataset

git clone https://gitlab.com/norcivilian-labs/csvs-template-en

Point the dir variable to the dataset. In NodeJS, that would be the path to a dataset directory.

import fs from 'fs';

const dir = "/path/to/csvs-template-en"

And query the records

const records = await new CSVS({ fs, dir }).select(new URLSearchParams());
// [{
//   _: 'datum',
//   UUID: '...',
//   datum: 'value1',
//   actdate: '2001-01-01'
// }]

To edit a record, change its json and pass it back to csvs

const recordNew = { datum: 'value2', ...records[0] }

await new CSVS({ fs, dir }).update(recordNew);

To add a new record, pass a json object without a UUID to CSVS.update(record). The function will return the new record with a generated UUID.

const recordNew = await new CSVS({ fs, dir }).update({
  _: 'datum',
  datum: 'value2',
  actdate: '2003-03-03'
});

To delete an record, pass it to CSVS.delete(record) and the library will remove record UUID from the dataset.

await new CSVS({ fs, dir }).delete(record);

Learn more about csvs in the User Guides.

CSVS in the Browser

Csvs-lib changes a csvs dataset by passing filepaths to the methods on an FS interface. The simplest way is to import from the fs Node module to interact with local filesystem. The browser is barred from filesystem access, but can emulate it using LightningFS, which stores data in IndexedDB.

See BrowserFS and filer for other examples of emulating a filesystem in the browser.

First, initialize the FS instance. The name 'virtualfs' is used to determine the IndexedDb store name. It is recommended to only create one of these instances for the entire lifetime of the application.

const fs = new LightningFS('virtualfs');

Now, let's populate the filesystem with a dataset by cloning it with isomorphic-git.

const { clone } = await import('isomorphic-git');

const http = await import('isomorphic-git/http/web/index.cjs');

const dir = '/csvs-template-en';

const url = 'https://gitlab.com/norcivilian-labs/csvs-template-en';

await clone({
  fs,
  http,
  dir,
  url
})

At this point there is an IndexedDB store called 'virtualfs' that stores a csvs dataste at '/csvs-template-en'. Let's point the dir variable there.

const dir = "/csvs-template-en"

Now query the dataset

const CSVS = await import('@fetsorn/csvs-js');

const entries = await new CSVS({ fs, dir }).select(new URLSearchParams());
// [{
//   _: 'datum',
//   UUID: '...',
//   datum: 'value1',
//   actdate: '2001-01-01'
// }]
0.21.2

7 months ago

0.21.1

9 months ago

0.21.0

9 months ago

0.20.2

12 months ago

0.20.1

1 year ago

0.20.0

1 year ago

0.19.2

1 year ago

0.19.3

1 year ago

0.19.4

1 year ago

0.19.1

1 year ago

0.18.2

1 year ago

0.18.0

1 year ago

0.17.1

1 year ago

0.17.0

1 year ago

0.16.4

1 year ago

0.16.5

1 year ago

0.16.3

2 years ago

0.16.1

2 years ago

0.16.2

2 years ago

0.16.0

2 years ago

0.13.0

2 years ago

0.12.0

2 years ago

0.14.0

2 years ago

0.11.1

3 years ago

0.11.0

3 years ago

0.10.5

3 years ago

0.10.4

3 years ago

0.9.3

3 years ago

0.10.1

3 years ago

0.10.2

3 years ago

0.10.3

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.9.2

3 years ago

0.7.4

3 years ago

0.9.1

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.6.7

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.3

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.4.8

3 years ago

0.4.7

3 years ago

0.3.8

3 years ago

0.3.9

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.6

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.6

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.3.0

4 years ago

0.3.5

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.8

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.1.20

4 years ago

0.1.21

4 years ago

0.1.10

4 years ago

0.1.22

4 years ago

0.1.11

4 years ago

0.1.23

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.8

4 years ago

0.1.17

4 years ago

0.1.7

4 years ago

0.1.18

4 years ago

0.1.19

4 years ago

0.1.9

4 years ago

0.1.4

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.0

4 years ago