• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang db.DB类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/ardanlabs/kit/db.DB的典型用法代码示例。如果您正苦于以下问题:Golang DB类的具体用法?Golang DB怎么用?Golang DB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了DB类的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: UpdateUserPassword

// UpdateUserPassword updates an existing user's password and token in the database.
func UpdateUserPassword(context interface{}, db *db.DB, u *User, password string) error {
	log.Dev(context, "UpdateUserPassword", "Started : PublicID[%s]", u.PublicID)

	if err := u.Validate(); err != nil {
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	if len(password) < 8 {
		err := errors.New("Invalid password length")
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	newPassHash, err := crypto.BcryptPassword(u.PrivateID + password)
	if err != nil {
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": u.PublicID}
		upd := bson.M{"$set": bson.M{"password": newPassHash, "modified_at": time.Now().UTC()}}
		log.Dev(context, "UpdateUserPassword", "MGO : db.%s.Update(%s, CAN'T SHOW)", c.Name, mongo.Query(q))
		return c.Update(q, upd)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	log.Dev(context, "UpdateUserPassword", "Completed")
	return nil
}
开发者ID:DmitryZinchenko,项目名称:kit,代码行数:36,代码来源:auth.go


示例2: removeSessions

// removeSessions is used to clear out all the test sessions that are
// created from tests.
func removeSessions(db *db.DB) error {
	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		_, err := c.RemoveAll(q)
		return err
	}

	if err := db.ExecuteMGO(tests.Context, session.Collection, f); err != nil {
		return err
	}

	return nil
}
开发者ID:ramakrishna580,项目名称:kit,代码行数:15,代码来源:session_test.go


示例3: removeUser

// removeUser is used to clear out all the test user from the collection.
func removeUser(db *db.DB, publicID string) error {
	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		return c.Remove(q)
	}

	if err := db.ExecuteMGO(tests.Context, auth.Collection, f); err != nil {
		return err
	}

	f = func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		_, err := c.RemoveAll(q)
		return err
	}

	if err := db.ExecuteMGO(tests.Context, session.Collection, f); err != nil {
		return err
	}

	return nil
}
开发者ID:decebal,项目名称:kit,代码行数:23,代码来源:auth_test.go


示例4: createCollection

// createCollection creates a collection in the new database.
func createCollection(db *db.DB, dbMeta *DBMeta, col *Collection, dropIdxs bool) error {
	mCol, err := db.CollectionMGO("", col.Name)
	if err != nil {
		return err
	}

	if err := mCol.Create(new(mgo.CollectionInfo)); err != nil {
		return err
	}

	if err := createIndexes(mCol, col, dropIdxs); err != nil {
		return err
	}

	return nil
}
开发者ID:decebal,项目名称:kit,代码行数:17,代码来源:create.go


示例5: GetAllMedia

// GetAllMedia deletes a Media from the Media database.
// Returns a non-nil error if it fails.
func GetAllMedia(context interface{}, db *db.DB) ([]*Media, error) {
	log.Dev(context, "GetAllMedias", "Started")

	var medias []*Media
	f := func(c *mgo.Collection) error {
		log.Dev(context, "GetAllMedias", "MGO : db.%s.findAll()", c.Name)
		return c.Find(nil).All(&medias)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetAllMedias", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetAllMedias", "Completed")
	return medias, nil
}
开发者ID:Cheetio,项目名称:api,代码行数:19,代码来源:media.go


示例6: GetAllCourse

// GetAllCourse deletes a Course from the Course database.
// Returns a non-nil error if it fails.
func GetAllCourse(context interface{}, db *db.DB) (Courses, error) {
	log.Dev(context, "GetAllCourses", "Started")

	var Courses Courses
	f := func(c *mgo.Collection) error {
		log.Dev(context, "GetAllCourses", "MGO : db.%s.findAll()", c.Name)
		return c.Find(nil).All(&Courses)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetAllCourses", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetAllCourses", "Completed")
	return Courses, nil
}
开发者ID:Cheetio,项目名称:api,代码行数:19,代码来源:courses.go


示例7: GetMediaByTitle

// GetMediaByTitle retrieves a Media from the Media database using the title.
// Returns a non-nil error if it fails.
func GetMediaByTitle(context interface{}, db *db.DB, title string) (*Media, error) {
	log.Dev(context, "GetMediaByTitle", "Started : Media[%s]", title)

	var media Media
	f := func(c *mgo.Collection) error {
		q := bson.M{"title": title}
		log.Dev(context, "GetMediaByTitle", "MGO : db.%s.findOne(%q)", c.Name, mongo.Query(q))
		return c.Find(q).One(&media)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetMediaByTitle", err, "Completed : Media[%s]", title)
		return nil, err
	}

	log.Dev(context, "GetMediaByTitle", "Completed : Media[%s]", title)
	return &media, nil
}
开发者ID:Cheetio,项目名称:api,代码行数:20,代码来源:media.go


示例8: GetBySessionID

// GetBySessionID retrieves a session from the session store.
func GetBySessionID(context interface{}, db *db.DB, sessionID string) (*Session, error) {
	log.Dev(context, "GetBySessionID", "Started : SessionID[%s]", sessionID)

	var s Session
	f := func(c *mgo.Collection) error {
		q := bson.M{"session_id": sessionID}
		log.Dev(context, "GetBySessionID", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
		return c.Find(q).One(&s)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetBySessionID", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetBySessionID", "Completed")
	return &s, nil
}
开发者ID:ramakrishna580,项目名称:kit,代码行数:19,代码来源:session.go


示例9: DeleteMedia

// DeleteMedia deletes a Media from the Media database.
// Returns a non-nil error if it fails.
func DeleteMedia(context interface{}, db *db.DB, code string) error {
	log.Dev(context, "DeleteMedia", "Started : Media[%s]", code)

	f := func(c *mgo.Collection) error {
		q := bson.M{"name": code}
		log.Dev(context, "DeleteMedia", "MGO : db.%s.remove(%s)", c.Name, mongo.Query(q))
		return c.Remove(q)
	}

	err := db.ExecuteMGO(context, Collection, f)
	if err != nil {
		log.Error(context, "DeleteMedia", err, "Completed : Media[%s]", code)
		return err
	}

	log.Dev(context, "DeleteMedia", "Completed : Media[%s]", code)
	return nil
}
开发者ID:Cheetio,项目名称:api,代码行数:20,代码来源:media.go


示例10: GetCoursesByAuthor

// GetCoursesByAuthor retrieves a Course from the Course database using the author.
// Returns a non-nil error if it fails.
func GetCoursesByAuthor(context interface{}, db *db.DB, author string) (Courses, error) {
	log.Dev(context, "GetCoursesByAuthor", "Started : Author[%s]", author)

	var Courses Courses
	f := func(c *mgo.Collection) error {
		q := bson.M{"author": author}
		log.Dev(context, "GetCoursesByAuthor", "MGO : db.%s.find(%q)", c.Name, mongo.Query(q))
		return c.Find(q).All(&Courses)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetCoursesByAuthor", err, "Completed : Author[%s]", author)
		return nil, err
	}

	log.Dev(context, "GetCoursesByAuthor", "Completed : Author[%s]", author)
	return Courses, nil
}
开发者ID:Cheetio,项目名称:api,代码行数:20,代码来源:courses.go


示例11: GetLibraryByCode

// GetLibraryByCode retrieves a Library from the Library database using the Code.
// Returns a non-nil error if it fails.
func GetLibraryByCode(context interface{}, db *db.DB, code string) (*Library, error) {
	log.Dev(context, "GetLibraryByCode", "Started : Library[%s]", code)

	var Library Library
	f := func(c *mgo.Collection) error {
		q := bson.M{"code": code}
		log.Dev(context, "GetLibraryByCode", "MGO : db.%s.findOne(%q)", c.Name, mongo.Query(q))
		return c.Find(q).One(&Library)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetLibraryByCode", err, "Completed : Library[%s]", code)
		return nil, err
	}

	log.Dev(context, "GetLibraryByCode", "Completed : Library[%s]", code)
	return &Library, nil
}
开发者ID:Cheetio,项目名称:api,代码行数:20,代码来源:libraries.go


示例12: GetByLatest

// GetByLatest retrieves the latest session for the specified user.
func GetByLatest(context interface{}, db *db.DB, publicID string) (*Session, error) {
	log.Dev(context, "GetByLatest", "Started : PublicID[%s]", publicID)

	var s Session
	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		log.Dev(context, "GetByLatest", "MGO : db.%s.find(%s).sort({\"date_created\": -1}).limit(1)", c.Name, mongo.Query(q))
		return c.Find(q).Sort("-date_created").One(&s)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetByLatest", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetByLatest", "Completed")
	return &s, nil
}
开发者ID:ramakrishna580,项目名称:kit,代码行数:19,代码来源:session.go


示例13: UpsertMedia

// UpsertMedia add/updates a Media into the Media database.
// Returns a non-nil error if it fails.
func UpsertMedia(context interface{}, db *db.DB, b *Media) error {
	log.Dev(context, "UpsertMedia", "Started : Add Media : %s", mongo.Query(b))

	f := func(c *mgo.Collection) error {
		q := bson.M{"code": b.Code}
		log.Dev(context, "UpsertMedia", "MGO : db.%s.upsert(%s,%s)", c.Name, mongo.Query(q), mongo.Query(b))
		_, err := c.Upsert(q, b)
		return err
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpsertMedia", err, "Completed : Add Media")
		return err
	}

	log.Dev(context, "UpsertMedia", "Completed : Add Media")
	return nil
}
开发者ID:Cheetio,项目名称:api,代码行数:20,代码来源:media.go


示例14: CreateUser

// CreateUser adds a new user to the database.
func CreateUser(context interface{}, db *db.DB, u *User) error {
	log.Dev(context, "CreateUser", "Started : PublicID[%s]", u.PublicID)

	if err := u.Validate(); err != nil {
		log.Error(context, "CreateUser", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		log.Dev(context, "CreateUser", "MGO : db.%s.insert(%s)", c.Name, mongo.Query(&u))
		return c.Insert(u)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "CreateUser", err, "Completed")
		return err
	}

	log.Dev(context, "CreateUser", "Completed")
	return nil
}
开发者ID:DmitryZinchenko,项目名称:kit,代码行数:22,代码来源:auth.go


示例15: Create

// Create adds a new session for the specified user to the database.
func Create(context interface{}, db *db.DB, publicID string, expires time.Duration) (*Session, error) {
	log.Dev(context, "Create", "Started : PublicID[%s]", publicID)

	s := Session{
		SessionID:   uuid.New(),
		PublicID:    publicID,
		DateExpires: time.Now().Add(expires),
		DateCreated: time.Now(),
	}

	f := func(c *mgo.Collection) error {
		log.Dev(context, "Create", "MGO : db.%s.insert(%s)", c.Name, mongo.Query(&s))
		return c.Insert(&s)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "Create", err, "Completed")
		return nil, err
	}

	log.Dev(context, "Create", "Completed : SessionID[%s]", s.SessionID)
	return &s, nil
}
开发者ID:ramakrishna580,项目名称:kit,代码行数:24,代码来源:session.go


示例16: GetUserByPublicID

// GetUserByPublicID retrieves a user record by using the provided PublicID.
func GetUserByPublicID(context interface{}, db *db.DB, publicID string, activeOnly bool) (*User, error) {
	log.Dev(context, "GetUserByPublicID", "Started : PID[%s]", publicID)

	var user User
	f := func(c *mgo.Collection) error {
		var q bson.M
		if activeOnly {
			q = bson.M{"public_id": publicID, "status": StatusActive}
		} else {
			q = bson.M{"public_id": publicID}
		}
		log.Dev(context, "GetUserByPublicID", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
		return c.Find(q).One(&user)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetUserByPublicID", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetUserByPublicID", "Completed")
	return &user, nil
}
开发者ID:DmitryZinchenko,项目名称:kit,代码行数:24,代码来源:auth.go


示例17: UpdateUser

// UpdateUser updates an existing user to the database.
func UpdateUser(context interface{}, db *db.DB, uu UpdUser) error {
	log.Dev(context, "UpdateUser", "Started : PublicID[%s]", uu.PublicID)

	if err := uu.Validate(); err != nil {
		log.Error(context, "UpdateUser", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": uu.PublicID}
		upd := bson.M{"$set": bson.M{"full_name": uu.FullName, "email": uu.Email, "status": uu.Status, "modified_at": time.Now().UTC()}}
		log.Dev(context, "UpdateUser", "MGO : db.%s.Update(%s, %s)", c.Name, mongo.Query(q), mongo.Query(upd))
		return c.Update(q, upd)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpdateUser", err, "Completed")
		return err
	}

	log.Dev(context, "UpdateUser", "Completed")
	return nil
}
开发者ID:DmitryZinchenko,项目名称:kit,代码行数:24,代码来源:auth.go


示例18: GetUserByEmail

// GetUserByEmail retrieves a user record by using the provided email.
func GetUserByEmail(context interface{}, db *db.DB, email string, activeOnly bool) (*User, error) {
	log.Dev(context, "GetUserByEmail", "Started : Email[%s]", email)

	var user User
	f := func(c *mgo.Collection) error {
		var q bson.M
		if activeOnly {
			q = bson.M{"email": strings.ToLower(email), "status": StatusActive}
		} else {
			q = bson.M{"email": strings.ToLower(email)}
		}
		log.Dev(context, "GetUserByEmail", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
		return c.Find(q).One(&user)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetUserByEmail", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetUserByEmail", "Completed")
	return &user, nil
}
开发者ID:DmitryZinchenko,项目名称:kit,代码行数:24,代码来源:auth.go


示例19: UpdateUserStatus

// UpdateUserStatus changes the status of a user to make them active or disabled.
func UpdateUserStatus(context interface{}, db *db.DB, publicID string, status int) error {
	log.Dev(context, "UpdateUserStatus", "Started : PublicID[%s] Status[%d]", publicID, status)

	if status != StatusActive && status != StatusDisabled {
		err := errors.New("Invalid status code")
		log.Error(context, "LoginUser", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		upd := bson.M{"$set": bson.M{"status": status, "modified_at": time.Now().UTC()}}
		log.Dev(context, "UpdateUserStatus", "MGO : db.%s.Update(%s, %s)", c.Name, mongo.Query(q), mongo.Query(upd))
		return c.Update(q, upd)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpdateUserStatus", err, "Completed")
		return err
	}

	log.Dev(context, "UpdateUserStatus", "Completed")
	return nil
}
开发者ID:DmitryZinchenko,项目名称:kit,代码行数:25,代码来源:auth.go



注:本文中的github.com/ardanlabs/kit/db.DB类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang mongo.Query函数代码示例发布时间:2022-05-24
下一篇:
Golang db.NewMGO函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap