Skip to content

ryanicle/angular-websql

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Angular WebSql Service

Helps you generate websql simple queries and run them without writing any sql code.

Setup

  1. bower install angular-websql
  2. Include the angular-websql.min.js and angular itself.
  3. Add angular-websql as a module dependency to your app.

Usage

1- Add $webSql provider to a controller.
2- Open a database. See method.
3- Use returned database object's methods.

Methods

Open Database

$webSql.openDatabase(dbName, version, desc, size)

Example:

$scope.db = $webSql.openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024); 

1- Database name
2- Version number
3- Text description
4- Size of database

Returns

An object, containing database operation methods, is returned with openDatabase method. All methods return a promise which takes query result object as parameter. These methods are:

Database Methods

Create Table

createTable(string tableName, object fields)

Example:

createTable('user', {
  "id":{
    "type": "INTEGER",
    "null": "NOT NULL", // default is "NULL" (if not defined)
    "primary": true, // primary
    "auto_increment": true // auto increment
  },
  "created":{
    "type": "TIMESTAMP",
    "null": "NOT NULL",
    "default": "CURRENT_TIMESTAMP" // default value
  },
  "username":{
    "type": "TEXT",
    "null": "NOT NULL"
  },
  "password": {
    "type": "TEXT",
    "null": "NOT NULL"
  },
  "age": {
    "type": "INTEGER"
  }
})

Drop Table

dropTable(string tableName)

Insert

insert(string tableName, object fields)

Example:

$scope.db.insert('user', {"username": 'pc', "password": '1234', 'age': 22}).then(function(results) {
  console.log(results.insertId);
})
INSERT INTO user (username, password, age) VALUES('pc', '1234', 22)

Update

update(string tableName, object fields)

Examples:

$scope.db.update("user", {"username": 'paulo.caldeira'}, {
  'id': 1
})
UPDATE user SET username='paulo.caldeira' WHERE id=1
$scope.db.update("user", {"age": 23}, {
  "username": {
    "operator":'LIKE',
    "value":'paulo.*',
    "union":'AND' // condition suffix
  },
  "age": 22
})
UPDATE user SET age=23 WHERE username LIKE 'paulo.*' AND age=22

Delete

delete(string tableName, object where)

$scope.db.del("user", {"id": 1})
DELETE user WHERE id=1

Select

select(string tableName, object where)

$scope.db.select("user", {
  "age": {
    "value":'IS NULL',
    "union":'AND'
  },
  "username":'IS NOT NULL'
}).then(function(results) {
  $scope.users = [];
  for(i=0; i < results.rows.length; i++){
    $scope.users.push(results.rows.item(i));
  }
})
SELECT * FROM user WHERE age IS NULL AND username IS NOT NULL

Select All

selectAll(string tableName)

$scope.db.selectAll("user").then(function(results) {
  $scope.users = [];
  for(var i=0; i < results.rows.length; i++){
    $scope.users.push(results.rows.item(i));
  }
})
SELECT * FROM user

Operators

Your can use common operators like =, >=, <= and LIKE. You can use also IS NULL and NOT NULL as condition values. Changelog

v1.0.1

  • escape single quote or double quote value(s)
  • changing callback to angular promise

About

Angular WebSql Service - Query less

Resources

License

Stars

Watchers

Forks

Packages

No packages published