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

dyncol

v0.2.2

Published

A tool to convert JSON schemas to dynamic column queries (like the ones from MariaDB)

Downloads

16

Readme

Build Status Dependency Status devDependency Status

Dynamic Columns Helper

This library provides functionality to convert a JSON schema into a SQL Query part, called dynamic columns. Currently this is only interesting together with MariaDB.

As MariaDB doesn't support array types, as of the 29th September 2015, we don't handle arrays explicitly and just store them by their index, as the specified key. When extracting arrays back from a database, you going to receive an object instead. You should be easily able to handle this, if you mind this little fact.

MariaDBs dynamic columns are created by COLUMN_CREATE and you can manipulate the created column with the methods like COLUMN_ADD, to add, update and delete columns. This library returns the JSON schema transposed to the matching COLUMN_CREATE(...) SQL query string.

This library gets useless, as soon as MariaDB supports parsing JSON, but until we finally have this feature, this library will help you.

API

Important

We handle numbers by default as double, booleans as unsigned integer and everything else as string. If you need or want to specify the types, you can set the second parameter to call the type defined variant instead.

Also please note currently the typeSpecification is not supported yet.

createQuery: ( json, typeSpecification )

A non recursive way to resolve our json schema to the create query.

Parameters

  • json - Your input schema
  • typeSpecficiation - trigger the type defined version

Example

var dyncol = require('dyncol');

var schema = {
  user: {
    uuid: 122,
    name: 'test'
  },
  activated: true
};

var sql = 'INSERT INTO `user` (`data`) VALUES (' +
  dyncol.createQuery( schema ) + ')';

updateQuery: ( inputColumn, json, typeSpecification )

A non recursive way to resolve our json schema to the update query.

Parameters

  • inputColumn - the column containing the current dynamic column
  • json - Your input schema
  • typeSpecficiation - trigger the type defined version

Example

var dyncol = require('dyncol');

var schema = {
  user: {
    uuid: 122
    name: 'test'
  },
  activated: true
};

var sql = 'UPDATE `user` SET `data` = ' +
  dyncol.updateQuery( 'data', schema ) + ')';

Example of Complex schema

To give you an example of a complex schema and how the result looks like, you can view the following input and result. You will see, especially the update query is quite complex and long, due to the nesting of elements.

JSON create Input

test.createQuery( {
  test: 'test',
  qr: {
    test: 'tester',
    rofl: {
      jaja: 'neinnein',
      testagain: {
        yip: 'datworks'
      }
    }
  },
  another: {
    one: 'yey another one!'
  },
  arrayone: {
    arr: [ 'arr', 'imma pirate', {
      yay: 'ditworks'
    }]
  }
} );

Query create Output

COLUMN_CREATE (
	'test',
	'test',
	'arrayone',
	COLUMN_CREATE (
		'arr',
		COLUMN_CREATE (
			'0',
			'arr',
			'1',
			'imma pirate',
			'2',
			COLUMN_CREATE ('yay', 'ditworks')
		)
	),
	'another',
	COLUMN_CREATE ('one', 'yey another one!'),
	'qr',
	COLUMN_CREATE (
		'test',
		'tester',
		'rofl',
		COLUMN_CREATE (
			'jaja',
			'neinnein',
			'testagain',
			COLUMN_CREATE ('yip', 'datworks')
		)
	)
)

JSON update Input

test.updateQuery( 'example', {
  test: 'test',
  qr: {
    test: 'tester',
    rofl: {
      jaja: 'neinnein',
      testagain: {
        yip: 'datworks'
      }
    }
  },
  another: {
    one: 'yey another one!'
  },
  arrayone: {
    arr: [ 'arr', 'imma pirate', {
      yay: 'ditworks'
    }]
  }
} );

Query update Output

COLUMN_ADD (
	`example`,
	'test',
	'test',
	'arrayone',
	COLUMN_ADD (
		COLUMN_GET (`example`, 'arrayone' AS CHAR),
		'arr',
		COLUMN_ADD (
			COLUMN_GET (
				COLUMN_GET (`example`, 'arrayone' AS CHAR),
				'arr' AS CHAR
			),
			'0',
			'arr',
			'1',
			'imma pirate',
			'2',
			COLUMN_ADD (
				COLUMN_GET (
					COLUMN_GET (
						COLUMN_GET (`example`, 'arrayone' AS CHAR),
						'arr' AS CHAR
					),
					'2' AS CHAR
				),
				'yay',
				'ditworks'
			)
		)
	),
	'another',
	COLUMN_ADD (
		COLUMN_GET (`example`, 'another' AS CHAR),
		'one',
		'yey another one!'
	),
	'qr',
	COLUMN_ADD (
		COLUMN_GET (`example`, 'qr' AS CHAR),
		'test',
		'tester',
		'rofl',
		COLUMN_ADD (
			COLUMN_GET (
				COLUMN_GET (`example`, 'qr' AS CHAR),
				'rofl' AS CHAR
			),
			'jaja',
			'neinnein',
			'testagain',
			COLUMN_ADD (
				COLUMN_GET (
					COLUMN_GET (
						COLUMN_GET (`example`, 'qr' AS CHAR),
						'rofl' AS CHAR
					),
					'testagain' AS CHAR
				),
				'yip',
				'datworks'
			)
		)
	)
)

License

(The MIT License)

Copyright (c) 2015 Tobias Gurtzick

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.