パッケージの詳細

datasource-sql

cvburgess10.6kMIT2.1.0

SQL DataSource for Apollo GraphQL projects

apollo, apollo-server, datasource, graphql

readme

SQLDataSource

This package combines the power of Knex with the ease of use of Apollo DataSources.

BREAKING CHANGES IN v1.0.0

In v1.0.0 this lib has a new fluid interface that plays nicely with Knex and stays more true to the spirit of Apollo DataSources.

const query = this.knex.select("*").from("fruit").where({ id: 1 }).cache();

query.then(data => /* ... */ );

To use ( or not use ) the caching feature in v1, simply add .cache() to your Knex query.

Read more below about getting set up and customizing the cache controls.

Getting Started

Installation

To install SQLDataSource: npm i datasource-sql

Usage

// MyDatabase.js

const { SQLDataSource } = require("datasource-sql");

const MINUTE = 60;

class MyDatabase extends SQLDataSource {
  getFruits() {
    return this.knex
      .select("*")
      .from("fruit")
      .where({ id: 1 })
      .cache(MINUTE);
  }
}

module.exports = MyDatabase;

And use it in your Apollo server configuration:

// index.js

const MyDatabase = require("./MyDatabase");

const knexConfig = {
  client: "pg",
  connection: {
    /* CONNECTION INFO */
  }
};

// you can also pass a knex instance instead of a configuration object
const db = new MyDatabase(knexConfig);

const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({ db })
});

Caching ( .cache( ttl ) )

If you were to make the same query over the course of multiple requests to your server you could also be making needless requests to your server - especially for expensive queries.

SQLDataSource leverages Apollo's caching strategy to save results between requests and makes that available via .cache().

This method accepts one OPTIONAL parameter, ttl that is the number of seconds to retain the data in the cache.

The default value for cache is 5 seconds.

Unless configured, SQLDataSource uses an InMemoryLRUCache like the RESTDataSource.

SQLDataSource Properties

SQLDataSource is an ES6 Class that can be extended to make a new SQLDataSource and extends Apollo's base DataSource class under the hood.

( See the Usage section above for an example )

Like all DataSources, SQLDataSource has an initialize method that Apollo will call when a new request is routed.

If no cache is provided in your Apollo server configuration, SQLDataSource falls back to the same InMemoryLRUCache leveraged by RESTDataSource.

context

The context from your Apollo server is available as this.context.

knex

The knex instance is made available as this.knex or this.db.

Debug mode

To enable more enhanced logging via knex-tiny-logger, set DEBUG to a truthy value in your environment variables.

NPM 7 note

While peer dependencies are not installed by default for NPM 6, v7 will create a tree which could have peerDependencies added correctly.

Contributing

All contributions are welcome!

Please open an issue or pull request.

更新履歴

Changelog

v2.1.0 - 04-24-2023

  • Resolve issue with .cache declaration (#84) Thanks @daniel-keller

v2.0.1 - 03-15-2022

  • Update dependencies

v2.0.0 - 03-15-2022

  • Update imports to match Knex v0.95+ ( Thanks @grusite )

v1.6.0 - 10-04-2021

  • Migrate knex to a peerDependencies ( Thanks @roy-coder )

v1.5.0 - 08-09-2021

  • Add types for this.db and this.context ( Thanks @astorije )

v1.4.1 - 04-09-2021

  • Support GraphQL versions >= v14

v1.4.0 - 03-27-2021

  • Add Typescript types ( Thanks @freshollie )

v1.3.1 - 12-11-2020

  • Various dependency security updates via dependabot

v1.3.0 - 04-22-2020

  • knex-tiny-logger update ( requires node v10+ )

v1.2.1 - 04-22-2020

  • Knex update ( v0.20.x )

v1.2.0 - 04-07-2020

  • Support specifying a knex instance instead of config object ( Thanks @theogravity! )

v1.1.1 - 01-23-2020

  • Knex update for security

v1.1.0 - 12-30-2019

  • Allow knex instance to be referenced by this.knex in addition to this.db

v1.0.2 - 10-07-2019

  • Fix issue with repeated constructor calls

v1.0.1 - 09-18-2019

  • Fix issue with Redis keys and values

v1.0.0 - 09-17-2019

  • Switch to an extended knex method (.cache) vs the old wrapper fns
  • Remove batching support

v0.2.0 - 08-07-2019

  • Fix issues with lexical scoping [#19]
  • Deprecate this.knex in favor of this.db
  • Mark batching as experimental
  • Update docs to reflect current development status

v0.1.7 - 24-04-2019

  • Fix for responses from sqlite and mssql drivers [#9]

v0.1.6 - 05-04-2019

  • Fix issue setting TTL on cache requests [#15]

v0.1.5 - 28-02-2019

  • Update dependencies to resolve cache TTL issue [#12]

v0.1.4 - 28-11-2018

  • Fix multiple onQuery logging events [#5]

v0.1.3 - 20-11-2018

  • Add support for multiple node and npm versions [#3]

v0.1.2 - 06-11-2018

  • Fix broken import

v0.1.1 - 05-11-2018

  • Add more documentation [#2]
  • Extend the Apollo DataSource class

v0.1.0 - 31-10-2018