@contentrain/query 2.0.1 → 3.1.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 +284 -26
- package/dist/index.d.ts +284 -26
- package/dist/index.js +630 -171
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +626 -148
- package/dist/index.mjs.map +1 -0
- package/package.json +37 -13
- 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/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,286 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
interface ContentrainConfig {
|
|
2
|
+
contentDir: string;
|
|
3
|
+
defaultLocale?: string;
|
|
4
|
+
models: {
|
|
5
|
+
[modelId: string]: {
|
|
6
|
+
localized: boolean;
|
|
7
|
+
defaultLocale?: string;
|
|
8
|
+
locales?: string[];
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
}
|
|
3
12
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
interface BaseContentrainType {
|
|
14
|
+
ID: string;
|
|
15
|
+
createdAt: string;
|
|
16
|
+
updatedAt: string;
|
|
17
|
+
status: 'draft' | 'changed' | 'publish';
|
|
18
|
+
scheduled: boolean;
|
|
19
|
+
_relations?: {
|
|
20
|
+
[key: string]: BaseContentrainType | BaseContentrainType[];
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
type ContentrainStatus = 'draft' | 'changed' | 'publish';
|
|
24
|
+
interface ModelMetadata {
|
|
25
|
+
name: string;
|
|
26
|
+
modelId: string;
|
|
27
|
+
localization: boolean;
|
|
28
|
+
type: 'JSON';
|
|
29
|
+
createdBy: string;
|
|
30
|
+
isServerless: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface FieldMetadata {
|
|
33
|
+
name: string;
|
|
34
|
+
fieldId: string;
|
|
35
|
+
modelId: string;
|
|
36
|
+
componentId: ContentrainComponentId;
|
|
37
|
+
fieldType: ContentrainFieldType;
|
|
38
|
+
options: FieldOptions;
|
|
39
|
+
validations: FieldValidations;
|
|
40
|
+
system?: boolean;
|
|
41
|
+
defaultField?: boolean;
|
|
42
|
+
}
|
|
43
|
+
type ContentrainFieldType = 'string' | 'number' | 'boolean' | 'array' | 'date' | 'media' | 'relation';
|
|
44
|
+
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';
|
|
45
|
+
interface FieldOptions {
|
|
46
|
+
'title-field'?: {
|
|
47
|
+
value: boolean;
|
|
48
|
+
};
|
|
49
|
+
'default-value'?: {
|
|
50
|
+
value: boolean;
|
|
51
|
+
form: {
|
|
52
|
+
[key: string]: {
|
|
53
|
+
value: any;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
'reference'?: {
|
|
58
|
+
value: boolean;
|
|
59
|
+
form: {
|
|
60
|
+
reference: {
|
|
61
|
+
value: string;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
interface FieldValidations {
|
|
67
|
+
'required-field'?: {
|
|
68
|
+
value: boolean;
|
|
69
|
+
};
|
|
70
|
+
'unique-field'?: {
|
|
71
|
+
value: boolean;
|
|
72
|
+
};
|
|
73
|
+
'input-range-field'?: {
|
|
74
|
+
value: {
|
|
75
|
+
min: number;
|
|
76
|
+
max: number;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
type ContentrainLocales = string;
|
|
81
|
+
|
|
82
|
+
interface ContentLoaderOptions {
|
|
83
|
+
contentDir: string;
|
|
84
|
+
defaultLocale?: string;
|
|
85
|
+
cache?: boolean;
|
|
86
|
+
ttl?: number;
|
|
87
|
+
maxCacheSize?: number;
|
|
88
|
+
modelTTL?: {
|
|
89
|
+
[model: string]: number;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
interface ModelConfig {
|
|
93
|
+
metadata: ModelMetadata;
|
|
94
|
+
fields: FieldMetadata[];
|
|
95
|
+
}
|
|
96
|
+
interface ContentFile<T extends BaseContentrainType = BaseContentrainType> {
|
|
97
|
+
model: string;
|
|
98
|
+
locale?: string;
|
|
99
|
+
data: T[];
|
|
100
|
+
}
|
|
101
|
+
interface AssetMetadata {
|
|
102
|
+
path: string;
|
|
103
|
+
mimetype: string;
|
|
104
|
+
size: number;
|
|
105
|
+
alt: string;
|
|
106
|
+
meta: {
|
|
107
|
+
user: {
|
|
108
|
+
name: string;
|
|
109
|
+
email: string;
|
|
110
|
+
avatar: string;
|
|
111
|
+
};
|
|
112
|
+
createdAt: string;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
interface LoaderResult<T extends BaseContentrainType = BaseContentrainType> {
|
|
116
|
+
model: ModelConfig;
|
|
117
|
+
content: {
|
|
118
|
+
[locale: string]: T[];
|
|
119
|
+
};
|
|
120
|
+
assets?: AssetMetadata[];
|
|
121
|
+
}
|
|
122
|
+
interface RelationConfig {
|
|
123
|
+
model: string;
|
|
124
|
+
type: 'one-to-one' | 'one-to-many';
|
|
125
|
+
foreignKey: string;
|
|
126
|
+
}
|
|
127
|
+
interface CacheStats {
|
|
128
|
+
hits: number;
|
|
129
|
+
misses: number;
|
|
130
|
+
size: number;
|
|
131
|
+
lastCleanup: number;
|
|
132
|
+
}
|
|
133
|
+
interface CacheEntry<T> {
|
|
134
|
+
data: T;
|
|
135
|
+
expireAt: number;
|
|
136
|
+
size: number;
|
|
137
|
+
createdAt: number;
|
|
138
|
+
}
|
|
139
|
+
interface MemoryCacheOptions {
|
|
140
|
+
maxSize?: number;
|
|
141
|
+
defaultTTL?: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type StringOperator = 'eq' | 'ne' | 'contains' | 'startsWith' | 'endsWith';
|
|
145
|
+
type NumericOperator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte';
|
|
146
|
+
type ArrayOperator = 'in' | 'nin';
|
|
147
|
+
type Operator = StringOperator | NumericOperator | ArrayOperator;
|
|
148
|
+
interface Filter<T = any> {
|
|
149
|
+
field: string;
|
|
150
|
+
operator: Operator;
|
|
151
|
+
value: T extends Array<infer U> ? (ArrayOperator extends 'in' | 'nin' ? U[] : U) : T;
|
|
152
|
+
}
|
|
153
|
+
interface Sort {
|
|
154
|
+
field: string;
|
|
155
|
+
direction: 'asc' | 'desc';
|
|
156
|
+
}
|
|
157
|
+
interface Pagination {
|
|
158
|
+
limit?: number;
|
|
159
|
+
offset?: number;
|
|
160
|
+
}
|
|
161
|
+
interface Include {
|
|
162
|
+
[relation: string]: {
|
|
163
|
+
fields?: string[];
|
|
164
|
+
include?: Include;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
interface QueryOptions {
|
|
168
|
+
locale?: string;
|
|
169
|
+
cache?: boolean;
|
|
170
|
+
ttl?: number;
|
|
171
|
+
}
|
|
172
|
+
interface QueryResult<T> {
|
|
173
|
+
data: T[];
|
|
174
|
+
total: number;
|
|
175
|
+
pagination?: {
|
|
176
|
+
limit: number;
|
|
177
|
+
offset: number;
|
|
178
|
+
hasMore: boolean;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
interface QueryConfig<TFields extends BaseContentrainType, TLocales extends ContentrainLocales = 'en' | 'tr', TRelations extends Record<string, BaseContentrainType> = Record<string, never>> {
|
|
182
|
+
fields: TFields;
|
|
183
|
+
locales: TLocales;
|
|
184
|
+
relations: TRelations;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
declare class ContentLoader {
|
|
188
|
+
private options;
|
|
189
|
+
private modelConfigs;
|
|
9
190
|
private relations;
|
|
10
|
-
private
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
private
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
191
|
+
private cache;
|
|
192
|
+
constructor(options: ContentLoaderOptions);
|
|
193
|
+
private getCacheKey;
|
|
194
|
+
private getModelTTL;
|
|
195
|
+
clearCache(): Promise<void>;
|
|
196
|
+
refreshCache(model: string): Promise<void>;
|
|
197
|
+
getCacheStats(): CacheStats;
|
|
198
|
+
private loadModelConfig;
|
|
199
|
+
private loadContentFile;
|
|
200
|
+
private loadRelations;
|
|
201
|
+
private getModelLocales;
|
|
202
|
+
load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
|
|
203
|
+
resolveRelation<T extends BaseContentrainType, R extends BaseContentrainType>(model: string, relationField: keyof T, data: T[], locale?: string): Promise<R[]>;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
declare class QueryExecutor {
|
|
207
|
+
private loader;
|
|
208
|
+
constructor(loader: ContentLoader);
|
|
209
|
+
private applyFilters;
|
|
210
|
+
private applySorting;
|
|
211
|
+
private applyPagination;
|
|
212
|
+
private resolveIncludes;
|
|
213
|
+
private applyStringOperation;
|
|
214
|
+
execute<T extends BaseContentrainType>({ model, data, filters, includes, sorting, pagination, options, }: {
|
|
215
|
+
model: string;
|
|
216
|
+
data: T[];
|
|
217
|
+
filters?: Filter[];
|
|
218
|
+
includes?: Include;
|
|
219
|
+
sorting?: Sort[];
|
|
220
|
+
pagination?: {
|
|
221
|
+
limit?: number;
|
|
222
|
+
offset?: number;
|
|
223
|
+
};
|
|
224
|
+
options?: QueryOptions;
|
|
225
|
+
}): Promise<QueryResult<T>>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
declare class ContentrainQueryBuilder<TFields extends BaseContentrainType, TLocales extends ContentrainLocales = 'en' | 'tr', TRelations extends Record<string, BaseContentrainType> = Record<string, never>> {
|
|
229
|
+
private model;
|
|
230
|
+
private filters;
|
|
231
|
+
private includes;
|
|
232
|
+
private sorting;
|
|
233
|
+
private pagination;
|
|
234
|
+
private options;
|
|
235
|
+
private executor;
|
|
236
|
+
private loader;
|
|
237
|
+
constructor(model: string, executor: QueryExecutor, loader: ContentLoader);
|
|
238
|
+
where<K extends keyof TFields, O extends Operator>(field: K, operator: O, value: O extends 'in' ? TFields[K][] : TFields[K]): this;
|
|
239
|
+
include<K extends keyof TRelations>(relation: K | K[]): this;
|
|
240
|
+
orderBy<K extends keyof TFields>(field: K, direction?: 'asc' | 'desc'): this;
|
|
241
|
+
limit(count: number): this;
|
|
242
|
+
offset(count: number): this;
|
|
243
|
+
locale(code: TLocales): this;
|
|
244
|
+
cache(ttl?: number): this;
|
|
245
|
+
noCache(): this;
|
|
246
|
+
bypassCache(): this;
|
|
247
|
+
toJSON(): {
|
|
248
|
+
model: string;
|
|
249
|
+
filters: Filter<any>[];
|
|
250
|
+
includes: Include;
|
|
251
|
+
sorting: Sort[];
|
|
252
|
+
pagination: Pagination;
|
|
253
|
+
options: QueryOptions;
|
|
254
|
+
};
|
|
255
|
+
get(): Promise<QueryResult<TFields>>;
|
|
256
|
+
first(): Promise<TFields | null>;
|
|
257
|
+
count(): Promise<number>;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
declare class MemoryCache {
|
|
261
|
+
private cache;
|
|
262
|
+
private options;
|
|
263
|
+
private stats;
|
|
264
|
+
constructor(options?: MemoryCacheOptions);
|
|
265
|
+
private calculateSize;
|
|
266
|
+
set<T>(key: string, data: T, ttl?: number): Promise<void>;
|
|
267
|
+
private findOldestKey;
|
|
268
|
+
get<T>(key: string): Promise<T | null>;
|
|
269
|
+
delete(key: string): Promise<void>;
|
|
270
|
+
clear(): Promise<void>;
|
|
271
|
+
private cleanupCache;
|
|
272
|
+
getStats(): CacheStats;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
declare class ContentrainSDK {
|
|
276
|
+
private loader;
|
|
277
|
+
private executor;
|
|
278
|
+
constructor(options: ContentLoaderOptions);
|
|
279
|
+
query<T extends QueryConfig<BaseContentrainType, string, Record<string, BaseContentrainType>>>(model: string): ContentrainQueryBuilder<T['fields'], T['locales'], T['relations']>;
|
|
280
|
+
load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
|
|
281
|
+
clearCache(): Promise<void>;
|
|
282
|
+
refreshCache(model: string): Promise<void>;
|
|
283
|
+
getCacheStats(): CacheStats;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export { type ArrayOperator, 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 NumericOperator, type Operator, type Pagination, type QueryConfig, QueryExecutor, type QueryOptions, type QueryResult, type RelationConfig, type Sort, type StringOperator };
|