sheetsorm

package module
v0.0.0-...-280a614 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 13 Imported by: 0

README

Sheets-orm for Golang

Build Status

This is a very-simple orm-like library for Google Sheets, that we use in our internal administration tasks.

This project is currently under heavy development.

Example

package main

import (
	"context"
	"fmt"

	"github.com/pproj/sheetsorm"
)

type Record struct {
	Name  string `sheet:"A,uid"` // The "name" field of the record is considered the UID here, record lookups will be based on this column.
	Age   int    `sheet:"B"`
	Happy *bool  `sheet:"C,True=yes,False=no"`
}

func main() {

	// Create new Sheet instance

	cfg := sheetsorm.StructureConfig{
		DocID:    "", // Google sheets ID
		Sheet:    "", // Empty string means the default sheet here
		SkipRows: 1,  // The first row is a header, so we should skip it
	}

	srv, err := sheets.NewService(context.Background(), option.WithCredentialsFile("path/To/Credentials"))
	if err != nil {
		panic(err)
	}

	sheet := sheetsorm.NewSheet(srv, cfg)

	// Load a record

	var recordToLoad Record
	recordToLoad.Name = "Bob"

	sheet.GetRecord(context.Background(), &recordToLoad)

	fmt.Println(recordToLoad)

	// Update a record

	var recordToUpdate Record
	recordToUpdate.Name = "Alice"
	recordToUpdate.Age = 22

	sheet.UpdateRecords(context.Background(), &recordToUpdate) // Happy is not updated, because it was nil

	fmt.Println(recordToUpdate) // The updated record is read back entirely... so the Happy field will be filled here
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sheet

type Sheet interface {
	// GetRecord fetches a single record from the sheet, the passed struct must have its UID field filled, or it returns an error
	GetRecord(ctx context.Context, out interface{}) error

	// GetAllRecords returns all valid records from the the sheet, the argument must be a list of structs
	GetAllRecords(ctx context.Context, out interface{}) error

	// UpdateRecords take individual records, or list of records, or both as vararg. The UID field of each record must be filled, otherwise it returns an error
	UpdateRecords(ctx context.Context, records ...interface{}) error
}

type SheetImpl

type SheetImpl struct {
	// contains filtered or unexported fields
}

func (*SheetImpl) GetAllRecords

func (si *SheetImpl) GetAllRecords(ctx context.Context, out interface{}) error

func (*SheetImpl) GetRecord

func (si *SheetImpl) GetRecord(ctx context.Context, out interface{}) error

func (*SheetImpl) UpdateRecords

func (si *SheetImpl) UpdateRecords(ctx context.Context, records ...interface{}) error

UpdateRecords the corresponding uid field must be filled in the records in receives, if the uid can not be found in the table, it throws an error

type SheetInitializationOption

type SheetInitializationOption func(*SheetImpl)

func WithLogger

func WithLogger(l *zap.Logger) SheetInitializationOption

type StructureConfig

type StructureConfig struct {
	DocID string
	Sheet string

	SkipRows int
}

func (StructureConfig) Validate

func (st StructureConfig) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL