🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

@msml/angular-migrations

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@msml/angular-migrations

A package to manage migrations within an Angular application. Angular migrations makes it easier to create local tables and manage their structure. It includes a schema builder to create, alter or drop tables.

latest
npmnpm
Version
0.4.1
Version published
Maintainers
2
Created
Source

Angular migrations

A package to manage migrations within an Angular application. Angular migrations makes it easier to create local tables and manage their structure. It includes a schema builder to create, alter or drop tables.

Installation

Note: this requires angular ^6.1.0

Add the package via your favorite package manager.

yarn add @msml/angular-migrations

npm i @msml/angular-migrations

Create a migration.

// ./create-users-table.migration.ts

import { Schema, Migration } from '@msml/angular-migrations';

export class CreateUsersTable extends Migration {
    public name = 'create_users_table';

    up(): Array<Promise<any>> {
        return [
            Schema.create('users', (b) => {
                b.column('id', 'integer', ['PRIMARY KEY', 'AUTOINCREMENT']);
                b.column('name', 'string');
                b.column('email', 'string');
            }).execute(this.sql)
        ];
    }

    down(): Array<Promise<any>> {
        return [];
    }
}

Add the migration module to your main AppModule.

// app.module.ts

import { MigrationModule, Migration } from '@msml/angular-migrations';

// Import the migration you just created
import { CreateUsersTable } from './create-users-table.migration'

// Create a new factory method that creates your migrations
export function myMigrationFactory(): Migration[] {
    return [
        // Add the migration to the factory
        new CreateUsersTable()
    ];
}

@NgModule({
    imports: [
        //...

        // Insert the module in your root module
        MigrationModule.forRoot(myMigrationFactory),

        //...
    ]
})
export class AppModule {}

Create an SQL service that implements the SQLService (this example only works in browsers).

import { SQLService } from '@msml/angular-migrations';

const win: any = window;

export class BrowserSQLiteService implements SQLService {

    protected db;

    constructor() {
        this.db = win.openDatabase(
            '@msml/angular-migrations-test',
            '1.0',
            'database',
            5 * 1024 * 1024
        );
    }

    public query(query: string, bindings: any[]): Promise<{ [key: string]: any }[]> {
        return new Promise((resolve, reject) => {
            this.execute(query, bindings).then(response => {
                const rows: { [key: string]: any }[] = [];

                for (let i = 0; i < response.res.rows.length; i++) {
                    rows.push(response.res.rows.item(i));
                }

                resolve(rows);
            }, response => {
                reject({
                    query: {
                        statement: query,
                        bindings: bindings
                    },
                    err: response.err
                });
            });
        });
    }

    protected execute(query, bindings): Promise<{
        tx: any,
        res: any
    }> {
        return new Promise((resolve, reject) => {
            try {
                // Generate a new transaction
                this.db.transaction(
                    (tx: any) => {
                        // Execute the sql and handle exceptions when it fails
                        tx.executeSql(
                            query,
                            bindings,
                            (tx: any, res: any) => resolve({tx: tx, res: res}),
                            (tx: any, err: any) => reject({tx: tx, err: err})
                        );
                    },
                    (err: any) => {
                        reject({err: err});
                    }
                );
            } catch (err) {
                reject({err: err});
            }
        });
    }
}

Configure

Nesting

When certain migrations should be executed one after another, you can add them in the constructor as follows:

new CreateUsersTable([
    new AddCreatedAtToUsersTable([
        new removeCreatedAtFromUsersTable(),
    ]),
    new AddActiveToUsersTable(),
])

Child modules

You can add migrations in child modules using the forChild method provided with a factory. These migrations will only be executed when the child module is included within the app module.

// ./modules/fiend/app.module.ts

import { MigrationModule, Migration } from '@msml/angular-migrations';

// Import the migration you just created
import { CreateFriendsTable } from './create-friends-table.migration'

// Create a new factory method that creates your migrations
export function myMigrationFactory(): Migration[] {
    return [
        // Add the migration to the factory
        new CreateFriendsTable()
    ];
}

@NgModule({
    imports: [
        //...

        // Insert the module in your root module
        MigrationModule.forChild(myMigrationsFactory),

        //...
    ]
})
export class ChildModule {}

Keywords

ionic

FAQs

Package last updated on 29 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts