Giter Club home page Giter Club logo

fluent-migrations-mariadb's Introduction

Migrations Fluent MariaDB

npm version Build Status Coverage Status Greenkeeper badge dependencies Status devDependencies Status

A simple and chained MariaDB database installation and migrations API.

Usage

Sample: Initial database / application creation

This module makes it also simple for you to handle database installations. The following sample code demonstrates this:

// 000.initial-db-setup.ts
import { SqlScript, Database, Table, TableColumnChar, TableColumnCustom, emitType, ConnectionConfig, MariaDbError } from 'migrations-fluent-mariadb';
import { LOGGER } from '../../logger';

const conf: ConnectionConfig;
const VERSION: number = 0;

return new SqlScript(conf, VERSION)
    .attachLogger(emitType.ALL, (m) => LOGGER.debug(m)) // Plug in your own logger callback
    .useDatabase(
        new Database('sample-database'),
    )
    .createTable(
        new Table('config')
            .addColumn(
                new TableColumnChar('key', 24)
                    .notNull(),
            )
            .addColumn(
                new TableColumnChar('value', 255)
                    .nullable(),
            )
            .definePrimary('key'),
    )
    // TODO: Remove mitigation and add possibility to add table rows
    .addRawSql(`INSERT INTO \`sample-database\`.\`config\` (\`key\`, \`value\`)
    VALUES ('schemaVersion', '${VERSION}');`)
    .createTable(
        new Table('list-items')
            .addColumn(new TableColumnChar('id', 255).notNull())
            .addColumn(new TableColumnChar('name', 255).nullable().setDefaultValue(null))
            // TODO: Create timestamp type
            .addColumn(new TableColumnCustom('createdOn', 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'))
            .addColumn(new TableColumnCustom(
                'lastUpdatedOn',
                'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
            .definePrimary('id'),
    )
    .execute(false) // increaseVersion is set to false, as we already inserted the version into the config table.
    .catch((e: MariaDbError) => {
        LOGGER.debug(`Error: ${JSON.stringify(e)}`);
        if (e.code === 'ECONNREFUSED') { LOGGER.error(`Connection to database has been refused.\n${e.stack}`); }
    });

Sample: Database migrations

TODO: Create an example for this passage.

Repo Maintenance

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.