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

my-object

v0.0.12

Published

JSON serialization to schema-less MySQL. myObject lets you store, load, and search hierarchical data, represented by JSON objects, in a MySQL table. Conceptually, you can consider the data stored in myObject as one, gigantic, JSON object.

Downloads

14

Readme

myObject

JSON serialization to schema-less MySQL

myObject lets you store, load, and search hierarchical data, represented by JSON objects, in a MySQL table. Conceptually, you can consider the data stored in myObject as one, gigantic, JSON object.

myObject is not an ORM tool: it stores a hierarchical representation of JSON objects

myObject doesn't map your data to a specific table structure (schema). Instead, each key/value is stored along with a reference to its parent, allowing any structure to be stored and recreated, and allowing somewhat efficient searches across the key/value pairs. The project is used by us with Node.js and can be installed with npm install my-object, but the schema and stored procedures may prove useful to any language or environment that can use MySQL.

Quick Examples

myo.store('britons.peasant', { name: 'Dennis', old: false, age: 37 }, function(err) {
	if (err) throw err;
}); // store

myo.load('britons.peasant', function(err, obj) {
	if (err) throw err;
    console.log(obj.name); // Dennis
    console.log(obj.old); // false
    console.log(obj.age); // 37
}); // load

myo.load('britons.peasant.age', function(err, obj) {
	if (err) throw err;
    console.log(obj); // 37
}); // load

myo.search('name', 'Dennis', function(err, matchingKeys) {
	if (err) throw err;
    // matchingKeys is an array of matching fully qualified key paths
    console.log(matchingKeys[0]); // britons.peasant.name
}); // search

That's just about it! store will store the JSON representation (e.g.: no functions and undefined values aren't recorded), and load will retrieve it, or any key in the hierarchy. All keys can be searched, but presently only strings and numbers will be matched (NaN, Infinity, -Infinity, true, and false values maybe in a future update?).

Download

The source is on GitHub. Alternatively, you can install using Node Package Manager (npm):

npm install my-object

Setup MySQL

You'll need to create the table and the stored procedures in you MySQL database before you can do anything else. Running the script schema.sql will create the table, stored procedures, as well as the root object.

WARNING! The first line of this file is drop table if exists okeys; this will destroy any prior data or anything else that was in that table.

Documentation

When you require my-object, it returns function that, when called with database connection info, returns an object with the rest of the functions closed over a connection pool. In other words, you have to initialize the require call with the connection info.

Arguments

  • connectionInfo - An object containing the connection info. The values are passed directly to mysql. The minimum required values are:
    • host
    • user
    • password
    • database

The supportBigNumbers option is required, and will be set true regardless of what you pass in.

  • The following values are set by default (and were optimal in my use case) but may be overridden:
    • connectionLimit: 60
    • connectTimeout: 60000
    • acquireTimeout: 60000

Example

var myo = require('my-object')({
		host: env('TEST_HOST'),
		user: env('TEST_USER'),
		password: env('TEST_PASSWORD'),
		database: env('TEST_DATABASE'),
		port: process.env['TEST_PORT'] || 3306,
	}); // myo

Store the value, and all child objects, in the database at the given key location.

Arguments

  • key - The fully qualified path of where to store the value.
  • value - The value, which can be any valid JSON datatype (string, number, boolean, etc.), to store.
  • callback(err) - A required function that is called when the store completes. If an error has occured err will be truthy and contain details. If err is falsey you can assume success.

Example

myo.store('britons.peasant', { name: 'Dennis', old: false, age: 37 }, function(err) {
	if (err) throw err;
}); // store

Loads object specified by the given key from the database, including all child objects.

Arguments

  • key - The fully qualified path of where to begin contstructing the object
  • callback(err, obj) - A required function that is called when the load completes. If an error has occured err will be truthy and contain details. If err is falsey you can assume success. obj will be the complete object stored at the key or undefined if there was no object at that location.

Examples


// this key specifies an object, so the object, and all its properties, will be returned

myo.load('britons.peasant', function(err, obj) {
	if (err) throw err;
    console.log(obj.name); // Dennis
    console.log(obj.old); // false
    console.log(obj.age); // 37
}); // load

// this key specifies a single value, so only that value is returned

myo.load('britons.peasant.age', function(err, obj) {
	if (err) throw err;
    console.log(obj); // 37
}); // load

Searches the database for all objects with properties named name that equal the value. The callback will will contain an array of fully qualified keys of matching objects.

Arguments

  • name - The non-qualified property name to test.
  • value - The value to match against. Presently, this must be either a string or numeric number (i.e.: not NaN, Infinity, or -Infinity).
  • callback(err, matchingKeys) - A required function that is called when the search completes. If an error has occured err will be truthy and contain details. If err is falsey you can assume success. matchingKeys is an array containing the fully qualified key names of all objects that had a matching property. Construct your object property names accordingly.

Example

myo.search('name', 'Dennis', function(err, matchingKeys) {
	if (err) throw err;
    // matchingKeys is an array of matching fully qualified key paths
    console.log(matchingKeys[0]); // britons.peasant.name
}); // search

Ends the underlying connection pool to MySQL, closing all connections. Used mainly for testing so Node will close.

Arguments

  • callback(err) - An optional function that is called when the pool has closed all connections. If an error has occured err will be truthy and contain details. If err is falsey you can assume success.

Example

myo.end(function(err) {
	if (err) throw err;
    console.log('myObject connection pool closed');
}); // search