@contentrain/query 2.0.0 → 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 -20
- 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/dist/index.d.ts
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 };
|