Giter Club home page Giter Club logo

angular-websql's Introduction

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.
angular.module('yourModule', ['angular-websql']);

Usage

1- Add $webSql provider to a controller.

angular.module('yourModule', ['angular-websql'])
  .controller('yourController', ['$scope','$webSql', function($scope, $webSql){
        . . .
  }]);;

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"
  }
})

Create or Alter Table

createOrAlterTable(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"
  },
  "newAddedField": {
    "type": "TEXT"
  }
})

Drop Table

dropTable(string tableName)

Insert

insert(string tableName, object fields, boolean replace)

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)

Bulk insert

bulkInsert(string tableName, [object fields], boolean replace)

Example:

$scope.db.insert('user', [
	{"username": 'pc1', "password": '1234', 'age': 22},
	{"username": 'pc2', "password": '5678', 'age': 23},
	{"username": 'pc3', "password": '9101', 'age': 24},
	{"username": 'pc4', "password": '1213', 'age': 25},
]).then(function(results) {
  console.log(results.insertId);
})
INSERT INTO user (username, password, age) VALUES('pc1', '1234', 22)
INSERT INTO user (username, password, age) VALUES('pc2', '5678', 23)
INSERT INTO user (username, password, age) VALUES('pc3', '9101', 24)
INSERT INTO user (username, password, age) VALUES('pc4', '1213', 25)

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

del(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 limit

selectLimit(string table, object where, int limit)

$scope.db.selectLimit("user", {
  "age": {
    "value":'IS NULL',
    "union":'AND'
  },
  "username":'IS NOT NULL'
}, 10).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 LIMIT 10

Select All

selectAll(string tableName, [{operator:"string",postOperator:"string optionnal",columns["string column","string column"]}] Array/Object)

$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
$scope.db.selectAll("user", [{operator:"GROUP BY",columns:['age','username']}]).then(function(results) {
  $scope.users = [];
  for(var i=0; i < results.rows.length; i++){
    $scope.users.push(results.rows.item(i));
  }
})
SELECT * FROM user GROUP BY age, username
$scope.db.selectAll("user", [
				{operator:"GROUP BY",columns:['age']},
				{operator:"ORDER BY",postOperator:'DESC',columns:['username']},
			    ])
.then(function(results) {
  $scope.users = [];
  for(var i=0; i < results.rows.length; i++){
    $scope.users.push(results.rows.item(i));
  }
})
SELECT * FROM user GROUP BY age ORDER BY username DESC

Select All limit

selectAllLimit(string tableName, int limit)

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

Select One

selectOne(string tableName)

$scope.db.selectOne("user")
SELECT * FROM user LIMIT 1

Operators

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

Contributors

Thanks to github community, our libraries do not depend only from our work but also from work of contributors. I want to thank all those who in any way participated in the development of this library.

Special thanks to these contributors:

  • @gfauchart
  • @dbtek

Changelog

v1.0.2

  • prevent empty operator in where clause
  • insert method update with replace flag to "INSERT OR REPLACE" queries

v1.0.1

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

angular-websql's People

Contributors

atwright147 avatar coleim avatar coliva avatar dbtek avatar gfauchart avatar jaapjanfrans avatar jorge-ivan avatar paulocaldeira17 avatar vanarman avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jorge-ivan

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.