Skip to content

Commit 23e4965

Browse files
authored
1.0.0 (#30)
- Switch to an extended knex method (.cache) vs the old wrapper fns - Remove batching support
1 parent 5851569 commit 23e4965

File tree

10 files changed

+1789
-2215
lines changed

10 files changed

+1789
-2215
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v1.0.0 - 09-17-2019
4+
5+
- Switch to an extended knex method (.cache) vs the old wrapper fns
6+
- Remove batching support
7+
38
## v0.2.0 - 08-07-2019
49

510
- Fix issues with lexical scoping [#19]

README.md

Lines changed: 40 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,147 +2,94 @@
22

33
This package combines the power of [Knex] with the ease of use of [Apollo DataSources].
44

5-
## BREAKING CHANGE IN v0.2.0
5+
## BREAKING CHANGES IN v1.0.0
66

7-
Batching of queries is hacked together with `knex.raw()` - while this is not ideal, due to the way Knex works, I have not found a more consistent way to do this.
7+
In 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.
88

9-
As such, when you use `getBatched` or `getBatchedAndCached` the result will not be the pretty, normalized output you may expect from Knex, it will be the raw output from your DB driver of choice as if you had run the query with `knex.raw()`.
9+
```js
10+
const query = this.db.select("*").from("fruit").where({ id: 1 }).cache();
11+
12+
query.then(data => /* ... */ );
13+
```
1014

11-
**If you find a way to implement caching without using `knex.raw()` please open a PR!**
15+
To use ( or not use ) the caching feature in v1, simply add `.cache()` to your Knex query.
1216

13-
While I would love to spend more time investigating this, unfortunately my time is limited at the moment.
17+
Read more below about getting set up and customizing the cache controls.
1418

1519
## Getting Started
1620

1721
### Installation
1822

1923
To install SQLDataSource: `npm i datasource-sql`
2024

21-
And the peer dependencies (if you do not have them already): `npm i knex graphql`
22-
2325
### Usage
2426

2527
```js
26-
const Knex = require("knex");
28+
// MyDatabase.js
29+
2730
const { SQLDataSource } = require("datasource-sql");
2831

2932
const MINUTE = 60;
3033

31-
const knex = Knex({
32-
client: "pg",
33-
connection: {
34-
/* CONNECTION INFO */
35-
}
36-
});
37-
3834
class MyDatabase extends SQLDataSource {
39-
constructor() {
40-
super();
41-
// Add your instance of Knex to the DataSource
42-
this.db = knex;
35+
getFruits() {
36+
return this.db
37+
.select("*")
38+
.from("fruit")
39+
.where({ id: 1 })
40+
.cache(MINUTE);
4341
}
42+
}
4443

45-
getUsers() {
46-
// This can be any valid Knex query
47-
const query = this.db.select().from("users");
44+
module.exports = MyDatabase;
45+
```
4846

49-
// A promise without any caching or batching
50-
return query;
47+
And use it in your Apollo server configuration:
5148

52-
// Batch the query with DataLoader - RETURNS A RAW RESPONSE
53-
return this.getBatched(query);
49+
```js
50+
// index.js
5451

55-
// Cache the result for 1 minute
56-
return this.getCached(query, MINUTE);
52+
const MyDatabase = require("./MyDatabase");
5753

58-
// Batch the query and cache the result for 1 minute - RETURNS A RAW RESPONSE
59-
return this.getBatchedAndCached(query, MINUTE);
54+
const knexConfig = {
55+
client: "pg",
56+
connection: {
57+
/* CONNECTION INFO */
6058
}
61-
}
62-
```
59+
};
6360

64-
And use it in your Apollo server configuration:
61+
const db = new MyDatabase(knexConfig);
6562

66-
```js
6763
const server = new ApolloServer({
6864
typeDefs,
6965
resolvers,
7066
cache,
7167
context,
72-
dataSources: () => ({
73-
db: new MyDatabase()
74-
})
68+
dataSources: () => ({ db })
7569
});
7670
```
7771

78-
## SQLCache
79-
80-
SQLCache is what makes SQLDataSource incredibly smart and preferment. A thin wrapper around Apollo's caching strategy and the optional addition of [DataLoader] make for the smallest number of queries possible hitting your database.
81-
82-
### Batching ( getBatched )
83-
84-
If you were to make the same query (example: `knex.select().from('users')`) in multiple resolvers you could be making a bunch of duplicate reads from your database.
85-
86-
SQLCache uses DataLoader to create a batched wrapper for knex ( `getBatched` ) that will only fire duplicate queries once, but returns the same result to all resolvers.
87-
88-
This method accepts one parameter:
89-
90-
`getBatched(knexQuery)`
91-
92-
- `knexQuery`: <knexObject> A knex object that has not been then'd
93-
94-
**NOTE: This method will return the raw response from your DB driver.**
95-
96-
### Caching ( getCached )
72+
### Caching ( .cache( ttl ) )
9773

9874
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.
9975

100-
SQLCache leverages Apollo's caching strategy to save results between requests and makes that available via `getCached`.
101-
102-
This method accepts two parameters:
103-
104-
`getCached(knexQuery, ttlInSeconds)`
105-
106-
- `knexQuery`: <knexObject> A knex object that has not been then'd
107-
- `ttlInSeconds`: <Number> number of seconds to keep cached results
108-
109-
### Why not both?
110-
111-
To leverage caching _*and*_ batching for a query, use the method `getCachedAndBatched` which wraps both methods.
76+
SQLDataSource leverages Apollo's caching strategy to save results between requests and makes that available via `.cache()`.
11277

113-
This method accepts the same two params as `getCached`:
78+
This method accepts one OPTIONAL parameter, `ttl` that is the number of seconds to retain the data in the cache.
11479

115-
`getBatchedAndCached(knexQuery, ttlInSeconds)`
80+
The default value for cache is `5 seconds`.
11681

117-
- `knexQuery`: <knexObject> A knex object that has not been then'd
118-
- `ttlInSeconds`: <Number> number of seconds to keep cached results
82+
configuration, SQLDataSource falls back to an InMemoryLRUCache like the [RESTDataSource].
11983

120-
From the example in the usage section above:
121-
122-
```js
123-
const query = this.db.select().from("users");
124-
return this.getBatchedAndCached(query, MINUTE);
125-
```
126-
127-
**NOTE: This method will return the raw response from your DB driver.**
128-
129-
### initialize
130-
131-
SQLDataSource creates a new SQLCache on every initialize call with the cache and context from Apollo Server so you never should be directly creating a new SQLCache.
132-
133-
Note: If no cache is configured, an [InMemoryLRUCache] cache is used.
134-
135-
## SQLDataSource
84+
## SQLDataSource Properties
13685

13786
SQLDataSource is an ES6 Class that can be extended to make a new SQLDataSource and extends Apollo's base DataSource class under the hood.
13887

13988
( See the Usage section above for an example )
14089

14190
Like all DataSources, SQLDataSource has an initialize method that Apollo will call when a new request is routed.
14291

143-
The initialize call accepts a configuration object with a cache and context.
144-
145-
If no cache is provided in your Apollo configuration, SQLDataSource falls back to an InMemoryLRUCache like the [RESTDataSource].
92+
If no cache is provided in your Apollo server
14693

14794
### context
14895

@@ -152,13 +99,9 @@ The context from your Apollo server is available as `this.context`.
15299

153100
The instance of knex you reference in the constructor is made available as `this.db`.
154101

155-
### Query methods
156-
157-
The methods from SQLCache ( `getBatched`, `getCached`, and `getBatchedAndCached` ) are combined with the provided knex object and appended to `this`.
158-
159-
### Debug mode
102+
## Debug mode
160103

161-
To enable more enhanced logging via [knex-tiny-logger], set DEBUG=true in your environment variables.
104+
To enable more enhanced logging via [knex-tiny-logger], set `DEBUG` to a truthy value in your environment variables.
162105

163106
## Contributing
164107

SQLCache.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

SQLDataSource.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

SQLDataSource.test.js

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)