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

@contexture/export

v1.1.0

Published

Contexture Exports

Downloads

25

Readme

@contexture/export

Contexture Export is a set of contexture utilities to get all of the records from a given tree.

This is accomplished by inserting a node in the tree that gets the data for you over one or more iterations. To do this correctly and efficiently, the tree is wrapped in an AND group (in case the root was an OR) and recursively marked filterOnly (to prevent getting results for any other nodes on the tree).

Usage

Simple usage

import { results } from '@contexture/export'

let service = Contexture({
  /*...*/
})
let tree = {
  schema: 'someCollection',
  children: [
    /*...*/
  ],
}

let report = results({ tree, service, pageSize: 10 })

// Count results
let totalCount = await report.getTotalRecords()

// Stream
let stream = createWriteStream('someFile.txt')
for await (let record of report) stream.write(record)
stream.end()

// To Array
let array = []
for await (let record of report) array = array.concat(record)

// To array with it-all
import all from 'it-all'
let array = await all(report)

CSV Usage

Using our fast-csv wrapper, you can pass it a write stream, async iterable, and transforms based on contexture schemas

import _ from 'lodash/fp'
import { createWriteStream } from 'fs'
import { results, csv } from '@contexture/export'

let service = Contexture({/*...*/})
let tree = { schema: 'someCollection', children: [/*...*/] }
let schema = { field1: {/*...*/} }

await csv(
  stream: createWriteStream('./test/actualFile.csv'),
  iterableData: results({ service, tree }),
  transform: [
    {key: 'name', label: 'THE,NAME', display: _.capitalize},
    {key: 'value', label: 'Value', display: _.identity},
  ],
)

API

  • nodes: Correspond to contexture nodes

    • results: This strategy extracts the records out of the node with results type. It's not affected by the position of the results node.
      • args:
        • options: object
          • service: (REQUIRED) An async function that will receive a single parameter: the Contexture DSL with the changes required to retrieve only the necessary data for the results strategy.
          • tree: (REQUIRED) The Contexture DSL! It must contain a node with the results type. It doesn't matter where!
          • include: An array with the list of fields that will be included on each retrieved record. This is relevant to the results type. It's undefined by default (which is valid).
          • sortField: Specifies what field will be used to sort the data. This is relevant to the results type. It's undefined by default (which is valid).
          • sortDir: Specifies in which direction the data will be sorted (asc or desc). This is relevant to the results type. It's undefined by default (which is valid).
          • pageSize: It allows you to specify how many records per page (per call of getNext) are returned. It defaults to 100.
          • page: Indicates the starting page of the specified search. Defaults to 1.
      • return:
        • iterableObject
          • getTotalRecords: function, returns the number of total records
    • terms_stats:
      • args:
        • options: object
          • service: (REQUIRED) An async function that will receive a single parameter: the Contexture DSL with the changes required to retrieve only the necessary data for the results strategy.
          • tree: (REQUIRED) The Contexture DSL! It must contain a node with the results type. It doesn't matter where!
          • key_field: The field to calculate stats for
          • size: the number of records to perfom the stats on
  • csv: writes csv data to a stream. The parameter it receives are:

    • args:
      • options: object
        • stream, // writable stream target stream
        • iterableData: an iterable data object where each iteraction yields an object
        • transform: order list of which indicates the header label, display function for the field,and key of the record. [{ key: string, label: string, display: funciton}...]
        • onWrite: function to intercept writing a records, recieves {recordsWriten: int, record: object}
    • return:
      • object
        • cancel: function, stops the writing to the stream
        • promise: resolves when the writing is complete