Modules@robinpath/database
database

@robinpath/database

0.1.1Node.jsPublic

SQLite database operations for RobinPath automation workflows

Database

SQLite database with query builder, transactions, bulk insert, table management, and backup

Package: @robinpath/database | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the database module when you need to:

  • Open a SQLite database (file or in-memory) -- Use database.open to perform this operation
  • Close a database connection -- Use database.close to perform this operation
  • Run a SELECT query and return all matching rows -- Use database.query to perform this operation
  • Run a SELECT query and return only the first row -- Use database.queryOne to perform this operation
  • Execute an INSERT, UPDATE, DELETE, or DDL statement -- Use database.execute to perform this operation

Quick Reference

FunctionDescriptionReturns
openOpen a SQLite database (file or in-memory){name, path, readonly}
closeClose a database connectionTrue if closed
queryRun a SELECT query and return all matching rowsArray of row objects
queryOneRun a SELECT query and return only the first rowFirst matching row or null
executeExecute an INSERT, UPDATE, DELETE, or DDL statement{changes, lastInsertRowid}
insertInsert a row using an object of column-value pairs{id, changes}
insertManyInsert multiple rows in a single transaction{inserted: number}
updateUpdate rows matching a WHERE clause{changes}
removeDelete rows matching a WHERE clause{changes}
createTableCreate a new table with column definitions{table, columns}
dropTableDrop a table if it existsTrue
listTablesList all tables in the databaseArray of table name strings
tableInfoGet column information for a tableArray of column info objects
countCount rows in a table with optional WHERE conditionsRow count
transactionRun multiple SQL statements in a single atomic transactionArray of results for each statement
backupBackup the database to a file{backed_up: path}

Functions

open

Open a SQLite database (file or in-memory)

Module: database | Returns: object -- {name, path, readonly}

database.open "mydb" "./data.db"
ParameterTypeRequiredDescription
namestringNoConnection name (default: 'default')
pathstringNoFile path or ':memory:' (default)
optionsobjectNo{readonly, fileMustExist}

close

Close a database connection

Module: database | Returns: boolean -- True if closed

database.close "mydb"
ParameterTypeRequiredDescription
namestringYesConnection name

query

Run a SELECT query and return all matching rows

Module: database | Returns: array -- Array of row objects

database.query "mydb" "SELECT * FROM users WHERE age > ?" 18
ParameterTypeRequiredDescription
namestringYesConnection name
sqlstringYesSQL SELECT statement
paramsanyNoBind parameters (positional or named object)

queryOne

Run a SELECT query and return only the first row

Module: database | Returns: object -- First matching row or null

database.queryOne "mydb" "SELECT * FROM users WHERE id = ?" 1
ParameterTypeRequiredDescription
namestringYesConnection name
sqlstringYesSQL SELECT statement
paramsanyNoBind parameters

execute

Execute an INSERT, UPDATE, DELETE, or DDL statement

Module: database | Returns: object -- {changes, lastInsertRowid}

database.execute "mydb" "UPDATE users SET name = ? WHERE id = ?" "Alice" 1
ParameterTypeRequiredDescription
namestringYesConnection name
sqlstringYesSQL statement
paramsanyNoBind parameters

insert

Insert a row using an object of column-value pairs

Module: database | Returns: object -- {id, changes}

database.insert "mydb" "users" {"name": "Alice", "age": 30}
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name
dataobjectYesObject with column names as keys

insertMany

Insert multiple rows in a single transaction

Module: database | Returns: object -- {inserted: number}

database.insertMany "mydb" "users" $rows
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name
rowsarrayYesArray of row objects

update

Update rows matching a WHERE clause

Module: database | Returns: object -- {changes}

database.update "mydb" "users" {"name": "Bob"} {"id": 1}
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name
dataobjectYesColumns to update
whereobjectYesWHERE conditions (AND)

remove

Delete rows matching a WHERE clause

Module: database | Returns: object -- {changes}

database.remove "mydb" "users" {"id": 1}
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name
whereobjectYesWHERE conditions (required for safety)

createTable

Create a new table with column definitions

Module: database | Returns: object -- {table, columns}

database.createTable "mydb" "users" {"id": "INTEGER PRIMARY KEY", "name": "TEXT NOT NULL", "age": "INTEGER"}
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name
columnsobjectYesColumn definitions {name: 'type'}
optionsobjectNo{ifNotExists: boolean}

dropTable

Drop a table if it exists

Module: database | Returns: boolean -- True

database.dropTable "mydb" "users"
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name

listTables

List all tables in the database

Module: database | Returns: array -- Array of table name strings

database.listTables "mydb"
ParameterTypeRequiredDescription
namestringYesConnection name

tableInfo

Get column information for a table

Module: database | Returns: array -- Array of column info objects

database.tableInfo "mydb" "users"
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name

count

Count rows in a table with optional WHERE conditions

Module: database | Returns: number -- Row count

database.count "mydb" "users" {"active": 1}
ParameterTypeRequiredDescription
namestringYesConnection name
tablestringYesTable name
whereobjectNoOptional WHERE conditions

transaction

Run multiple SQL statements in a single atomic transaction

Module: database | Returns: array -- Array of results for each statement

database.transaction "mydb" [{"sql": "INSERT INTO users (name) VALUES (?)", "params": ["Alice"]}, {"sql": "SELECT * FROM users"}]
ParameterTypeRequiredDescription
namestringYesConnection name
statementsarrayYesArray of {sql, params} objects

backup

Backup the database to a file

Module: database | Returns: object -- {backed_up: path}

database.backup "mydb" "./backup.db"
ParameterTypeRequiredDescription
namestringYesConnection name
destPathstringYesDestination file path

Error Handling

All functions throw on failure. Common errors:

ErrorCause
No data to insertCheck the error message for details
No data to updateCheck the error message for details
WHERE clause is required for safetyCheck the error message for details
WHERE clause is required for safety. Use database.execute for raw DELETE.Check the error message for details
Destination path is requiredCheck the error message for details
Database "..." not found. Open it first with database.open.Check the error message for details
@desc "Open and validate result"
do
  set $result as database.open "mydb" "./data.db"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Tables

Retrieve all items and loop through them.

@desc "List tables and iterate results"
do
  set $result as database.listTables "mydb"
  each $item in $result
    print $item
  end
enddo

2. Create a new item with createTable

Create a new resource and capture the result.

set $result as database.createTable "mydb" "users" {"id": "INTEGER PRIMARY KEY", "name": "TEXT NOT NULL", "age": "INTEGER"}
print "Created: " + $result

3. Create and update workflow

Create an item and then update it.

@desc "Create table and update"
do
  set $created as database.createTable "mydb" "users" {"id": "INTEGER PRIMARY KEY", "name": "TEXT NOT NULL", "age": "INTEGER"}
  # Update the created item
  database.update "mydb" "users" {"name": "Bob"} {"id": 1}
enddo

4. Check before creating

List existing items and only create if needed.

@desc "List tables and create table"
do
  set $existing as database.listTables "mydb"
  if $existing == null
    database.createTable "mydb" "users" {"id": "INTEGER PRIMARY KEY", "name": "TEXT NOT NULL", "age": "INTEGER"}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

5. Multi-step Database workflow

Chain multiple database operations together.

@desc "Open, close, and more"
do
  set $r_open as database.open "mydb" "./data.db"
  set $r_close as database.close "mydb"
  set $r_query as database.query "mydb" "SELECT * FROM users WHERE age > ?" 18
  print "All operations complete"
enddo

6. Safe open with validation

Check results before proceeding.

@desc "Open and validate result"
do
  set $result as database.open "mydb" "./data.db"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/database

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size6.8 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities