Skip to main content

RelationalStoreSupport

@webda/sql


Class: RelationalStoreSupport

Defined in: packages/sql/src/relational/support.ts:196

The engine-neutral behavior of a relational store, used by composition by each engine store: builds the plan from the store models, syncs the schema at init, resolves tables, creates the repositories, prints the schema command

Constructors​

Constructor​

new RelationalStoreSupport(host, dialect): RelationalStoreSupport

Defined in: packages/sql/src/relational/support.ts:231

Create a new RelationalStoreSupport instance

Parameters​

host​

RelationalStoreHost

the store

dialect​

RelationalDialect

its relational dialect

Returns​

RelationalStoreSupport

Properties​

appliedSteps​

appliedSteps: SchemaStep[] = []

Defined in: packages/sql/src/relational/support.ts:212

The steps the last sync applied


changes​

changes: SchemaChange[] = []

Defined in: packages/sql/src/relational/support.ts:216

The changes the last check found against the live versions


dialect​

readonly dialect: RelationalDialect

Defined in: packages/sql/src/relational/support.ts:233

its relational dialect


heartbeat?​

protected optional heartbeat?: Timeout

Defined in: packages/sql/src/relational/support.ts:224

Heartbeat


host​

protected readonly host: RelationalStoreHost

Defined in: packages/sql/src/relational/support.ts:232

the store


manager​

readonly manager: RelationalSchemaManager

Defined in: packages/sql/src/relational/support.ts:200

Manager


referencedTables​

protected referencedTables: object = {}

Defined in: packages/sql/src/relational/support.ts:204

The JSON tables the plan references (foreignKeysToJsonb), created before the DDL

Index Signature​

[identifier: string]: ReferencedTable


version?​

optional version?: SchemaVersion

Defined in: packages/sql/src/relational/support.ts:220

The entry of this version in the registry, once registered


versions​

readonly versions: SchemaVersionRegistry

Defined in: packages/sql/src/relational/support.ts:208

The versions of the store in the Registry

Methods​

appVersion()​

appVersion(): string

Defined in: packages/sql/src/relational/support.ts:280

The version of the application this store records

Returns​

string

the version


backfillCommand()​

backfillCommand(table, from, to, batch, connect): Promise<number>

Defined in: packages/sql/src/relational/support.ts:763

The schema backfill command: copy a column into another one

Parameters​

table​

string

the table

from​

string

the source column

to​

string

the target column

batch​

number

rows per statement

connect​

() => Promise<{ client: SQLClient; close: () => Promise<void>; }>

opens a connection

Returns​

Promise<number>

0


checkCommand()​

checkCommand(against, forceIncompatible, json): Promise<number>

Defined in: packages/sql/src/relational/support.ts:653

The schema check command: compare the plan with the live versions (or one version) and print the changes

Parameters​

against​

string | undefined

an identifier, application version or plan hash to compare with, instead of the live versions

forceIncompatible​

boolean

downgrade every blocking change to a warning

json​

boolean

print the changes as JSON

Returns​

Promise<number>

0, 1 when a change blocks, 2 when against matches no version


checkVersions()​

checkVersions(): Promise<LiveSchemaPlan[]>

Defined in: packages/sql/src/relational/support.ts:290

Read the live versions of the registry and compare the plan with theirs

Returns​

Promise<LiveSchemaPlan[]>

the plans of the live versions

Throws​

SchemaIncompatibleError when a change blocks

Throws​

SchemaRegistryError when the registry cannot be read: init fails closed


cleanStatements()​

cleanStatements(): string[]

Defined in: packages/sql/src/relational/support.ts:580

Clean statements

Returns​

string[]

the statements emptying every table of the plan, junctions and referencing tables first


contractCommand()​

contractCommand(dryRun, connect): Promise<number>

Defined in: packages/sql/src/relational/support.ts:690

The schema contract command: what no live version uses any more, printed or applied

Parameters​

dryRun​

boolean

print the statements without running them

connect​

() => Promise<{ client: SQLClient; close: () => Promise<void>; }>

opens a connection

Returns​

Promise<number>

0


createRepository()​

createRepository<T>(model, options): RelationalRepository<T>

Defined in: packages/sql/src/relational/support.ts:552

Create repository

Type Parameters​

T​

T extends ModelClass<Storable<any, any>>

Parameters​

model​

T

a model class

options​

the connection of the store

client​

() => SQLClient | undefined

the client, or a function returning the store's current one

prepare?​

() => Promise<void>

awaited before every statement

transact​

Transact

runs statements in one transaction on one connection

Returns​

RelationalRepository<T>

the repository

Throws​

WebdaError.CodeError MODEL_NOT_PLANNED when the model is not stored by this store


getSchemaPlan()​

getSchemaPlan(): RelationalSchemaPlan

Defined in: packages/sql/src/relational/support.ts:362

The relational plan of the store, built once from the metadata of its models (subclasses included); every application model is known so references to other stores get a column without foreign key

Returns​

RelationalSchemaPlan

the plan


importFromJson()​

importFromJson(source, options): Promise<ImportReport[]>

Defined in: packages/sql/src/relational/support.ts:449

Promote models from a JSON store (importFromJsonb): their rows are read through the JSON repository of the source, written through the relational repositories in one transaction (the deferred foreign keys let the links between imported rows point in any direction), and deleted from the source once committed when purge is set. A row whose key is already in the relational table is skipped. The models must already be listed by the store.

Parameters​

source​

SQLStore<any>

the JSON store holding the rows

options​

what to import

models?​

string[]

the model identifiers, every table root of the plan by default

purge?​

boolean

delete the imported rows from the source after the commit

repository​

(model, tx?) => RelationalRepository<any>

the relational repository of a model: the store's own (inside its event wrapper), or one bound to the handed-over connection

transact​

<R>(fn) => Promise<R>

runs the import in one transaction of the store: either one the repositories of the store join (the async chain transaction of withTransaction), or one on a connection it hands over

Returns​

Promise<ImportReport[]>

one report per model

Throws​

WebdaError.CodeError RELATIONAL_IMPORT_SOURCE when the source is this store or a relational store, MODEL_NOT_PLANNED for a model the store does not list, RELATIONAL_IMPORT_TABLE when the JSON table of a model is the relational one (name it differently with tables)


initError()​

initError(err): unknown

Defined in: packages/sql/src/relational/support.ts:350

The error init fails with: a schema refused by the live versions or an unreachable registry stops the application (FatalInitError), anything else is returned as is

Parameters​

err​

unknown

the error of init

Returns​

unknown

the error to throw


jsonTables()​

protected jsonTables(known): object

Defined in: packages/sql/src/relational/support.ts:386

The JSON tables a foreign key may reference: those of the models outside the store served by a JSON store of the same engine on the same database (foreignKeysToJsonb), keyed by their uuid column

Parameters​

known​

SchemaModelMetadata[]

every application model

Returns​

object

the tables by model identifier


readyRegistry()​

protected readyRegistry(): Promise<void>

Defined in: packages/sql/src/relational/support.ts:638

The registry, readable from a resolved command: the repositories are computed and the Registry store is initialized when it is not running yet

Returns​

Promise<void>


refuseRegistry()​

refuseRegistry(alternative): void

Defined in: packages/sql/src/relational/support.ts:266

The Registry must accept any model: a typed schema cannot be the catch-all store. The store depends on the Registry (its version registry), so the Registry is initialized first

Parameters​

alternative​

string

the JSON store of the engine, for the message

Returns​

void

Throws​

WebdaError.CodeError RELATIONAL_STORE_AS_REGISTRY


registerVersion()​

registerVersion(): Promise<void>

Defined in: packages/sql/src/relational/support.ts:307

Record this version as live and start its heartbeat

Returns​

Promise<void>

Throws​

SchemaRegistryError when the registry cannot be written


resolveTable()​

resolveTable(model): string | undefined

Defined in: packages/sql/src/relational/support.ts:529

Resolve table

Parameters​

model​

ModelClass

a model class

Returns​

string | undefined

its table, undefined when the model is not in the plan


resolveTableModel()​

resolveTableModel(model): string | undefined

Defined in: packages/sql/src/relational/support.ts:538

Resolve table model

Parameters​

model​

ModelClass

a model class

Returns​

string | undefined

the model its table was declared for, undefined when the model is not in the plan


retireCommand()​

retireCommand(version): Promise<number>

Defined in: packages/sql/src/relational/support.ts:741

The schema retire command: mark a version retired

Parameters​

version​

string

an identifier, application version or plan hash

Returns​

Promise<number>

0, 1 when no version matches


schemaCommand()​

schemaCommand(sql, diff, json, connect): Promise<number>

Defined in: packages/sql/src/relational/support.ts:593

The schema command: print the plan, its DDL or the differences with the database

Parameters​

sql​

boolean

print the CREATE statements of the whole plan

diff​

boolean

connect and print the statements alter would run, then the differences it never resolves (the columns of the live versions are not differences)

json​

boolean

print the plan as JSON

connect​

() => Promise<{ client: SQLClient; close: () => Promise<void>; }>

opens a connection for diff

Returns​

Promise<number>

0, 1 when diff found differences the store cannot resolve


stop()​

stop(): void

Defined in: packages/sql/src/relational/support.ts:337

Stop the heartbeat

Returns​

void


sync()​

sync(client, live?): Promise<SchemaStep[]>

Defined in: packages/sql/src/relational/support.ts:415

Bring the database to the plan (schemaSync) under the version registry: the JSON tables the plan references are created first, with the DDL of their store, then lock, read the live versions, check, DDL, register this version, unlock. A store whose registry may share the connection (SQLite) runs checkVersions and registerVersion itself, around the lock, and gives the live plans here

Parameters​

client​

SQLClient

a client whose statements run on one connection

live?​

LiveSchemaPlan[]

the plans of the live versions when the store checked them already

Returns​

Promise<SchemaStep[]>

the applied steps