在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):cvburgess/SQLDataSource开源软件地址(OpenSource Url):https://github.com/cvburgess/SQLDataSource开源编程语言(OpenSource Language):JavaScript 60.3%开源软件介绍(OpenSource Introduction):SQLDataSourceThis package combines the power of Knex with the ease of use of Apollo DataSources. BREAKING CHANGES IN v1.0.0In 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 Read more below about getting set up and customizing the cache controls. Getting StartedInstallationTo install SQLDataSource: 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 This method accepts one OPTIONAL parameter, The default value for cache is Unless configured, SQLDataSource uses an InMemoryLRUCache like the RESTDataSource. SQLDataSource PropertiesSQLDataSource 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. contextThe context from your Apollo server is available as knexThe knex instance is made available as Debug modeTo enable more enhanced logging via knex-tiny-logger, set NPM 7 noteWhile peer dependencies are not installed by default for NPM 6, v7 will create a tree which could have peerDependencies added correctly. ContributingAll contributions are welcome! Please open an issue or pull request. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论