How to use @orbit/data - 10 common examples

To help you get started, we’ve selected a few @orbit/data examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github orbitjs / orbit / packages / @orbit / indexeddb / src / indexeddb-cache.ts View on Github external
return new Promise((resolve, reject) => {
      let request = Orbit.globals.indexedDB.deleteDatabase(this.dbName);

      request.onerror = (/* event */) => {
        // console.error('error deleting indexedDB', this.dbName);
        reject(request.error);
      };

      request.onsuccess = (/* event */) => {
        // console.log('success deleting indexedDB', this.dbName);
        resolve();
      };
    });
  }
github orbitjs / orbit / packages / @orbit / indexeddb / src / indexeddb-cache.ts View on Github external
return new Promise((resolve, reject) => {
      if (this._db) {
        resolve(this._db);
      } else {
        let request = Orbit.globals.indexedDB.open(this.dbName, this.dbVersion);

        request.onerror = (/* event */) => {
          // console.error('error opening indexedDB', this.dbName);
          reject(request.error);
        };

        request.onsuccess = (/* event */) => {
          // console.log('success opening indexedDB', this.dbName);
          const db = (this._db = request.result);
          resolve(db);
        };

        request.onupgradeneeded = (event: any) => {
          // console.log('indexedDB upgrade needed');
          const db = (this._db = event.target.result);
          if (event && event.oldVersion > 0) {
github orbitjs / orbit / packages / @orbit / jsonapi / src / jsonapi-request-processor.ts View on Github external
fetch(url: string, customSettings?: FetchSettings): Promise {
    let settings = this.initFetchSettings(customSettings);

    let fullUrl = url;
    if (settings.params) {
      fullUrl = this.urlBuilder.appendQueryParams(fullUrl, settings.params);
      delete settings.params;
    }

    let fetchFn = (Orbit as any).fetch || Orbit.globals.fetch;

    // console.log('fetch', fullUrl, settings, 'polyfill', fetchFn.polyfill);

    if (settings.timeout) {
      let timeout = settings.timeout;
      delete settings.timeout;

      return new Promise((resolve, reject) => {
        let timedOut: boolean;

        let timer = Orbit.globals.setTimeout(() => {
          timedOut = true;
          reject(new NetworkError(`No fetch response within ${timeout}ms.`));
        }, timeout);

        fetchFn(fullUrl, settings)
github sillsdev / web-languageforge / src / SIL.XForge.Scripture / ClientApp / src / xforge-common / json-api.service.ts View on Github external
private query(
    localQueryExpression: QueryOrExpression,
    remoteQueryExpression: QueryOrExpression,
    include: string[][]
  ): QueryObservable {
    const localQuery = buildQuery(localQueryExpression, {}, undefined, this.store.queryBuilder);
    const remoteQuery = buildQuery(
      remoteQueryExpression,
      this.getRemoteQueryOptions(RequestType.OfflineFirst, include),
      undefined,
      this.store.queryBuilder
    );

    // initialize subject with current cached results
    const changes$ = new BehaviorSubject>(this.getQueryResults(localQuery));

    // listen for any changes resulting from the remote query
    const handler = (transform: Transform, results: PatchResultData[]) => {
      this.ngZone.run(() => {
        if (this.isChangeApplicable(remoteQuery, transform, results, include)) {
          changes$.next(this.getQueryResults(localQuery));
        }
github sillsdev / web-languageforge / src / SIL.XForge.Scripture / ClientApp / src / xforge-common / json-api.service.ts View on Github external
private query(
    localQueryExpression: QueryOrExpression,
    remoteQueryExpression: QueryOrExpression,
    include: string[][]
  ): QueryObservable {
    const localQuery = buildQuery(localQueryExpression, {}, undefined, this.store.queryBuilder);
    const remoteQuery = buildQuery(
      remoteQueryExpression,
      this.getRemoteQueryOptions(RequestType.OfflineFirst, include),
      undefined,
      this.store.queryBuilder
    );

    // initialize subject with current cached results
    const changes$ = new BehaviorSubject>(this.getQueryResults(localQuery));

    // listen for any changes resulting from the remote query
    const handler = (transform: Transform, results: PatchResultData[]) => {
      this.ngZone.run(() => {
        if (this.isChangeApplicable(remoteQuery, transform, results, include)) {
          changes$.next(this.getQueryResults(localQuery));
        }
      });
github sillsdev / web-languageforge / src / SIL.XForge.Scripture / ClientApp / src / xforge-common / json-api.service.ts View on Github external
case 'replaceRelatedRecord':
          const updateRelated = operation as
            | AddToRelatedRecordsOperation
            | RemoveFromRelatedRecordsOperation
            | ReplaceRelatedRecordsOperation
            | ReplaceRelatedRecordOperation;
          transformRecords.push([updateRelated.record, updateRelated.relationship]);
          break;
      }

      for (const [transformRecord, transformRelationship] of transformRecords) {
        // check if the transformed record matches record being queried
        // if performing a related record query, ensure that the transformed relationship matches the queried
        // relationship
        if (
          equalRecordIdentities(queryRecord, transformRecord) &&
          (queryRelationship == null || queryRelationship.name === transformRelationship)
        ) {
          return true;
        }

        // check if the type of the transformed record matches the type being queried
        if (queryType === transformRecord.type) {
          return true;
        }

        // check if the type of the transformed record matches one of the included types
        if (includedTypes.has(transformRecord.type)) {
          return true;
        }

        // check if the transformed record is one of the related records being queried
github orbitjs / orbit / packages / @orbit / jsonapi / src / lib / queries.ts View on Github external
function parseSortExpression(source: JSONAPISource, sortExpression) {
  if (sortExpression.field.op === 'attribute') {
    const [attribute] = sortExpression.field.args;
    // Note: We don't know the `type` of the attribute here, so passing `null`
    const resourceAttribute = source.serializer.resourceAttribute(null, attribute);
    return (sortExpression.order === 'descending' ? '-' : '') + resourceAttribute;
  }
  throw new QueryExpressionParseError('Query expression could not be parsed.', sortExpression.field);
}
github orbitjs / orbit / packages / @orbit / jsonapi / src / lib / get-operators.ts View on Github external
return sortSpecifiers.map(sortSpecifier => {
    if (sortSpecifier.kind === 'attribute') {
      const attributeSort = sortSpecifier as AttributeSortSpecifier;

      // Note: We don't know the `type` of the attribute here, so passing `null`
      const resourceAttribute = source.serializer.resourceAttribute(null, attributeSort.attribute);
      return (sortSpecifier.order === 'descending' ? '-' : '') + resourceAttribute;
    }
    throw new QueryExpressionParseError(`Sort specifier ${sortSpecifier.kind} not recognized for JSONAPISource.`, sortSpecifier);
  }).join(',');
}
github orbitjs / orbit / packages / @orbit / jsonapi / src / jsonapi-url-builder.ts View on Github external
.map(sortSpecifier => {
        if (sortSpecifier.kind === 'attribute') {
          const attributeSort = sortSpecifier as AttributeSortSpecifier;

          // Note: We don't know the `type` of the attribute here, so passing `null`
          const resourceAttribute = this.serializer.resourceAttribute(
            null,
            attributeSort.attribute
          );
          return (
            (sortSpecifier.order === 'descending' ? '-' : '') +
            resourceAttribute
          );
        }
        throw new QueryExpressionParseError(
          `Sort specifier ${sortSpecifier.kind} not recognized for JSONAPISource.`,
          sortSpecifier
        );
      })
      .join(',');
github sillsdev / web-languageforge / src / SIL.XForge.Scripture / ClientApp / src / xforge-common / orbit-service.ts View on Github external
async init(accessToken: string): Promise {
    const schemaDef = await this.http
      .get(`${NAMESPACE}/schema`, { headers: { 'Content-Type': 'application/json' } })
      .toPromise();
    schemaDef.generateId = () => objectId();
    this._schema = new Schema(schemaDef);

    this.bucket = new IndexedDBBucket({
      namespace: 'xforge-state'
    });

    this._store = new XForgeStore({
      schema: this._schema,
      bucket: this.bucket
    });

    this.remote = new XForgeJSONAPISource({
      schema: this._schema,
      bucket: this.bucket,
      name: REMOTE,
      host: this.locationService.origin,
      namespace: NAMESPACE