npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ionic-database-builder

v0.6.1

Published

Extended library from database-builder to assist in creating and maintaining SQL commands. Allowing integrate execute commands with SQLite ('@ionic-native/sqlite'), Web Sql, etc. Through the interface injection 'DatabaseCreatorContract' returning an imple

Downloads

126

Readme

npm version contributions welcome

ionic-database-builder

Ionic module for database-builder library. Allowing integrate execute commands with SQLite ('@ionic-native/sqlite'), Web Sql, etc. Through the interface injection 'DatabaseCreatorContract' returning an implementation of 'DatabaseObject'.

Getting Started

Step 1: Install npm module

npm install --save ionic-database-builder 

This will install the current stable version of ionic-database-builder in your node_modules directory and save the entry in package.json.

Step 1.1: If it will be used with SQLite plugin install:

Install as instructed.

Step 2: Add Module in App and Settings

Simple Setup

import { DatabaseModule, DatabaseSettingsFactoryDefault, MappersTableSimple } from 'ionic-database-builder';
import { DatabaseHelper } from 'database-builder';

@NgModule({
    ...
    imports: [
        DatabaseModule.forRoot(
            {// settings database: name, version and mapper
                useValue: // object to simple settings database
                new DatabaseSettingsFactoryDefault(
                    1, // version database 
                    "database1", // name database
                    // mapper for database
                    new MappersTableSimple(new DatabaseHelper(), {
                    references: false, // if "true" generate column for serialize object reference to JSON.
                    // Example in "TestClazz", create column "testClazzRef" to serialize "TestClazzRef" object
                    referencesId: true, // if "true" generate column for id reference.
                    // Example in "TestClazz", create column "testClazzRef_id" to save "TestClazzRef" property "id"
                    referencesIdRecursive: false, // if "true" generate column for id reference recursive for all references inner.
                    referencesIdColumn: "id" // name id column references
                    })
                    .mapper(
                        false, // readonly
                        void 0, // keyColumn: default "id"
                        void 0, // default settings constructor
                        // Type models for mapper
                        TestClazz,
                        TestClazzRef
                    ))
            },
            {// is available database in context
                // As SQLite is only available on the platform cordova this is used to verify this parameter
                // useFactory: (platform: Platform) => {
                //   return platform.is("cordova");
                // },
                // deps: [Platform],
                // or simply can pass true without conditions
                useValue: true
            },
            {
                // Declare the implementation of 'DatabaseCreatorContract' that you want to use, you can include a proxy, use libraries with different signatures, or create mocks for tests, etc.
                useClass: SQLite
            },
            {
                // Enable log SQL execute
                useValue: false
            },
            // implementation of "DatabaseMigrationContract" to estrategy migration upgrade versions database
            DatabaseMigrationService
            ),
        ...
    ],
    ...
})
export class AppModule { }

DatabaseMigrationService

import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { SQLiteTransaction } from '@ionic-native/sqlite';
import { Ddl } from 'database-builder';
import { DatabaseMigrationContract, Database, MappersTableBase } from 'ionic-database-builder';
import { Version } from 'ionic-database-builder/src/model/version-model';

@Injectable()
export class DatabaseMigrationService extends DatabaseMigrationContract {

    // implemented of "DatabaseMigrationContract"
    public to(version: Version, transation: SQLiteTransaction, mappers: MappersTableBase): Observable<any>[] {
        let observablesNested: Observable<any>[] = [];

        if (version.oldVersion < 2.0) {
            observablesNested.push(this.migration_v2_0(transation, version, mappers));
        }

        return observablesNested;
    }

    private migration_v2_0(transation: SQLiteTransaction, version: Version, mappers: MappersTableBase): Observable<any> {
        let observablesWait: Observable<any>[] = [];

        let ddl = new Ddl(transation, mappers, true);

        // drop tables deprecated
        observablesWait.push(Observable.fromPromise(ddl.drop(OldModel).execute()));
        
        // create new tables
        observablesWait.push(Observable.fromPromise(ddl.create(TestClazzRef).execute()));

        return Observable.forkJoin(observablesWait);
    }
}

Advanced Setup

import { DatabaseModule } from 'ionic-database-builder';

@NgModule({
    ...
    imports: [
        DatabaseModule.forRoot(
            {// settings database: name, version and mapper
                useClass: DatabaseSettingsFactory
            },
            {// is available database in context
                // As SQLite is only available on the platform cordova this is used to verify this parameter
                useFactory: (platform: Platform) => {
                return platform.is("cordova");
                },
                deps: [Platform],
                // // or simply can pass true without conditions
                // useValue: true
            },
            {
                // Declare the implementation of 'DatabaseCreatorContract' that you want to use, you can include a proxy, use libraries with different signatures, or create mocks for tests, etc.
                useClass: SQLite
            },
            {
                // Enable log SQL execute
                useValue: false
            },
            // implementation of "DatabaseMigrationContract" to estrategy migration upgrade versions database
            DatabaseMigrationService
            ),
        ...
    ],
    ...
})
export class AppModule { }

DatabaseSettingsFactory

import { EnvironmentService } from './../providers/environment-service';
import { Injector } from '@angular/core';
import { DatabaseSettingsFactoryContract, MappersTableBase } from "ionic-database-builder";
import { MappersTable } from './mappers-table';

export class DatabaseSettingsFactory extends DatabaseSettingsFactoryContract {

    databaseName(injector: Injector): string {
        let environmentService: EnvironmentService = injector.get(EnvironmentService);
        return `database_${environmentService.isProdution ? 'prod' : 'test'}`;
    }

    version(injector: Injector): number {
        return 2.0;
    }

    mapper(injector: Injector): MappersTableBase {
        return injector.get(MappersTable);
    }

}

MappersTable

import { MappersTableSimple, DatabaseHelperService } from "ionic-database-builder";
import { Injectable } from "@angular/core";

@Injectable()
export class MappersTable extends MappersTableSimple {

    constructor(_databaseHelper: DatabaseHelperService) {
        super(
            _databaseHelper,
            {
                references: false,
                referencesId: true,
                referencesIdRecursive: false,
                referencesIdColumn: void 0
            }
        );

        this.mapper(false, void 0, this._defaultSettings,
            // Type models for mapper
            TestClazz,
            TestClazzRef
        );

        this.add(TestClazzAdvanced, false, void 0, {
            references: false,
            referencesId: false,
            referencesIdRecursive: false
        }, metadata => {
            metadata
                // add column reference1_id
                .mapper(x => x.reference1.id)
                // add column reference1_anything
                .mapper(x => x.reference1.anything);
        });
    }
}

Step 3: Use Database in Components

DatabaseModule provides the injection of Database in its components and services, as can be seen in the following example:

MyApp

import { Database } from 'ionic-database-builder';
import { Component } from '@angular/core';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  constructor(
    // inject "Database"
    database: Database
  ) {
    database.query(TestClazz).then(query => {
      query
        .select(x => x.description)
        .where(where => where.equal(x => x.id, 1));
      console.log(query.compile());
      /**
       * {
       *  params: [1],
       *  query: "SELECT tes.description AS description FROM TestClazz AS tes WHERE tes.id > ?"
       * }
       */
      // to execute in database return promise with result
      query.toList();
    });
  }
}

More documentation on database-builder (Query, Crud, etc).