Files
revanced-bots/bots/discord/docs/5_databases.md
PalmDevs 7e5f6481c5 feat(bots/discord)!: read commit description
FEATURES:
- Updated documentation
- Improved configuration format
- Allow filter overriding for each response config (closes #29)
- Improved commands directory structure
- Improved slash command reload script
- New commands
- New command exception handling
2024-06-24 18:23:36 +07:00

1.9 KiB

🫙 Storing data

We use SQLite to store every piece of persistent data. By using Bun, we get access to the bun:sqlite module which allows us to easily do SQLite operations.

🪄 Creating a database

You can easily create a database by initializing the BasicDatabase class:

interface MyDatabase {
    field: string
    key: string
}

const db = new BasicDatabase<MyDatabase>(
    // File path
    'database_file.db',
    // Database schema, in SQL
    `field TEXT NOT NULL, key TEXT PRIMARY KEY NOT NULL`,
    // Custom table name (optional, defaults to 'data'),
    'data'
)

📝 Writing data

Initializing MyDatabase will immediately create/open the database_file.db file. To write data, you can use the insert or update method:

const key = 'my key'
const field = 'some data'

// Order is according to the schema
// db.insert(...columns)
db.insert(field, key)

const field2 = 'some other data'

// db.update(data, filter)
db.update({
    field: field2
}, `key = ${key}`)

You can also delete a row:

db.delete(`key = ${key}`)

console.log(db.select(`key = ${key}`)) // null

👀 Reading data

To get data using a filter, you can use the select method:

// We insert it back
db.insert(field, key)

const data = db.select('*', `key = ${key}`)
console.log(data) // { key: 'my key', field: 'some other data' }

const { key: someKey } = db.select('key', `field = '${field2}'`)
console.log(someKey) // 'my key'

If the existing abstractions aren't enough, you can also use the run, prepare, or query method:

// Enable WAL
db.run('PRAGMA journal_mode=WAL')

const selectFromKey = db.prepare('SELECT * FROM data WHERE key = $key')

console.log(
    selectFromKey.get({
        $key: key
    })
) // { key: 'my key', field: 'some other data' }

console.log(
    selectFromKey.get({
        $key: 'non existent key'
    })
) // null