Multitenant application with golang and gorm

This page summarizes the projects mentioned and recommended in the original post on /r/golang

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • GORM

    The fantastic ORM library for Golang, aims to be developer friendly

    "gorm.io/driver/postgres" "gorm.io/gorm" gocache "zgo.at/zcache" ) var cacheHandler *gocache.Cache type StoreDB struct { CreationMutex sync.Mutex cache *gocache.Cache } type Context struct { Schema string } func (s *StoreDB) CreateDBHandler(ctx *Context) *gorm.DB { // Maybe we could use a better mechanism to do this. s.CreationMutex.Lock() defer s.CreationMutex.Unlock() // Double check before moving to Creation precedures if db, found := s.cache.Touch(ctx.Schema, gocache.DefaultExpiration); found { return db.(*gorm.DB) } dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable search_path=%s", getEnv("DB_HOST", "db"), getEnv("DB_USER", "postgres"), getEnv("DB_PASSWORD", "example"), getEnv("DB_NAME", "some-database"), ctx.Schema) db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { panic(err) } cacheHandler.Set(ctx.Schema, db, gocache.DefaultExpiration) return db } func (s *StoreDB) GetDBHandler(ctx *Context) *gorm.DB { db, found := s.cache.Touch(ctx.Schema, gocache.DefaultExpiration) if found { return db.(*gorm.DB) } return s.CreateDBHandler(ctx) } func initManager() *StoreDB { cacheHandler = gocache.New(10*time.Minute, 10*time.Minute) cacheHandler.OnEvicted(func(s string, i interface{}) { // https://github.com/go-gorm/gorm/issues/3145 sql, err := i.(*gorm.DB).DB() if err != nil { panic(err) } sql.Close() }) return &StoreDB{ cache: cacheHandler, } }

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts