Skip to content

Commit 4ad75a0

Browse files
committed
update docs
closes #2
1 parent 7db03de commit 4ad75a0

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# SQLDataSource
22

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

55
## Getting Started
66

77
### Installation
88

9-
`npm i datasource-sql`
9+
To install SQLDataSource: `npm i datasource-sql`
10+
11+
And the peer dependencies (if you do not have them already): `npm i knex graphql`
1012

1113
### Usage
1214

@@ -34,6 +36,15 @@ class MyDatabase extends SQLDataSource {
3436
// This can be any valid Knex query
3537
const query = this.db.select().from("users");
3638

39+
// A promise without any caching or batching
40+
return query;
41+
42+
// Batch the query with DataLoader
43+
return this.getBatched(query);
44+
45+
// Cache the result for 1 minute
46+
return this.getCached(query, MINUTE);
47+
3748
// Batch the query and cache the result for 1 minute
3849
return this.getBatchedAndCached(query, MINUTE);
3950
}
@@ -54,8 +65,96 @@ const server = new ApolloServer({
5465
});
5566
```
5667

68+
## SQLCache
69+
70+
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.
71+
72+
### Batching ( getBatched )
73+
74+
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.
75+
76+
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.
77+
78+
This method accepts one parameter:
79+
80+
`getBatched(knexQuery)`
81+
82+
- `knexQuery`: <knexObject> A knex object that has not been then'd
83+
84+
### Caching ( getCached )
85+
86+
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.
87+
88+
SQLCache leverages Apollo's caching strategy to save results between requests and makes that available via `getCached`.
89+
90+
This method accepts two parameters:
91+
92+
`getCached(knexQuery, ttlInMilliseconds)`
93+
94+
- `knexQuery`: <knexObject> A knex object that has not been then'd
95+
- `ttlInMilliseconds`: <Number> number of milliseconds to keep cached results
96+
97+
### Why not both?
98+
99+
To leverage caching _*and*_ batching for a query, use the method `getCachedAndBatched` which wraps both methods.
100+
101+
This method accepts the same two params as `getCached`:
102+
103+
`getBatchedAndCached(knexQuery, ttlInMilliseconds)`
104+
105+
- `knexQuery`: <knexObject> A knex object that has not been then'd
106+
- `ttlInMilliseconds`: <Number> number of milliseconds to keep cached results
107+
108+
From the example in the usage section above:
109+
110+
```js
111+
const query = this.db.select().from("users");
112+
return this.getBatchedAndCached(query, MINUTE);
113+
```
114+
115+
### initialize
116+
117+
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.
118+
119+
Note: If no cache is configured, an [InMemoryLRUCache] cache is used.
120+
121+
## SQLDataSource
122+
123+
SQLDataSource is an ES6 Class that can be extended to make a new SQLDataSource and extends Apollo's base DataSource class under the hood.
124+
125+
( See the Usage section above for an example )
126+
127+
Like all DataSources, SQLDataSource has an initialize method that Apollo will call when a new request is routed.
128+
129+
The initialize call accepts a configuration object with a cache and context.
130+
131+
If no cache is provided in your Apollo configuration, SQLDataSource falls back to an InMemoryLRUCache like the [RESTDataSource].
132+
133+
### context
134+
135+
The context from your Apollo server is available as `this.context`.
136+
137+
### db
138+
139+
The instance of knex you reference in the constructor is made available as `this.db`.
140+
141+
### Query methods
142+
143+
The methods from SQLCache ( `getBatched`, `getCached`, and `getBatchedAndCached` ) are combined with the provided knex object and appended to `this`.
144+
145+
### Debug mode
146+
147+
To enable more enhanced logging via [knex-tiny-logger], set DEBUG=true in your environment variables.
148+
57149
## Contributing
58150

59151
All contributions are welcome!
60152

61153
Please open an issue or pull request.
154+
155+
[Knex](https://knexjs.org/)
156+
[Apollo DataSources](https://www.apollographql.com/docs/apollo-server/features/data-sources.html)
157+
[DataLoader](https://github.com/facebook/dataloader)
158+
[InMemoryLRUCache](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-caching)
159+
[RESTDataSource](https://www.apollographql.com/docs/apollo-server/features/data-sources.html#REST-Data-Source)
160+
[knex-tiny-logger](https://github.com/khmm12/knex-tiny-logger)

0 commit comments

Comments
 (0)