Migrate Cache and Database types into backend-plugin-api

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2023-01-04 10:44:04 +01:00
parent dcf3b33189
commit 5437fe488f
11 changed files with 146 additions and 144 deletions
@@ -14,7 +14,79 @@
* limitations under the License.
*/
import { PluginCacheManager } from '@backstage/backend-common';
import { JsonValue } from '@backstage/types';
/** @public */
export interface CacheService extends PluginCacheManager {}
/**
* Manages access to cache stores that plugins get.
*
* @public
*/
export interface CacheService {
/**
* Provides backend plugins cache connections for themselves.
*
* @remarks
*
* The purpose of this method is to allow plugins to get isolated data stores
* so that plugins are discouraged from cache-level integration and/or cache
* key collisions.
*/
getClient: (options?: CacheClientOptions) => CacheClient;
}
/**
* Options passed to {@link CacheClient.set}.
*
* @public
*/
export type CacheClientSetOptions = {
/**
* Optional TTL in milliseconds. Defaults to the TTL provided when the client
* was set up (or no TTL if none are provided).
*/
ttl?: number;
};
/**
* A pre-configured, storage agnostic cache client suitable for use by
* Backstage plugins.
*
* @public
*/
export interface CacheClient {
/**
* Reads data from a cache store for the given key. If no data was found,
* returns undefined.
*/
get(key: string): Promise<JsonValue | undefined>;
/**
* Writes the given data to a cache store, associated with the given key. An
* optional TTL may also be provided, otherwise it defaults to the TTL that
* was provided when the client was instantiated.
*/
set(
key: string,
value: JsonValue,
options?: CacheClientSetOptions,
): Promise<void>;
/**
* Removes the given key from the cache store.
*/
delete(key: string): Promise<void>;
}
/**
* Options given when constructing a {@link CacheClient}.
*
* @public
*/
export type CacheClientOptions = {
/**
* An optional default TTL (in milliseconds) to be set when getting a client
* instance. If not provided, data will persist indefinitely by default (or
* can be configured per entry at set-time).
*/
defaultTtl?: number;
};
@@ -14,7 +14,31 @@
* limitations under the License.
*/
import { PluginDatabaseManager } from '@backstage/backend-common';
import { Knex } from 'knex';
/** @public */
export interface DatabaseService extends PluginDatabaseManager {}
/**
* The DatabaseService manages access to databases that Plugins get.
*gs
* @public
*/
export interface DatabaseService {
/**
* getClient provides backend plugins database connections for itself.
*
* The purpose of this method is to allow plugins to get isolated data
* stores so that plugins are discouraged from database integration.
*/
getClient(): Promise<Knex>;
/**
* This property is used to control the behavior of database migrations.
*/
migrations?: {
/**
* skip database migrations. Useful if connecting to a read-only database.
*
* @defaultValue false
*/
skip?: boolean;
};
}
@@ -15,7 +15,12 @@
*/
export { coreServices } from './coreServices';
export type { CacheService } from './CacheService';
export type {
CacheService,
CacheClient,
CacheClientOptions,
CacheClientSetOptions,
} from './CacheService';
export type { ConfigService } from './ConfigService';
export type { DatabaseService } from './DatabaseService';
export type { DiscoveryService } from './DiscoveryService';