@@ -163,59 +163,39 @@ import {inject} from "@tsed/di";
163163const myService = inject (MyService );
164164```
165165
166- ### Cache Decorator
166+ ### Directus Context Service
167167
168- Cache method results using Directus cache infrastructure :
168+ Access Directus context from anywhere in your code :
169169
170170``` ts
171- import {Injectable } from " @tsed/di" ;
172- import {Cache } from " @tsed/directus-sdk" ;
171+ import {injectable } from " @tsed/di" ;
172+ import {DirectusContextService } from " @tsed/directus-sdk" ;
173173
174- @Injectable ()
175- export class ApiClient {
176- @Cache ({ttl: 900000 })
177- async search(query : string ) {
178- // Expensive operation
179- return this .performSearch (query );
174+ export class MyService {
175+ directusContext = inject (DirectusContextService );
176+
177+ async doSomething() {
178+ const context = this .directusContext .get ();
179+ const schema = await context .getSchema ();
180+ // Use schema...
180181 }
181182
182- @Cache ({
183- ttl: 60000 ,
184- keyGenerator : (userId : string ) => ` user:${userId } `
185- })
186- async getUserById(userId : string ) {
187- return this .fetchUser (userId );
183+ async getUsersService() {
184+ // Convenience method to get ItemsService
185+ return this .directusContext .getItemsService (" users" );
188186 }
189187}
190- ```
191-
192- ** Options:**
193188
194- - ` ttl ` - Time to live in milliseconds (default: 900000ms = 15 minutes)
195- - ` keyGenerator ` - Custom function to generate cache keys
196- - ` namespace ` - Custom namespace for cache keys
197- - ` useSystemCache ` - Use system cache (true) or regular cache (false). Default: true
198-
199- > Important: to use decorator you have to configure your tsconfig with the appropriate options (
200- > ` "experimentalDecorators": true ` , ` "emitDecoratorMetadata": true ` )
201-
202- ### Programmatic Cache Binding
203-
204- Apply cache programmatically using ` useDirectusCache ` :
189+ injectable (MyService );
190+ ```
205191
206- ``` ts
207- import {injectable } from " @tsed/di" ;
208- import {useDirectusCache } from " @tsed/directus-sdk" ;
192+ ** Methods:**
209193
210- export class JiraIssueClient {
211- search(query : string ) {
212- return this .performSearch (query );
213- }
214- }
194+ - ` set(context) ` - Set the Directus context (internal use)
195+ - ` get() ` - Get the current Directus context
196+ - ` getItemsService(collection, options?) ` - Create an ItemsService for a collection
215197
216- injectable (JiraIssueClient );
217- useDirectusCache (JiraIssueClient , " search" , {ttl: 900000 });
218- ```
198+ ---
219199
220200### Repository Pattern
221201
@@ -292,39 +272,87 @@ export default defineEndpoint({
292272});
293273```
294274
295- ### Directus Context Service
275+ ### Cache
296276
297- Access Directus context from anywhere in your code:
277+ Because ` @directus/api/cache ` isn't declared as external dependency to build the extension you have to configure the
278+ rollup bundler to
279+ exclude it from the build.
298280
299- ``` ts
300- import {injectable } from " @tsed/di" ;
301- import {DirectusContextService } from " @tsed/directus-sdk" ;
281+ So for each extension you have to add the following to the ` extension.config.js ` , if your extension build fail:
302282
303- export class MyService {
304- directusContext = inject (DirectusContextService );
283+ ``` js
284+ function externals () {
285+ return {
286+ name: " external-plus" , // this name will show up in logs and errors
287+ version: " 1.0.0" ,
288+ options (rawOptions ) {
289+ if (rawOptions .external ? .includes (" directus:api" )) {
290+ rawOptions .external .push (" @directus/api/cache" );
291+ }
305292
306- async doSomething() {
307- const context = this .directusContext .get ();
308- const schema = await context .getSchema ();
309- // Use schema...
293+ return rawOptions;
294+ }
295+ };
296+ }
297+
298+ export default {
299+ plugins: [externals ()]
300+ };
301+ ` ` `
302+
303+ #### Cache Decorator
304+
305+ Cache method results using Directus cache infrastructure:
306+
307+ ` ` ` ts
308+ import {Injectable } from " @tsed/di" ;
309+ import {Cache } from " @tsed/directus-sdk/cache" ;
310+
311+ @Injectable ()
312+ export class ApiClient {
313+ @Cache ({ttl: 900000 })
314+ async search (query : string ) {
315+ // Expensive operation
316+ return this .performSearch (query);
310317 }
311318
312- async getUsersService() {
313- // Convenience method to get ItemsService
314- return this .directusContext .getItemsService (" users" );
319+ @Cache ({
320+ ttl: 60000 ,
321+ keyGenerator : (userId : string ) => ` user:${ userId} `
322+ })
323+ async getUserById (userId : string ) {
324+ return this .fetchUser (userId);
315325 }
316326}
317-
318- injectable (MyService );
319327` ` `
320328
321- ** Methods :**
329+ **Options :**
322330
323- - ` set(context) ` - Set the Directus context (internal use)
324- - ` get() ` - Get the current Directus context
325- - ` getItemsService(collection, options?) ` - Create an ItemsService for a collection
331+ - ` ttl` - Time to live in milliseconds (default: 900000ms = 15 minutes)
332+ - ` keyGenerator` - Custom function to generate cache keys
333+ - ` namespace` - Custom namespace for cache keys
334+ - ` useSystemCache` - Use system cache (true) or regular cache (false). Default: true
326335
327- ---
336+ > Important: to use decorator you have to configure your tsconfig with the appropriate options (
337+ > ` " experimentalDecorators" : true ` , ` " emitDecoratorMetadata" : true ` )
338+
339+ #### Programmatic Cache Binding
340+
341+ Apply cache programmatically using ` useDirectusCache` :
342+
343+ ` ` ` ts
344+ import {injectable } from " @tsed/di" ;
345+ import {useDirectusCache } from " @tsed/directus-sdk/cache" ;
346+
347+ export class JiraIssueClient {
348+ search (query : string ) {
349+ return this .performSearch (query);
350+ }
351+ }
352+
353+ injectable (JiraIssueClient);
354+ useDirectusCache (JiraIssueClient, " search" , {ttl: 900000 });
355+ ` ` `
328356
329357## 🔧 Advanced Usage
330358
0 commit comments