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

dbasefy

v1.0.14

Published

Framework to handler databases SQL OOP

Downloads

57

Readme

DBasefy (Beta)

npm i dbasefy

DBasefy is a framework that encapsulate database libraries. Easy to use, created in TypeScript, serves to facilitate the use of several databases without that you need change a lot of lines of code.

import { SqlConnection } from 'dbasefy/lib/SQL'
import { OracleConnection } from 'dbasefy-oracle/lib' // install separately

const createConnection(provider: string): SqlConnection {
    const connention: SqlConnenction
    switch (provider) {
        case 'mock': 
            connention = new MockConnection()
            break

        case 'oracle': 
            connection = new OracleConnection()
            break

        default:
            throw new Error(`${provider} not implemented yet`)
    }
    return connection
}

async function getDataSample(provide: string): Promise<void> {
    const conn = await createConnection(provide).open()
    try {
        return await conn
            .createQuery({ commandText: 'SELECT...' // SQL statement })
            .execute()
    } finally {
        await conn.close()
    }
}

async function run(environment: string) {
    const provider = 'oracle'
    if (environment === 'test') {
        provider = 'mock'
    }
    console.log(await getDataSample(provider))
}

run('test')

SQL Statements

It is possible to use the library to facilitate the creation of your SQL statements, sample:

SELECT

import { DB } from 'dbasefy/DB'
import { MockConnection } from 'dbasefy/Mock/MockConnection'
import { SelectSqlStatement } from 'dbasefy/SQL/statements'

DB.session(MockConnection, async (conn: MockConnection) => {
    const select = new SelectSqlStatement(conn.createSqlStatementProvider())
    const statement = select
        .field('ID')
        .field('TEXT')
        .from('TEST_TABLE')
        .where.field('ID').equal(10)
        .and.field('TEXT').different('Test')
        .toStatement()
  
    console.log(statement)

    /*
    {
        commandText: 'SELECT ID, TEXT FROM TEST_TABLE WHERE 1=1 AND ID = :FILTER_0_0 AND TEXT != :FILTER_1_0'
        binds: { FILTER_0_0: 10, FILTER_1_0: 'Test' }
    }
    */

    const rows = await conn.createQuery(statement).execute()
})

If you need to create a more complex select statement, you should use string declaration instead a SelectSqlStatement

INSERT

import { DB } from 'dbasefy/DB'
import { MockConnection } from 'dbasefy/Mock/MockConnection'
import { InsertSqlStatement } from 'dbasefy/SQL/statements'

DB.session(MockConnection, async (conn: MockConnection) => {
    const insert = new InsertSqlStatement(conn.createSqlStatementProvider())
    const statement = insert
        .into('TEST_TABLE')
        .value('ID', 10)
        .value('TEXT', 'Test')
        .toStatement()
  
    console.log(statement)

    /*
    { 
        commandText: 'INSERT INTO TEST_TABLE (ID, TEXT) VALUES (:ID, :TEXT)',
        binds: { ID: 10, TEXT: 'Test' } 
    }
     */

    await conn.createCommand(statement).execute()
})

UPDATE

import { DB } from 'dbasefy/DB'
import { MockConnection } from 'dbasefy/Mock/MockConnection'
import { UpdateSqlStatement } from 'dbasefy/SQL/statements'

DB.session(MockConnection, async (conn: MockConnection) => {
    const update = new UpdateSqlStatement(conn.createSqlStatementProvider())
    const statement = update
        .on('TEST_TABLE')
        .set('TEXT', 'Test')
        .where.field('ID').equal(10)
        .toStatement()
  
    console.log(statement)

    /*
    { 
        commandText: 'UPDATE TEST_TABLE SET TEXT = :TEXT WHERE 1=1 AND ID = :FILTER_0_0',
        binds: { TEXT: 'Test', FILTER_0_0: 10 } 
    }
     */

    await conn.createCommand(statement).execute()
})

DELETE

import { DB } from 'dbasefy/DB'
import { MockConnection } from 'dbasefy/Mock/MockConnection'
import { DeleteSqlStatement } from 'dbasefy/SQL/statements'

DB.session(MockConnection, async (conn: MockConnection) => {
    const del = new DeleteSqlStatement(conn.createSqlStatementProvider())
    const statement = del
        .from('TEST_TABLE')
        .where.field('ID').equal(10)
        .toStatement()
  
    console.log(statement)

    /*
    { 
        commandText: 'DELETE FROM TEST_TABLE WHERE 1=1 AND ID = :FILTER_0_0',
        binds: { FILTER_0_0: 10 }
    }
     */

    await conn.createCommand(statement).execute()
})

Tables

You can extends from Table class to create a representation of SQL table and to be able have access the methods like insert, update, delete and select. Sample:

/*TABLE SAMPLE*/
CREATE TABLE TEST (
  ID NUMBER(22) NOT NULL,
  DESCRIPTION VARCHAR2(250) NOT NULL,
  CREATION_DATE DATE NOT NULL,
);
import { Table } from 'dbasefy/lib/SQL'

// typescript representation of SQL table
class TestTable extends Table {
    ID: number
    DESCRIPTION: string
    DATE: Date

    getTableName(): string {
        return 'TEST'
    }
}
import { DB } from 'dbasefy'
import { Table } from 'dbasefy/lib/SQL'

DB.session(MockConnection, async (conn: MockConnection) => {
    const trx = conn.createTransaction()
    try {
        const test = new TestTable(conn.createSqlStatementProvider())
        test.ID = 1
        test.DESCRIPTION = 'Description test'
        test.DATE = new Date()

        await DB.execCommand(conn, test.insert())

        test.DESCRIPTION = 'descrition TEST'

        await DB.execCommand(conn, test.update().where.field('ID').equal(1))

        await trx.commit()
    } catch(err) {

        await trx.rollback()
        throw err

    }
}

Connections

Available connections: