index

Type doc:

Tealsql is a sql library made to be easy and safe to use. Its RAII centric API prevents mistakes like forgetting to close connections. The library also makes prepared statements easy to use as it does the binding of parameters for you.

There are also several helper functions to do basic tasks like deleting, updating or inserting values. These allow you to quickly get something going without the need to write SQL for these basic operation

Lastly, this library is made with teal in mind. This means that a automatically generated .d.tl file is shipped with the dependency. Allowing teal users to always have correct type information about this library.

Further more: This library also has a CLI that acts similar to pgtyped but for teal. This gives teal users the ability to write totally type safe sql queries.

Fields:

Methods:

connect_pool: function(string):( Pool )

Connect to the server and create a connection pool

Params:

  • connection_string:The string used to connect to the server.

Example:

local tealsql = require("pages/tealsql/definitions.tealsql")
local pool = tealsql.connect_pool("postgres://userName:password@host/database")
local res = pool:get_connection(function(con:tealsql.Connection):{string:integer}
    return con:fetch_one("SELECT $1 as test",{2}) as {string:integer}
end)
assert(res.test ==  2)
local tealsql = require("pages/tealsql/definitions.tealsql")
local pool = tealsql.connect_pool("postgres://userName:password@host/database")
local res = pool:get_connection(function(con)
   return con:fetch_one("SELECT $1 as test", { 2 })
end)
assert(res.test == 2)

gen_json: function(boolean):(string)

Returns a json string representing the definitions of this library

This can be used to generate online documentation

Params:

  • pretty: If the json needs to be pretty printed or not

connect: function<Res>(string , function( Connection ):(Res)):(Res)

Connect to the server and create a single connection

Params:

  • connection_string: The string used to connect to the server.

  • func: The function that will be executed after the connection has been made.

This function receives the connection object, which will be cleaned up after the function has been executed.

A value returned from this function will also be returned by the connect function

Example:

local tealsql = require("pages/tealsql/definitions.tealsql")
local res = tealsql.connect("postgres://userName:password@host/database",function(con:tealsql.Connection):{string:integer}
    return con:fetch_one("SELECT $1 as test",{2}) as {string:integer}
end)
assert(res.test ==  2)
local tealsql = require("pages/tealsql/definitions.tealsql")
local res = tealsql.connect("postgres://userName:password@host/database", function(con)
   return con:fetch_one("SELECT $1 as test", { 2 })
end)
assert(res.test == 2)

nul: function():(any)

Returns the value used to represent null values in json.

interval: function(integer , integer , integer):( Interval )

Creates the interval type from postgresql.

Params:

  • months: The amount of months in this interval. Defaults to 0

  • days: The amount of days in this interval. Defaults to 0

  • microseconds: The amount of microseconds in this interval. Defaults to 0

help: function(string):(string)

__index: function(string):(any)

You can index this type with "null" to get the value back that is used to represent null in json.

Globals:

Types:

A connection pool containing at most 10 connections.

Opening a database connection for each and every operation to the database can quickly become expensive.

A connection pool is a standard technique that can manage opening and re-using connections. Normally it also enforces a maximum number of connections as these are an expensive resource on the database server.

A single database connection

Returned from connection:fetch_all_async(). It allows you to do other things while the query is running in a background thread.