github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/models/wasm_log.go (about)

     1  package models
     2  
     3  import (
     4  	"github.com/machinefi/w3bstream/pkg/depends/base/types"
     5  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
     6  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
     7  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
     8  )
     9  
    10  // WasmLog database model event
    11  // @def primary                     ID
    12  // @def unique_index UI_wasm_log_id WasmLogID
    13  // @def index        I_project_name ProjectName
    14  // @def index        I_log_time     LogTime
    15  // @def index        I_created_at   CreatedAt
    16  // @def index        I_level        Level
    17  // @def index        I_src          Src
    18  // @def index        I_instance_id  InstanceID
    19  // @def index        I_event_id     EventID
    20  //
    21  //go:generate toolkit gen model WasmLog --database DB
    22  type WasmLog struct {
    23  	datatypes.PrimaryID
    24  	RelWasmLog
    25  	WasmLogInfo
    26  	datatypes.OperationTimes
    27  }
    28  
    29  type RelWasmLog struct {
    30  	WasmLogID types.SFID `db:"f_wasm_log_id" json:"wasmLogID"`
    31  }
    32  
    33  type WasmLogInfo struct {
    34  	ProjectName string     `db:"f_project_name"               json:"projectName"`
    35  	AppletName  string     `db:"f_applet_name,default=''"     json:"appletName"`
    36  	InstanceID  types.SFID `db:"f_instance_id,default='0'"    json:"instanceID"`
    37  	EventID     string     `db:"f_event_id,default=''"        json:"eventID"`
    38  	Src         string     `db:"f_src,default=''"             json:"src"`
    39  	Level       string     `db:"f_level,default=''"           json:"level"`
    40  	LogTime     int64      `db:"f_log_time,default='0'"       json:"logTime"`
    41  	Msg         string     `db:"f_msg,default='',size=1024"   json:"msg"`
    42  }
    43  
    44  func BatchCreateWasmLogs(db sqlx.DBExecutor, vs ...*WasmLog) error {
    45  	vals := make([]any, 0, len(vs)*8)
    46  	for _, v := range vs {
    47  		vals = append(vals,
    48  			v.WasmLogID,
    49  			v.ProjectName,
    50  			v.AppletName,
    51  			v.InstanceID,
    52  			v.EventID,
    53  			v.Src,
    54  			v.Level,
    55  			v.LogTime,
    56  			v.Msg,
    57  			v.CreatedAt,
    58  			v.UpdatedAt,
    59  		)
    60  	}
    61  	if len(vals) == 0 {
    62  		return nil
    63  	}
    64  
    65  	m := &WasmLog{}
    66  	t := db.T(m)
    67  
    68  	// return sqlx.NewTasks(db).With(
    69  	// 	func(db sqlx.DBExecutor) error {
    70  	_, err := db.Exec(builder.Insert().Into(t).Values(
    71  		builder.Cols(
    72  			m.ColWasmLogID().Name,
    73  			m.ColProjectName().Name,
    74  			m.ColAppletName().Name,
    75  			m.ColInstanceID().Name,
    76  			m.ColEventID().Name,
    77  			m.ColSrc().Name,
    78  			m.ColLevel().Name,
    79  			m.ColLogTime().Name,
    80  			m.ColMsg().Name,
    81  			m.ColCreatedAt().Name,
    82  			m.ColUpdatedAt().Name,
    83  		),
    84  		vals...,
    85  	))
    86  	// 		return err
    87  	// 	},
    88  	// ).Do()
    89  	return err
    90  }