@contentrain/query 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +220 -86
- package/dist/index.d.mts +270 -26
- package/dist/index.d.ts +270 -26
- package/dist/index.js +534 -175
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +530 -152
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -14
- package/CHANGELOG.md +0 -27
- package/src/index.test.ts +0 -150
- package/src/index.ts +0 -233
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -8
- package/vitest.config.ts +0 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Contentrain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,134 +1,268 @@
|
|
|
1
1
|
# @contentrain/query
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Core package of the Contentrain SDK. This package provides the fundamental functionality and types for interacting with Contentrain CMS.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 High-performance content loading
|
|
8
|
+
- 💾 LRU caching with size-based eviction
|
|
9
|
+
- 🔍 Advanced query capabilities with type-safe operators
|
|
10
|
+
- 📦 Full TypeScript support with generic types
|
|
11
|
+
- 🛡️ Comprehensive error handling
|
|
12
|
+
- ⚡ Memory-optimized performance
|
|
13
|
+
- 🌍 Multi-language support
|
|
14
|
+
- 🔄 Relation resolution
|
|
4
15
|
|
|
5
16
|
## Installation
|
|
6
17
|
|
|
7
18
|
```bash
|
|
8
|
-
|
|
19
|
+
# Using npm
|
|
20
|
+
npm install @contentrain/query
|
|
21
|
+
|
|
22
|
+
# Using yarn
|
|
23
|
+
yarn add @contentrain/query
|
|
24
|
+
|
|
25
|
+
# Using pnpm
|
|
26
|
+
pnpm add @contentrain/query
|
|
9
27
|
```
|
|
10
28
|
|
|
11
29
|
## Usage
|
|
12
30
|
|
|
31
|
+
### Content Loading
|
|
32
|
+
|
|
13
33
|
```typescript
|
|
14
|
-
import {
|
|
15
|
-
|
|
34
|
+
import { ContentLoader } from '@contentrain/query';
|
|
35
|
+
|
|
36
|
+
const loader = new ContentLoader({
|
|
37
|
+
contentDir: './content',
|
|
38
|
+
defaultLocale: 'en',
|
|
39
|
+
cache: true,
|
|
40
|
+
ttl: 60 * 1000, // 1 minute
|
|
41
|
+
maxCacheSize: 100 // 100 MB
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Load all blog posts
|
|
45
|
+
const posts = await loader.load('posts');
|
|
46
|
+
|
|
47
|
+
// Load with locale
|
|
48
|
+
const trPosts = await loader.load('posts').locale('tr');
|
|
49
|
+
|
|
50
|
+
// Error handling
|
|
51
|
+
try {
|
|
52
|
+
const post = await loader.load('posts', 'non-existent-post');
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof ContentNotFoundError) {
|
|
55
|
+
console.error('Post not found');
|
|
56
|
+
} else if (error instanceof ContentValidationError) {
|
|
57
|
+
console.error('Content validation failed');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
16
61
|
|
|
17
|
-
|
|
18
|
-
const core = new ContentrainCore()
|
|
19
|
-
const query = new ContentrainQuery(core)
|
|
62
|
+
### Query Operations
|
|
20
63
|
|
|
21
|
-
|
|
64
|
+
```typescript
|
|
65
|
+
import { ContentrainSDK } from '@contentrain/query';
|
|
66
|
+
|
|
67
|
+
const sdk = new ContentrainSDK({
|
|
68
|
+
contentDir: './content'
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Type-safe querying
|
|
72
|
+
interface Post {
|
|
73
|
+
ID: string;
|
|
74
|
+
title: string;
|
|
75
|
+
status: 'draft' | 'published';
|
|
76
|
+
tags: string[];
|
|
77
|
+
createdAt: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const query = sdk.query<{
|
|
81
|
+
fields: Post;
|
|
82
|
+
locales: 'en' | 'tr';
|
|
83
|
+
relations: {
|
|
84
|
+
author: Author;
|
|
85
|
+
categories: Category[];
|
|
86
|
+
}
|
|
87
|
+
}>('posts');
|
|
88
|
+
|
|
89
|
+
// Available operators
|
|
22
90
|
const posts = await query
|
|
23
|
-
.
|
|
24
|
-
.where('
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
// Complex query
|
|
28
|
-
const featuredPosts = await query
|
|
29
|
-
.from('posts')
|
|
30
|
-
.where('status', 'publish')
|
|
31
|
-
.where('featured', true)
|
|
91
|
+
.where('status', 'eq', 'published')
|
|
92
|
+
.where('tags', 'contains', ['javascript'])
|
|
93
|
+
.where('createdAt', 'gt', '2024-01-01')
|
|
94
|
+
.where('category', 'in', ['tech', 'programming'])
|
|
32
95
|
.orderBy('createdAt', 'desc')
|
|
33
96
|
.limit(5)
|
|
34
|
-
.get()
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
const
|
|
38
|
-
.
|
|
39
|
-
.
|
|
40
|
-
.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.
|
|
46
|
-
.
|
|
47
|
-
['status', 'publish'],
|
|
48
|
-
['category', 'technology'],
|
|
49
|
-
['title', 'startsWith', 'How to']
|
|
50
|
-
])
|
|
51
|
-
.get()
|
|
97
|
+
.get();
|
|
98
|
+
|
|
99
|
+
// Relation handling
|
|
100
|
+
const postsWithRelations = await query
|
|
101
|
+
.include(['author', 'categories'])
|
|
102
|
+
.where('status', 'eq', 'published')
|
|
103
|
+
.get();
|
|
104
|
+
|
|
105
|
+
// Locale support
|
|
106
|
+
const trPosts = await query
|
|
107
|
+
.locale('tr')
|
|
108
|
+
.where('status', 'eq', 'published')
|
|
109
|
+
.get();
|
|
52
110
|
```
|
|
53
111
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
### Constructor
|
|
112
|
+
### Caching
|
|
57
113
|
|
|
58
114
|
```typescript
|
|
59
|
-
|
|
115
|
+
import { MemoryCache } from '@contentrain/query';
|
|
116
|
+
|
|
117
|
+
const cache = new MemoryCache({
|
|
118
|
+
maxSize: 100, // Maximum cache size in MB
|
|
119
|
+
defaultTTL: 60 * 1000, // Default TTL in ms
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Set with custom TTL
|
|
123
|
+
await cache.set('key', data, 5 * 60 * 1000); // 5 minutes TTL
|
|
124
|
+
|
|
125
|
+
// Get with type safety
|
|
126
|
+
const data = await cache.get<Post[]>('key');
|
|
127
|
+
|
|
128
|
+
// Cache stats
|
|
129
|
+
const stats = cache.getStats();
|
|
130
|
+
console.log(`
|
|
131
|
+
Hits: ${stats.hits}
|
|
132
|
+
Misses: ${stats.misses}
|
|
133
|
+
Size: ${stats.size} bytes
|
|
134
|
+
Last Cleanup: ${stats.lastCleanup}
|
|
135
|
+
`);
|
|
60
136
|
```
|
|
61
137
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
### Methods
|
|
138
|
+
## API Reference
|
|
65
139
|
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
from(collection: string): ContentrainQuery
|
|
69
|
-
```
|
|
140
|
+
### ContentrainSDK
|
|
70
141
|
|
|
71
|
-
|
|
142
|
+
Main entry point for the SDK.
|
|
72
143
|
|
|
73
|
-
#### where
|
|
74
144
|
```typescript
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
145
|
+
class ContentrainSDK {
|
|
146
|
+
constructor(options: ContentLoaderOptions)
|
|
147
|
+
query<T extends QueryConfig>(model: string): ContentrainQueryBuilder<T>
|
|
148
|
+
load<T>(model: string): Promise<LoaderResult<T>>
|
|
149
|
+
}
|
|
78
150
|
```
|
|
79
151
|
|
|
80
|
-
|
|
152
|
+
### Query Builder
|
|
81
153
|
|
|
82
|
-
#### orderBy
|
|
83
154
|
```typescript
|
|
84
|
-
|
|
155
|
+
interface QueryBuilder<T> {
|
|
156
|
+
// Filter operations
|
|
157
|
+
where<K extends keyof T>(
|
|
158
|
+
field: K,
|
|
159
|
+
operator: QueryOperator,
|
|
160
|
+
value: T[K] | T[K][]
|
|
161
|
+
): this
|
|
162
|
+
|
|
163
|
+
// Available operators:
|
|
164
|
+
// 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' |
|
|
165
|
+
// 'in' | 'nin' | 'contains' | 'startsWith' | 'endsWith'
|
|
166
|
+
|
|
167
|
+
// Relation operations
|
|
168
|
+
include(relations: string | string[]): this
|
|
169
|
+
|
|
170
|
+
// Sorting
|
|
171
|
+
orderBy(field: keyof T, direction?: 'asc' | 'desc'): this
|
|
172
|
+
|
|
173
|
+
// Pagination
|
|
174
|
+
limit(count: number): this
|
|
175
|
+
offset(count: number): this
|
|
176
|
+
|
|
177
|
+
// Locale
|
|
178
|
+
locale(code: string): this
|
|
179
|
+
|
|
180
|
+
// Cache control
|
|
181
|
+
cache(ttl?: number): this
|
|
182
|
+
noCache(): this
|
|
183
|
+
bypassCache(): this
|
|
184
|
+
|
|
185
|
+
// Execution
|
|
186
|
+
get(): Promise<QueryResult<T>>
|
|
187
|
+
first(): Promise<T | null>
|
|
188
|
+
count(): Promise<number>
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
interface QueryResult<T> {
|
|
192
|
+
data: T[]
|
|
193
|
+
total: number
|
|
194
|
+
pagination?: {
|
|
195
|
+
limit: number
|
|
196
|
+
offset: number
|
|
197
|
+
hasMore: boolean
|
|
198
|
+
}
|
|
199
|
+
}
|
|
85
200
|
```
|
|
86
201
|
|
|
87
|
-
|
|
202
|
+
### Cache Manager
|
|
88
203
|
|
|
89
|
-
#### limit
|
|
90
204
|
```typescript
|
|
91
|
-
|
|
205
|
+
interface CacheManager {
|
|
206
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>
|
|
207
|
+
get<T>(key: string): Promise<T | null>
|
|
208
|
+
delete(key: string): Promise<void>
|
|
209
|
+
clear(): Promise<void>
|
|
210
|
+
getStats(): CacheStats
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
interface CacheStats {
|
|
214
|
+
hits: number
|
|
215
|
+
misses: number
|
|
216
|
+
size: number
|
|
217
|
+
lastCleanup: number
|
|
218
|
+
}
|
|
92
219
|
```
|
|
93
220
|
|
|
94
|
-
|
|
221
|
+
### Content Loader
|
|
95
222
|
|
|
96
|
-
#### offset
|
|
97
223
|
```typescript
|
|
98
|
-
|
|
224
|
+
interface ContentLoader {
|
|
225
|
+
load<T>(model: string): Promise<LoaderResult<T>>
|
|
226
|
+
resolveRelation<T, R>(
|
|
227
|
+
model: string,
|
|
228
|
+
relationField: keyof T,
|
|
229
|
+
data: T[],
|
|
230
|
+
locale?: string
|
|
231
|
+
): Promise<R[]>
|
|
232
|
+
clearCache(): Promise<void>
|
|
233
|
+
refreshCache(model: string): Promise<void>
|
|
234
|
+
getCacheStats(): CacheStats
|
|
235
|
+
}
|
|
99
236
|
```
|
|
100
237
|
|
|
101
|
-
|
|
238
|
+
## Error Handling
|
|
102
239
|
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
with(...relations: string[]): ContentrainQuery
|
|
106
|
-
```
|
|
240
|
+
The package provides specific error types for different scenarios:
|
|
107
241
|
|
|
108
|
-
Includes related content in the results.
|
|
109
|
-
|
|
110
|
-
#### get
|
|
111
242
|
```typescript
|
|
112
|
-
|
|
243
|
+
import {
|
|
244
|
+
ContentrainError, // Base error class
|
|
245
|
+
ContentNotFoundError,
|
|
246
|
+
ContentValidationError,
|
|
247
|
+
CacheError,
|
|
248
|
+
RelationError
|
|
249
|
+
} from '@contentrain/query';
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
const posts = await loader.load('posts');
|
|
253
|
+
} catch (error) {
|
|
254
|
+
if (error instanceof ContentNotFoundError) {
|
|
255
|
+
// Handle not found
|
|
256
|
+
} else if (error instanceof ContentValidationError) {
|
|
257
|
+
// Handle validation errors
|
|
258
|
+
} else if (error instanceof CacheError) {
|
|
259
|
+
// Handle cache errors
|
|
260
|
+
} else if (error instanceof RelationError) {
|
|
261
|
+
// Handle relation errors
|
|
262
|
+
}
|
|
263
|
+
}
|
|
113
264
|
```
|
|
114
265
|
|
|
115
|
-
Executes the query and returns the results.
|
|
116
|
-
|
|
117
|
-
### Filter Operators
|
|
118
|
-
|
|
119
|
-
- `equals` (default)
|
|
120
|
-
- `notEquals`
|
|
121
|
-
- `contains`
|
|
122
|
-
- `notContains`
|
|
123
|
-
- `startsWith`
|
|
124
|
-
- `endsWith`
|
|
125
|
-
- `exists`
|
|
126
|
-
- `notExists`
|
|
127
|
-
- `gt` (greater than)
|
|
128
|
-
- `gte` (greater than or equal)
|
|
129
|
-
- `lt` (less than)
|
|
130
|
-
- `lte` (less than or equal)
|
|
131
|
-
|
|
132
266
|
## License
|
|
133
267
|
|
|
134
268
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,28 +1,272 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
interface ContentrainConfig {
|
|
2
|
+
contentDir: string;
|
|
3
|
+
models: {
|
|
4
|
+
[modelId: string]: {
|
|
5
|
+
localized?: boolean;
|
|
6
|
+
defaultLocale?: string;
|
|
7
|
+
locales?: string[];
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
}
|
|
3
11
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
interface BaseContentrainType {
|
|
13
|
+
ID: string;
|
|
14
|
+
createdAt: string;
|
|
15
|
+
updatedAt: string;
|
|
16
|
+
status: 'draft' | 'changed' | 'publish';
|
|
17
|
+
scheduled: boolean;
|
|
18
|
+
_relations?: {
|
|
19
|
+
[key: string]: BaseContentrainType | BaseContentrainType[];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
type ContentrainStatus = 'draft' | 'changed' | 'publish';
|
|
23
|
+
interface ModelMetadata {
|
|
24
|
+
name: string;
|
|
25
|
+
modelId: string;
|
|
26
|
+
localization: boolean;
|
|
27
|
+
type: 'JSON';
|
|
28
|
+
createdBy: string;
|
|
29
|
+
isServerless: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface FieldMetadata {
|
|
32
|
+
name: string;
|
|
33
|
+
fieldId: string;
|
|
34
|
+
modelId: string;
|
|
35
|
+
componentId: ContentrainComponentId;
|
|
36
|
+
fieldType: ContentrainFieldType;
|
|
37
|
+
options: FieldOptions;
|
|
38
|
+
validations: FieldValidations;
|
|
39
|
+
system?: boolean;
|
|
40
|
+
defaultField?: boolean;
|
|
41
|
+
}
|
|
42
|
+
type ContentrainFieldType = 'string' | 'number' | 'boolean' | 'array' | 'date' | 'media' | 'relation';
|
|
43
|
+
type ContentrainComponentId = 'single-line-text' | 'multi-line-text' | 'email' | 'url' | 'slug' | 'color' | 'json' | 'md-editor' | 'rich-text-editor' | 'integer' | 'decimal' | 'rating' | 'percent' | 'phone-number' | 'checkbox' | 'switch' | 'date' | 'date-time' | 'media' | 'one-to-one' | 'one-to-many';
|
|
44
|
+
interface FieldOptions {
|
|
45
|
+
'title-field'?: {
|
|
46
|
+
value: boolean;
|
|
47
|
+
};
|
|
48
|
+
'default-value'?: {
|
|
49
|
+
value: boolean;
|
|
50
|
+
form: {
|
|
51
|
+
[key: string]: {
|
|
52
|
+
value: any;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
'reference'?: {
|
|
57
|
+
value: boolean;
|
|
58
|
+
form: {
|
|
59
|
+
reference: {
|
|
60
|
+
value: string;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
interface FieldValidations {
|
|
66
|
+
'required-field'?: {
|
|
67
|
+
value: boolean;
|
|
68
|
+
};
|
|
69
|
+
'unique-field'?: {
|
|
70
|
+
value: boolean;
|
|
71
|
+
};
|
|
72
|
+
'input-range-field'?: {
|
|
73
|
+
value: {
|
|
74
|
+
min: number;
|
|
75
|
+
max: number;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
interface AssetMetadata {
|
|
80
|
+
path: string;
|
|
81
|
+
size: number;
|
|
82
|
+
type: string;
|
|
83
|
+
createdAt: string;
|
|
84
|
+
updatedAt: string;
|
|
85
|
+
}
|
|
86
|
+
type ContentrainLocales = string;
|
|
87
|
+
interface QueryConfig<TFields extends BaseContentrainType, TLocales extends ContentrainLocales, TRelations extends Record<string, BaseContentrainType>> {
|
|
88
|
+
fields: TFields;
|
|
89
|
+
locales: TLocales;
|
|
90
|
+
relations: TRelations;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface ContentLoaderOptions {
|
|
94
|
+
contentDir: string;
|
|
95
|
+
defaultLocale?: string;
|
|
96
|
+
cache?: boolean;
|
|
97
|
+
ttl?: number;
|
|
98
|
+
maxCacheSize?: number;
|
|
99
|
+
modelTTL?: {
|
|
100
|
+
[model: string]: number;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
interface ModelConfig {
|
|
104
|
+
metadata: ModelMetadata;
|
|
105
|
+
fields: FieldMetadata[];
|
|
106
|
+
}
|
|
107
|
+
interface ContentFile<T extends BaseContentrainType = BaseContentrainType> {
|
|
108
|
+
model: string;
|
|
109
|
+
locale?: string;
|
|
110
|
+
data: T[];
|
|
111
|
+
}
|
|
112
|
+
interface LoaderResult<T extends BaseContentrainType = BaseContentrainType> {
|
|
113
|
+
model: ModelConfig;
|
|
114
|
+
content: {
|
|
115
|
+
[locale: string]: T[];
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
interface RelationConfig {
|
|
119
|
+
model: string;
|
|
120
|
+
type: 'one-to-one' | 'one-to-many';
|
|
121
|
+
foreignKey: string;
|
|
122
|
+
}
|
|
123
|
+
interface CacheStats {
|
|
124
|
+
hits: number;
|
|
125
|
+
misses: number;
|
|
126
|
+
size: number;
|
|
127
|
+
lastCleanup: number;
|
|
128
|
+
}
|
|
129
|
+
interface CacheEntry<T> {
|
|
130
|
+
data: T;
|
|
131
|
+
expireAt: number;
|
|
132
|
+
size: number;
|
|
133
|
+
createdAt: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
type Operator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'contains' | 'startsWith' | 'endsWith';
|
|
137
|
+
interface Filter {
|
|
138
|
+
field: string;
|
|
139
|
+
operator: Operator;
|
|
140
|
+
value: any;
|
|
141
|
+
}
|
|
142
|
+
interface Sort {
|
|
143
|
+
field: string;
|
|
144
|
+
direction: 'asc' | 'desc';
|
|
145
|
+
}
|
|
146
|
+
interface Pagination {
|
|
147
|
+
limit?: number;
|
|
148
|
+
offset?: number;
|
|
149
|
+
}
|
|
150
|
+
interface Include {
|
|
151
|
+
[relation: string]: {
|
|
152
|
+
fields?: string[];
|
|
153
|
+
include?: Include;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
interface QueryOptions {
|
|
157
|
+
locale?: string;
|
|
158
|
+
cache?: boolean;
|
|
159
|
+
ttl?: number;
|
|
160
|
+
}
|
|
161
|
+
interface QueryResult<T> {
|
|
162
|
+
data: T[];
|
|
163
|
+
total: number;
|
|
164
|
+
pagination?: {
|
|
165
|
+
limit: number;
|
|
166
|
+
offset: number;
|
|
167
|
+
hasMore: boolean;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
declare class ContentLoader {
|
|
172
|
+
private options;
|
|
173
|
+
private modelConfigs;
|
|
9
174
|
private relations;
|
|
10
|
-
private
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
private
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
175
|
+
private cache;
|
|
176
|
+
constructor(options: ContentLoaderOptions);
|
|
177
|
+
private getCacheKey;
|
|
178
|
+
private getModelTTL;
|
|
179
|
+
clearCache(): Promise<void>;
|
|
180
|
+
refreshCache(model: string): Promise<void>;
|
|
181
|
+
getCacheStats(): CacheStats;
|
|
182
|
+
private loadModelConfig;
|
|
183
|
+
private loadContentFile;
|
|
184
|
+
private loadRelations;
|
|
185
|
+
load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
|
|
186
|
+
resolveRelation<T extends BaseContentrainType, R extends BaseContentrainType>(model: string, relationField: keyof T, data: T[], locale?: string): Promise<R[]>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
declare class QueryExecutor {
|
|
190
|
+
private loader;
|
|
191
|
+
constructor(loader: ContentLoader);
|
|
192
|
+
private applyFilters;
|
|
193
|
+
private applySorting;
|
|
194
|
+
private applyPagination;
|
|
195
|
+
private resolveIncludes;
|
|
196
|
+
execute<T extends BaseContentrainType>({ model, data, filters, includes, sorting, pagination, options, }: {
|
|
197
|
+
model: string;
|
|
198
|
+
data: T[];
|
|
199
|
+
filters?: Filter[];
|
|
200
|
+
includes?: Include;
|
|
201
|
+
sorting?: Sort[];
|
|
202
|
+
pagination?: {
|
|
203
|
+
limit?: number;
|
|
204
|
+
offset?: number;
|
|
205
|
+
};
|
|
206
|
+
options?: QueryOptions;
|
|
207
|
+
}): Promise<QueryResult<T>>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
declare class ContentrainQueryBuilder<TFields extends BaseContentrainType, TLocales extends ContentrainLocales = 'en' | 'tr', TRelations extends Record<string, BaseContentrainType> = Record<string, never>> {
|
|
211
|
+
private model;
|
|
212
|
+
private filters;
|
|
213
|
+
private includes;
|
|
214
|
+
private sorting;
|
|
215
|
+
private pagination;
|
|
216
|
+
private options;
|
|
217
|
+
private executor;
|
|
218
|
+
private loader;
|
|
219
|
+
constructor(model: string, executor: QueryExecutor, loader: ContentLoader);
|
|
220
|
+
where<K extends keyof TFields, O extends Operator>(field: K, operator: O, value: O extends 'in' ? TFields[K][] : TFields[K]): this;
|
|
221
|
+
include<K extends keyof TRelations>(relation: K | K[]): this;
|
|
222
|
+
orderBy<K extends keyof TFields>(field: K, direction?: 'asc' | 'desc'): this;
|
|
223
|
+
limit(count: number): this;
|
|
224
|
+
offset(count: number): this;
|
|
225
|
+
locale(code: TLocales): this;
|
|
226
|
+
cache(ttl?: number): this;
|
|
227
|
+
noCache(): this;
|
|
228
|
+
bypassCache(): this;
|
|
229
|
+
toJSON(): {
|
|
230
|
+
model: string;
|
|
231
|
+
filters: Filter[];
|
|
232
|
+
includes: Include;
|
|
233
|
+
sorting: Sort[];
|
|
234
|
+
pagination: {
|
|
235
|
+
limit?: number;
|
|
236
|
+
offset?: number;
|
|
237
|
+
};
|
|
238
|
+
options: QueryOptions;
|
|
239
|
+
};
|
|
240
|
+
get(): Promise<QueryResult<TFields>>;
|
|
241
|
+
first(): Promise<TFields | null>;
|
|
242
|
+
count(): Promise<number>;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
interface MemoryCacheOptions {
|
|
246
|
+
maxSize?: number;
|
|
247
|
+
defaultTTL?: number;
|
|
248
|
+
}
|
|
249
|
+
declare class MemoryCache {
|
|
250
|
+
private cache;
|
|
251
|
+
private options;
|
|
252
|
+
private stats;
|
|
253
|
+
constructor(options?: MemoryCacheOptions);
|
|
254
|
+
private calculateSize;
|
|
255
|
+
set<T>(key: string, data: T, ttl?: number): Promise<void>;
|
|
256
|
+
private findOldestKey;
|
|
257
|
+
get<T>(key: string): Promise<T | null>;
|
|
258
|
+
delete(key: string): Promise<void>;
|
|
259
|
+
clear(): Promise<void>;
|
|
260
|
+
private cleanupCache;
|
|
261
|
+
getStats(): CacheStats;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
declare class ContentrainSDK {
|
|
265
|
+
private loader;
|
|
266
|
+
private executor;
|
|
267
|
+
constructor(options: ContentLoaderOptions);
|
|
268
|
+
query<T extends QueryConfig<BaseContentrainType, string, Record<string, BaseContentrainType>>>(model: string): ContentrainQueryBuilder<T['fields'], T['locales'], T['relations']>;
|
|
269
|
+
load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export { type AssetMetadata, type BaseContentrainType, type CacheEntry, type CacheStats, type ContentFile, ContentLoader, type ContentLoaderOptions, type ContentrainComponentId, type ContentrainConfig, type ContentrainFieldType, type ContentrainLocales, ContentrainQueryBuilder, ContentrainSDK, type ContentrainStatus, type FieldMetadata, type FieldOptions, type FieldValidations, type Filter, type Include, type LoaderResult, MemoryCache, type MemoryCacheOptions, type ModelConfig, type ModelMetadata, type Operator, type Pagination, type QueryConfig, QueryExecutor, type QueryOptions, type QueryResult, type RelationConfig, type Sort };
|