Documentation
¶
Overview ¶
Package mongodriver provides a database/sql compatible driver for MongoDB. It accepts JSON query DSL generated by GraphJin's MongoDB dialect and translates it to MongoDB aggregation pipelines.
Index ¶
- Constants
- type ColumnRows
- type Conn
- func (c *Conn) Begin() (driver.Tx, error)
- func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)
- func (c *Conn) Close() error
- func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
- func (c *Conn) Prepare(query string) (driver.Stmt, error)
- func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
- type Connector
- type CursorColumn
- type CursorInfo
- type Driver
- type FKConnect
- type FieldInfo
- type IntrospectOptions
- type NestedInsert
- type NestedUpdate
- type QueryCondition
- type QueryDSL
- type Result
- type Rows
- type SingleValueRows
- type Stmt
- type Tx
Constants ¶
const ( OpAggregate = "aggregate" OpMultiAggregate = "multi_aggregate" OpFind = "find" OpFindOne = "findOne" OpInsertOne = "insertOne" OpInsertMany = "insertMany" OpUpdateOne = "updateOne" OpUpdateMany = "updateMany" OpDeleteOne = "deleteOne" OpDeleteMany = "deleteMany" OpNestedInsert = "nested_insert" OpNestedUpdate = "nested_update" OpIntrospectInfo = "introspect_info" OpIntrospectColumns = "introspect_columns" OpIntrospectFuncs = "introspect_functions" OpEmpty = "empty" // For dropped root selections (@add/@remove directives) OpNull = "null" // For nulled selections (@skip/@include directives) )
Supported operations
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ColumnRows ¶
type ColumnRows struct {
// contains filtered or unexported fields
}
ColumnRows returns schema introspection results as multiple columns. Used for returning table/column metadata to GraphJin.
func NewColumnRows ¶
func NewColumnRows(columns []string, data [][]any) *ColumnRows
NewColumnRows creates rows with multiple columns per row.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn implements driver.Conn for MongoDB.
func (*Conn) ExecContext ¶
func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
ExecContext executes a statement that doesn't return rows.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector implements driver.Connector for MongoDB.
func NewConnector ¶
NewConnector creates a new MongoDB connector that can be used with sql.OpenDB.
type CursorColumn ¶
type CursorColumn struct {
Col string `json:"col"` // Column name
Order string `json:"order"` // "asc" or "desc"
}
CursorColumn represents an order-by column for cursor extraction.
type CursorInfo ¶
type CursorInfo struct {
SelID int `json:"sel_id"` // Selection ID for cursor prefix
Prefix string `json:"prefix"` // Cursor prefix (e.g., "gj-")
OrderBy []CursorColumn `json:"order_by"` // Order-by columns for cursor value
}
CursorInfo contains metadata for cursor-based pagination. Used to extract cursor values from query results.
type FKConnect ¶
type FKConnect struct {
Path string `json:"path"` // Field path in document (e.g., "owner")
Column string `json:"column"` // FK column name (e.g., "owner_id")
}
FKConnect represents metadata for FK connect operations. Used to transform owner.connect.id -> owner_id during document processing.
type FieldInfo ¶
type FieldInfo struct {
Name string
BSONType string
SQLType string
Required bool
IsArray bool
IsUnique bool
}
FieldInfo describes a discovered field.
type IntrospectOptions ¶
type IntrospectOptions struct {
SampleSize int `json:"sample_size"`
IncludeValidators bool `json:"include_validators"`
}
IntrospectOptions configures schema discovery.
type NestedInsert ¶
type NestedInsert struct {
Collection string `json:"collection"`
ID int `json:"id"`
ParentID int `json:"parent_id"`
RelType string `json:"rel_type,omitempty"` // "one_to_one" or "one_to_many"
FKCol string `json:"fk_col,omitempty"` // FK column name (e.g., "owner_id")
FKOnParent bool `json:"fk_on_parent,omitempty"` // true if FK is on parent table, false if on child
IsConnect bool `json:"is_connect,omitempty"` // true if this is a connect (UPDATE) rather than insert
Document map[string]any `json:"document"`
}
NestedInsert represents a single insert in a nested mutation operation.
type NestedUpdate ¶
type NestedUpdate struct {
Collection string `json:"collection"`
ID int `json:"id"`
ParentID int `json:"parent_id"`
Type string `json:"type"` // "update", "connect", "disconnect"
RelType string `json:"rel_type,omitempty"` // Relationship type
FKCol string `json:"fk_col,omitempty"` // FK column to update for connect/disconnect
FKOnParent bool `json:"fk_on_parent,omitempty"` // true if FK is on parent table
Filter map[string]any `json:"filter"`
Update map[string]any `json:"update,omitempty"`
}
NestedUpdate represents a single update in a nested mutation operation.
type QueryCondition ¶
type QueryCondition struct {
// VarParam is the parameter placeholder (e.g., "$1") for the variable value
VarParam string `json:"var_param"`
// Op is the operation: "eq" for @include (show if var == true),
// "ne" for @skip (show if var != true)
Op string `json:"op"`
}
QueryCondition represents a condition for variable-based directives. Used for @skip(ifVar: $var) and @include(ifVar: $var) on root selections.
type QueryDSL ¶
type QueryDSL struct {
Operation string `json:"operation"`
Collection string `json:"collection,omitempty"`
FieldName string `json:"field_name,omitempty"` // GraphQL field name to wrap result in
Singular bool `json:"singular,omitempty"` // If true, return single object instead of array
Typename string `json:"typename,omitempty"` // If set, add __typename field with this value to each result
QueryTypename string `json:"query_typename,omitempty"` // If set, add root __typename field with this value
Pipeline []map[string]any `json:"pipeline,omitempty"`
Document map[string]any `json:"document,omitempty"`
Documents []map[string]any `json:"documents,omitempty"` // For bulk inserts (insertMany)
RawDocument string `json:"raw_document,omitempty"` // Raw document placeholder (e.g., "$1")
ConnectColumn string `json:"connect_column,omitempty"` // Array column to populate from connect
ConnectPath string `json:"connect_path,omitempty"` // Path to connect IDs in document (e.g., "$2")
FKConnect *FKConnect `json:"fk_connect,omitempty"` // FK column to populate from connect (single value)
FKConnects []FKConnect `json:"fk_connects,omitempty"` // Multiple FK columns to populate from connects
FKValues map[string]any `json:"fk_values,omitempty"` // Direct FK values to set on root document
ReturnPipeline []map[string]any `json:"return_pipeline,omitempty"` // Pipeline to run after insert to fetch return data
Filter map[string]any `json:"filter,omitempty"`
Update map[string]any `json:"update,omitempty"`
Options map[string]any `json:"options,omitempty"`
Presets map[string]any `json:"presets,omitempty"` // Preset values to merge with document
Params []string `json:"params,omitempty"`
Queries []*QueryDSL `json:"queries,omitempty"` // For multi_aggregate operations
Inserts []NestedInsert `json:"inserts,omitempty"` // For nested_insert operations
Updates []NestedUpdate `json:"updates,omitempty"` // For nested_update operations
RootCollection string `json:"root_collection,omitempty"` // Root collection for nested_insert/nested_update
RootMutateID int `json:"root_mutate_id,omitempty"` // ID of root mutation for nested_insert
AllSameCollection bool `json:"all_same_collection,omitempty"` // True if all inserts are in same collection (recursive-only)
Condition *QueryCondition `json:"condition,omitempty"` // Condition for variable-based directives
CursorInfo *CursorInfo `json:"cursor_info,omitempty"` // Cursor pagination metadata
CursorParam string `json:"cursor_param,omitempty"` // Parameter placeholder for cursor value (e.g., "$1")
}
QueryDSL represents the JSON query structure generated by the MongoDB dialect. This is the "SQL" that GraphJin generates for MongoDB.
func ParseQuery ¶
ParseQuery parses a JSON query DSL string into a QueryDSL struct.
func (*QueryDSL) SubstituteParams ¶
SubstituteParams replaces parameter placeholders ($1, $2, etc.) with actual values.
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result implements driver.Result.
func (*Result) InsertedID ¶
InsertedID returns the inserted document ID as a string.
func (*Result) LastInsertId ¶
LastInsertId returns the ID of the last inserted document.
func (*Result) RowsAffected ¶
RowsAffected returns the number of affected rows.
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows implements driver.Rows for MongoDB query results.
type SingleValueRows ¶
type SingleValueRows struct {
// contains filtered or unexported fields
}
SingleValueRows returns a single row with a single JSON value. Used for aggregate results that need to be wrapped.
func NewSingleValueRows ¶
func NewSingleValueRows(value []byte, columns []string) *SingleValueRows
NewSingleValueRows creates rows that return a single JSON value.
func (*SingleValueRows) Columns ¶
func (r *SingleValueRows) Columns() []string
Columns returns column names.