cdl-websql
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Web SQL

This is simple Web sql packege which can be used to store information in the form of tables. You can use this packege instead of localStorage or sessionStorage.

Installation instruction

Please follow below steps to utilize functions of indexed db.

Step-1 npm install rxjs --save
Step-2 npm install cdl-websql --save

Instruction to use the package

How to use?

Please follow below instruction to implement web sql in your angular application.

// In your component ts file

import { WebsqlService } from 'cdl-websql';

First way to utilize service

// Extend the service to utilize its functions

export class AppComponent extends WebsqlService {

    constructor() {
        super();
    }

    ngOnInit() {

        // Give web sql Database Name
        this.setDBName('DatabaseName');

        // Create web sql databse
        this.createDB().subscribe( result => {
            console.log('createDB', result);
        });

        // Create table. Here you can create multiple tables under single database
        this.createTable('TableName').subscribe( result => {
            console.log('createTable', result);
        });

        // Insert record to the table. Here you can also insert json string.
        this.insert('TableName', 'Value to insert').subscribe( result => {
            console.log('insert', result);
        });

        // Update record to the table by id.
        // params TableName --> Name of table
        // params id --> Id of which record updated.
        // params value --> value of which record want to update.
        this.update('TableName', id, 'Value to update').subscribe( result => {
            console.log('updateById', result);
        });

        // Delete record from table by id.
        // params TableName --> Name of table
        // params id --> Id of which record delete.
        this.delete('TableName', id).subscribe( result => {
            console.log('deleteById', result);
        });

        // Get record from table by id.
        // params TableName --> Name of table
        // params id --> Id of which record fetch.
        this.getById('TableName', id).subscribe( result => {
            console.log('getAll', result);
        });

        // Get all records from table
        this.getAll('TableName').subscribe( result => {
            console.log('getAll', result);
        });

        // You can write any sql statement to execute.
        // let sql = 'SELECT * FROM TableName where city = "Pune" ';
        // let sql = 'CREATE TABLE USER (userId integer primary key, name text,
                        city text, state text, country text)';
        // let sql = 'INSERT INTO USER (name, city, state, country) VALUES
                     ("Ritesh", "Anand", "Gujarat", "India")'
        // let sql = "DROP TABLE USER"
        this.executeQuery(sql).subscribe( result => {
            console.log('executeQuery ', result);
        });
    }
}

Second way to utilize service

// Initilize the service to the constructor

export class AppComponent {

    constructor(private websqlService: WebsqlService) {
        //TODO:
    }

    ngOnInit() {

        // Give web sql Database Name
        this.websqlService.setDBName('DatabaseName');

        // Create web sql databse
        this.websqlService.createDB().subscribe( result => {
            console.log('createDB', result);
        });

        // Create table. Here you can create multiple tables under single database
        this.websqlService.createTable('TableName').subscribe( result => {
            console.log('createTable', result);
        });

        // Insert record to the table. Here you can also insert json string.
        this.websqlService.insert('TableName', 'Value to insert').subscribe( result => {
            console.log('insert', result);
        });

        // Update record to the table by id.
        // params TableName --> Name of table
        // params id --> Id of which record updated.
        // params value --> value of which record want to update.
        this.websqlService.update('TableName', id, 'Value to update').subscribe( result => {
            console.log('updateById', result);
        });

        // Delete record from table by id.
        // params TableName --> Name of table
        // params id --> Id of which record delete.
        this.websqlService.delete('TableName', id).subscribe( result => {
            console.log('deleteById', result);
        });

        // Get record from table by id.
        // params TableName --> Name of table
        // params id --> Id of which record fetch.
        this.websqlService.getById('TableName', id).subscribe( result => {
            console.log('getAll', result);
        });

        // Get all records from table
        this.websqlService.getAll('TableName').subscribe( result => {
            console.log('getAll', result);
        });

        // You can write any sql statement to execute.
        // let sql = 'SELECT * FROM TableName where city = "Pune" ';
        // let sql = 'CREATE TABLE USER (userId integer primary key, name text,
                        city text, state text, country text)';
        // let sql = 'INSERT INTO USER (name, city, state, country) VALUES
                     ("Ritesh", "Anand", "Gujarat", "India")'
        // let sql = "DROP TABLE doctors"
        this.websqlService.executeQuery(sql).subscribe( result => {
            console.log('executeQuery ', result);
        });
    }
}

Package Sidebar

Install

npm i cdl-websql

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

109 kB

Total Files

23

Last publish

Collaborators

  • rsolanki