
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
datasource-sql
Advanced tools
This package combines the power of Knex with the ease of use of Apollo DataSources.
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.
To install SQLDataSource: npm i datasource-sql
// 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 })
});
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 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.
The context from your Apollo server is available as this.context.
The knex instance is made available as this.knex or this.db.
To enable more enhanced logging via knex-tiny-logger, set DEBUG to a truthy value in your environment variables.
While peer dependencies are not installed by default for NPM 6, v7 will create a tree which could have peerDependencies added correctly.
All contributions are welcome!
Please open an issue or pull request.
FAQs
SQL DataSource for Apollo GraphQL projects
The npm package datasource-sql receives a total of 1,806 weekly downloads. As such, datasource-sql popularity was classified as popular.
We found that datasource-sql demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.