aboutsummaryrefslogtreecommitdiff
path: root/store.go
blob: 4ab0f4bb6716ca3c42a0ab791d1da7aee7305f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package oblast

import (
	"reflect"

	"go.xyrillian.de/oblast/internal"
)

// Store is the main interface of this library.
// It holds information on how to read and write data into record type R, and can also be used to execute autogenerated queries if the respective [PlanOption]s were provided during [NewStore].
type Store[R any] struct {
	plan internal.Plan
}

// NewStore initializes a store for record type R.
func NewStore[R any](dialect Dialect, opts ...PlanOption) (Store[R], error) {
	var popts internal.PlanOpts
	for _, opt := range opts {
		opt(&popts)
	}
	plan, err := internal.BuildPlan(reflect.TypeFor[R](), dialect, popts)
	return Store[R]{plan}, err
}

// MustNewStore is like [NewStore], but panics on error.
func MustNewStore[R any](dialect Dialect, opts ...PlanOption) Store[R] {
	store, err := NewStore[R](dialect, opts...)
	if err != nil {
		panic(err.Error())
	}
	return store
}