MCPcopy
hub / github.com/pocketbase/pocketbase / NewWithConfig

Function NewWithConfig

pocketbase.go:88–162  ·  view source on GitHub ↗

NewWithConfig creates a new PocketBase instance with the provided config. Note that the application will not be initialized/bootstrapped yet, aka. DB connections, migrations, app settings, etc. will not be accessible. Everything will be initialized when [PocketBase.Start] is executed. If you want t

(config Config)

Source from the content-addressed store, hash-verified

86// If you want to initialize the application before calling [PocketBase.Start],
87// then you'll have to manually call [PocketBase.Bootstrap].
88func NewWithConfig(config Config) *PocketBase {
89 // initialize a default data directory based on the executable baseDir
90 if config.DefaultDataDir == "" {
91 baseDir, _ := inspectRuntime()
92 config.DefaultDataDir = filepath.Join(baseDir, "pb_data")
93 }
94
95 if config.DefaultQueryTimeout == 0 {
96 config.DefaultQueryTimeout = core.DefaultQueryTimeout
97 }
98
99 executableName := filepath.Base(os.Args[0])
100
101 pb := &PocketBase{
102 RootCmd: &cobra.Command{
103 Use: executableName,
104 Short: executableName + " CLI",
105 Version: Version,
106 FParseErrWhitelist: cobra.FParseErrWhitelist{
107 UnknownFlags: true,
108 },
109 // no need to provide the default cobra completion command
110 CompletionOptions: cobra.CompletionOptions{
111 DisableDefaultCmd: true,
112 },
113 },
114 devFlag: config.DefaultDev,
115 dataDirFlag: config.DefaultDataDir,
116 encryptionEnvFlag: config.DefaultEncryptionEnv,
117 hideStartBanner: config.HideStartBanner,
118 }
119
120 // replace with a colored stderr writer
121 pb.RootCmd.SetErr(newErrWriter())
122
123 // parse base flags
124 // (errors are ignored, since the full flags parsing happens on Execute())
125 pb.eagerParseFlags(&config)
126
127 // initialize the app instance
128 pb.App = core.NewBaseApp(core.BaseAppConfig{
129 IsDev: pb.devFlag,
130 DataDir: pb.dataDirFlag,
131 EncryptionEnv: pb.encryptionEnvFlag,
132 QueryTimeout: time.Duration(pb.queryTimeout) * time.Second,
133 DataMaxOpenConns: config.DataMaxOpenConns,
134 DataMaxIdleConns: config.DataMaxIdleConns,
135 AuxMaxOpenConns: config.AuxMaxOpenConns,
136 AuxMaxIdleConns: config.AuxMaxIdleConns,
137 DBConnect: config.DBConnect,
138 })
139
140 // hide the default help command (allow only `--help` flag)
141 pb.RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
142
143 // https://github.com/pocketbase/pocketbase/issues/6136
144 pb.OnBootstrap().Bind(&hook.Handler[*core.BootstrapEvent]{
145 Id: ModerncDepsCheckHookId,

Callers 4

NewFunction · 0.85
TestNewWithConfigFunction · 0.85
TestSkipBootstrapFunction · 0.85

Calls 9

eagerParseFlagsMethod · 0.95
NewBaseAppFunction · 0.92
FireAndForgetFunction · 0.92
inspectRuntimeFunction · 0.85
newErrWriterFunction · 0.85
checkModerncDepsFunction · 0.85
OnBootstrapMethod · 0.65
NextMethod · 0.65
BindMethod · 0.45

Tested by 3

TestNewWithConfigFunction · 0.68
TestSkipBootstrapFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…