How to use @graphile/lru - 5 common examples

To help you get started, we’ve selected a few @graphile/lru 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 graphile / graphile-engine / packages / graphile-build-pg / src / pgPrepareAndRun.ts View on Github external
values: any
) {
  const connection: unknown = pgClient["connection"];
  if (
    !values ||
    POSTGRAPHILE_PREPARED_STATEMENT_CACHE_SIZE < 1 ||
    typeof connection !== "object" ||
    !connection ||
    typeof connection[PARSED_STATEMENTS] !== "object" ||
    !connection[PARSED_STATEMENTS]
  ) {
    return pgClient.query(text, values);
  } else {
    const name = hash(text);
    if (!connection[GRAPHILE_PREPARED_STATEMENT_CACHE]) {
      connection[GRAPHILE_PREPARED_STATEMENT_CACHE] = new LRU({
        maxLength: POSTGRAPHILE_PREPARED_STATEMENT_CACHE_SIZE,
        dispose(key) {
          if (connection[PARSED_STATEMENTS][key]) {
            pgClient
              .query(`deallocate ${pgClient.escapeIdentifier(key)}`)
              .then(() => {
                delete connection[PARSED_STATEMENTS][key];
              })
              .catch(e => {
                // eslint-disable-next-line no-console
                console.error("Error releasing prepared query", e);
              });
          }
        },
      });
    }
github graphile / graphile-engine / packages / graphile-build-pg / src / plugins / PgTypesPlugin.ts View on Github external
}

  interface Inflection {
    hstoreType(): string;
  }
}

function indent(str: string) {
  return "  " + str.replace(/\n/g, "\n  ");
}

function identity(value: T): T {
  return value;
}

const parseCache = new LRU({ maxLength: 500 });
function parseInterval(str: string) {
  let result = parseCache.get(str);
  if (!result) {
    result = rawParseInterval(str);
    Object.freeze(result);
    parseCache.set(str, result);
  }
  return result;
}

export default (function PgTypesPlugin(
  builder,
  {
    pgExtendedTypes = true,
    // Adding hstore support is technically a breaking change; this allows people to opt out easily:
    pgSkipHstore = false,
github graphile / postgraphile / src / postgraphile / http / createPostGraphileHttpRequestHandler.ts View on Github external
let graphiqlHtml: string | null;

  const withPostGraphileContextFromReqRes = withPostGraphileContextFromReqResGenerator(options);

  const staticValidationRules = pluginHook('postgraphile:validationRules:static', specifiedRules, {
    options,
  });

  // Typically clients use static queries, so we can cache the parse and
  // validate stages for when we see the same query again.
  interface CacheEntry {
    queryDocumentAst: DocumentNode;
    validationErrors: ReadonlyArray;
    length: number;
  }
  const queryCache = new LRU({
    maxLength: Math.ceil(queryCacheMaxSize / 100000),
  });

  let lastGqlSchema: GraphQLSchema;
  const parseQuery = (
    gqlSchema: GraphQLSchema,
    queryString: string,
  ): {
    queryDocumentAst: DocumentNode;
    validationErrors: ReadonlyArray;
  } => {
    if (gqlSchema !== lastGqlSchema) {
      queryCache.reset();
      lastGqlSchema = gqlSchema;
    }
github graphile / graphile-engine / packages / pg-sql2 / src / index.ts View on Github external
names: Array;
  type: "IDENTIFIER";
  [$$trusted]: true;
}

export interface SQLValueNode {
  value: any;
  type: "VALUE";
  [$$trusted]: true;
}

export type SQLNode = SQLRawNode | SQLValueNode | SQLIdentifierNode;
export type SQLQuery = Array;
export type SQL = SQLNode | SQLQuery;

const CACHE_RAW_NODES = new LRU({ maxLength: 2000 });

function makeRawNode(text: string): SQLRawNode {
  const n = CACHE_RAW_NODES.get(text);
  if (n) {
    return n;
  }
  if (typeof text !== "string") {
    throw new Error("Invalid argument to makeRawNode - expected string");
  }
  const newNode: SQLRawNode = { type: "RAW", text, [$$trusted]: true };
  CACHE_RAW_NODES.set(text, newNode);
  return newNode;
}

function isStringOrSymbol(val: any): val is string | symbol {
  return typeof val === "string" || typeof val === "symbol";
github graphile / graphile-engine / packages / graphile-build / src / makeNewBuild.ts View on Github external
let recurseDataGeneratorsForFieldWarned = false;

const isString = (str: unknown): str is string => typeof str === "string";
const isDev = ["test", "development"].indexOf(process.env.NODE_ENV || "") >= 0;
const debug = debugFactory("graphile-build");

/*
 * This should be more than enough for normal usage. If you come under a
 * sophisticated attack then the attacker can empty this of useful values (with
 * a lot of work) but because we use SHA1 hashes under the covers the aliases
 * will still be consistent even after the LRU cache is exhausted. And SHA1 can
 * produce half a million hashes per second on my machine, the LRU only gives
 * us a 10x speedup!
 */
const hashCache = new LRU({ maxLength: 100000 });

/*
 * This function must never return a string longer than 56 characters.
 *
 * This function must only output alphanumeric and underscore characters.
 *
 * Collisions in SHA1 aren't problematic here (for us; they will be problematic
 * for the user deliberately causing them, but that's their own fault!), so
 * we'll happily take the performance boost over SHA256.
 */
function hashFieldAlias(str: string) {
  const precomputed = hashCache.get(str);
  if (precomputed) return precomputed;
  const hash = createHash("sha1")
    .update(str)
    .digest("hex");

@graphile/lru

Extremely simple zero-dependencies ES6 LRU (you probably want lru-cache instead)

MIT
Latest version published 4 years ago

Package Health Score

69 / 100
Full package analysis

Popular @graphile/lru functions