Coder Social home page Coder Social logo

oursql's Introduction

--Introdiuction--

OurSql generates live database objects that respond similarly to the ActiveRecord pattern, however it does not support model creation, table generation, etc. As a result, OurSql is geared primarily toward those who are working with pre-existing MySql databases, or those who are willing to create tables from scratch. The only requirement is that every table initialized by OurSql must have an auto-increment, unique field named 'id'.

To install OurSql, it is recommended that you install NPM and run the command:

$ npm install oursql

--Model Creation--

Import OurSql, connect to the database, and create models. Models are based upon pre-existing mysql tables in the given mysql database client. You don't need to define properties and types for any pre-existing columns in each table, however you can define relationships between tables by adding methods:

var	OurSql = require('oursql'),
sqloptions = {user:'dbuser',password:'dbpassword',database:'blog',host:'localhost'};

OurSql.connect(sqloptions, function(){

	// SYNTAX: Object = new OurSql.Model(TableName,mysqlclient)
	
	User = new OurSql.Model('Users',mysqlclient);
	Entry = new OurSql.Model('Entries',mysqlclient);
	Tag = new OurSql.Model('Tags',mysqlclient);
	
	User.addMethod('getEntries',function(callback){
		return Entry.findWhere({userId:this.id},callback);
	});
	User.addMethod('getLastFiveEntries',function(callback){
		return Entry.retrieve({where:{userId:this.id},orderBy:'dateline DESC',limit:5},callback);
	});

});	

--Model Usage--

Now, you can use your object models to perform CRUD operations. You can either pass an object of key-value pairs when you create the instance, or you can create a blank instance and set values thereafter:

// Create a brand new User

	var jack = new User.Instance({name:'Jack'});	
	jack.age = 25;
	jack.girlfriend = 'Jill';

// Since jack is an object you just created without first retrieving it from the DB, 
// the save() operation will create a new database entry. 

	jack.save();	

// Now, jack will have an ID

	jack.id   // returns int 5 

// Later, you'll want to retrieve jack from the database.

User.find(5,function(oneUser){
	
	jack = oneUser;
	jack.id   // returns int 5
	jack.girlfriend     // returns str 'Jill'
	jack.getLastFiveEntries(function(entries){
		
		// entries is an array of Entry Instances
		
		entries[0].title = 'NEW TITLE';
		entries[0].save();
		
	});
	
	jack.delete(); //supports callback to determine success or failure
	
})

oursql's People

Contributors

objectundefined avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

oursql's Issues

Error on require

/node_modules/oursql/lib/OurSql.js:96
                };
                 ^
SyntaxError: Unexpected token ;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.