Comments (3)
Table name is an implementation detail of the relational db stores so I think it's fine to implement it store by store like in the PR at #177.
For consistency it may be usefull to add it to all of stores backed by a relational db, more or less the equivalent of the prefix option for the KV stores.
Setting the tablename after init though (using SetSessionsTableName like in #177) does not seem right so maybe it would be better to have a new constructor:
func NewWithOptions(pool *pgxpool.Pool, cleanupInterval time.Duration, tableName string) *PostgresStore
This again mirrors more or less the
func NewWithOptions(client *api.Client, cleanupInterval time.Duration, prefix string) *ConsulStore
for example.
from scs.
I like the idea of a NewWithOptions()
approach. Perhaps we could make Options
a struct and pass it as a 2nd parameter to allow further options in the future without making breaking changes to the public API. So, for postgresstore
for example:
type Options struct {
CleanupInterval time.Duration
TableName string
}
Which could then be used like:
postgresstore.NewWithOptions(db, postgresstore.Options{
CleanupInterval: time.Minute,
TableName: "mycustomsessions",
})
If any fields in Options
are not provided (i.e. they contain the relevant zero value), then we can use a sensible default (i.e. cleanup interval of 5 minutes, table name of "sessions").
I think we could potentially roll this pattern out across all the different stores and deprecate the NewWithPrefix()
and NewWithCleanupInterval()
functions on the individual stores, leaving each store with two ways to initialize it: either New()
(which uses the defaults) or NewWithOptions()
which allows you to customize the behavior in whatever way makes sense for that particular store.
The only problem I can see right now is that the consulstore package already has a NewWithOptions()
function with a different signature. So we could either:
a. Make a breaking change to consulstore.
b. Not implement the Options
struct pattern in consulstore.
c. Think of an alternative name for NewWithOptions()
. I'm blanking on a decent name. Any suggestions?
from scs.
That would be a nice feature!
Maybe instead of whole separate constructor one might consider using Functional Options pattern. See pull request #190 for the prototype.
In this case, no backwards compatibility is broken: New(db)
will fall back to the same names as now.
If one wants to change the default name (and schema for that matter) of the table as well as column names, they can use nice syntax:
New(db,
WithSessionTableName("schema.session_table"),
WithExpiryColumnName("expiry_date"),
WithTokenColumnName("token"),
// data column will not change!
)
As a bonus, one may change cleanupInterval the same way (also without breaking backwards compatibility):
New(db, WithCleanupInterval(10*time.Minute))
It seems to me that such implementation would solve all the problems above and is applicable to any other store. Feel free to point at anything I might have forgotten!
from scs.
Related Issues (20)
- manage all session HOT 1
- Redis example is using a different Redis library HOT 8
- support partitioned CHIPS cookies HOT 3
- pgxstore expects a *pgxpool.Pool instead of a *sql.DB HOT 5
- Flush / streaming HOT 2
- *scs.sessionResponseWriter does not implement http.Hijacker HOT 3
- Support Turso and libsql HOT 2
- Session getting added to DB, but not to cookie, when set to SameSiteNone, but works in SameSiteLax HOT 2
- Sign session ids/tokens HOT 5
- 2.7.0 breaks websocket compatibility HOT 2
- NATS Jetstream and session replication might be of interest HOT 3
- Submodules not being released with tags HOT 1
- Usage with TursoDB HOT 1
- sqlite3 store with custom table name
- Gob register custom struct HOT 2
- [interest check for new store] sqlite3 store using CGO-less modernc.org/sqlite
- [bug] Pop doesn't remove value if w.Write happened before sm.Pop HOT 2
- Database query built from user controller sources
- Superfluous WriterHeader Call Warnings HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scs.