{"id":24119372,"url":"https://github.com/cvburgess/sqldatasource","last_synced_at":"2025-11-03T23:04:19.665Z","repository":{"id":33229399,"uuid":"155608432","full_name":"cvburgess/SQLDataSource","owner":"cvburgess","description":"SQL DataSource for Apollo GraphQL projects","archived":false,"fork":false,"pushed_at":"2023-04-24T16:30:26.000Z","size":970,"stargazers_count":237,"open_issues_count":7,"forks_count":39,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-04T12:49:55.716Z","etag":null,"topics":["apollo","apollo-server","datasources","graphql","javascript","knex","nodejs","sql"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cvburgess.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-31T18:54:12.000Z","updated_at":"2025-03-27T01:09:02.000Z","dependencies_parsed_at":"2024-06-18T15:27:25.765Z","dependency_job_id":"a5aea1f4-8ecf-4c0c-8abd-813d36f56d8a","html_url":"https://github.com/cvburgess/SQLDataSource","commit_stats":{"total_commits":47,"total_committers":10,"mean_commits":4.7,"dds":0.4042553191489362,"last_synced_commit":"ea1fee32461d7656adfebc790127c2dac3f59368"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cvburgess%2FSQLDataSource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cvburgess%2FSQLDataSource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cvburgess%2FSQLDataSource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cvburgess%2FSQLDataSource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cvburgess","download_url":"https://codeload.github.com/cvburgess/SQLDataSource/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247182389,"owners_count":20897381,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apollo","apollo-server","datasources","graphql","javascript","knex","nodejs","sql"],"created_at":"2025-01-11T09:52:25.923Z","updated_at":"2025-11-03T23:04:19.606Z","avatar_url":"https://github.com/cvburgess.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQLDataSource\n\nThis package combines the power of [Knex] with the ease of use of [Apollo DataSources].\n\n## BREAKING CHANGES IN v1.0.0\n\nIn 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.\n\n```js\nconst query = this.knex.select(\"*\").from(\"fruit\").where({ id: 1 }).cache();\n\nquery.then(data =\u003e /* ... */ );\n```\n\nTo use ( or not use ) the caching feature in v1, simply add `.cache()` to your Knex query.\n\nRead more below about getting set up and customizing the cache controls.\n\n## Getting Started\n\n### Installation\n\nTo install SQLDataSource: `npm i datasource-sql`\n\n### Usage\n\n```js\n// MyDatabase.js\n\nconst { SQLDataSource } = require(\"datasource-sql\");\n\nconst MINUTE = 60;\n\nclass MyDatabase extends SQLDataSource {\n  getFruits() {\n    return this.knex\n      .select(\"*\")\n      .from(\"fruit\")\n      .where({ id: 1 })\n      .cache(MINUTE);\n  }\n}\n\nmodule.exports = MyDatabase;\n```\n\nAnd use it in your Apollo server configuration:\n\n```js\n// index.js\n\nconst MyDatabase = require(\"./MyDatabase\");\n\nconst knexConfig = {\n  client: \"pg\",\n  connection: {\n    /* CONNECTION INFO */\n  }\n};\n\n// you can also pass a knex instance instead of a configuration object\nconst db = new MyDatabase(knexConfig);\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  cache,\n  context,\n  dataSources: () =\u003e ({ db })\n});\n```\n\n### Caching ( .cache( ttl ) )\n\nIf 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.\n\nSQLDataSource leverages Apollo's caching strategy to save results between requests and makes that available via `.cache()`.\n\nThis method accepts one OPTIONAL parameter, `ttl` that is the number of seconds to retain the data in the cache.\n\nThe default value for cache is `5 seconds`.\n\nUnless [configured](https://www.apollographql.com/docs/apollo-server/data/data-sources/#using-memcachedredis-as-a-cache-storage-backend), SQLDataSource uses an InMemoryLRUCache like the [RESTDataSource].\n\n## SQLDataSource Properties\n\nSQLDataSource is an ES6 Class that can be extended to make a new SQLDataSource and extends Apollo's base DataSource class under the hood.\n\n( See the Usage section above for an example )\n\nLike all DataSources, SQLDataSource has an initialize method that Apollo will call when a new request is routed.\n\nIf no cache is provided in your Apollo server configuration, SQLDataSource falls back to the same InMemoryLRUCache leveraged by [RESTDataSource].\n\n### context\n\nThe context from your Apollo server is available as `this.context`.\n\n### knex\n\nThe knex instance is made available as `this.knex` or `this.db`.\n\n## Debug mode\n\nTo enable more enhanced logging via [knex-tiny-logger], set `DEBUG` to a truthy value in your environment variables.\n\n## NPM 7 note\n\nWhile peer dependencies are not installed by default for NPM 6, [v7 will create a tree which could have peerDependencies added correctly](https://github.com/npm/rfcs/blob/main/implemented/0025-install-peer-deps.md).\n\n## Contributing\n\nAll contributions are welcome!\n\nPlease open an issue or pull request.\n\n[knex]: https://knexjs.org/\n[apollo datasources]: https://www.apollographql.com/docs/apollo-server/features/data-sources.html\n[dataloader]: https://github.com/facebook/dataloader\n[inmemorylrucache]: https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-caching\n[restdatasource]: https://www.apollographql.com/docs/apollo-server/features/data-sources.html#REST-Data-Source\n[knex-tiny-logger]: https://github.com/khmm12/knex-tiny-logger\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcvburgess%2Fsqldatasource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcvburgess%2Fsqldatasource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcvburgess%2Fsqldatasource/lists"}