Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dbdts.db

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dbdts.db

Easy to use wrapper for sqlite databases, mainly designed for use with dbd.ts package.

  • 3.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
2
Weekly downloads
 
Created
Source

dbdts.db

Documentation can be found here

NPM Downloads NPM Version Discord Server

Example

This is a small example of how you would create a database instance and a table with 2 columns with a primary column and a json column, connecting database and doing some operations with it.

const { Database, Column, Table } = require("dbdts.db")

const db = new Database({ path: "./db.sqlite" })

const table = db.createTable("mytable").addColumns([
    new Column()
    .setName("id")
    .setPrimary(true)
    .setType("TEXT"),
    new Column()
    .setName("json")
    .setType("JSON")
    .setDefault({})
])

db.once("ready", () => {
    console.log(`Database is ready!`)

    const changes = table.set({
        id: "12345",
        json: {
            swords: 1,
            hammers: 0
        }
    })

    console.log(changes)

    const results = table.get({
        where: {
            column: "id",
            equals: "12345"
        }
    })

    console.log(results)

    const deletion = table.delete({
        where: {
            column: "id",
            equals: "12345"
        }
    })

    console.log(deletion)

    const all = table.all()

    console.log(all)
})

db.connect()

Creating an API Database:

const { APIDatabase, Column } = require("dbdts.db")

//You will need to install axios yourself.
const axios = require("axios")

const db = new APIDatabase({
    port: 3444,
    password: "youshallnotpass"
})

const table = db.createTable("mytable").addColumns([
    new Column()
    .setName("id")
    .setPrimary(true)
    .setType("TEXT"),
    new Column()
    .setName("json")
    .setType("JSON")
    .setDefault({})
])

db.open(async () => {
    console.log(`Database ready!`)

    await axios.post(`${db.url}/set`, {
        table: "mytable",
        data: {
            id: "12345",
            json: {
                swords: 1,
                hammers: 0
            }
        },
        options: {
            where: {
                column: "id",
                equals: "12345"
            }
        }
    }, {
        headers: {
            Authorization: db.options.password
        }
    })

    const request = await axios.post(`${db.url}/get`, {
        table: "mytable",
        options: {
            where: {
                column: "id",
                equals: "12345"
            }
        }
    }, {
        headers: {
            Authorization: db.options.password
        }
    })

    console.log(request.data)
})

Setting expiring time to data

That's right! With the release of v3.5.0, we've finally added the ability to add a expiring duration to data in the database.
Here's a small example:

... // Assuming you got a working database setup of dbdts.db and the variable is defined as db.

db.on('expire', (table, data) => {
    console.log(`Data`, data, 'expired in table', table)
})

// Set some data as usual to db.
db.set("sometable", {
    id: 'someid',
    data: {},
    value: 10
}, {
    where: { column: "id", equals: "someid" }
})

// The above data will be deleted from the database after 25000ms.
db.setTimedData('sometable', 'someid', 25000)

And that's it! Make sure to adapt the example to what you got in your project and remember the id is attached to the table's primary key.

Keywords

FAQs

Package last updated on 27 Oct 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc