Skip to content

database ​

Database connections: SQLite, MySQL, PostgreSQL, Redis and MongoDB.

Import

ts
import { DbError, DbErrorCode, DbTls, DbExecResult, ... } from "database";

Needs the "database" permission in thicket.json.

DbError class ​

ts
class DbError extends Error

A database operation or connection failure.

DbError.code ​

ts
readonly code: DbErrorCode

Broad, portable classification of the failure.

DbError.driver ​

ts
readonly driver: DatabaseDriver

Driver that reported the failure.

DbError.serverCode ​

ts
readonly serverCode: int | string | null

Driver/server-specific code when one is available; otherwise null.

DbErrorCode enum ​

ts
enum DbErrorCode

Portable database-error classifications.

  • DbErrorCode.DUPLICATE_KEY
  • DbErrorCode.NETWORK
  • DbErrorCode.TIMEOUT
  • DbErrorCode.AUTH
  • DbErrorCode.WRITE_CONCERN
  • DbErrorCode.SERVER
  • DbErrorCode.CONFIG

DbTls enum ​

ts
enum DbTls

TLS policy for server database connections.

  • DbTls.OFF: Plaintext; the default for localhost and Unix sockets.
  • DbTls.REQUIRE: Encrypt the connection without validating the server certificate.
  • DbTls.VERIFY: Encrypt and validate the certificate chain and hostname.

DbExecResult class ​

ts
class DbExecResult

DbExecResult.insertId ​

ts
insertId: int

The ID of a newly inserted row (driver-dependent).

DbExecResult.rowsChanged ​

ts
rowsChanged: int

The number of rows affected by an INSERT, UPDATE, or DELETE.

DbCursor class ​

ts
class DbCursor<T>

DbCursor.fetch ​

ts
fetch(n?: int): Promise<T[]>

Fetches up to n rows from the cursor.

Parameters

  • n: Maximum number of rows to fetch; default fetches all remaining rows.

Returns: A Promise resolving to an array of rows.

DbCursor.hasMore ​

ts
hasMore: boolean

True if more rows are available.

DbCursor.close ​

ts
close(): Promise<void>

Closes the cursor and frees resources.

Returns: A Promise that resolves when the cursor is closed.

ObjectId class ​

ts
class ObjectId

BSON ObjectId. Construct one or parse its 24-character hex form.

new ObjectId() ​

ts
constructor()

Constructs a new ObjectId.

ObjectId.parse ​

ts
static parse(hex: string): ObjectId

Parses a 24-character hexadecimal string into an ObjectId.

Parameters

  • hex: A 24-character hex string.

Returns: An ObjectId.

ObjectId.toString ​

ts
toString(): string

Converts the ObjectId to its 24-character hexadecimal string representation.

Returns: The hex string.

MongoConnection class ​

ts
class MongoConnection

MongoConnection.collection ​

ts
collection<T = any>(name: string): MongoCollection<T>

Gets or creates a collection by name.

Parameters

  • name: The collection name.

Returns: A MongoCollection for the given name.

MongoConnection.close ​

ts
close(): Promise<void>

Closes the database connection.

Returns: A Promise that resolves when the connection is closed.

MongoConnection.ping ​

ts
ping(): Promise<boolean>

Tests the connection to the MongoDB server.

Returns: A Promise resolving to true if the connection is active.

MongoCollection class ​

ts
class MongoCollection<T>

MongoCollection.insertOne ​

ts
insertOne(document: T): Promise<ObjectId>

Inserts a single document.

Parameters

  • document: The document to insert.

Returns: A Promise resolving to the inserted document's ObjectId.

MongoCollection.insertMany ​

ts
insertMany(documents: T[]): Promise<ObjectId[]>

Inserts multiple documents.

Parameters

  • documents: An array of documents to insert.

Returns: A Promise resolving to an array of inserted ObjectIds.

MongoCollection.findOne ​

ts
findOne(filter?: any, options?: any): Promise<T | null>

Finds at most one document matching the filter.

Parameters

  • filter: Optional filter query.
  • options: Optional query options.

Returns: A Promise resolving to the found document or null.

MongoCollection.find ​

ts
find(filter?: any, options?: any): Promise<DbCursor<T>>

Finds all documents matching the filter.

Parameters

  • filter: Optional filter query.
  • options: Optional query options.

Returns: A Promise resolving to a DbCursor over the results.

MongoCollection.updateOne ​

ts
updateOne(filter: any, update: any): Promise<any>

Updates the first document matching the filter.

Parameters

  • filter: The filter query.
  • update: The update operation.

Returns: A Promise resolving to the update result.

MongoCollection.updateMany ​

ts
updateMany(filter: any, update: any): Promise<any>

Updates all documents matching the filter.

Parameters

  • filter: The filter query.
  • update: The update operation.

Returns: A Promise resolving to the update result.

MongoCollection.replaceOne ​

ts
replaceOne(filter: any, replacement: T): Promise<any>

Replaces the first document matching the filter.

Parameters

  • filter: The filter query.
  • replacement: The replacement document.

Returns: A Promise resolving to the replacement result.

MongoCollection.deleteOne ​

ts
deleteOne(filter: any): Promise<any>

Deletes the first document matching the filter.

Parameters

  • filter: The filter query.

Returns: A Promise resolving to the deletion result.

MongoCollection.deleteMany ​

ts
deleteMany(filter: any): Promise<any>

Deletes all documents matching the filter.

Parameters

  • filter: The filter query.

Returns: A Promise resolving to the deletion result.

MongoCollection.countDocuments ​

ts
countDocuments(filter?: any): Promise<int | float>

Counts documents matching the filter.

Parameters

  • filter: Optional filter query.

Returns: A Promise resolving to the count.

MongoCollection.createIndex ​

ts
createIndex(keys: any, options?: any): Promise<string>

Creates an index on the collection.

Parameters

  • keys: An object specifying the indexed fields.
  • options: Optional index options.

Returns: A Promise resolving to the index name.

MongoCollection.dropIndex ​

ts
dropIndex(name: string): Promise<void>

Drops an index by name.

Parameters

  • name: The name of the index to drop.

Returns: A Promise that resolves when the index is dropped.

MongoCollection.aggregate ​

ts
aggregate(pipeline: any[]): Promise<DbCursor<any>>

Executes an aggregation pipeline.

Parameters

  • pipeline: An array of aggregation stages.

Returns: A Promise resolving to a DbCursor over the results.

DbConnection class ​

ts
class DbConnection

DbConnection.begin ​

ts
begin(): any

Begins a transaction.

Returns: A transaction object, or undefined if already in a transaction.

DbConnection.close ​

ts
close(): Promise<void>

Closes the database connection.

Returns: A Promise that resolves when the connection is closed.

DbConnection.commit ​

ts
commit(): any

Commits the active transaction.

Returns: A Promise that resolves when the transaction is committed.

DbConnection.execute ​

ts
execute(sql: string, params?: any): any

Executes a statement without returning rows.

Parameters

  • sql: The SQL statement.
  • params: Optional query parameters.

Returns: A Promise resolving to a DbExecResult.

DbConnection.ping ​

ts
ping(): any

Tests the connection to the database server.

Returns: A Promise resolving to true if the connection is active.

DbConnection.query ​

ts
query(sql: string, params?: any): any

Executes a query and returns all rows.

Parameters

  • sql: The SQL query.
  • params: Optional query parameters.

Returns: A Promise resolving to an array of rows.

DbConnection.queryCursor ​

ts
queryCursor(sql: string, params?: any): Promise<DbCursor<any>>

Executes a query and returns a cursor over the results.

Parameters

  • sql: The SQL query.
  • params: Optional query parameters.

Returns: A Promise resolving to a DbCursor.

DbConnection.queryRow ​

ts
queryRow(sql: string, params?: any): any

Executes a query and returns the first row.

Parameters

  • sql: The SQL query.
  • params: Optional query parameters.

Returns: A Promise resolving to the first row object, or null.

DbConnection.queryScalar ​

ts
queryScalar(sql: string, params?: any): any

Executes a query and returns a single scalar value.

Parameters

  • sql: The SQL query.
  • params: Optional query parameters.

Returns: A Promise resolving to the scalar value.

DbConnection.rollback ​

ts
rollback(): any

Rolls back the active transaction.

Returns: A Promise that resolves when the transaction is rolled back.

RedisConnection class ​

ts
class RedisConnection

RedisConnection.close ​

ts
close(): any

Closes the Redis connection.

Returns: A Promise that resolves when the connection is closed.

RedisConnection.decr ​

ts
decr(key: string): any

Decrements a numeric counter by 1.

Parameters

  • key: The counter key.

Returns: A Promise resolving to the new value.

RedisConnection.decrby ​

ts
decrby(key: string, amount: int): any

Decrements a numeric counter by the given amount.

Parameters

  • key: The counter key.
  • amount: The decrement amount.

Returns: A Promise resolving to the new value.

RedisConnection.del ​

ts
del(...keys: string[]): any

Deletes one or more keys.

Parameters

  • keys: The keys to delete.

Returns: A Promise resolving to the number of keys deleted.

RedisConnection.exists ​

ts
exists(key: string): any

Checks if a key exists.

Parameters

  • key: The key to check.

Returns: A Promise resolving to 1 if the key exists, 0 otherwise.

RedisConnection.expire ​

ts
expire(key: string, seconds: int): any

Sets a key expiration timeout.

Parameters

  • key: The key.
  • seconds: Time to live in seconds.

Returns: A Promise resolving to 1 if the timeout was set.

RedisConnection.get ​

ts
get(key: string): any

Gets the value of a string key.

Parameters

  • key: The key.

Returns: A Promise resolving to the string value, or null if not found.

RedisConnection.hdel ​

ts
hdel(key: string, field: string): any

Removes a field from a hash.

Parameters

  • key: The hash key.
  • field: The field name.

Returns: A Promise resolving to the number of fields removed.

RedisConnection.hexists ​

ts
hexists(key: string, field: string): any

Checks if a field exists in a hash.

Parameters

  • key: The hash key.
  • field: The field name.

Returns: A Promise resolving to 1 if the field exists, 0 otherwise.

RedisConnection.hget ​

ts
hget(key: string, field: string): any

Gets a field value from a hash.

Parameters

  • key: The hash key.
  • field: The field name.

Returns: A Promise resolving to the field value, or null if not found.

RedisConnection.hgetall ​

ts
hgetall(key: string): any

Gets all fields and values from a hash.

Parameters

  • key: The hash key.

Returns: A Promise resolving to an object of field-value pairs.

RedisConnection.hset ​

ts
hset(key: string, field: string, value: string): any

Sets a field value in a hash.

Parameters

  • key: The hash key.
  • field: The field name.
  • value: The field value.

Returns: A Promise resolving to the number of fields added.

RedisConnection.incr ​

ts
incr(key: string): any

Increments a numeric counter by 1.

Parameters

  • key: The counter key.

Returns: A Promise resolving to the new value.

RedisConnection.incrby ​

ts
incrby(key: string, amount: int): any

Increments a numeric counter by the given amount.

Parameters

  • key: The counter key.
  • amount: The increment amount.

Returns: A Promise resolving to the new value.

RedisConnection.llen ​

ts
llen(key: string): any

Gets the length of a list.

Parameters

  • key: The list key.

Returns: A Promise resolving to the list length.

RedisConnection.lpop ​

ts
lpop(key: string): any

Removes and returns the first element of a list.

Parameters

  • key: The list key.

Returns: A Promise resolving to the removed value, or null if the list is empty.

RedisConnection.lpush ​

ts
lpush(key: string, value: string): any

Pushes a value to the front of a list.

Parameters

  • key: The list key.
  • value: The value to push.

Returns: A Promise resolving to the new list length.

RedisConnection.lrange ​

ts
lrange(key: string, start: int, stop: int): any

Gets a range of elements from a list.

Parameters

  • key: The list key.
  • start: The start index (inclusive).
  • stop: The stop index (inclusive).

Returns: A Promise resolving to an array of list elements.

RedisConnection.ping ​

ts
ping(): any

Tests the connection to the Redis server.

Returns: A Promise resolving to "PONG" if the connection is active.

RedisConnection.publish ​

ts
publish(channel: string, message: string): any

Publishes a message to a channel.

Parameters

  • channel: The channel name.
  • message: The message text.

Returns: A Promise resolving to the number of subscribers that received the message.

RedisConnection.rpop ​

ts
rpop(key: string): any

Removes and returns the last element of a list.

Parameters

  • key: The list key.

Returns: A Promise resolving to the removed value, or null if the list is empty.

RedisConnection.rpush ​

ts
rpush(key: string, value: string): any

Pushes a value to the back of a list.

Parameters

  • key: The list key.
  • value: The value to push.

Returns: A Promise resolving to the new list length.

RedisConnection.sadd ​

ts
sadd(key: string, ...members: string[]): any

Adds members to a set.

Parameters

  • key: The set key.
  • members: The members to add.

Returns: A Promise resolving to the number of members added.

RedisConnection.scard ​

ts
scard(key: string): any

Gets the cardinality (size) of a set.

Parameters

  • key: The set key.

Returns: A Promise resolving to the set size.

RedisConnection.set ​

ts
set(key: string, value: string, opts?: any): any

Sets a string value with optional options.

Parameters

  • key: The key.
  • value: The string value.
  • opts: Optional set options.

Returns: A Promise resolving to "OK" on success.

RedisConnection.sismember ​

ts
sismember(key: string, member: string): any

Checks if a member is in a set.

Parameters

  • key: The set key.
  • member: The member.

Returns: A Promise resolving to 1 if the member exists, 0 otherwise.

RedisConnection.smembers ​

ts
smembers(key: string): any

Gets all members of a set.

Parameters

  • key: The set key.

Returns: A Promise resolving to an array of set members.

RedisConnection.srem ​

ts
srem(key: string, member: string): any

Removes a member from a set.

Parameters

  • key: The set key.
  • member: The member to remove.

Returns: A Promise resolving to the number of members removed.

RedisConnection.subscribe ​

ts
subscribe(channel: string, callback: any): any

Subscribes to a channel for messages.

Parameters

  • channel: The channel name.
  • callback: A function called with each message.

Returns: A subscription object that can be unsubscribed.

RedisConnection.ttl ​

ts
ttl(key: string): any

Gets the remaining time to live for a key in seconds.

Parameters

  • key: The key.

Returns: A Promise resolving to the TTL, or -1 if the key has no expiration.

RedisConnection.unsubscribe ​

ts
unsubscribe(channel: string): any

Unsubscribes from a channel.

Parameters

  • channel: The channel name.

Returns: A Promise that resolves when the unsubscription is complete.

Database static class ​

ts
static class Database

Database.getConnection ​

ts
static getConnection(name: string): any

Gets a registered database connection by name.

Parameters

  • name: The connection name.

Returns: A MongoConnection, DbConnection, or RedisConnection depending on the driver.

Database.registerConnection ​

ts
static registerConnection(name: string, driver: DatabaseDriver, config: any): any

Registers a new database connection.

Parameters

  • name: The connection name for later retrieval.
  • driver: The database driver (SQLITE, MYSQL, POSTGRES, MONGO, REDIS).
  • config: Driver-specific configuration object.

Returns: A Promise that resolves when the connection is established.

Example

ts
import { Database, DatabaseDriver } from "database";
await Database.registerConnection("main", DatabaseDriver.SQLITE, { path: "app.db" });
const db: any = Database.getConnection("main");

DatabaseDriver enum ​

ts
enum DatabaseDriver
  • DatabaseDriver.SQLITE
  • DatabaseDriver.MYSQL: Included in Linux release builds.
  • DatabaseDriver.REDIS
  • DatabaseDriver.POSTGRES: Included in Linux release builds.
  • DatabaseDriver.MONGO: Included only when TKS_ENABLE_DB and HEARTWOOD_ENABLE_MONGO are enabled; not included in release builds.

Generated from lang/types/database.d.tks in the ThicketScript repository.