Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chainable query builder interfaces #241

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 80 additions & 16 deletions addon/-private/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,69 @@ export default class Cache {
*/
retrieveRecordData(type: string, id: string): Record | undefined {
deprecate(
'`Cache#retrieveRecordData(type, id)` is deprecated, use `Cache#peekRecordData(type, id)`.'
'`Cache#retrieveRecordData(type, id)` is deprecated, use `Cache#peekRecordData({ type, id })`.'
);
return this.peekRecordData(type, id);
return this.peekRecordData({ type, id });
}

peekRecordData(type: string, id: string): Record | undefined {
return this._sourceCache.getRecordSync({ type, id });
peekRecordData(
identifier: RecordIdentity | string,
id?: string
): Record | undefined {
if (typeof identifier === 'string' && id) {
deprecate(
'`Cache#peekRecordData(type, id)` is deprecated, use `Cache#peekRecordData({ type, id })`.'
);
identifier = { type: identifier, id };
}
if (typeof identifier !== 'object') {
throw new TypeError(
'peekRecordData should be called with an identifier.'
);
}
return this._sourceCache.getRecordSync(identifier);
}

includesRecord(type: string, id: string): boolean {
return !!this.peekRecordData(type, id);
includesRecord(identifier: RecordIdentity | string, id?: string): boolean {
if (typeof identifier === 'string' && id) {
deprecate(
'`Cache#includesRecord(type, id)` is deprecated, use `Cache#includesRecord({ type, id })`.'
);
identifier = { type: identifier, id };
}
if (typeof identifier !== 'object') {
throw new TypeError(
'includesRecord should be called with an identifier.'
);
}
return !!this.peekRecordData(identifier);
}

/**
* @deprecated
*/
retrieveRecord(type: string, id: string): Model | undefined {
deprecate(
'`Cache#retrieveRecord(type, id)` is deprecated, use `Cache#peekRecord(type, id)`.'
'`Cache#retrieveRecord(type, id)` is deprecated, use `Cache#peekRecord({ type, id })`.'
);
return this.peekRecord(type, id);
return this.peekRecord({ type, id });
}

peekRecord(type: string, id: string): Model | undefined {
if (this.includesRecord(type, id)) {
return this.lookup({ type, id }) as Model;
peekRecord(
identifier: RecordIdentity | string,
id?: string
): Model | undefined {
if (typeof identifier === 'string' && id) {
deprecate(
'`Cache#peekRecord(type, id)` is deprecated, use `Cache#peekRecord({ type, id })`.'
);
identifier = { type: identifier, id };
}
if (typeof identifier !== 'object') {
throw new TypeError('peekRecord should be called with an identifier.');
}
if (this.includesRecord(identifier)) {
return this.lookup(identifier) as Model;
}
return undefined;
}
Expand All @@ -108,7 +145,10 @@ export default class Cache {
keyName: string,
keyValue: string
): Model | undefined {
return this.peekRecord(type, this.recordIdFromKey(type, keyName, keyValue));
return this.peekRecord({
type,
id: this.recordIdFromKey(type, keyName, keyValue)
});
}

recordIdFromKey(type: string, keyName: string, keyValue: string): string {
Expand Down Expand Up @@ -253,26 +293,50 @@ export default class Cache {
return liveQuery;
}

/**
* @deprecated
*/
find(type: string, id?: string): Model | Model[] {
if (id === undefined) {
return this.findRecords(type);
deprecate(
'`Cache#find(type)` is deprecated, use `Store#findRecords(type).peek()`.'
);
return this.query(q => q.findRecords(type)) as Model[];
} else {
return this.findRecord(type, id);
deprecate(
'`Cache#find(type, id)` is deprecated, use `Store#findRecord({ type, id }).peek()`.'
);
return this.query(q => q.findRecord({ type, id })) as Model[];
}
}

/**
* @deprecated
*/
findAll(type: string, options?: object): Model[] {
deprecate(
'`Cache.findAll(type)` is deprecated, use `Cache.findRecords(type)`.'
'`Cache#findAll(type)` is deprecated, use `Store#findRecords(type).peek()`.'
);
return this.findRecords(type, options);
return this.query(q => q.findRecords(type), options) as Model[];
}

/**
* @deprecated
*/
findRecord(type: string, id: string, options?: object): Model {
deprecate(
'`Cache#findRecord(type, id)` is deprecated, use `Store#findRecord({ type, id }).peek()`.'
);
return this.query(q => q.findRecord({ type, id }), options) as Model;
}

/**
* @deprecated
*/
findRecords(type: string, options?: object): Model[] {
deprecate(
'`Cache#findRecords(type)` is deprecated, use `Store#findRecords(type).peek()`.'
);
return this.query(q => q.findRecords(type), options) as Model[];
}

Expand Down
2 changes: 1 addition & 1 deletion addon/-private/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Model extends EmberObject {
}

getData(): Record | undefined {
return this.store.cache.peekRecordData(this.type, this.id);
return this.store.cache.peekRecordData({ type: this.type, id: this.id });
}

getKey(field: string): string | undefined {
Expand Down
198 changes: 198 additions & 0 deletions addon/-private/query-builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { deepMerge } from '@orbit/utils';
import {
RecordIdentity,
FindRecordTerm,
FindRecordsTerm,
FindRelatedRecordTerm,
FindRelatedRecordsTerm
} from '@orbit/data';

import Store from './store';
import Model from './model';

export class FindRecordQueryBuilder extends FindRecordTerm {
store: Store;
_options = {};

constructor(store: Store, record: RecordIdentity) {
super(record);
this.store = store;
}

options(options: object) {
deepMerge(this._options, options);
return this;
}

then(...args: any[]): Promise<Model> {
return this._promise.then(...args);
}

catch(cb: any) {
return this._promise.catch(cb);
}

finally(cb: any) {
return this._promise.finally(cb);
}

peek(): Model | undefined {
return this.store.cache.query(
this.toQueryExpression(),
this._options
) as Model;
}

private get _promise() {
return this.store.query(this.toQueryExpression(), this._options);
}
}

export class FindRecordsQueryBuilder extends FindRecordsTerm {
store: Store;
_options = {};
_live = false;

constructor(store: Store, typeOrIdentities?: string | RecordIdentity[]) {
super(typeOrIdentities);
this.store = store;
}

options(options: object) {
deepMerge(this._options, options);
return this;
}

live() {
this._live = true;
return this;
}

then(...args: any[]): Promise<Model[]> {
return this._promise.then(...args);
}

catch(cb: any) {
return this._promise.catch(cb);
}

finally(cb: any) {
return this._promise.finally(cb);
}

peek(): Model[] | any {
if (this._live) {
return this.store.cache.liveQuery(
this.toQueryExpression(),
this._options
);
}
return this.store.cache.query(this.toQueryExpression(), this._options);
}

private get _promise() {
return this.store
.query(this.toQueryExpression(), this._options)
.then(result => {
if (this._live) {
return this.peek();
}
return result;
});
}
}

export class FindRelatedRecordQueryBuilder extends FindRelatedRecordTerm {
store: Store;
_options = {};

constructor(store: Store, record: RecordIdentity, relationship: string) {
super(record, relationship);
this.store = store;
}

options(options: object) {
deepMerge(this._options, options);
return this;
}

then(...args: any[]): Promise<Model> {
return this._promise.then(...args);
}

catch(cb: any) {
return this._promise.catch(cb);
}

finally(cb: any) {
return this._promise.finally(cb);
}

peek(): Model | null {
return this.store.cache.query(
this.toQueryExpression(),
this._options
) as Model | null;
}

private get _promise() {
return this.store.query(this.toQueryExpression(), this._options);
}
}

export class FindRelatedRecordsQueryBuilder extends FindRelatedRecordsTerm {
store: Store;
_options = {};
_live = false;

constructor(store: Store, record: RecordIdentity, relationship: string) {
super(record, relationship);
this.store = store;
}

options(options: object) {
deepMerge(this._options, options);
return this;
}

live() {
this._live = true;
return this;
}

then(...args: any[]): Promise<Model[]> {
return this._promise.then(...args);
}

catch(cb: any) {
return this._promise.catch(cb);
}

finally(cb: any) {
return this._promise.finally(cb);
}

peek(): Model[] | any {
if (this._live) {
return this.store.cache.liveQuery(
this.toQueryExpression(),
this._options
);
}
return this.store.cache.query(
this.toQueryExpression(),
this._options
) as Model[];
}

private get _promise() {
return this.store
.query(this.toQueryExpression(), this._options)
.then(result => {
if (this._live) {
return this.peek();
}
return result;
});
}
}
Loading