Files
m3o-go/examples/db
2021-11-17 22:21:48 +00:00
..
2021-11-03 15:36:34 +00:00
2021-11-17 22:21:48 +00:00
2021-11-03 15:36:34 +00:00
2021-11-09 10:50:05 +00:00
2021-11-03 15:36:34 +00:00
2021-11-17 16:55:33 +00:00
2021-11-17 22:21:48 +00:00

Db

An m3o.com API. For example usage see m3o.com/Db/api.

Endpoints:

ListTables

List tables in the DB

https://m3o.com/db/api#ListTables

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// List tables in the DB
func ListTables() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.ListTables(&db.ListTablesRequest{
		
	})
	fmt.Println(rsp, err)
	
}

Read

Read data from a table. Lookup can be by ID or via querying any field in the record.

https://m3o.com/db/api#Read

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Read data from a table. Lookup can be by ID or via querying any field in the record.
func ReadRecords() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.Read(&db.ReadRequest{
		Query: "age == 43",
Table: "users",

	})
	fmt.Println(rsp, err)
	
}

Truncate

Truncate the records in a table

https://m3o.com/db/api#Truncate

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Truncate the records in a table
func TruncateTable() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.Truncate(&db.TruncateRequest{
		Table: "users",

	})
	fmt.Println(rsp, err)
	
}

DropTable

Drop a table in the DB

https://m3o.com/db/api#DropTable

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Drop a table in the DB
func DropTable() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.DropTable(&db.DropTableRequest{
		Table: "users",

	})
	fmt.Println(rsp, err)
	
}

Count

Count records in a table

https://m3o.com/db/api#Count

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Count records in a table
func CountEntriesInAtable() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.Count(&db.CountRequest{
		Table: "users",

	})
	fmt.Println(rsp, err)
	
}

RenameTable

Rename a table

https://m3o.com/db/api#RenameTable

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Rename a table
func RenameTable() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.RenameTable(&db.RenameTableRequest{
		From: "events",
To: "events_backup",

	})
	fmt.Println(rsp, err)
	
}

Create

Create a record in the database. Optionally include an "id" field otherwise it's set automatically.

https://m3o.com/db/api#Create

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Create a record in the database. Optionally include an "id" field otherwise it's set automatically.
func CreateArecord() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.Create(&db.CreateRequest{
		Record: map[string]interface{}{
	"isActive": true,
	"id": "1",
	"name": "Jane",
	"age": 42,
},
Table: "users",

	})
	fmt.Println(rsp, err)
	
}

Update

Update a record in the database. Include an "id" in the record to update.

https://m3o.com/db/api#Update

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Update a record in the database. Include an "id" in the record to update.
func UpdateArecord() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.Update(&db.UpdateRequest{
		Record: map[string]interface{}{
	"id": "1",
	"age": 43,
},
Table: "users",

	})
	fmt.Println(rsp, err)
	
}

Delete

Delete a record in the database by id.

https://m3o.com/db/api#Delete

package example

import(
	"fmt"
	"os"

	"go.m3o.com/db"
)

// Delete a record in the database by id.
func DeleteArecord() {
	dbService := db.NewDbService(os.Getenv("M3O_API_TOKEN"))
	rsp, err := dbService.Delete(&db.DeleteRequest{
		Id: "1",
Table: "users",

	})
	fmt.Println(rsp, err)
	
}