Cannot define SQL Database resource in non-service package

cannot define SQL Database resource in non-service package

─[ tasks\store\database.go:14:32 ]

12 │
13 │ // get the service name
14 │ var tasksDatabase = sqlx.NewDb(sqldb.Named(“tasks”).Stdlib(), “postgres”)

This is intended. Since Encore tracks which services depend on what infrastructure, you cannot define infrastructure dependencies from outside of services.

To do what you want you can accept the database as a parameter to a function in the library package instead.

The same structure works for other services.

The error is saying that package is not considered a service. Do you have any APIs defined in that package?

No, I write database logic in <service>/store/database.go it works for all services.

But I just noticed since I called an endpoint using the database function in the <service>/<service>.go. I don’t get the error.

So the solution I found to this is that package eg. <service>/<another-package> should at least have a resource been used in the main service ie. <service>/<service>.go.

Yeah exactly, the <service> root package needs an API endpoint defined to be treated as a package. Glad to hear it works now!