DB

Documentation for DB package

Connect to Database

You can use connect function to connect to database, connect returns an object with these fields:

Connect requires an object as parameter which has these fields

Create Table

To create new table you need to use createTable function, like this:


        import {connect} from '@ulibs/db'

        const {createTable} = connect({client: 'sqlite3', filename: ':memory:'})

        await createTable("users", {
            name: 'string|required',
            email: 'string',
            username: 'string|unique',
            age: 'number',
        })

        //with references

        await createTable("tasks", {
            name: 'string|required',
            user_id: 'number|reference=users'
          })

        // createTable automatically adds 'id' field to all tables.
    

create Model

to create a model you can use this code:


        const {createTable, getModel} = connect({...})

        // createTable(...)


        const Users = getModel('users')

        // then you can use methods of model on User object
    
methods of model are: