@@ -226,3 +226,51 @@ func createIndexesOnly(ctx context.Context, c *mongox.ClientCollection, keys, un
226226 log .Infofc (ctx , "mongo: %s: ensured indexes exist (without dropping any)\n " , c .Client ().Name ())
227227 return nil
228228}
229+
230+ // NewAccountRepoContainer creates account repositories using wrappers that prevent index dropping
231+ func NewAccountRepoContainer (ctx context.Context , client * mongo.Client , database string , txAvailable , accountRepoCompat bool , accountUsers []accountrepo.User ) (* accountrepo.Container , error ) {
232+ mongoxClient := mongox .NewClient (database , client )
233+ if txAvailable {
234+ mongoxClient = mongoxClient .WithTransaction ()
235+ }
236+
237+ var ws accountrepo.Workspace
238+ if accountRepoCompat {
239+ ws = NewWorkspaceWrapper (mongoxClient )
240+ } else {
241+ ws = NewWorkspaceWrapper (mongoxClient )
242+ }
243+
244+ container := & accountrepo.Container {
245+ Workspace : ws ,
246+ User : NewUserWrapper (mongoxClient ),
247+ Transaction : mongoxClient .Transaction (),
248+ Users : accountUsers ,
249+ Role : NewRoleWrapper (mongoxClient ),
250+ Permittable : NewPermittableWrapper (mongoxClient ),
251+ }
252+
253+ if err := initAccountWrappers (container ); err != nil {
254+ return nil , err
255+ }
256+
257+ return container , nil
258+ }
259+
260+ func initAccountWrappers (r * accountrepo.Container ) error {
261+ if r == nil {
262+ return nil
263+ }
264+
265+ ctx := context .Background ()
266+ return util .Try (
267+ r .Workspace .(* WorkspaceWrapper ).Init ,
268+ r .User .(* UserWrapper ).Init ,
269+ func () error { return r .Role .(* RoleWrapper ).Init (ctx ) },
270+ func () error { return r .Permittable .(* PermittableWrapper ).Init (ctx ) },
271+ )
272+ }
273+
274+ func NewUserWithHost (client * mongox.Client , host string ) accountrepo.User {
275+ return NewUserWrapper (client )
276+ }
0 commit comments