|
| 1 | +--- |
| 2 | +description: "Reference for Nitric's Go library - Creates a reference to a SQL database." |
| 3 | +--- |
| 4 | + |
| 5 | +# Go - NewSqlDatabase() |
| 6 | + |
| 7 | +Creates a reference to a SQL database. |
| 8 | + |
| 9 | +```go |
| 10 | +import ( |
| 11 | + "github.com/nitrictech/go-sdk/nitric" |
| 12 | +) |
| 13 | + |
| 14 | +db := nitric.NewSqlDatabase("my-database") |
| 15 | +``` |
| 16 | + |
| 17 | +## Parameters |
| 18 | + |
| 19 | +<Properties> |
| 20 | + <Property name="name" required type="string"> |
| 21 | + The unique name of this database within the project. |
| 22 | + </Property> |
| 23 | + <Property name="options" type="...sqlDatabaseOption"> |
| 24 | + Additional options for the SQL Database. See below. |
| 25 | + </Property> |
| 26 | +</Properties> |
| 27 | + |
| 28 | +### SQL Database options |
| 29 | + |
| 30 | +<Properties> |
| 31 | + <Property name="WithMigrationsPath()" type="sqlDatabaseOption"> |
| 32 | + <Properties nested> |
| 33 | + <Property name="path" required type="string"> |
| 34 | + Points to the location of migration files, prefixed with `file://`, or a |
| 35 | + migration dockerfile, prefixed with `dockerfile://`. |
| 36 | + </Property> |
| 37 | + </Properties> |
| 38 | + </Property> |
| 39 | +</Properties> |
| 40 | + |
| 41 | +## Examples |
| 42 | + |
| 43 | +### Create a reference to a database |
| 44 | + |
| 45 | +```go |
| 46 | +import ( |
| 47 | + "github.com/nitrictech/go-sdk/nitric" |
| 48 | +) |
| 49 | + |
| 50 | +db := nitric.NewSqlDatabase("my-database") |
| 51 | +``` |
| 52 | + |
| 53 | +### With a migrations directory |
| 54 | + |
| 55 | +```go |
| 56 | +import ( |
| 57 | + "github.com/nitrictech/go-sdk/nitric" |
| 58 | + "github.com/nitrictech/go-sdk/nitric/sql" |
| 59 | +) |
| 60 | + |
| 61 | +func main() { |
| 62 | + db := nitric.NewSqlDatabase("my-database", sql.WithMigrationsPath("file://migrations/my-database")) |
| 63 | + |
| 64 | + nitric.Run() |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### With a migrations dockerfile |
| 69 | + |
| 70 | +```go |
| 71 | +import ( |
| 72 | + "github.com/nitrictech/go-sdk/nitric" |
| 73 | + "github.com/nitrictech/go-sdk/nitric/sql" |
| 74 | +) |
| 75 | + |
| 76 | +func main() { |
| 77 | + db := nitric.NewSqlDatabase("my-database", sql.WithMigrationsPath("dockerfile://migrations.dockerfile")) |
| 78 | + |
| 79 | + nitric.Run() |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### See also |
| 84 | + |
| 85 | +- [SqlDatabase.ConnectionString()](./sql-connection-string) |
0 commit comments