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 🙏

© 2026 – Pkg Stats / Ryan Hefner

objs-to-sql

v1.0.1

Published

Takes an array of data and makes a bunch of statements to create a table and insert the data. The commands are targetted at MySQL/MariaDB when targeted commands are necessary, but they're generally compliant with SQL otherwise.

Readme

Objs-to-sql

Takes an array of data and makes a bunch of statements to create a table and insert the data. The commands are targetted at MySQL/MariaDB when targeted commands are necessary, but they're generally compliant with SQL otherwise.

Why

I have coworker who is more comfortable working with data when it's in an SQL database, rather than as JS objects or in MongoDB. This code attempts to create statements that preserve a lot of the underlying types from the JS objects, offering a big increase in queryability over a mechnism like dumping to CSV and importing it.

For example, if the code can detect that the values of a given field are always Date objects, it will make a create table statement with that field as an SQL DATATIME and insert the values as dates. The same is true for INTs and FLOATs and BOOLs

Install

npm install objs-to-sql

Usage

import generateSQL from "objs-to-sql"

let data = [
	{
		id: 1
		, name: 'abc'
		, birthdate: new Date(1738706896000)
		, bio: 'hello \' ` " % $ 	\\ ?  _  abc'
		, married: true
		, height: 6.1
		, info: { msg: 'hello' }
	}
	, {
		id: 2
		, name: 'abc'
		, birthdate: new Date(1738706896000)
		, bio: 'hello \' ` " % $ 	\\ ?  _  abc'
		, married: true
		, height: 6.1
		, info: { msg: 'hello' }
	}

]

let sql = generateSQL('mytablename', data, {useIndividualInserts: false})

As is typical, the test cases show more use of the code.

The parameter useIndividualInserts changes whether the code creates one INSERT statement for every object (the default), or tries to insert all the objects as part of one statement. If set to true, the sql will also include statements to start a new transaction to do the insert. (It's just painfully slow otherwise.)

Fields which contain arrays or complex objects are skipped.

Fields which contain text are analyzed to see what the "real" data type is. For example, if all the values across all objects for a field are all text representations of integer numbers, the generated table will use an INT type for that field.

However, if the numbers fall outside the range of the DB's INT/FLOAT, the column will be converted to TEXT. That is true whether the numbers started out as text strings or instances of the JS Number class.

If any column's text length looks like it will exceed the size of MySQL's TEXT type (about 64k), the column will be a MEDIUMTEXT type (about 16m).