@contentrain/query 3.2.0 → 5.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/dist/index.d.mts CHANGED
@@ -1,291 +1,108 @@
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
- }
12
-
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;
190
- private relations;
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 const logger: {
276
- debug: (...args: unknown[]) => void;
277
- error: (...args: unknown[]) => void;
278
- };
279
-
280
- declare class ContentrainSDK {
281
- private loader;
282
- private executor;
283
- constructor(options: ContentLoaderOptions);
284
- query<T extends QueryConfig<BaseContentrainType, string, Record<string, BaseContentrainType>>>(model: string): ContentrainQueryBuilder<T['fields'], T['locales'], T['relations']>;
285
- load<T extends BaseContentrainType>(model: string): Promise<LoaderResult<T>>;
286
- clearCache(): Promise<void>;
287
- refreshCache(model: string): Promise<void>;
288
- getCacheStats(): CacheStats;
289
- }
1
+ import { ContentStatus, ContentrainConfig, FieldDef, FieldType, ModelDefinition, ModelKind } from "@contentrain/types";
290
2
 
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 };
3
+ //#region src/runtime/query.d.ts
4
+ interface RelationMeta {
5
+ target: string | string[];
6
+ multi: boolean;
7
+ }
8
+ type RelationResolver = (model: string, id: string, locale: string | null) => Record<string, unknown> | undefined;
9
+ declare class QueryBuilder<T extends object> {
10
+ private _data;
11
+ private _locale;
12
+ private _filters;
13
+ private _sortField;
14
+ private _sortOrder;
15
+ private _limit;
16
+ private _offset;
17
+ private _includes;
18
+ private _relationMeta;
19
+ private _resolver;
20
+ private _defaultLocale;
21
+ constructor(data: Map<string, T[]>, relationMeta?: Record<string, RelationMeta>, resolver?: RelationResolver, defaultLocale?: string);
22
+ locale(lang: string): this;
23
+ where<K extends string & keyof T>(field: K, value: T[K]): this;
24
+ sort<K extends string & keyof T>(field: K, order?: 'asc' | 'desc'): this;
25
+ limit(n: number): this;
26
+ offset(n: number): this;
27
+ include(...fields: string[]): this;
28
+ all(): T[];
29
+ first(): T | undefined;
30
+ private _resolveData;
31
+ private _resolveIncludes;
32
+ private _resolveId;
33
+ }
34
+ //#endregion
35
+ //#region src/runtime/singleton.d.ts
36
+ declare class SingletonAccessor<T extends Record<string, unknown>> {
37
+ private _data;
38
+ private _locale;
39
+ private _defaultLocale;
40
+ private _includes;
41
+ private _relationMeta;
42
+ private _resolveEntry?;
43
+ constructor(data: Map<string, T>, defaultLocale?: string, relationMeta?: Record<string, {
44
+ target: string | string[];
45
+ multi: boolean;
46
+ }>, resolveEntry?: (model: string, id: string, locale: string) => unknown);
47
+ locale(lang: string): this;
48
+ include(...fields: string[]): this;
49
+ get(): T;
50
+ private _resolveIncludes;
51
+ private _resolveId;
52
+ }
53
+ //#endregion
54
+ //#region src/runtime/dictionary.d.ts
55
+ declare class DictionaryAccessor {
56
+ private _data;
57
+ private _locale;
58
+ private _defaultLocale;
59
+ constructor(data: Map<string, Record<string, string>>, defaultLocale?: string);
60
+ locale(lang: string): this;
61
+ get(): Record<string, string>;
62
+ get(key: string): string | undefined;
63
+ get(key: string, params: Record<string, string | number>): string;
64
+ private _resolveData;
65
+ }
66
+ //#endregion
67
+ //#region src/runtime/document.d.ts
68
+ declare class DocumentQuery<T extends object> {
69
+ private _data;
70
+ private _locale;
71
+ private _filters;
72
+ private _includes;
73
+ private _relationMeta;
74
+ private _resolver;
75
+ private _defaultLocale;
76
+ constructor(data: Map<string, T[]>, relationMeta?: Record<string, RelationMeta>, resolver?: RelationResolver, defaultLocale?: string);
77
+ locale(lang: string): this;
78
+ where<K extends string & keyof T>(field: K, value: T[K]): this;
79
+ include(...fields: string[]): this;
80
+ bySlug(slug: string): T | undefined;
81
+ all(): T[];
82
+ first(): T | undefined;
83
+ private _resolveData;
84
+ private _resolveIncludes;
85
+ private _resolveId;
86
+ }
87
+ //#endregion
88
+ //#region src/index.d.ts
89
+ /**
90
+ * Factory for framework SDK authors.
91
+ * Returns the generated client module loaded from .contentrain/client/.
92
+ *
93
+ * Usage (Nuxt composable):
94
+ * ```ts
95
+ * import { createContentrainClient } from '@contentrain/query'
96
+ * const client = createContentrainClient()
97
+ * const posts = client.query('blog-post').locale('en').all()
98
+ * ```
99
+ */
100
+ declare function createContentrainClient(projectRoot?: string): Promise<{
101
+ query: (model: string) => QueryBuilder<Record<string, unknown>>;
102
+ singleton: (model: string) => SingletonAccessor<Record<string, unknown>>;
103
+ dictionary: (model: string) => DictionaryAccessor;
104
+ document: (model: string) => DocumentQuery<Record<string, unknown>>;
105
+ }>;
106
+ //#endregion
107
+ export { type ContentStatus, type ContentrainConfig, DictionaryAccessor, DocumentQuery, type FieldDef, type FieldType, type ModelDefinition, type ModelKind, QueryBuilder, type RelationMeta, type RelationResolver, SingletonAccessor, createContentrainClient, createContentrainClient as default };
108
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/runtime/query.ts","../src/runtime/singleton.ts","../src/runtime/dictionary.ts","../src/runtime/document.ts","../src/index.ts"],"mappings":";;;UAAiB,YAAA;EACf,MAAA;EACA,KAAA;AAAA;AAAA,KAGU,gBAAA,IAAoB,KAAA,UAAe,EAAA,UAAY,MAAA,oBAA0B,MAAA;AAAA,cAExE,YAAA;EAAA,QACH,KAAA;EAAA,QACA,OAAA;EAAA,QACA,QAAA;EAAA,QACA,UAAA;EAAA,QACA,UAAA;EAAA,QACA,MAAA;EAAA,QACA,OAAA;EAAA,QACA,SAAA;EAAA,QACA,aAAA;EAAA,QACA,SAAA;EAAA,QACA,cAAA;cAGN,IAAA,EAAM,GAAA,SAAY,CAAA,KAClB,YAAA,GAAe,MAAA,SAAe,YAAA,GAC9B,QAAA,GAAW,gBAAA,EACX,aAAA;EAQF,MAAA,CAAO,IAAA;EAKP,KAAA,0BAA+B,CAAA,CAAA,CAAG,KAAA,EAAO,CAAA,EAAG,KAAA,EAAO,CAAA,CAAE,CAAA;EASrD,IAAA,0BAA8B,CAAA,CAAA,CAAG,KAAA,EAAO,CAAA,EAAG,KAAA;EAM3C,KAAA,CAAM,CAAA;EAKN,MAAA,CAAO,CAAA;EAKP,OAAA,CAAA,GAAW,MAAA;EAKX,GAAA,CAAA,GAAO,CAAA;EAsCP,KAAA,CAAA,GAAS,CAAA;EAAA,QAID,YAAA;EAAA,QAeA,gBAAA;EAAA,QAoCA,UAAA;AAAA;;;cChKG,iBAAA,WAA4B,MAAA;EAAA,QAC/B,KAAA;EA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