Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

12: fix ttl vs options obj #16

Merged
merged 2 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.1.6 - 05-04-2019

- Fix issue setting TTL on cache requests [#15]

## v0.1.5 - 28-02-2019

- Update dependencies to resolve cache TTL issue [#12]
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ And the peer dependencies (if you do not have them already): `npm i knex graphql
const Knex = require("knex");
const { SQLDataSource } = require("datasource-sql");

const MINUTE = 60 * 1000;
const MINUTE = 60;

const knex = Knex({
client: "pg",
Expand Down Expand Up @@ -89,21 +89,21 @@ SQLCache leverages Apollo's caching strategy to save results between requests an

This method accepts two parameters:

`getCached(knexQuery, ttlInMilliseconds)`
`getCached(knexQuery, ttlInSeconds)`

- `knexQuery`: <knexObject> A knex object that has not been then'd
- `ttlInMilliseconds`: <Number> number of milliseconds to keep cached results
- `ttlInSeconds`: <Number> number of seconds to keep cached results

### Why not both?

To leverage caching _*and*_ batching for a query, use the method `getCachedAndBatched` which wraps both methods.

This method accepts the same two params as `getCached`:

`getBatchedAndCached(knexQuery, ttlInMilliseconds)`
`getBatchedAndCached(knexQuery, ttlInSeconds)`

- `knexQuery`: <knexObject> A knex object that has not been then'd
- `ttlInMilliseconds`: <Number> number of milliseconds to keep cached results
- `ttlInSeconds`: <Number> number of seconds to keep cached results

From the example in the usage section above:

Expand Down
4 changes: 2 additions & 2 deletions SQLCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SQLCache {
return this.cache.get(cacheKey).then(entry => {
if (entry) return Promise.resolve(entry);
return query.then(rows => {
if (rows) this.cache.set(cacheKey, rows, ttl);
if (rows) this.cache.set(cacheKey, rows, { ttl });
return Promise.resolve(rows);
});
});
Expand All @@ -37,7 +37,7 @@ class SQLCache {
.load(queryString)
.then(result => result && result.rows)
.then(rows => {
if (rows) this.cache.set(cacheKey, rows, ttl);
if (rows) this.cache.set(cacheKey, rows, { ttl });
return Promise.resolve(rows);
});
});
Expand Down
43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datasource-sql",
"version": "0.1.5",
"version": "0.1.6",
"description": "SQL DataSource for Apollo GraphQL projects",
"main": "index.js",
"scripts": {
Expand Down