While each Modus SDK offers similar capabilities, the APIs and usage may vary between languages.Modus PostgreSQL APIs documentation is available on the following pages:
The Modus PostgreSQL APIs allow you to run queries against PostgreSQL or any
PostgreSQL-compatible database platform.
To begin, import the postgresql
package from the SDK:
import "github.com/hypermodeinc/modus/sdk/go/pkg/postgresql"
PostgreSQL APIs
The APIs in the postgresql
package are below, organized by category.
Weβre constantly introducing new APIs through ongoing development with early
users. Please open an issue if
you have ideas on what would make Modus even more powerful for your next app!
Functions
Execute
Execute a SQL statement against a PostgreSQL database, without any data
returned. Use this for insert, update, or delete operations, or for other SQL
statements that donβt return data.
The Execute
function is for operations that donβt return data. However, some
insert/update/delete operations may still return data. In these cases, you can
use the QueryScalar
or Query
functions instead.
func Execute(connection, statement string, params ...any) (*db.Response, error)
SQL statement containing the query or mutation operation to execute.While itβs possible to directly include parameter values into your SQL
statement, itβs highly recommended to pass parameters as arguments instead.
This can help to protect injection attacks and other security vulnerabilities.
Optional parameters to include with the query.
Execute a SQL statement against a PostgreSQL database, returning a set of rows.
In the results, each row converts to an object of type T
, with fields matching
the column names.
func Query[T any](connection, statement string, params ...any) (*db.QueryResponse[T], error)
Type of object to use for the data returned from the query. This can be any
type, including a custom type defined in your project. It should match the shape
of the row returned from the SQL query.Define custom types in the projectβs source code. All types must be JSON
serializable. You can also use built-in types such as strings, numbers, slices,
and maps.If working with PostgreSQLβs point
data type, you can use a Point
or Location
object to represent the data.
SQL statement containing the query or mutation operation to execute.While itβs possible to directly include parameter values into your SQL
statement, itβs highly recommended to pass parameters as arguments instead.
This can help to protect injection attacks and other security vulnerabilities.
Optional parameters to include with the query.
QueryScalar
Execute a SQL statement against a PostgreSQL database, returning a single scalar
value. For example, the result could be a count, sum, or average, or it could be
an identifier.
func QueryScalar[T any](connection, statement string, params ...any) (*db.ScalarResponse[T], error)
Type of object to use for the data returned from the query. This should
generally be a scalar data type, such as a number or string. It should match
the type of the data returned from the SQL query.
SQL statement containing the query or mutation operation to execute.While itβs possible to directly include parameter values into your SQL
statement, itβs highly recommended to pass parameters as arguments instead.
This can help to protect injection attacks and other security vulnerabilities.
Optional parameters to include with the query.
Location
Represents a location on Earth, having Longitude
and Latitude
coordinates.
Correctly serializes to and from PostgreSQLβs point type, in (longitude,
latitude) order.
This struct is identical to the Point struct, but uses different
field names.
type Location struct {
Longitude float64
Latitude float64
}
The longitude coordinate of the location, in degrees.
The latitude coordinate of the location, in degrees.
Represents a point in 2D space, having X
and Y
coordinates. Correctly
serializes to and from PostgreSQLβs point type, in (x, y) order.
This struct is identical to the Location struct, but uses
different field names.
type Point struct {
X float64
Y float64
}
The X coordinate of the point.
The Y coordinate of the point.
QueryResponse
Represents the response from a Query
operation.
type QueryResponse[T any] struct {
RowsAffected uint32
LastInsertId uint64
Rows []T
}
The number of rows affected by the operation, which typically corresponds to
the number of rows returned.
This field is available for other database types, but isnβt populated for
PostgreSQL. Instead, use Query
or QueryScalar
with a RETURNING
clause to
get the last inserted ID.
An slice of objects, each representing a row returned from the query. Each
object has fields corresponding to the columns in the result set.
Response
Represents the response from an Execute
operation.
type Response struct {
RowsAffected uint32
LastInsertId uint64
}
The number of rows affected by the operation, which typically corresponds to
the number of rows returned.
This field is available for other database types, but isnβt populated for
PostgreSQL. Instead, use Query
or QueryScalar
with a RETURNING
clause to
get the last inserted ID.
ScalarResponse
Represents the response from a QueryScalar
operation.
type ScalarResponse[T any] struct {
RowsAffected uint32
LastInsertId uint64
Value T
}
The number of rows affected by the operation, which typically corresponds to
the number of rows returned.
This field is available for other database types, but isnβt populated for
PostgreSQL. Instead, use Query
or QueryScalar
with a RETURNING
clause to
get the last inserted ID.
The scalar value returned from the query.