Coder Social home page Coder Social logo

n0n1 / jssql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ricardoalcocer/jssql

0.0 2.0 0.0 6.22 MB

CommonJS wrapper around Ti.Database that makes accessing SQLite Databases feel more like accessing JavaScript objects

License: Other

JavaScript 78.30% CSS 4.24% Python 17.46%

jssql's Introduction

JSSQL

With this library you'll be able to use SQLite databases in your Titanium apps using a JavaScript interface. I personally feel comfortable with SQL Syntax, but prefer to use this library because makes the code more readable.

This is not a replacement for Alloy Models.

If you're looking for a full-blown ORM for Titanium you might want to check out Joli.js.

Usage

// require the module
var DBH=require('com.alcoapps.dbhelper');

// create an instance with your local db
var db=new DBH.dbhelper('/alco.sqlite','alco');

SQL SELECT

Returns a JSON object with the full result set.

Option 1

Returns JSON.

var myTable=db.get({
	fields 	: '*',
	table 	: 'events',
	where 	: 'country like "U%"',
	order 	: 'id DESC'
});

Option 2

Send a callback function .

db.get({
	fields 	: '*',
	table 	: 'events',
	where 	: 'country="US"',
	order 	: 'id DESC'
},function(evt){
	console.log(evt);
});

SQL SELECT with JOIN

Returns JSON.

db.get({
    joiner : 'e'
    fields 	: 'e.*',
    table 	: 'events e, parties p',
    where 	: 'e.country="US" AND p.event_id=e.id',
    order 	: 'eid DESC'
},function(evt){
    console.log(evt);
});

SQL LAST ID

Returns the Id of the last inserted row.

db.getLastId({
	table: 'events'
},function(evt){
     console.log(evt);
});

SQL LAST ID

Returns the last inserted row object.

db.getLastEntry({
    table: 'events'
},function(evt){
     console.log(evt);
});

SQL INSERT

Returns the Id of the last inserted row.

var rowId=db.set({
	table: 'events',
	data : {
		country 	: 'Puerto Rico',
		name 		: 'TiConf PR'
	}
});

SQL UPDATE

Returns the amount of rows affected by the edit.

var rowsAffected=db.update({
	table 	: 'events',
	data 	:{
		name : 'xTiConf NY',
		country : 'PR'
	},
	where 	: 'id = 1'
});

or

var rowsAffected=db.update({
		table 	: 'events',
		data 	:{
			name : 'xTiConf NY',
			country : 'PR'
		},
		id: 1
	});

SQL DELETE

Returns the amount of rows affected by the delete.

var rowsAffected=db.delete({
	table 	: "events",
	where 	: 'name="xTiConf PR"'
});

EXEC

Takes an SQL String and returns a JSON object with the result set

var myTable=db.exec('SELECT * FROM events where id > 5');

GET IMAGE FROM BLOB

Returns an image from a Blob column. In this example, assuming you have a column named 'images' inside a table named 'myimages', grab the image with ID=1

var img=db.getImage({
	field: 'image',
	table: 'myimages',
	where: 'id=1'
});
win.backgroundImage=img;

or

db.getImage({
	field: 'image',
	table: 'myimages',
	where: 'id=1'
},function(img){
	win.backgroundImage=img;
});

GET ENTRY

Used when you only want to get a single value from a database record

var userEmail=db.getEntry({
	table: 	'users',
	id: 	'5'
	field: 	'email'
});

or

db.getEntry({
	table: 	'users',
	id: 	'5'
	field: 	'email'
},function(value){
	console.log(value);
});

COUNT ROWS

Returns the amount of rows returned by a give query

var totalActiveUsers=db.countRows({
	table: 	'users',
	where:	'active=true'
});

or

db.countRows({
	table: 	'users',
	where:	'active=true'
},function(value){
	console.log(value);
});

or with join

db.countRows({
    joiner: 'u',
	table: 	'users u, images i',
	where:	'u.active=true AND u.id = i.image_id'
},function(value){
	console.log(value);
});	

CREATE FROM JSON

Takes a flat JSON string and creates a table. Good for times when you get data from a Web Service and then need to search or manipulate the data in any way.

db.createFromJSON(jsonString,'mytable');

DROP

Deletes a table

db.drop('myTable');

ADD COLUMN

Alter a table if the field doesn't exist

db.addColumn('user', 'street', 'VARCHAR');

TABLE EXISTS

Checks if a given table exists.

var exists=db.tableExists('myTable');

CLOSE

Closes the database connection

db.close();

Contributors

License

MIT - http://alco.mit-license.org

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.