tableio
is a small and simple library that helps with persisting structs in a database. Great for quick proto-typing. Only tested PostgreSQL
Create structs to represent the data you want to persist. Here's an example:
type Address struct {
City string
State string
}
type Person struct {
ID int
Name string
Age int
Address Address
}
Let's attempt to store the above Person
struct in the database. The struct you want to persist in the DB must have a ID
and Name
field.
The TableIO constructor NewTableIO
creates a connection to your database and returns a handle to it. Specify your struct's type as a type parameter, for e.g., the below example passes [Person]
as a type parameter:
peopleTable, err := NewTableIO[Person]("mysql", "user:password@tcp(127.0.01:3306)/mydb")
peopleTable, err := NewTableIO[Person]("postgres", "postgresql://user:password@127.0.01/ark?sslmode=disable")
Note the database drivername and connection string are passed in the constructor.
The fields of your struct (that is passed via the type parameter) are used to determine the structure of your database table. For e.g., in the above case Person
was passed, as such the fields in the Person
struct will generate the following SQL at table creation:
CREATE TABLE IF NOT EXISTS people (
ID SERIAL PRIMARY KEY,
Name VARCHAR(255) NOT NULL UNIQUE,
Age INTEGER NULL,
Address JSONB NULL
);
Any complex type (i.e. not an int/string etc), is stored as a JSONB in the DB. For example, in the above case, Address
is stored as a json object in that table. These are transparently marshalled/unmarshalled on read/write.
Call the CreateTableIfNotExists
method to create a table for your struct:
peopleTable.CreateTableIfNotExists()
To insert data, call the insert method passing in your struct
// Insert data in to table
person := Person{
Name: "John",
Age: 30,
Address: Address{
City: "New York",
State: "NY",
},
}
peopleTable.Insert(person)
people := []Person{
{
Name: "Ahmed",
Age: 45,
Address: Address{
City: "Cairo",
State: "Cairo",
},
},
{
Name: "Jack",
Age: 6,
Address: Address{
City: "Terra Haute",
State: "Indiana",
},
},
}
peopleTable.InsertMany(people)
You data is now saved in the DB !
Call the All()
func to retrieve all the data:
data,_ := peopleTable.All()
for i, person := range data {
fmt.Printf("%d. ID:%d Name:%s Age:%d City:%s \n", i+1, person.ID, person.Name, person.Age, person.Address.City)
}
Output:
1. ID:1 Name:John Age:30 City:New York
2. ID:2 Name:Ahmed Age:45 City:Cairo
3. ID:3 Name:Jack Age:6 City:Terra Haute
Call the ById()
func to retrieve record with the given Id
result, _ := peopleTable.ById(2)
person := result[0]
fmt.Printf("ID:%d Name:%s Age:%d City:%s \n", person.ID, person.Name, person.Age, person.Address.City)
Output:
ID:2 Name:Ahmed Age:45 City:Cairo
Call the ByName()
func to retrieve record with the given Id
result, _ := peopleTable.ByName("John")
person := result[0]
fmt.Printf("ID:%d Name:%s Age:%d City:%s \n", person.ID, person.Name, person.Age, person.Address.City)
Output:
ID:1 Name:John Age:30 City:New York
Call the DeleteId()
func to delete the record with the given Id
peopleTable.DeleteId(1)
Call the DeleteByName()
func to delete the record with the given name:
peopleTable.DeleteByName("Ahmed")
The following deletes the table:
peopleTable.DeleteTableIfExists()
Close the DB connection
// Close DB connection
peopleTable.Close()