
Prism
The universal database connector for Go. NewSQL and NewNoSQL is all you need.
Prism is a simple, "batteries-included" data layer for Go. It stops the headache of managing different database drivers and complex configurations. It gives you a single way to connect to almost any database using just two functions.
Why use Prism?
- Simple API: You only need to learn two functions: NewSQL and NewNoSQL.
- Zero Config: No need to manually import drivers or setup connection pools. Prism handles the best settings for you.
- DSN Driven: Your connection string (URL) is the only thing you need to change to swap databases.
- Dynamic Tuning: Control pooling and timeouts directly from your DSN string without changing code.
- Auto-Provisioning: Prism automatically creates folders for SQLite and enables production-grade settings (WAL mode) by default.
- Unified NoSQL: Use the same code for Redis, MongoDB.
- High Performance: Features a JSON bypass that detects raw strings and []byte to avoid unnecessary serialization overhead.
- Scale Ready: Redis logic automatically detects and switches between Single Node, Cluster, and Sentinel modes.
- Hardened Security: Built-in support for TLS/SSL, mTLS (Client Certs), SASL, and skip_verify for development.
Install
go get github.com/mukailasam/prism
How to use
Relational Databases (NewSQL)
Connect to Postgres, MySQL, or SQLite. Prism returns a standard *sql.DB that is already tuned for production.
import "github.com/mukailasam/prism"
// SQLite: Automatically creates the 'data' folder and enables WAL mode.
db, _ := prism.NewSQL("sqlite://file:data/prism.db")
// Postgres: Dynamic tuning via DSN.
db, _ := prism.NewSQL("postgres://user:pass@localhost:5432/db?max_open=50&conn_lifetime=1h")
// MariaDB/MySQL: Standard protocol support.
db, _ := prism.NewSQL("mariadb://user:pass@tcp(localhost:3306)/db")
NoSQL & Cache Gateways (NewNoSQL)
Connect to Redis or MongoDB. Prism returns a Driver gateway.
import "github.com/mukailasam/prism"
// Connect to Redis
cache, _ := prism.NewNoSQL("redis://localhost:6379")
// Connect to MongoDB (Specify DB in path and Collection in query)
db, _ := prism.NewNoSQL("mongodb://localhost:27017/my_app?collection=users")
Database Tuning via DSN
You can control your database performance directly inside the DSN string.
|Parameter | Description | Example |
|---------------|----------------------------------|---------------------|
| max_open | active connections | ?max_open=100 |
| max_idle | idle connections | ?max_idle=20 |
| pool size | Max connection pool size | ?pool_size=50 |
| timeout | Connection/Dial timeout | ?timeout=5s or 5000 |
| conn_lifetime | How long a connection stays open | ?conn_lifetime=1h |
| ssl | Force TLS/SSL encryption | ?ssl=true |
Production Security
Prism handles complex TLS configurations through simple URI options.
- ?skip_verify=true: Ignore certificate errors (Local Dev only).
- ?ca_file=path.pem: Add a custom Root CA to verify the database.
- ?cert_file & key_file: Enable mTLS (Client Authentication).
The Capability Gateway
When you use NewNoSQL, Prism provides a Driver struct that intelligently exposes what you need.
1: Common API (Works everywhere)
These methods work across all NoSQL drivers. Prism handles serialization automatically, with a high-speed bypass for raw string and []byte types.
// Set any struct, string, or bytes
conn.Set(ctx, "user:101", myUser, 24 * time.Hour)
// Get data back into a destination pointer
var u User
err := conn.Get(ctx, "user:101", &u)
// Delete
conn.Del(ctx, "user:101")
2: Native Access (Unique to the Owner)
Prism is a gateway, not a cage. If you need features specific to your database (like Redis PubSub or Mongo Aggregation), the native clients are directly available.
if conn.Redis != nil {
conn.Redis.Incr(ctx, "page_views")
}
if conn.Mongo.Client != nil {
conn.Mongo.Database("admin").RunCommand(ctx, bson.D{{"ping", 1}})
}
DSN Cheatsheet
Not sure how to connect? Call prism.Help() in your code or refer to this table:
prism.Help()
|Database | connection string or dsn |
|----------|-----------------------------------------------|
| Postgres | postgres://user:pass@host:port/db |
| MySQL | mysql://user:pass@tcp(host:port)/db |
| SQLite | sqlite://file.db |
| Redis | redis://host:6379 |
| MongoDB | mongodb://host:27017/database?collection=name |
The "Honest" Engineering Trade-off
Binary Size:
Prism is built for simplicity and developer speed. Because we bundle all major drivers internally (pgx, mysql, mongo, redis, etc.), your final compiled binary will be approximately 10MB to 15MB larger than if you manually imported a single driver.
Is this a problem?
- For Microservices/Web/Docker: No. 15MB is negligible in modern cloud environments and container images.
- For CLI/Embedded/WASM: Maybe. You should be aware of the footprint before choosing Prism for these targets
Pro-Tip: You can significantly reduce the binary size by compiling with the -s -w flags to strip debug information:
go build -ldflags="-s -w" -o myapp .
The Verdict: For a "Universal Connector" that works instantly with zero configuration, this is a small price to pay for massive productivity gains.
Contributing
Contributions are welcome!
To contribute:
Make sure to follow Go’s code formatting and keep your commits clean and descriptive.