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

adon-candle

v1.0.18

Published

A simple file to database parser from CSV - JSON - XML - YAML

Downloads

31

Readme

AdOn Candle

A simple file to database parser from CSV - JSON - XML - YAML

Installing

Using npm :

npm install adon-candle

Using yarn :

yarn add adon-candle

Setup

Import ES6 module style :

import Candle from 'adon-candle'

Or CommonJS style :

const Candle = require('adon-candle')

Then provide a configuration object to the class constructor containing your mongoose models (this example use ES6 shorthand object property notation and imports) :

// Example models configuration

import MyModel from 'path/to/models/my-model'
import MyOtherModel from 'path/to/models/my-other-model'

const candle = new Candle({ MyModel, MyOtherModel })

Useage

fileToDatabase(options)

Assuming you already got a mongoose instance connected to MongoDB and your file uploaded somewhere in your application scope, you should first create a database document to reference all lines for further queries :

// Example Database model, using ES6 Import

import mongoose from 'mongoose'

const { Schema } = mongoose
  , { ObjectId } = Schema.Types
  , databaseSchema = new Schema({
    _id: { type: ObjectId, required: true }
    , date: { type: Date, default: Date.now }
    , type: { type: String, required: true }
})

export default mongoose.model('Database', databaseSchema)
// Example database creation
import mongoose from 'mongoose'
import Database from 'path/to/models/Database'

const { ObjectId } = mongoose.Types

new Database({ _id: ObjectId(), type: 'MyModel' }).save()
  .then(database => // You can use database
  .catch(err => // Treat errors)

Then provide a single object to the fileToDatabase function with the following properties :

candle.fileToDatabase({
  file: // The path to your file
  , type: // The type of database model to use (from those provided to the class constructor)
  , database: // A valid mongoose ObjectId for reference, default to null
  , date: // Overwrite the 'date' data field with Date.now() if set to true
  , safe: // Reject the promise if set to true and a line contains errors
  , delimiter: // The CSV delimiter, default to ','
  , root: // The XML root path to lines, default to 'root.line'
})

If set, the property database will be added to each line before matching with your model and must be defined as a reference to your Database model :

import mongoose from 'mongoose'
import Database from 'path/to/models/Database'

const { Schema } = mongoose
  , { ObjectId } = Schema.Types
  , mySchema = new Schema({
    datebase: { type: ObjectId, ref: 'Database' }
    // Other data fields
})

export default mongoose.model('MyModel', mySchema)

linesFromDatabase(options)

To retrieve all lines referencing the same Database instance, simply pass a single config object with database property

candle.linesFromDatabase({
  type: // The type of database model to use (from those provided to the class constructor)
  , database: // A valid mongoose ObjectId 
})
  .then(lines => // Do something with your lines)
  .catch(err => // Treat errors)

removeFromDatabase(options)

This function is called by fileToDatabase() if an error occur on any lines and the safe property is set to true. You can also call it to remove all lines associated to a database :

candle.removeFromDatabase({
  type: // The type of database model to use (from those provided to the class constructor)
  , database: // A valid mongoose ObjectId 
})
  .then(() => // All lines are removed)
  .catch(err => // Treat errors)

Behaviors

The file format is automatically detected by its extension (currently CSV, JSON and XML).

A cleaning operation removing all saved lines in case the safe property is set to true and a line does not match its model is performed.

Coming Soon

  • YAML and XLS format

  • Reverse operation databaseToFile

Dependencies

  • bluebird - A full featured promise library with unmatched performance
  • csvtojson - CSV parser to convert CSV to JSON or column arrays

License

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