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
+40-97Lines changed: 40 additions & 97 deletions
Original file line number
Diff line number
Diff line change
@@ -2,147 +2,94 @@
2
2
3
3
This package combines the power of [Knex] with the ease of use of [Apollo DataSources].
4
4
5
-
## BREAKING CHANGE IN v0.2.0
5
+
## BREAKING CHANGES IN v1.0.0
6
6
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.
8
8
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()`.
// Batch the query with DataLoader - RETURNS A RAW RESPONSE
53
-
returnthis.getBatched(query);
49
+
```js
50
+
// index.js
54
51
55
-
// Cache the result for 1 minute
56
-
returnthis.getCached(query, MINUTE);
52
+
constMyDatabase=require("./MyDatabase");
57
53
58
-
// Batch the query and cache the result for 1 minute - RETURNS A RAW RESPONSE
59
-
returnthis.getBatchedAndCached(query, MINUTE);
54
+
constknexConfig= {
55
+
client:"pg",
56
+
connection: {
57
+
/* CONNECTION INFO */
60
58
}
61
-
}
62
-
```
59
+
};
63
60
64
-
And use it in your Apollo server configuration:
61
+
constdb=newMyDatabase(knexConfig);
65
62
66
-
```js
67
63
constserver=newApolloServer({
68
64
typeDefs,
69
65
resolvers,
70
66
cache,
71
67
context,
72
-
dataSources: () => ({
73
-
db:newMyDatabase()
74
-
})
68
+
dataSources: () => ({ db })
75
69
});
76
70
```
77
71
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 ) )
97
73
98
74
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.
99
75
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()`.
112
77
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.
114
79
115
-
`getBatchedAndCached(knexQuery, ttlInSeconds)`
80
+
The default value for cache is `5 seconds`.
116
81
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].
119
83
120
-
From the example in the usage section above:
121
-
122
-
```js
123
-
constquery=this.db.select().from("users");
124
-
returnthis.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
136
85
137
86
SQLDataSource is an ES6 Class that can be extended to make a new SQLDataSource and extends Apollo's base DataSource class under the hood.
138
87
139
88
( See the Usage section above for an example )
140
89
141
90
Like all DataSources, SQLDataSource has an initialize method that Apollo will call when a new request is routed.
142
91
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
146
93
147
94
### context
148
95
@@ -152,13 +99,9 @@ The context from your Apollo server is available as `this.context`.
152
99
153
100
The instance of knex you reference in the constructor is made available as `this.db`.
154
101
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
160
103
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.
0 commit comments