Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mysql-activerecord

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mysql-activerecord

  • 0.4.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

MySQL ActiveRecord Adapter for Node.js

Active Record Database Pattern implementation for use with node-mysql (https://github.com/felixge/node-mysql) as MySQL connection driver.

It enables similar database operations as in CodeIgniter (a PHP web applications framework). The main benefit and the reason I worked on this was the ability to direct JavaScript objects straight to MySQL queries without having to worry about constructing the query itself. Although Active Record is maybe a tiny step closer to ORM, I see a lot of value in the Active Record as it allows more control over database queries than traditional ORM.

  • Light-weight
  • Supports all basic MySQL commands
  • Supports method chaining
  • Automatically escapes field values

How to install

npm install mysql-activerecord

Licence info

Copyright (c) 2011 Martin Tajur (martin@tajur.ee) Licensed under the GPL license and MIT:

Basic support of MySQL commands

  • SELECT
  • UPDATE
  • INSERT
  • DELETE
  • JOIN
  • LIMIT and OFFSET
  • ORDER BY

To Do (not supported yet)

  • LIKE, NOT LIKE
  • operators such as !=, >, <, >=, <=
  • WHERE OR
  • etc.

Usage examples

Establishing a connection

var db = new require('mysql-activerecord').Adapter({
	server: 'localhost',
	username: 'root',
	password: '12345',
	database: 'test'
});

Basic SELECT query

db.get('people', function(err, results, fields) {
	console.log(results);
});

INSERT query

var data = {
	name: 'Martin',
	email: 'martin@example.com'
};

db.insert('people', data, function(err, info) {
	console.log('New row ID is ' + info.insertId);
});

SELECT query with WHERE clause

db
	.where({ name: 'Martin' })
	.get('people', function(err, results, fields) {
		console.log(results);
	});

SELECT query with custom fields, WHERE, JOIN and LIMIT

db
	.select(['people.id', 'people.name', 'people.email', 'songs.title'])
	.join('songs', 'people.favorite_song_id', 'left')
	.where({
		'people.name': 'Martin',
		'songs.title': 'Yesterday'
	})
	.limit(5, 10)
	.order_by('people.name asc')
	.get('people', function(err, results, fields) {
		console.log(results);
	});

Basic UPDATE query

var newData = {
	name: 'John',
	email: 'john@doe.com'
};

db
	.where({ id: 1 });
	.update('people', newData, function(err) {
		if (!err) {
			console.log('Updated!');
		}
	});

Basic DELETE query

db
	.where({ id: 1 })
	.delete('people', function(err) {
		if (!err) {
			console.log('Deleted!')
		}
	});

Methods

  • .select(selectFieldName)
  • .select([selectFieldName, selectFieldName, ... ])
  • .where(fieldName, fieldValue)
  • .where({ fieldName: fieldValue, fieldName: fieldValue, ... })
  • .order_by(orderByCondition)
  • .order_by([orderByCondition, orderByCondition, ... ])
  • .join(tableName, joinCondition, joinDirection)
  • .update(tableName, newData, responseCallback)
  • .delete(tableName, responseCallback)
  • .insert(tableName, newData, responseCallback)
  • .get(tableName, responseCallback)
  • .limit(limitNumber)
  • .limit(limitNumber, offsetNumber)
  • .query(sqlQueryString, responseCallback)
  • .ping()
  • ._last_query()

FAQs

Package last updated on 02 Nov 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc