Import path:
gitlab.soludian.com/soludian/fountain/libs/stored/fongo
fongo
import "gitlab.soludian.com/soludian/fountain/libs/stored/fongo"Index
- Constants
- Variables
- func BuildRegexQuery(query string, caseInsensitive bool) map[string]any
- func CreateDefaultCtx() (context.Context, context.CancelFunc)
- func CreateSafeRegexFilter(field, query string, caseInsensitive bool) map[string]any
- func EscapeRegexSpecialChars(input string) string
- func QueryAggregateDataParser[T any](collection *mongo.Collection, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) []*T
- func QueryAggregateDataParserContext[T any](collection *mongo.Collection, ctx context.Context, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) []*T
- func QueryDataParser[T any](collection *mongo.Collection, filter any, opts ...options.Lister[options.FindOneOptions]) *T
- func QueryDataParserContext[T any](collection *mongo.Collection, ctx context.Context, filter any, opts ...options.Lister[options.FindOneOptions]) *T
- func QueryListDataParser[T any](collection *mongo.Collection, filter any, opts ...options.Lister[options.FindOptions]) []*T
- func QueryListDataParserContext[T any](collection *mongo.Collection, ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) []*T
- func QueryPageListDataParser[T any](collection *mongo.Collection, filter any, opts ...options.Lister[options.FindOptions]) ([]*T, int)
- func QueryPageListDataParserContext[T any](collection *mongo.Collection, ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) ([]*T, int)
- func Upsert[T any](collection *mongo.Collection, filterFields []string, setOnInsertKeys []string, mds ...*T) error
- func ValidateRegexPattern(pattern string) error
- func WithConfig(conf *config) lib_3rd.Option[config]
- func WithDSN(dsn string) lib_3rd.Option[config]
- func WithDatabase(database string) lib_3rd.Option[config]
- func WithEnvironment(environment string) lib_3rd.Option[config]
- func WithMaxConnIdleTime(timeout time.Duration) lib_3rd.Option[config]
- func WithMaxConnecting(max int) lib_3rd.Option[config]
- func WithMaxPoolSize(max int) lib_3rd.Option[config]
- func WithMinPoolSize(min int) lib_3rd.Option[config]
- func WithName(name string) lib_3rd.Option[config]
- func WithTimeout(timeout time.Duration) lib_3rd.Option[config]
- type Client
Constants
const (
KDefaultTimeout = 15 * time.Second
)const KPackageName = "fongo"Variables
var GetFountainInstance = Lib.GetFountainInstancevar GetFountainManager = Lib.GetFountainManagerSử dụng khi config instance ở dạng key:value; Nếu config instance ở dạng key:array thì sử dụng hàm InstallFountainInstances Nếu config ở dạng key:array thì sẽ chỉ install config phần tử đầu tiên mà thôi
Install with config format <key>:<value>; eg: fongo:<value>
Usage:
config.yaml:
fongo:
name: default_name
...
code.go
fongo.InstallFountainInstance()
fongo.WithConfigKey("fongo").InstallFountainInstance()var InstallFountainInstance = Lib.InstallFountainInstanceSử dụng khi config instance ở dạng key:array<value>; Sẽ luôn cố gắng khởi tạo kể cả khi config ở dạng key:value
Install with config format <key>:array<value>; eg: fongo:array<value>
Usage:
config.yaml:
fongo:
- name: default_name
...
code.go
fongo.InstallFountainInstances()
fongo.WithConfigKey("fongo").InstallFountainInstances()var InstallFountainInstances = Lib.InstallFountainInstancesTruy cập thẳng tới bộ quản lý thư viện
var Lib = lib_3rd.NewLib(newClient, lib_3rd.WithDefaultConfigsFunc[config, Client](defaultConfigs))var WithConfigKey = Lib.WithConfigKeyfunc BuildRegexQuery
func BuildRegexQuery(query string, caseInsensitive bool) map[string]anyBuildRegexQuery tạo regex query an toàn cho MongoDB.
func CreateDefaultCtx
func CreateDefaultCtx() (context.Context, context.CancelFunc)func CreateSafeRegexFilter
func CreateSafeRegexFilter(field, query string, caseInsensitive bool) map[string]anyCreateSafeRegexFilter tạo filter regex an toàn cho MongoDB
func EscapeRegexSpecialChars
func EscapeRegexSpecialChars(input string) stringEscapeRegexSpecialChars escape các ký tự đặc biệt trong regex để tìm kiếm literal. Single-pass O(n), không allocate nếu input không chứa ký tự đặc biệt.
func QueryAggregateDataParser
func QueryAggregateDataParser[T any](collection *mongo.Collection, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) []*TQueryAggregateDataParser thực hiện truy vấn aggregate đến một collection trong MongoDB để lấy về nhiều document khớp với pipeline được cung cấp, parse các document này vào một danh sách các biến kiểu T.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình thực thi aggregate. Nếu có lỗi, hàm sẽ trả về nil.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- pipeline: Pipeline (ví dụ: một chuỗi các stage BSON) để xác định cách xử lý và biến đổi dữ liệu.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.AggregateOptions]) để tùy biến hành vi khi aggregate.
Returns:
- Một slice chứa các con trỏ tới đối tượng kiểu T (mỗi T tương ứng với một document) nếu thành công.
Usage:
// Định nghĩa pipeline để nhóm các document theo trường "category" và tính tổng "value".
pipeline := mongo.Pipeline{
bson.D{{"$group", bson.D{{"_id", "$category"}, {"total", bson.D{{"$sum", "$value"}}}}}}
}
// Thực hiện truy vấn và xử lý kết quả
results := QueryAggregateDataParser[models.T](
collection,
pipeline,
)
if len(results) == 0 {
// Xử lý khi không tìm thấy document nào
} else {
// Sử dụng results tùy theo nhu cầu
}func QueryAggregateDataParserContext
func QueryAggregateDataParserContext[T any](collection *mongo.Collection, ctx context.Context, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) []*TQueryAggregateDataParserContext thực hiện truy vấn aggregate đến một collection trong MongoDB để lấy về nhiều document khớp với pipeline được cung cấp, parse các document này vào một danh sách các biến kiểu T.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình thực thi aggregate. Nếu có lỗi, hàm sẽ trả về nil.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- ctx: Context cha dùng để kiểm soát vòng đời của truy vấn.
- pipeline: Pipeline (ví dụ: một chuỗi các stage BSON) để xác định cách xử lý và biến đổi dữ liệu.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.AggregateOptions]) để tùy biến hành vi khi aggregate.
Returns:
- Một slice chứa các con trỏ tới đối tượng kiểu T (mỗi T tương ứng với một document) nếu thành công.
Usage:
// Định nghĩa pipeline để nhóm các document theo trường "category" và tính tổng "value".
pipeline := mongo.Pipeline{
bson.D{{"$group", bson.D{{"_id", "$category"}, {"total", bson.D{{"$sum", "$value"}}}}}}
}
// Thực hiện truy vấn và xử lý kết quả
results := QueryAggregateDataParserContext[models.T](
collection,
context.Background(),
pipeline,
)
if len(results) == 0 {
// Xử lý khi không tìm thấy document nào
} else {
// Sử dụng results tùy theo nhu cầu
}func QueryDataParser
func QueryDataParser[T any](collection *mongo.Collection, filter any, opts ...options.Lister[options.FindOneOptions]) *TQueryDataParser thực hiện truy vấn đến một collection trong MongoDB để lấy ra duy nhất một document khớp với bộ lọc (filter) được cung cấp, giải mã document này vào một biến kiểu T và tùy chọn thực thi thêm hàm xử lý fnAfterParse sau khi giải mã.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- filter: Bộ lọc (ví dụ: một BSON document) để xác định document cần lấy.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.FindOneOptions]) để tùy biến hành vi khi truy vấn.
Returns:
- Con trỏ tới một đối tượng kiểu T chứa document đã được giải mã nếu thành công, hoặc nil nếu có lỗi.
Usage:
// Định nghĩa filter để tìm document theo trường _id
filter := bson.M{"_id": id}
// Thực hiện truy vấn và xử lý hậu giải mã
result := QueryDataParser[models.T](collection, filter)
if result == nil {
// Xử lý khi không tìm thấy document hoặc có lỗi
}func QueryDataParserContext
func QueryDataParserContext[T any](collection *mongo.Collection, ctx context.Context, filter any, opts ...options.Lister[options.FindOneOptions]) *TQueryDataParserContext thực hiện truy vấn đến một collection trong MongoDB để lấy ra duy nhất một document khớp với bộ lọc (filter) được cung cấp, giải mã document này vào một biến kiểu T và tùy chọn thực thi thêm hàm xử lý fnAfterParse sau khi giải mã.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình truy vấn hoặc giải mã. Nếu có lỗi hoặc nếu fnAfterParse trả về lỗi, hàm sẽ trả về nil.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- ctx: Context cha dùng để kiểm soát vòng đời của truy vấn.
- filter: Bộ lọc (ví dụ: một BSON document) để xác định document cần lấy.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.FindOneOptions]) để tùy biến hành vi khi truy vấn.
Returns:
- Con trỏ tới một đối tượng kiểu T chứa document đã được giải mã nếu thành công, hoặc nil nếu có lỗi.
Usage:
// Định nghĩa filter để tìm document theo trường _id
filter := bson.M{"_id": id}
// Thực hiện truy vấn và xử lý hậu giải mã
result := QueryDataParserContext[models.T](collection, context.Background(), filter)
if result == nil {
// Xử lý khi không tìm thấy document hoặc có lỗi
}func QueryListDataParser
func QueryListDataParser[T any](collection *mongo.Collection, filter any, opts ...options.Lister[options.FindOptions]) []*TQueryListDataParser thực hiện truy vấn đến một collection trong MongoDB để lấy về nhiều document khớp với bộ lọc (filter) được cung cấp, giải mã các document này vào một danh sách các biến kiểu T, đồng thời trả về tổng số lượng document thỏa mãn filter.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình đếm (CountDocuments) hoặc tìm kiếm (Find). Nếu có lỗi, hàm sẽ trả về nil và 0.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- filter: Bộ lọc (ví dụ: một BSON document) để xác định các document cần lấy.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.FindOptions]) để tùy biến hành vi khi truy vấn.
Returns:
- Một slice chứa các con trỏ tới đối tượng kiểu T (mỗi T tương ứng với một document) nếu thành công.
Usage:
// Định nghĩa filter để tìm các document có trường "status" là "active".
filter := bson.M{"status": "active"}
// Thực hiện truy vấn và xử lý kết quả
results := QueryListDataParser[models.T](
collection,
filter,
)
if len(results) == 0 {
// Xử lý khi không tìm thấy document nào
} else {
// Sử dụng results và thông tin total tùy theo nhu cầu
}func QueryListDataParserContext
func QueryListDataParserContext[T any](collection *mongo.Collection, ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) []*TQueryListDataParserContext thực hiện truy vấn đến một collection trong MongoDB để lấy về nhiều document khớp với bộ lọc (filter) được cung cấp, giải mã các document này vào một danh sách các biến kiểu T, đồng thời trả về tổng số lượng document thỏa mãn filter.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình đếm (CountDocuments) hoặc tìm kiếm (Find). Nếu có lỗi, hàm sẽ trả về nil và 0.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- ctx: Context cha dùng để kiểm soát vòng đời của truy vấn.
- filter: Bộ lọc (ví dụ: một BSON document) để xác định các document cần lấy.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.FindOptions]) để tùy biến hành vi khi truy vấn.
Returns:
- Một slice chứa các con trỏ tới đối tượng kiểu T (mỗi T tương ứng với một document) nếu thành công.
Usage:
// Định nghĩa filter để tìm các document có trường "status" là "active".
filter := bson.M{"status": "active"}
// Thực hiện truy vấn và xử lý kết quả
results := QueryListDataParserContext[models.T](
collection,
context.Background(),
filter,
)
if len(results) == 0 {
// Xử lý khi không tìm thấy document nào
} else {
// Sử dụng results và thông tin total tùy theo nhu cầu
}func QueryPageListDataParser
func QueryPageListDataParser[T any](collection *mongo.Collection, filter any, opts ...options.Lister[options.FindOptions]) ([]*T, int)QueryPageListDataParser thực hiện truy vấn đến một collection trong MongoDB để lấy về nhiều document khớp với bộ lọc (filter) được cung cấp, giải mã các document này vào một danh sách các biến kiểu T, đồng thời trả về tổng số lượng document thỏa mãn filter.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình đếm (CountDocuments) hoặc tìm kiếm (Find). Nếu có lỗi, hàm sẽ trả về nil và 0.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- filter: Bộ lọc (ví dụ: một BSON document) để xác định các document cần lấy.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.FindOptions]) để tùy biến hành vi khi truy vấn.
Returns:
- Một slice chứa các con trỏ tới đối tượng kiểu T (mỗi T tương ứng với một document) nếu thành công.
- Một số nguyên đại diện cho tổng số document khớp với filter trong collection.
Usage:
// Định nghĩa filter để tìm các document có trường "status" là "active".
filter := bson.M{"status": "active"}
// Thực hiện truy vấn và xử lý kết quả
results, total := QueryPageListDataParser[models.T](
collection,
filter,
)
if len(results) == 0 {
// Xử lý khi không tìm thấy document nào
} else {
// Sử dụng results và thông tin total tùy theo nhu cầu
}func QueryPageListDataParserContext
func QueryPageListDataParserContext[T any](collection *mongo.Collection, ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) ([]*T, int)QueryPageListDataParserContext thực hiện truy vấn đến một collection trong MongoDB để lấy về nhiều document khớp với bộ lọc (filter) được cung cấp, giải mã các document này vào một danh sách các biến kiểu T, đồng thời trả về tổng số lượng document thỏa mãn filter.
Hàm này khởi tạo một context với thời gian chờ mặc định (KDefaultTimeout) và ghi log bất kỳ lỗi nào xảy ra trong quá trình đếm (CountDocuments) hoặc tìm kiếm (Find). Nếu có lỗi, hàm sẽ trả về nil và 0.
Parameters:
- collection: Collection trong MongoDB cần truy vấn.
- ctx: Context cha dùng để kiểm soát vòng đời của truy vấn.
- filter: Bộ lọc (ví dụ: một BSON document) để xác định các document cần lấy.
- opts: Các tùy chọn truy vấn (có kiểu options.Lister[options.FindOptions]) để tùy biến hành vi khi truy vấn.
Returns:
- Một slice chứa các con trỏ tới đối tượng kiểu T (mỗi T tương ứng với một document) nếu thành công.
- Một số nguyên đại diện cho tổng số document khớp với filter trong collection.
Usage:
// Định nghĩa filter để tìm các document có trường "status" là "active".
filter := bson.M{"status": "active"}
// Thực hiện truy vấn và xử lý kết quả
results, total := QueryPageListDataParserContext[models.T](
collection,
context.Background(),
filter,
)
if len(results) == 0 {
// Xử lý khi không tìm thấy document nào
} else {
// Sử dụng results và thông tin total tùy theo nhu cầu
}func Upsert
func Upsert[T any](collection *mongo.Collection, filterFields []string, setOnInsertKeys []string, mds ...*T) errorfunc ValidateRegexPattern
func ValidateRegexPattern(pattern string) errorValidateRegexPattern kiểm tra xem pattern có hợp lệ không
func WithConfig
func WithConfig(conf *config) lib_3rd.Option[config]func WithDSN
func WithDSN(dsn string) lib_3rd.Option[config]func WithDatabase
func WithDatabase(database string) lib_3rd.Option[config]func WithEnvironment
func WithEnvironment(environment string) lib_3rd.Option[config]func WithMaxConnIdleTime
func WithMaxConnIdleTime(timeout time.Duration) lib_3rd.Option[config]func WithMaxConnecting
func WithMaxConnecting(max int) lib_3rd.Option[config]func WithMaxPoolSize
func WithMaxPoolSize(max int) lib_3rd.Option[config]func WithMinPoolSize
func WithMinPoolSize(min int) lib_3rd.Option[config]func WithName
func WithName(name string) lib_3rd.Option[config]func WithTimeout
func WithTimeout(timeout time.Duration) lib_3rd.Option[config]type Client
type Client struct {
*mongo.Database
// contains filtered or unexported fields
}func (*Client) GetDatabase
func (c *Client) GetDatabase() *mongo.DatabaseGenerated by gomarkdoc