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

datamapper

v1.0.5

Published

A unique, easy to use but powerful ORM

Downloads

10

Readme

DataMapper

DataMapper is an ORM with a focus on being easy to use.

DataMapper works Synchronously and is designed for applications where multiple concurrent requests are not important. It will reduce the concurrency of web servers. DataMapper was originally designed for use in an Electron Application where concurrency is not an issue and should not be used on web servers

Currently it only supports MySQL using the mysql package.

This is a Work in progress and currently only supports basic features:

  • Entirely synchronous code
  • Look up records by ID
  • Loop through all records in the table

DataMapper allows you to access any database table as if it was a simple array. No callback hell, no obscure function names, just a simple array interface for all your database tables.

DataMapper does not load the entire database table into memory, it queries the database for just-in-time responses

Usage:


//Load the MySQL module
const MySQL = require('mysql');

//Load the DataMapper module
const DataMapper = require('datamapper');

//Connect to the database and select a database:

var connection = new mysql.createConnection({
	host: '127.0.0.1',
	user: 'youruser',
	password: 'yourpassword',
	database: 'databasename'
});


//Now create a DataMapper instance by passing it a database connection and a table name, and name of the primary key column:

var albums = DataMapper.MySQL(connection, 'albums', 'id');

//Perform a SELECT query using the primary key:
//This will perform SELECT * FROM albums WHERE id = 123 and return the resulting record.
var album = albums[123];

console.log('Selected album title:');
console.log(album.title);

console.log('Selected album artist:');
console.log(album.artist);

console.log('end');

DataMapper is entirely synchronous so each command will run in order, avoiding callback hell and needlessly confusing code.

The code above will output something like:


Selected album title:
Empires
Selected album artist:
VNV Nation
end

You can also use the array directly:


var albums = DataMapper.MySQL(connection, 'albums', 'id');

console.log(albums[123].title);
console.log(albums[123].artist);

DataMapper will cache the record internally so this will only perform a single SELECT query.

Looping through results

You can loop through all the records in the table using a simple for..of loop:


var albums = Mapper.MySQL(connection, 'albums', 'id');

console.log('All albums:');

for (var album of albums) {
	console.log(album.id + ': ' + album.artist + ': ' + album.title);
}

console.log('End of list');

Will output something like

All albums:
123: VNV Nation: Empires
124: Tool: Lateralus
125: Gary Numan: Splinter
126: VNV Nation: Transnational
127: VNV Nation: Automatic
End of list

Searching, limits and offsets

You can search for records using a simple API and the for...of loop:


var albums = Mapper.MySQL(connection, 'albums', 'id');


for (album of album.filter({artist: 'VNV Nation'})) {
	console.log(album.id + ': ' + album.artist + ': ' + album.title);
}

Which will output something like:

123: VNV Nation: Empires
126: VNV Nation: Transnational
127: VNV Nation: Automatic

You can also apply limits and offsets:

var albums = Mapper.MySQL(connection, 'albums', 'id');


for (album of album.filter({artist: 'VNV Nation'}).limit(2).offset(1)) {
	console.log(album.id + ': ' + album.artist + ': ' + album.title);
}

:

126: VNV Nation: Transnational
127: VNV Nation: Automatic

Inserting data

To insert data you can either write to a specific primary key, e.g.

//Issue an INSERT query, automatically setting the primary key
albums[1000] = {name: 'OK Computer', artist: 'Radiohead'};

This will create an INSERT query: INSER INTO albums (id, name, artist) VALUES (1000, 'OK Computer', 'Radiohead').

If there is already a record using the ID 1000, it will issue an update query: UPDATE albums SET artist = 'Radiohead', name = 'OK Computer' WHERE id = 1000

Alternatively you can write to the end of the table (For MySQL the primary key must be auto_increment for SQLite, ROWID is used):


var newAlbum = {artist: 'Covenant', name: 'Northern Light'};
albums.push(newAlbum);

This will also set the primary key on the object:

var newAlbum = {artist: 'Covenant', name: 'Northern Light'};
albums.push(newAlbum);

console.log(newAlbum.id); //The automatically generated ID e.g. `145`