1.0.0-alpha.77 • Published 3 years ago

@dandi/data v1.0.0-alpha.77

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

@dandi/data

The @dandi/data package provides basic types and helpers for data clients that connect databases and other data providers to your Dandi application. These types provide a standard interface for working with external data within Dandi.

Implementations

The interfaces defined in @dandi/data are intended to allow wrapping existing database client implementations (e.g. node-postgres) and extending their functionality with Dandi.

Configuration

Use a @dandi/config configuration provider to supply connection and authentication information to the @dandi/data client. This example uses AwsSsmConfigClient and the @dandi-contrib/data-pg client.

import { AwsSsmConfigClient } from '@dandi-contrib/config-aws-ssm'
import { DandiApplication } from '@dandi/core'
import { PgDbModule } from '@dandi-contrib/data-pg'

const myApp = new DandiApplication({
  providers: [

    ...

    // database
    PgDbModule,

    AwsSsmConfigClient.provider(DbConnectionInfo.configToken(`myapp-postgres-connection-info`)),
    AwsSsmConfigClient.provider(DbUserCredentials.configToken(`myapp-postgres-credentials`)),

    ...

  ],
})

API Reference

This reference describes the generic interfaces defined by @dandi/data. Each implementation may vary, but this documentation describes the intention of the interfaces.

DbConnectionInfo

A model for describing connection info for a database connection. Used with @dandi/config to allow retrieving from a configuration provider like AWS SSM (see @dandi-contrib/config-aws-ssm.

DbUserCredentials

A model for describing user credentials for authenticating with a database. Used with @dandi/config to allow retrieving from a configuration provider like AWS SSM (see @dandi-contrib/config-aws-ssm.

DbQueryable

The most basic client interface for working with a database.

  • query(cmd: string, ...args: any[]): Promise<any[]> - sends a parameterized query to the underlying database client and returns the resulting rows directly, without any model conversion or validation.

  • queryModel<T>(model: Constructor<T>, cmd: string, ...args: any[]): Promise<T[]> - sends a parameterized query to the underlying database client and returns all rows, converting (and optionally validating) each row using the configured ModelBuilder.

  • queryModelSingle<T>(model: Constructor<T>, cmd: string, ...args: any[]): Promise<T> - same as queryModel, but only returns the first row. Throws a PgDbMultipleResultsError if more than one row is returned by the database. Returns null if no rows are returned.

DbClient

Extends DbQueryable to allow using transactions.

  • transaction<T>(transactionFn: TransactionFn<T>): Promise<T> - initiates a database transaction, creates a DbTransactionClient instance, and uses it to invoke the provided transactionFn.

DbTransactionClient

An extension of DbQueryable that is used during a transaction. Also extends Disposable. The transaction is automatically committed during dispose() unless an exception is thrown, in which case it is automatically rolled back.

  • commit() - commits the transaction.
  • rollback() - rolls back the transaction.

Transaction Example

export class MyModelManager {
  constructor(@Inject(DbClient) private dbClient: DbClient) {}

  public addModel(model: MyModelRequest): Promise<MyModel> {
    return this.dbClient.transaction(async (tran) => {
      const query1 = await tran.queryModelSingle(MyModel, INSERT_QUERY, model.name)
      const query2 = await tran.query(INSERT_MODEL_PERMISSION)
      return query1
    })
  }
}
1.0.0-alpha.76

3 years ago

1.0.0-alpha.77

3 years ago

1.0.0-alpha.74

3 years ago

1.0.0-alpha.73

3 years ago

1.0.0-alpha.75

3 years ago

1.0.0-alpha.72

3 years ago

1.0.0-alpha.71

4 years ago

1.0.0-alpha.70

4 years ago

1.0.0-alpha.69

4 years ago

1.0.0-alpha.67

4 years ago

1.0.0-alpha.68

4 years ago

1.0.0-alpha.66

4 years ago

1.0.0-alpha.65

4 years ago

1.0.0-alpha.63

4 years ago

1.0.0-alpha.64

4 years ago

1.0.0-alpha.62

4 years ago

1.0.0-alpha.61

4 years ago

1.0.0-alpha.60

4 years ago

1.0.0-alpha.59

4 years ago

1.0.0-alpha.58

4 years ago

1.0.0-alpha.56

4 years ago

1.0.0-alpha.57

4 years ago

1.0.0-alpha.55

4 years ago

1.0.0-alpha.54

4 years ago

1.0.0-alpha.52

4 years ago

1.0.0-alpha.53

4 years ago

1.0.0-alpha.50

5 years ago

1.0.0-alpha.49

5 years ago

1.0.0-alpha.48

5 years ago

1.0.0-alpha.47

5 years ago

1.0.0-alpha.46

5 years ago

1.0.0-alpha.45

5 years ago

1.0.0-alpha.44

5 years ago

1.0.0-alpha.43

5 years ago

1.0.0-alpha.42

5 years ago

1.0.0-alpha.41

5 years ago

1.0.0-alpha.40

5 years ago

1.0.0-alpha.39

5 years ago

1.0.0-alpha.38

6 years ago

1.0.0-alpha.37

6 years ago

1.0.0-alpha.36

6 years ago

1.0.0-alpha.35

6 years ago

1.0.0-alpha.34

6 years ago

1.0.0-alpha.33

6 years ago

1.0.0-alpha.32

6 years ago

1.0.0-alpha.31

6 years ago

1.0.0-alpha.30

6 years ago

1.0.0-alpha.29

6 years ago

1.0.0-alpha.28

6 years ago

1.0.0-alpha.27

6 years ago

1.0.0-alpha.26

6 years ago

1.0.0-alpha.25

6 years ago

1.0.0-alpha.24

6 years ago

1.0.0-alpha.23

6 years ago

1.0.0-alpha.22

6 years ago

1.0.0-alpha.21

6 years ago

1.0.0-alpha.20

6 years ago

1.0.0-alpha.19

6 years ago

1.0.0-alpha.18

6 years ago

1.0.0-alpha.17

6 years ago

1.0.0-alpha.16

6 years ago

1.0.0-alpha.15

6 years ago

1.0.0-alpha.14

6 years ago

1.0.0-alpha.13

6 years ago

1.0.0-alpha.12

6 years ago

1.0.0-alpha.11

6 years ago

1.0.0-alpha.10-2

6 years ago

1.0.0-alpha.10-1

6 years ago

1.0.0-alpha.10

6 years ago

1.0.0-alpha.8

6 years ago

1.0.0-alpha.7

6 years ago

1.0.0-alpha.6

6 years ago

1.0.0-alpha.5

6 years ago

1.0.0-alpha.3

6 years ago

1.0.0-alpha.2

6 years ago

1.0.0-alpha.1

6 years ago

1.0.0-pubtest.38

6 years ago