Registry / database / datasource-sql

datasource-sql

JSON →
library2.1.0jsnpmunverified

SQL DataSource for Apollo GraphQL projects (v2.1.0, released 2022-04-25). Combines Knex.js query builder with Apollo DataSources, providing caching, context, and declarative SQL queries. Ships TypeScript types. Requires peer dependencies graphql >=14.0.2 and knex >=0.95.0. Differentiates from raw Knex by integrating with Apollo's caching and request lifecycle, and from RESTDataSource by offering SQL-specific features like .cache() on queries. Active development with infrequent releases.

npm install datasource-sql
INSTALL
IMPORT
SIG · DATASOURCE-SQL
D
datasource-sql
databasejavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SQLDataSource
import { SQLDataSource } from 'datasource-sql'
const SQLDataSource = require('datasource-sql').SQLDataSource
Named export, not default. Use destructured import for ESM or require destructuring.
SQLDataSource (CommonJS)
const { SQLDataSource } = require('datasource-sql')
const SQLDataSource = require('datasource-sql')
Module exports an object with SQLDataSource property, not the class directly.
class MyDatabase extends SQLDataSource
class MyDatabase extends SQLDataSource { /* methods */ }
class MyDatabase extends SQLDataSource.default { }
Directly extend the exported SQLDataSource class; no .default needed.

Shows creating a custom SQLDataSource class with a cached query using Knex, integrating with ApolloServer.

const { SQLDataSource } = require('datasource-sql'); const knexConfig = { client: 'pg', connection: { host: 'localhost', user: 'user', password: 'pass', database: 'db' } }; class MyDB extends SQLDataSource { getFruits() { return this.knex.select('*').from('fruit').where({ id: 1 }).cache(60); } } const db = new MyDB(knexConfig); const server = new ApolloServer({ typeDefs: `type Query { fruits: [Fruit] }`, resolvers: { Query: { fruits: async (_, __, { dataSources }) => dataSources.db.getFruits() } }, cache: new InMemoryLRUCache(), context: () => ({ request: req }) });
Debug
Known issues
breakingv1.0.0 introduced fluid interface: queries must use .cache() method explicitly. Without .cache(), results are not cached.
fix
Append .cache(ttl) to your Knex query chains to enable caching.
affects: >=1.0.0
gotchaKnex must be configured as a peer dependency; if not installed, require('knex') will fail at runtime.
fix
Ensure knex >=0.95.0 is in your dependencies (not just devDependencies) and installed.
affects: >=2.0.0
deprecatedNode 10.6.0 minimum, but Node 10 is EOL. Upgrade to Node 12+ for security updates.
fix
Update engines field in package.json or use nvm to switch to Node >=12.
affects: >=2.0.0
gotchaIf no cache is provided to ApolloServer, SQLDataSource falls back to InMemoryLRUCache (same as RESTDataSource). This may be insufficient for production.
fix
Explicitly provide a cache (e.g., Memcached/Redis) in ApolloServer constructor via the cache option.
affects: *
Errors
Common errors & fixes
Cannot find module 'knex'
Missing peer dependency knex in node_modules
fix
npm install knex (and graphql) as runtime dependencies.
TypeError: SQLDataSource is not a constructor
Importing the module as default instead of destructuring
fix
Use const { SQLDataSource } = require('datasource-sql') or import { SQLDataSource } from 'datasource-sql'.
this.knex.select(...).from(...).where(...).cache is not a function
Forgetting to install peer dependency knex or using an older version of knex (<0.95.0)
fix
Install knex >=0.95.0 and ensure it's added to package.json.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency for Apollo integration
knexrequiredpeer dependency for SQL query building
Agent activity
9 hits · last 30 days
node
8
Resources
datasource-sql — npm install datasource-sql · libregistry