You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boundary/internal/db
Johan Brandhorst-Satzkorn 7f9b294a7e
refact(all): Use testing.TB for test helpers
4 years ago
..
assert Update to go-kms-wrapping version 2, and plugin-based KMS (#1901) 4 years ago
common feature (db): Add support for multi-column PKs (#1658) 4 years ago
db_test Switch to buf for protobuf generation (#1944) 4 years ago
sanitize Add sentinel and sanitize package (#1353) 5 years ago
schema refactor(session connections): Refactor connection closure 4 years ago
sentinel Add sentinel and sanitize package (#1353) 5 years ago
sqltest Add DW network address dimension tables (#1855) 4 years ago
timestamp Switch to buf for protobuf generation (#1944) 4 years ago
README.md refactor: Update gorm deps (#1591) 4 years ago
backoff.go basic internal db interfaces and Gorm implementation 6 years ago
clause.go chore: upgrade gofumpt to v0.3.1 (#2028) 4 years ago
db.go refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785) 4 years ago
db_test.go refact: Add db.DB wrapper and refact all test fixtures to use it. (#1535) 4 years ago
domains_test.go refact: Add db.DB wrapper and refact all test fixtures to use it. (#1535) 4 years ago
id.go Update primary error functions to take a context, deprecate old functions (#1358) 5 years ago
id_test.go Add ability to generate pseudo-random IDs (#984) 5 years ago
option.go Update to go-kms-wrapping version 2, and plugin-based KMS (#1901) 4 years ago
option_test.go Add more opts when using Create + WithOnConflict (#1706) 4 years ago
package_registration.go refactor(schema): Add support for migration editions 4 years ago
read_writer.go feat(db): Suppress not found events during db lookups (#1979) 4 years ago
read_writer_ext_test.go Add more opts when using Create + WithOnConflict (#1706) 4 years ago
read_writer_oplog.go Update to go-kms-wrapping version 2, and plugin-based KMS (#1901) 4 years ago
read_writer_test.go feat(event): Remove BOUNDARY_DEVELOPER_ENABLE_EVENTS env var 4 years ago
testing.go refact(all): Use testing.TB for test helpers 4 years ago
testing_test.go refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785) 4 years ago

README.md

db package

Usage

Just some high-level usage highlights to get you started. Read the godocs for a complete list of capabilities and their documentation.

    dialect = postgres.New(postgres.Config{
			DSN: connectionUrl},
		)
    conn, _ := gorm.Open(dialect, &gorm.Config{})
    
    // Db implements both the Reader and Writer interfaces
    rw := Db{Tx: conn}
    
    // There are writer methods like: Create, Update and Delete
    // that will write Gorm struct to the db.  These writer methods
    // all support options for writing Oplog entries for the 
    // caller: WithOplog(yourWrapper, yourMetadata)
    // the caller is responsible for the transaction life cycle of the writer
    // and if an error is returned the caller must decide what to do with 
    // the transaction, which is almost always a rollback for the caller.
    err = rw.Create(context.Background(), user)
   
    // There are reader methods like: LookupByPublicId,  
    // LookupByName, SearchBy, LookupBy, etc
    // which will lookup resources for you and scan them into your Gorm struct
    err = rw.LookupByPublicId(context.Background(), foundUser)

    // There's reader ScanRows that facilitates scanning rows from 
    // a query into your Gorm struct
    where := "select * from test_users where name in ($1, $2)"
    rows, err := rw.Query(context.Background(), where, []interface{}{"alice", "bob"})
	defer rows.Close()
	for rows.Next() {
        user := db_test.NewTestUser()
        // scan the row into your Gorm struct
		if err := rw.ScanRows(rows, &user); err != nil {
            return err
        }
        // Do something with the Gorm user struct
    }

    // DoTx is a writer function that wraps a TxHandler 
    // in a retryable transaction.  You simply implement a
    // TxHandler that does your writes and hand the handler
    // to DoTx, which will wrap the writes in a retryable 
    // transaction with the retry attempts and backoff
    // strategy you specify via options.
    _, err = w.DoTx(
        context.Background(),
        10,           // ten retries
        ExpBackoff{}, // exponential backoff
        func(w Writer) error {
            // the TxHandler updates the user's friendly name
            return w.Update(context.Background(), user, []string{"FriendlyName"},
                // write oplogs for this update
                WithOplog(
                    InitTestWrapper(t),
                    oplog.Metadata{
                        "key-only":   nil,
                        "deployment": []string{"amex"},
                        "project":    []string{"central-info-systems", "local-info-systems"},
                    },
                ),
            )
        })