@contentrain/query 3.0.0 → 3.2.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/dist/index.d.mts CHANGED
@@ -1,8 +1,9 @@
1
1
  interface ContentrainConfig {
2
2
  contentDir: string;
3
+ defaultLocale?: string;
3
4
  models: {
4
5
  [modelId: string]: {
5
- localized?: boolean;
6
+ localized: boolean;
6
7
  defaultLocale?: string;
7
8
  locales?: string[];
8
9
  };
@@ -76,19 +77,7 @@ interface FieldValidations {
76
77
  };
77
78
  };
78
79
  }
79
- interface AssetMetadata {
80
- path: string;
81
- size: number;
82
- type: string;
83
- createdAt: string;
84
- updatedAt: string;
85
- }
86
80
  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
81
 
93
82
  interface ContentLoaderOptions {
94
83
  contentDir: string;
@@ -109,11 +98,26 @@ interface ContentFile<T extends BaseContentrainType = BaseContentrainType> {
109
98
  locale?: string;
110
99
  data: T[];
111
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
+ }
112
115
  interface LoaderResult<T extends BaseContentrainType = BaseContentrainType> {
113
116
  model: ModelConfig;
114
117
  content: {
115
118
  [locale: string]: T[];
116
119
  };
120
+ assets?: AssetMetadata[];
117
121
  }
118
122
  interface RelationConfig {
119
123
  model: string;
@@ -132,12 +136,19 @@ interface CacheEntry<T> {
132
136
  size: number;
133
137
  createdAt: number;
134
138
  }
139
+ interface MemoryCacheOptions {
140
+ maxSize?: number;
141
+ defaultTTL?: number;
142
+ }
135
143
 
136
- type Operator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'contains' | 'startsWith' | 'endsWith';
137
- interface Filter {
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> {
138
149
  field: string;
139
150
  operator: Operator;
140
- value: any;
151
+ value: T extends Array<infer U> ? (ArrayOperator extends 'in' | 'nin' ? U[] : U) : T;
141
152
  }
142
153
  interface Sort {
143
154
  field: string;
@@ -167,6 +178,11 @@ interface QueryResult<T> {
167
178
  hasMore: boolean;
168
179
  };
169
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
+ }
170
186
 
171
187
  declare class ContentLoader {
172
188
  private options;
@@ -182,6 +198,7 @@ declare class ContentLoader {
182
198
  private loadModelConfig;
183
199
  private loadContentFile;
184
200
  private loadRelations;
201
+ private getModelLocales;
185
202
  load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
186
203
  resolveRelation<T extends BaseContentrainType, R extends BaseContentrainType>(model: string, relationField: keyof T, data: T[], locale?: string): Promise<R[]>;
187
204
  }
@@ -193,6 +210,7 @@ declare class QueryExecutor {
193
210
  private applySorting;
194
211
  private applyPagination;
195
212
  private resolveIncludes;
213
+ private applyStringOperation;
196
214
  execute<T extends BaseContentrainType>({ model, data, filters, includes, sorting, pagination, options, }: {
197
215
  model: string;
198
216
  data: T[];
@@ -228,13 +246,10 @@ declare class ContentrainQueryBuilder<TFields extends BaseContentrainType, TLoca
228
246
  bypassCache(): this;
229
247
  toJSON(): {
230
248
  model: string;
231
- filters: Filter[];
249
+ filters: Filter<any>[];
232
250
  includes: Include;
233
251
  sorting: Sort[];
234
- pagination: {
235
- limit?: number;
236
- offset?: number;
237
- };
252
+ pagination: Pagination;
238
253
  options: QueryOptions;
239
254
  };
240
255
  get(): Promise<QueryResult<TFields>>;
@@ -242,10 +257,6 @@ declare class ContentrainQueryBuilder<TFields extends BaseContentrainType, TLoca
242
257
  count(): Promise<number>;
243
258
  }
244
259
 
245
- interface MemoryCacheOptions {
246
- maxSize?: number;
247
- defaultTTL?: number;
248
- }
249
260
  declare class MemoryCache {
250
261
  private cache;
251
262
  private options;
@@ -261,12 +272,20 @@ declare class MemoryCache {
261
272
  getStats(): CacheStats;
262
273
  }
263
274
 
275
+ declare const logger: {
276
+ debug: (...args: unknown[]) => void;
277
+ error: (...args: unknown[]) => void;
278
+ };
279
+
264
280
  declare class ContentrainSDK {
265
281
  private loader;
266
282
  private executor;
267
283
  constructor(options: ContentLoaderOptions);
268
284
  query<T extends QueryConfig<BaseContentrainType, string, Record<string, BaseContentrainType>>>(model: string): ContentrainQueryBuilder<T['fields'], T['locales'], T['relations']>;
269
285
  load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
286
+ clearCache(): Promise<void>;
287
+ refreshCache(model: string): Promise<void>;
288
+ getCacheStats(): CacheStats;
270
289
  }
271
290
 
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 };
291
+ 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, logger };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  interface ContentrainConfig {
2
2
  contentDir: string;
3
+ defaultLocale?: string;
3
4
  models: {
4
5
  [modelId: string]: {
5
- localized?: boolean;
6
+ localized: boolean;
6
7
  defaultLocale?: string;
7
8
  locales?: string[];
8
9
  };
@@ -76,19 +77,7 @@ interface FieldValidations {
76
77
  };
77
78
  };
78
79
  }
79
- interface AssetMetadata {
80
- path: string;
81
- size: number;
82
- type: string;
83
- createdAt: string;
84
- updatedAt: string;
85
- }
86
80
  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
81
 
93
82
  interface ContentLoaderOptions {
94
83
  contentDir: string;
@@ -109,11 +98,26 @@ interface ContentFile<T extends BaseContentrainType = BaseContentrainType> {
109
98
  locale?: string;
110
99
  data: T[];
111
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
+ }
112
115
  interface LoaderResult<T extends BaseContentrainType = BaseContentrainType> {
113
116
  model: ModelConfig;
114
117
  content: {
115
118
  [locale: string]: T[];
116
119
  };
120
+ assets?: AssetMetadata[];
117
121
  }
118
122
  interface RelationConfig {
119
123
  model: string;
@@ -132,12 +136,19 @@ interface CacheEntry<T> {
132
136
  size: number;
133
137
  createdAt: number;
134
138
  }
139
+ interface MemoryCacheOptions {
140
+ maxSize?: number;
141
+ defaultTTL?: number;
142
+ }
135
143
 
136
- type Operator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'contains' | 'startsWith' | 'endsWith';
137
- interface Filter {
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> {
138
149
  field: string;
139
150
  operator: Operator;
140
- value: any;
151
+ value: T extends Array<infer U> ? (ArrayOperator extends 'in' | 'nin' ? U[] : U) : T;
141
152
  }
142
153
  interface Sort {
143
154
  field: string;
@@ -167,6 +178,11 @@ interface QueryResult<T> {
167
178
  hasMore: boolean;
168
179
  };
169
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
+ }
170
186
 
171
187
  declare class ContentLoader {
172
188
  private options;
@@ -182,6 +198,7 @@ declare class ContentLoader {
182
198
  private loadModelConfig;
183
199
  private loadContentFile;
184
200
  private loadRelations;
201
+ private getModelLocales;
185
202
  load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
186
203
  resolveRelation<T extends BaseContentrainType, R extends BaseContentrainType>(model: string, relationField: keyof T, data: T[], locale?: string): Promise<R[]>;
187
204
  }
@@ -193,6 +210,7 @@ declare class QueryExecutor {
193
210
  private applySorting;
194
211
  private applyPagination;
195
212
  private resolveIncludes;
213
+ private applyStringOperation;
196
214
  execute<T extends BaseContentrainType>({ model, data, filters, includes, sorting, pagination, options, }: {
197
215
  model: string;
198
216
  data: T[];
@@ -228,13 +246,10 @@ declare class ContentrainQueryBuilder<TFields extends BaseContentrainType, TLoca
228
246
  bypassCache(): this;
229
247
  toJSON(): {
230
248
  model: string;
231
- filters: Filter[];
249
+ filters: Filter<any>[];
232
250
  includes: Include;
233
251
  sorting: Sort[];
234
- pagination: {
235
- limit?: number;
236
- offset?: number;
237
- };
252
+ pagination: Pagination;
238
253
  options: QueryOptions;
239
254
  };
240
255
  get(): Promise<QueryResult<TFields>>;
@@ -242,10 +257,6 @@ declare class ContentrainQueryBuilder<TFields extends BaseContentrainType, TLoca
242
257
  count(): Promise<number>;
243
258
  }
244
259
 
245
- interface MemoryCacheOptions {
246
- maxSize?: number;
247
- defaultTTL?: number;
248
- }
249
260
  declare class MemoryCache {
250
261
  private cache;
251
262
  private options;
@@ -261,12 +272,20 @@ declare class MemoryCache {
261
272
  getStats(): CacheStats;
262
273
  }
263
274
 
275
+ declare const logger: {
276
+ debug: (...args: unknown[]) => void;
277
+ error: (...args: unknown[]) => void;
278
+ };
279
+
264
280
  declare class ContentrainSDK {
265
281
  private loader;
266
282
  private executor;
267
283
  constructor(options: ContentLoaderOptions);
268
284
  query<T extends QueryConfig<BaseContentrainType, string, Record<string, BaseContentrainType>>>(model: string): ContentrainQueryBuilder<T['fields'], T['locales'], T['relations']>;
269
285
  load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
286
+ clearCache(): Promise<void>;
287
+ refreshCache(model: string): Promise<void>;
288
+ getCacheStats(): CacheStats;
270
289
  }
271
290
 
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 };
291
+ 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, logger };