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 ErrorA database operation or connection failure.
DbError.code
ts
readonly code: DbErrorCodeBroad, portable classification of the failure.
DbError.driver
ts
readonly driver: DatabaseDriverDriver that reported the failure.
DbError.serverCode
ts
readonly serverCode: int | string | nullDriver/server-specific code when one is available; otherwise null.
DbErrorCode enum
ts
enum DbErrorCodePortable database-error classifications.
DbErrorCode.DUPLICATE_KEYDbErrorCode.NETWORKDbErrorCode.TIMEOUTDbErrorCode.AUTHDbErrorCode.WRITE_CONCERNDbErrorCode.SERVERDbErrorCode.CONFIG
DbTls enum
ts
enum DbTlsTLS 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 DbExecResultDbExecResult.insertId
ts
insertId: intThe ID of a newly inserted row (driver-dependent).
DbExecResult.rowsChanged
ts
rowsChanged: intThe 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: booleanTrue 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 ObjectIdBSON 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): ObjectIdParses a 24-character hexadecimal string into an ObjectId.
Parameters
hex: A 24-character hex string.
Returns: An ObjectId.
ObjectId.toString
ts
toString(): stringConverts the ObjectId to its 24-character hexadecimal string representation.
Returns: The hex string.
MongoConnection class
ts
class MongoConnectionMongoConnection.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 DbConnectionDbConnection.begin
ts
begin(): anyBegins 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(): anyCommits the active transaction.
Returns: A Promise that resolves when the transaction is committed.
DbConnection.execute
ts
execute(sql: string, params?: any): anyExecutes 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(): anyTests 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): anyExecutes 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): anyExecutes 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): anyExecutes 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(): anyRolls back the active transaction.
Returns: A Promise that resolves when the transaction is rolled back.
RedisConnection class
ts
class RedisConnectionRedisConnection.close
ts
close(): anyCloses the Redis connection.
Returns: A Promise that resolves when the connection is closed.
RedisConnection.decr
ts
decr(key: string): anyDecrements 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): anyDecrements 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[]): anyDeletes 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): anyChecks 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): anySets 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): anyGets 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): anyRemoves 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): anyChecks 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): anyGets 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): anyGets 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): anySets 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): anyIncrements 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): anyIncrements 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): anyGets the length of a list.
Parameters
key: The list key.
Returns: A Promise resolving to the list length.
RedisConnection.lpop
ts
lpop(key: string): anyRemoves 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): anyPushes 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): anyGets 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(): anyTests 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): anyPublishes 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): anyRemoves 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): anyPushes 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[]): anyAdds 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): anyGets 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): anySets 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): anyChecks 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): anyGets 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): anyRemoves 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): anySubscribes 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): anyGets 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): anyUnsubscribes from a channel.
Parameters
channel: The channel name.
Returns: A Promise that resolves when the unsubscription is complete.
Database static class
ts
static class DatabaseDatabase.getConnection
ts
static getConnection(name: string): anyGets 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): anyRegisters 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 DatabaseDriverDatabaseDriver.SQLITEDatabaseDriver.MYSQL: Included in Linux release builds.DatabaseDriver.REDISDatabaseDriver.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.