How to use sp2 - 10 common examples

To help you get started, we’ve selected a few sp2 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 phenyl / phenyl / modules / memory-db / src / phenyl-memory-db-client.ts View on Github external
async replaceOne>(
    command: ReplaceOneCommand>
  ): Promise {
    const { entityName, entity } = command;
    const operation = PhenylStateUpdater.register(
      this.entityState,
      entityName,
      entity
    );
    // @ts-ignore operation is nonbreaking
    this.entityState = update(this.entityState, operation);
  }
github phenyl / phenyl / modules / redux / src / local-state-updater.ts View on Github external
static patch(
    state: LocalStateOf,
    versionDiff: VersionDiff
  ): GeneralUpdateOperation {
    const { entityName, id, versionId, prevVersionId } = versionDiff;
    const entityInfo = LocalStateFinder.getEntityInfo(state, {
      id,
      entityName
    }); // Not applicable diff.

    if (entityInfo.versionId !== prevVersionId) {
      return {};
    }

    const newOrigin = update(entityInfo.origin);
    const newHead = update(newOrigin, ...entityInfo.commits);
    return {
      $set: {
        [createDocumentPath("entities", entityName, id, "origin")]: newOrigin,
        [createDocumentPath(
          "entities",
          entityName,
          id,
          "versionId"
        )]: versionId,
        [createDocumentPath("entities", entityName, id, "head")]: newHead
      }
    };
  }
  /**
github phenyl / phenyl / modules / standards / src / foreign-query-wrapper.ts View on Github external
find: async (query: ForeignWhereQuery) => {
        if (resData.type !== "find" || query.foreign == null) return resData;
        const foreignEntitiesById = await this.getForeignEntities(
          resData.payload.entities,
          query.foreign
        );
        const { $set, $docPath } = $bind();
        return update(
          resData,
          // @ts-ignore: has no foreign key
          $set($docPath("payload", "foreign", "entities"), foreignEntitiesById)
        );
      },
github phenyl / phenyl / modules / redux / src / local-state-updater.ts View on Github external
static synchronize>(
    state: LocalStateOf,
    pushCommand: PushCommand,
    localCommits: Array
  ): GeneralUpdateOperation {
    const { entityName, id, operations, versionId } = pushCommand;
    const entityInfo = LocalStateFinder.getEntityInfo(state, {
      id,
      entityName
    });
    const newOrigin = update(entityInfo.origin, ...operations, ...localCommits); // assert(localCommits.length === 0 || entityInfo.commits[0] === localCommits[0])

    const newCommits = entityInfo.commits.slice(localCommits.length);
    const newHead =
      newCommits.length > 0 ? update(newOrigin, ...newCommits) : null;
    return {
      $set: {
        [createDocumentPath("entities", entityName, id)]: {
          origin: newOrigin,
          versionId,
          commits: newCommits,
          head: newHead
        }
      }
    };
  }
  /**
github phenyl / phenyl / modules / standards / src / encrypt-password-in-request-data.ts View on Github external
Object.keys(operation).forEach((key: any) => {
        // @ts-ignore: Partial has no index signature.
        const password = getNestedValue(operation[key], passwordPropName);

        if (password) {
          const { $set, $docPath } = $bind();
          operationWithEncryptedPass = update(
            operationWithEncryptedPass,
            $set($docPath(key, passwordPropName), encrypt(password))
          );
        }
      });
github phenyl / phenyl / modules / memory-db / src / phenyl-memory-db-client.ts View on Github external
command: SingleInsertCommand>
  ): Promise {
    const { entityName, value } = command;
    // @ts-ignore newValue must contain id
    const newValue: M[N] = value.id
      ? value
      : update(value, {
          id: timeStampWithRandomString()
        });
    const operation = PhenylStateUpdater.register(
      this.entityState,
      entityName,
      newValue
    );
    // @ts-ignore operation is nonbreaking
    this.entityState = update(this.entityState, operation);
    return newValue;
  }
  /**
github phenyl / phenyl / modules / memory-db / src / phenyl-memory-db-client.ts View on Github external
async updateAndFetch>(
    command: MultiUpdateCommand
  ): Promise> {
    const { entityName, where } = command; // TODO Performance issue: find() runs twice for just getting N

    const values = PhenylStateFinder.find(this.entityState, {
      entityName,
      where
    });
    const ids = values.map(value => value.id);
    const operation = PhenylStateUpdater.updateMulti(this.entityState, command);
    // @ts-ignore operation is nonbreaking
    this.entityState = update(this.entityState, operation);
    const updatedValues = PhenylStateFinder.getByIds(this.entityState, {
      ids,
      entityName
    });
    return updatedValues;
  }
  /**
github phenyl / phenyl / modules / redux / src / local-state-updater.ts View on Github external
pushCommand: PushCommand,
    localCommits: Array
  ): GeneralUpdateOperation {
    const { entityName, id, operations, versionId } = pushCommand;
    const entityInfo = LocalStateFinder.getEntityInfo(state, {
      id,
      entityName
    });
    const newOrigin = update(entityInfo.origin, ...operations, ...localCommits); // assert(localCommits.length === 0 || entityInfo.commits[0] === localCommits[0])

    const newCommits = entityInfo.commits.slice(localCommits.length);
    const newHead =
      newCommits.length > 0 ? update(newOrigin, ...newCommits) : null;
    return {
      $set: {
        [createDocumentPath("entities", entityName, id)]: {
          origin: newOrigin,
          versionId,
          commits: newCommits,
          head: newHead
        }
      }
    };
  }
  /**
github phenyl / phenyl / modules / standards / src / encrypt-password-in-request-data.ts View on Github external
export function encryptPasswordInRequestData(
  reqData: GeneralUserEntityRequestData,
  passwordPropName: DocumentPath,
  encrypt: EncryptFunction
): GeneralUserEntityRequestData {
  switch (reqData.method) {
    case "insertOne":
    case "insertAndGet": {
      const { payload } = reqData;
      const { value } = payload;
      const password = getNestedValue(value, passwordPropName);

      if (password) {
        const { $set, $docPath } = $bind();
        const valueWithEncryptedPass = update(
          value,
          // @ts-ignore password always exists
          $set($docPath(passwordPropName), encrypt(password))
        );
        const { $set: $OtherSet, $docPath: $otherDocPath } = $bind<
          typeof reqData
        >();
        return update(
          reqData,
          $OtherSet($otherDocPath("payload", "value"), valueWithEncryptedPass)
        );
      } else {
github phenyl / phenyl / modules / standards / src / encrypt-password-in-request-data.ts View on Github external
const valuesWithEncryptedPass = values.map(value => {
        const password = getNestedValue(value, passwordPropName);

        if (password) {
          const { $set, $docPath } = $bind();
          return update(
            value,
            // @ts-ignore password always exists
            $set($docPath(passwordPropName), encrypt(password))
          );
        } else {
          return value;
        }
      });
      const { $set, $docPath } = $bind();