You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+101-2Lines changed: 101 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
# SQLDataSource
2
2
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].
4
4
5
5
## Getting Started
6
6
7
7
### Installation
8
8
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`
10
12
11
13
### Usage
12
14
@@ -34,6 +36,15 @@ class MyDatabase extends SQLDataSource {
34
36
// This can be any valid Knex query
35
37
constquery=this.db.select().from("users");
36
38
39
+
// A promise without any caching or batching
40
+
return query;
41
+
42
+
// Batch the query with DataLoader
43
+
returnthis.getBatched(query);
44
+
45
+
// Cache the result for 1 minute
46
+
returnthis.getCached(query, MINUTE);
47
+
37
48
// Batch the query and cache the result for 1 minute
38
49
returnthis.getBatchedAndCached(query, MINUTE);
39
50
}
@@ -54,8 +65,96 @@ const server = new ApolloServer({
54
65
});
55
66
```
56
67
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`:
-`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
+
constquery=this.db.select().from("users");
112
+
returnthis.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.
0 commit comments