Skip to content

Instantly share code, notes, and snippets.

@tchak
Last active October 20, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchak/cb5e3c079bd637b0b0065525087b1de2 to your computer and use it in GitHub Desktop.
Save tchak/cb5e3c079bd637b0b0065525087b1de2 to your computer and use it in GitHub Desktop.
Orbit Store Interface
import { QueryResultData } from '@orbit/record-cache';
import { FilterQBParam, SortQBParam, PageQBParam, QueryOrExpression, TransformOrOperations } from '@orbit/data';
export interface Identifier {
type?: string;
id?: string;
lid?: string;
}
export interface LiveArray<Model> extends Iterable<Model> {
unsubscribe(): () => void;
invalidate(): void;
}
export interface FindTerm<Term, Result> {
then(): Promise<Result>;
fields(...field: string[]): Term;
// ember-data terminology ?
reload(): Term;
backgroundReload(): Term;
// orbit terminology ?
optimistic(): Term;
pessimistic(): Term;
}
export interface PeekTerm<Term, Result> extends FindTerm<Term, Result> {
peek(): Result;
}
export interface FindRecordTerm<M = Model | null> extends PeekTerm<FindRecordTerm, M> {
}
export interface FindRecordsTerm<M = Model[]> extends PeekTerm<FindRecordsTerm, M> {
filter(...params: FilterQBParam[]): FindRecordsTerm<M>;
sort(...params: SortQBParam[]): FindRecordsTerm<M>;
page(param: PageQBParam): FindRecordsTerm<M>;
include(include: any): FindRecordsTerm<M>;
live(): LiveArray<M>;
}
export interface FindRelatedRecordTerm<M = Model | null> extends PeekTerm<FindRelatedRecordTerm, M> {
}
export interface LoadRelatedRecordTerm<M = Model | null> extends FindTerm<LoadRelatedRecordTerm, M> {
}
export interface FindRelatedRecordsTerm<M = Model[]> extends PeekTerm<FindRelatedRecordsTerm, M> {
filter(...params: FilterQBParam[]): FindRelatedRecordsTerm<M>;
sort(...params: SortQBParam[]): FindRelatedRecordsTerm<M>;
page(param: PageQBParam): FindRelatedRecordsTerm<M>;
include(include: any): FindRelatedRecordsTerm<M>;
live(): LiveArray<M>;
}
export interface LoadRelatedRecordsTerm<M = Model | null> extends FindTerm<LoadRelatedRecordsTerm, M> {
filter(...params: FilterQBParam[]): LoadRelatedRecordsTerm<M>;
sort(...params: SortQBParam[]): LoadRelatedRecordsTerm<M>;
page(param: PageQBParam): LoadRelatedRecordsTerm<M>;
include(include: any): LoadRelatedRecordsTerm<M>;
}
export interface Relationship {};
export interface HasOneRelationship<M = Model> extends Relationship {
value: null | M;
id: null | string;
load(options?: object): LoadRelatedRecordTerm<M>;
set(identifier: Identifier, options?: object): Promise<null | M>;
}
export interface HasManyRelationship<M = Model> extends Relationship {
value: M[];
ids: string[];
load(options?: object): LoadRelatedRecordsTerm<M>;
add(identifier: Identifier, options?: object): Promise<void>;
remove(identifier: Identifier, options?: object): Promise<void>;
replace(identifiers: Identifier[], options?: object): Promise<M[]>;
}
export interface Model extends Identifier, Record<string, unknown> {
store: Store;
transaction: Transaction;
identifier: Identifier;
type: string;
lid: string;
id?: string;
// The record have no remote id.
isNew: boolean;
// The record is part of forked store.
isDirty: boolean;
// The record is part of forked store and it is beeing merged.
isSaving: boolean;
hasOne<M = Model>(relationship: string): HasOneRelationship<M>;
hasMany<M = Model>(relationship: string): HasManyRelationship<M>;
/*
Merge operations from the forked store in to
the main store and propagates them to sources.
*/
save<M = Model>(): Promise<M>;
/*
Reload the record from the sources.
*/
reload<M = Model>(options?: object): Promise<M>;
/*
Apply recent changes from main store to forked store.
*/
rebase<M = Model>(): Promise<M>;
/*
Reassociate this record back to the main store and restore attributes and
relationships values.
*/
rollback(): void;
/*
Remove record from the store. This will queue a remove operation
to your sources.
*/
remove(options?: object): Promise<void>;
/*
Unload the record from the cache. This will not queue a remove operation
to your sources, it just unloads the record from memory.
*/
unload(): void;
invalidate(): void;
}
export type Result<Model> = null | Model | Model[];
export interface MergeOptions {
coalesce?: boolean;
}
export interface BaseStore {
findRecord<M = Model>(identifier: Identifier): FindRecordTerm<M>;
findRecords<M = Model>(type: string | Identifier[]): FindRecordsTerm<M>;
findRelatedRecord<M = Model>(identifier: Identifier, relationship: string, options: any): FindRelatedRecordTerm<M>;
findRelatedRecords<M = Model>(identifier: Identifier, relationship: string, options: any): FindRelatedRecordsTerm<M>;
addRecord<M = Model>(record: Partial<M>, options?: object): Promise<M>;
updateRecord<M = Model>(record: Partial<M>, options?: object): Promise<M>;
removeRecord(identifier: Identifier, options?: object): Promise<void>;
watchRecords<M = Model>(records: LiveArray<M> | M[]): () => void;
lookup<M = Model>(result: QueryResultData): Result<M>;
query<M = Model>(queryOrExpression: QueryOrExpression, options?: object, id?: string): Promise<Result<M> | Result<M>[]>;
update<M = Model>(transformOrOperations: TransformOrOperations, options?: object, id?: string): Promise<Result<M> | Result<M>[]>;
cache: Cache;
isFork: boolean;
isTransaction: boolean;
fork(): ForkedStore;
merge(store: ForkedStore, options?: MergeOptions): Promise<void>;
}
export interface Store extends BaseStore {
isFork: false;
isTransaction: false;
base: undefined;
defaultTransaction: Transaction;
}
export interface ForkedStore extends BaseStore {
isFork: true;
base: Store | ForkedStore;
rebase(): Promise<void>;
rollback(transformId: string, relativePosition?: number): Promise<void>;
}
export interface Transaction extends ForkedStore {
isTransaction: true;
save(options?: MergeOptions): Promise<void>;
}
export interface Cache {
query<M = Model>(): Result<M> | Result<M>[];
unload(identifier: Identifier): void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment