{"id":42585,"library":"datasource-sql","title":"datasource-sql","description":"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.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/cvburgess/SQLDataSource","tags":["javascript","apollo","apollo-server","datasource","graphql","knex","sql","typescript"],"install":[{"cmd":"npm install datasource-sql","lang":"bash","label":"npm"},{"cmd":"yarn add datasource-sql","lang":"bash","label":"yarn"},{"cmd":"pnpm add datasource-sql","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency for Apollo integration","package":"graphql","optional":false},{"reason":"peer dependency for SQL query building","package":"knex","optional":false}],"imports":[{"note":"Named export, not default. Use destructured import for ESM or require destructuring.","wrong":"const SQLDataSource = require('datasource-sql').SQLDataSource","symbol":"SQLDataSource","correct":"import { SQLDataSource } from 'datasource-sql'"},{"note":"Module exports an object with SQLDataSource property, not the class directly.","wrong":"const SQLDataSource = require('datasource-sql')","symbol":"SQLDataSource (CommonJS)","correct":"const { SQLDataSource } = require('datasource-sql')"},{"note":"Directly extend the exported SQLDataSource class; no .default needed.","wrong":"class MyDatabase extends SQLDataSource.default { }","symbol":"class MyDatabase extends SQLDataSource","correct":"class MyDatabase extends SQLDataSource { /* methods */ }"}],"quickstart":{"code":"const { SQLDataSource } = require('datasource-sql');\nconst knexConfig = { client: 'pg', connection: { host: 'localhost', user: 'user', password: 'pass', database: 'db' } };\nclass MyDB extends SQLDataSource {\n  getFruits() {\n    return this.knex.select('*').from('fruit').where({ id: 1 }).cache(60);\n  }\n}\nconst db = new MyDB(knexConfig);\nconst server = new ApolloServer({\n  typeDefs: `type Query { fruits: [Fruit] }`,\n  resolvers: { Query: { fruits: async (_, __, { dataSources }) => dataSources.db.getFruits() } },\n  cache: new InMemoryLRUCache(),\n  context: () => ({ request: req })\n});","lang":"javascript","description":"Shows creating a custom SQLDataSource class with a cached query using Knex, integrating with ApolloServer."},"warnings":[{"fix":"Append .cache(ttl) to your Knex query chains to enable caching.","message":"v1.0.0 introduced fluid interface: queries must use .cache() method explicitly. Without .cache(), results are not cached.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure knex >=0.95.0 is in your dependencies (not just devDependencies) and installed.","message":"Knex must be configured as a peer dependency; if not installed, require('knex') will fail at runtime.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Update engines field in package.json or use nvm to switch to Node >=12.","message":"Node 10.6.0 minimum, but Node 10 is EOL. Upgrade to Node 12+ for security updates.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Explicitly provide a cache (e.g., Memcached/Redis) in ApolloServer constructor via the cache option.","message":"If no cache is provided to ApolloServer, SQLDataSource falls back to InMemoryLRUCache (same as RESTDataSource). This may be insufficient for production.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'-04':13 '-25':14 '0.95.0':39 '14.0.2':36 '2022':12 'activ':66 'apollo':7,20,47,72,74 'apollo-serv':73 'builder':18 'cach':23,49,63 'combin':15 'context':24 'datasourc':2,5,21,76 'datasource-sql':1 'declar':26 'depend':34 'develop':67 'differenti':40 'featur':61 'graphql':8,35,77 'infrequ':69 'integr':45 'javascript':71 'knex':38,43,78 'knex.js':16 'lifecycl':52 'like':62 'offer':57 'peer':33 'project':9 'provid':22 'queri':17,28,65 'raw':42 'releas':11,70 'request':51 'requir':32 'restdatasourc':55 'server':75 'ship':29 'specif':60 'sql':3,4,27,59,79 'sql-specif':58 'type':31 'typescript':30,80 'v2.1.0':10","created_at":"2026-06-05T16:55:42.027923+00:00","updated_at":"2026-06-05T16:55:42.027923+00:00","problems":[{"fix":"npm install knex (and graphql) as runtime dependencies.","cause":"Missing peer dependency knex in node_modules","error":"Cannot find module 'knex'"},{"fix":"Use const { SQLDataSource } = require('datasource-sql') or import { SQLDataSource } from 'datasource-sql'.","cause":"Importing the module as default instead of destructuring","error":"TypeError: SQLDataSource is not a constructor"},{"fix":"Install knex >=0.95.0 and ensure it's added to package.json.","cause":"Forgetting to install peer dependency knex or using an older version of knex (<0.95.0)","error":"this.knex.select(...).from(...).where(...).cache is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/cvburgess/SQLDataSource#readme","github":"https://github.com/cvburgess/SQLDataSource","docs":null,"changelog":null,"pypi":null,"npm":"datasource-sql","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}