@orbit/records
Version:
A flexible data access and synchronization layer.
142 lines (141 loc) • 4.43 kB
TypeScript
import { Evented } from '@orbit/core';
import { Dict } from '@orbit/utils';
import { ArrayValidationOptions } from '@orbit/validators';
export interface FieldValidationOptions {
required?: boolean;
[key: string]: unknown;
}
export interface AttributeDefinition {
type?: string;
serialization?: Dict<unknown>;
deserialization?: Dict<unknown>;
validation?: FieldValidationOptions & {
notNull?: boolean;
};
meta?: Dict<unknown>;
}
export interface HasOneRelationshipDefinition {
kind: 'hasOne';
type?: string | string[];
model?: string | string[];
inverse?: string;
dependent?: 'remove';
validation?: FieldValidationOptions & {
notNull?: boolean;
};
meta?: Dict<unknown>;
}
export interface HasManyRelationshipDefinition {
kind: 'hasMany';
type?: string | string[];
model?: string | string[];
inverse?: string;
dependent?: 'remove';
validation?: FieldValidationOptions & ArrayValidationOptions;
meta?: Dict<unknown>;
}
export declare type RelationshipDefinition = HasOneRelationshipDefinition | HasManyRelationshipDefinition;
export interface KeyDefinition {
/**
* @deprecated since v0.17 - not used by any standard serializers
*/
primaryKey?: boolean;
validation?: FieldValidationOptions & {
notNull?: boolean;
};
meta?: Dict<unknown>;
}
export interface ModelDefinition {
keys?: Dict<KeyDefinition>;
attributes?: Dict<AttributeDefinition>;
relationships?: Dict<RelationshipDefinition>;
}
/**
* Settings used to initialze and/or upgrade schemas.
*/
export interface RecordSchemaSettings {
/**
* Schema version. Defaults to 1.
*/
version?: number;
/**
* Function used to generate record IDs.
*/
generateId?: (model?: string) => string;
/**
* Function used to pluralize names.
*
* @deprecated
*/
pluralize?: (word: string) => string;
/**
* Function used to singularize names.
*
* @deprecated
*/
singularize?: (word: string) => string;
/**
* Map of model definitions.
*/
models?: Dict<ModelDefinition>;
}
export declare type RecordSchemaEvent = 'upgrade';
export interface RecordSchema extends Evented<RecordSchemaEvent> {
}
/**
* A `Schema` defines the models allowed in a source, including their keys,
* attributes, and relationships. A single schema may be shared across multiple
* sources.
*/
export declare class RecordSchema {
private _models;
private _version;
constructor(settings?: RecordSchemaSettings);
/**
* Version
*/
get version(): number;
/**
* Upgrades Schema to a new version with new settings.
*
* Emits the `upgrade` event to cue sources to upgrade their data.
*/
upgrade(settings?: RecordSchemaSettings): Promise<void>;
/**
* Registers a complete set of settings
*/
protected _applySettings(settings: RecordSchemaSettings): void;
/**
* Generate an id for a given model type.
*/
generateId(type?: string): string;
/**
* A naive pluralization method.
*
* Deprecated in favor of inflectors now in @orbit/serializers
*
* @deprecated since v0.17, remove in v0.18
*/
pluralize(word: string): string;
/**
* A naive singularization method.
*
* Deprecated in favor of inflectors now in @orbit/serializers
*
* @deprecated since v0.17, remove in v0.18
*/
singularize(word: string): string;
get models(): Dict<ModelDefinition>;
getModel(type: string): ModelDefinition;
getAttribute(type: string, attribute: string): AttributeDefinition;
getKey(type: string, key: string): KeyDefinition;
getRelationship(type: string, relationship: string): RelationshipDefinition;
hasModel(type: string): boolean;
hasAttribute(type: string, attribute: string): boolean;
hasKey(type: string, key: string): boolean;
hasRelationship(type: string, relationship: string): boolean;
eachAttribute(type: string, callbackFn: (name: string, attribute: AttributeDefinition) => void): void;
eachKey(type: string, callbackFn: (name: string, key: KeyDefinition) => void): void;
eachRelationship(type: string, callbackFn: (name: string, relationship: RelationshipDefinition) => void): void;
_deprecateRelationshipModel(models: Dict<ModelDefinition>): void;
}