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

active-record

v0.0.9

Published

The active record pattern is an approach to accessing data in a Mysql or LDAP database (Object relational mapping).

Downloads

3

Readme

Intro

This module allows you to access and manipulate data in a Mysql or LDAP database without writing queries. Feel free to create pull requests at https://github.com/efortes/active-record .

You can use it with promise or by callbacks.

Set up

const activeRecord = require('active-record');
const SqlModel = activeRecord.SqlModel;
const LdapModel = activeRecord.LdapModel;

// Set config
activeRecord.setConfig({
  mysql: {
    logQuery: false
  },
  ldap: {
    ldapIgnoreSelfSignedCertificates: true,
    logQuery: false,
    manualLdapQueryTimeOut: 0 // 0 = No manual query timeout || milisec for the manual query timeoiut
  }
});

// Mysql connection See https://www.npmjs.com/package/mysql#pool-options for the mysql pool options
const mysqlConn = activeRecord.createMysqlConn({
    host: null,
    user: null,
    password: null,
    database: null,
    port: null
});

//Ldap connection. See http://ldapjs.org/client.html
const ldapConn = activeRecord.createLdapConn({
    url: 'ldap://url'
})

SQL methods

const ExampleModel = ExampleModel; // Extends SqlModel

// Static methods
ExampleModel.destroyById(id, options);
ExampleModel.update(options);
ExampleModel.destroy(options);
ExampleModel.findById(id, options);
ExampleModel.findAll(options); // Find one or more records
ExampleModel.find(options); // Find single record
ExampleModel.count(options);
ExampleModel.query(options);

// Instance methods
const exampleModel = new ExampleModel({name: 'Nodejs', version: 'v0.9'});
exampleModel.get('name'); // WIll ouput Nodejs
exampleModel.set('version', 'v1.0'); // WIll ouput Nodejs
exampleModel.isValid('version'); // bool
exampleModel.getModified(); // modified fields
exampleModel.setDirty(); // Set fields dirty
exampleModel.isDirty('version'); // check if fields are dirty. If you pass the field it will check only the field
exampleModel.removeDirty(); // Remove dirty
exampleModel.getData(); // WIll ouput {name: 'Nodejs', version: 'v1.0'}
exampleModel.getRawData(); // WIll ouput {name: 'Nodejs', version: 'v1.0'} but without converting etc.
exampleModel.erase(options); // WIll delete the record
exampleModel.getField(fieldName); // Instance of FieldModel
ExampleModel.save(options); // Save instance to DB

LDAP methods

// Static methods
ExampleModel.generateUniqueAttribute(options);  // Generate an unique key for a new record. override this method to gerenate your own unique keys
ExampleModel.destroy(options);
ExampleModel.findById(id, options);
ExampleModel.findByDn(dn, options);
ExampleModel.findAll(options); // Find one or more records
ExampleModel.find(options); // Find single record
ExampleModel.count(options);
ExampleModel.query(options);
ExampleModel.getBaseDn(options);
ExampleModel.getParentDn(options);

// Instance methods
const exampleModel = new ExampleModel({name: 'Nodejs', version: 'v0.9'});
exampleModel.get('name'); // WIll ouput Nodejs
exampleModel.set('version', 'v1.0'); // WIll ouput Nodejs
exampleModel.isValid('version'); // bool
exampleModel.getModified(); // modified fields
exampleModel.setDirty(); // Set fields dirty
exampleModel.isDirty('version'); // check if fields are dirty. If you pass the field it will check only the field
exampleModel.removeDirty(); // Remove dirty
exampleModel.getData(); // WIll ouput {name: 'Nodejs', version: 'v1.0'}
exampleModel.getRawData(); // WIll ouput {name: 'Nodejs', version: 'v1.0'} but without converting etc.
exampleModel.erase(options); // WIll delete the record
exampleModel.getField(fieldName); // Instance of FieldModel
ExampleModel.save(options); // options should always specify a parentDn attr (root lvl to search)

SQL model Example

const activeRecord = require('active-record'); const SqlModel = activeRecord.SqlModel; const LdapModel = activeRecord.LdapModel;

class Service extends activeRecord.SqlModel {

  constructor(data) {
    super(data);

    this.init({
    	fields: [SqlModel.createField({
        	name: "id"
        }), SqlModel.createField({
        	name: "name"
        })],
    	Model: Service,
    	data: data,
		associations: [
           { type: 'hasOne',  Model:Background,      name: 'background' },
           { type: 'hasOne',  Model:Language,        name: 'language' },
           { type: 'hasMany', Model:ServiceComment,  name: 'serviceComments' },
        ]
    });
  }
};

Service.primaryKey = "id";
Service.table = "service";
Service.adapter = "mysql";
Service.connection = mysqlConn;

SQL model - find with associations

TODO

SQL model save example

  const service = new Service{{name: 'Javascript'}};

	service.set('name', 'NodeJS')

	if (service.isDirty()) {
		 // Save service cb
		service.save({
		  callback: (err, result) => {
			if (err) return callback(err);

			callback();
		  }
		});

		 // Save service promise
		service.save().then(service => {

		}).catch(err => {

		});
	}

SQL count

ServiceModel.count({
	include: ['language'],
	callback: function(err, total) {
		if (err) {
			return console.log(err);
		}
		console.log("Total count: " + total);
	}
});

LDAP model Example

class LdapUser extends LdapModel {

	constructor(data) {
	  super(data);

	  var fields = [LdapModel.createField({
	  	name: "dn",
	  }),LdapModel.createField({
	  	name: "cn",
	  }),LdapModel.createField({
	  	name: "name",
	  })];

	  this.init({
	  	fields: [LdapModel.createField({
			name: "dn",
		}),LdapModel.createField({
			name: "cn",
		}),LdapModel.createField({
			name: "name",
		})],
	  	Model: LdapUser,
	  	data: data
	  });
	}

	generateUniqueAttribute(options) {
		return options.callback(null, "1000000");
	}
};

LdapUser.objectClasses = ["user"];
LdapUser.mandatoryAttributes = [];
LdapUser.uniqueAttribute = "ou";
LdapUser.connection = ldapConn;
LdapUser.baseDn = "ou=User,dc=CIDS";

LDAP model save example

var ldapUser = new LdapUser({
	cn: "test" + Math.random(),
	name: "test" + Math.random(),
});

ldapUser.set("name", "test2" + Math.random());

ldapUser.save({
	parentDn: LdapUser.getBaseDn(),
	callback: function(err, newModel) {
		if (err) {
			console.log("- ERROR ------");
			return console.log(err.toString());
		}
		console.log(newModel);
	}
});