modulepackage
0.20.87
Repository: https://github.com/effective-security/xdb.git
Documentation: pkg.go.dev
# README
xdb
Extensions around standard sql package
Usage
go get github.com/effective-security/xdb
Schema generator
Usage: xdbcli <command>
SQL schema tool
Flags:
-h, --help Show context-sensitive help.
-D, --debug Enable debug mode
--o="table" Print output format: json|yaml|table
--sql-source=STRING SQL sources, if not provided, will be used from XDB_DATASOURCE env var
Commands:
schema generate generate Go model for database schema
schema columns prints database schema
schema tables prints database tables and dependencies
schema views prints database views and dependencies
schema foreign-keys prints Foreign Keys
Run "xdbcli <command> --help" for more information on a command.
Examples:
export XDB_DATASOURCE="postgres://${XDB_PG_USER}:${XDB_PG_PASSWORD}@${XDB_PG_HOST}:${XDB_PG_PORT}?sslmode=disable"
Print tables:
bin/xdbcli schema tables --db testdb
public.org
public.orgmember
public.schema_migrations
public.user
Print columns
bin/xdbcli schema columns --db testdb --table orgmember --dependencies
Schema: public
Table: org
NAME | TYPE | NULL | MAX | REF
-----------------+--------------------------+------+-----+------
id | bigint | NO | |
name | character varying | NO | 64 |
email | character varying | NO | 160 |
billing_email | character varying | NO | 160 |
company | character varying | NO | 64 |
street_address | character varying | NO | 256 |
city | character varying | NO | 32 |
postal_code | character varying | NO | 16 |
region | character varying | NO | 16 |
country | character varying | NO | 16 |
phone | character varying | NO | 32 |
created_at | timestamp with time zone | YES | |
updated_at | timestamp with time zone | YES | |
quota | jsonb | YES | |
settings | jsonb | YES | |
Print FK
bin/xdbcli schema foreign-keys --db testdb
NAME | SCHEMA | TABLE | COLUMN | FK SCHEMA | FK TABLE | FK COLUMN
-------------------------+--------+-----------+---------+-----------+----------+------------
orgmember_org_id_fkey | public | orgmember | org_id | public | org | id
orgmember_user_id_fkey | public | orgmember | user_id | public | user | id
Generate model
xdbcli --sql-source=$(DATASOURCE) \
schema generate \
--dependencies \
--db=testdb \
--view=vwMembership \
--out-model=./testdata/e2e/postgres/model \
--out-schema=./testdata/e2e/postgres/schema
# Functions
DecodeCursor decodes the cursor into a map.
EncodeCursor encodes the offset or value into a cursor.
ExecuteListQuery runs a query and returns a list of models.
ExecuteQuery runs a query and populates the result with a list of models.
ExecuteQueryWithCursor runs a query and populates the result with a list of models and the next cursor, if there are more rows to fetch.
ExecuteQueryWithPagination runs a query and populates the result with a list of models and the next offset, if there are more rows to fetch.
FromNow returns Time in UTC after now, with Second presicions.
FromUnixMilli returns Time from Unix milliseconds elapsed since January 1, 1970 UTC.
GetQueryParams returns query parameters from an object.
IDArray returns IDArray.
IDString returns string id.
Int64Array returns pq.Int64Array.
IsNotFoundError returns true, if error is NotFound.
Merge merges two structs with xdb types.
MustID returns ID or panics if the value is invalid.
New creates a Provider instance.
NewID returns ID.
NewIDArray returns IDArray.
NewProvider creates a Provider instance.
NewQueryParams creates a new query parameters builder.
Now returns Time in UTC.
NullTime from *time.Time.
Open returns an SQL connection instance, provider name or error.
PageParam converts a parameter to uint32.
ParseConnectionString return parsed Source from sqlserver://username:password@host/instance?param1=value¶m2=value.
ParseID returns ID or empty if val is not valid ID.
ParseTime returns Time from RFC3339 format.
ParseUint returns id from the string.
QueryRow runs a query and returns a single model.
String returns string.
TimePtr returns nil if time is zero, or pointer with a value.
TryParseID returns ID or empty if val is not valid ID.
UTC returns Time in UTC,.
Validate returns error if the model is not valid.
# Constants
DefaultPageSize is the default page size.
Max values, common for strings.
Max values, common for strings.
Max values, common for strings.
# Variables
DefaultTimeFormat is the default format for Time.String().
DefaultTrucate is the default time to truncate as Postgres time precision is default to 6 However, JavaScript and AWS accept time milliseconds only, 3 digits, so we truncate to 3.
MergeOpts is a mergo option to merge structs.
# Structs
ID defines a type to convert between internal uint64 and external string representations of ID.
MigrationConfig defines migration configuration.
QueryParams is a placeholder for query parameters.
Source describes connection info.
SQLProvider represents SQL client instance.
# Interfaces
DB provides interface for Db operations It's an interface accepted by Query, QueryRow and Exec methods.
HasQueryParams is an interface for objects with query parameters.
IDGenerator defines an interface to generate unique ID accross the cluster.
PageableByCursor is an interface for pagination.
PageableByOffset is an interface for pagination.
Provider provides complete DB access.
QueryParams is an interface for query parameters.
Result describes the result of a list query.
ResultWithCursor describes the result of a list query with a cursor.
Row defines an interface for DB row.
RowPointer defines a generic interface to scan a single row.
Rows defines an interface for DB rows.
RowScanner defines an interface to scan a single row.
Tx provides interface for Tx operations.
Validator provides schema validation interface.
# Type aliases
Bool represents SQL bool NULL.
Float represents SQL float64 NULL.
ID32 defines a type to convert between internal uint32 and NULL values in DB.
IDArray defines a list of IDArray.
Int32 represents SQL int NULL.
Int64 represents SQL int64 NULL.
Metadata de/encodes the string map to/from a SQL string.
NULLString de/encodes the string a SQL string.
No description provided by the author
Strings de/encodes the string slice to/from a SQL string.
Time implements sql.Time functionality and always returns UTC.
UUID de/encodes the string a SQL string.