@contentrain/query 3.2.0 → 4.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/README.md +164 -172
- package/dist/index.d.mts +485 -211
- package/dist/index.d.ts +485 -211
- package/dist/index.js +30 -872
- package/dist/index.mjs +30 -865
- package/package.json +12 -3
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,154 +1,332 @@
|
|
|
1
|
-
interface
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
interface IBaseCacheStats {
|
|
2
|
+
size: number;
|
|
3
|
+
hits: number;
|
|
4
|
+
misses: number;
|
|
5
|
+
lastCleanup: number;
|
|
6
|
+
}
|
|
7
|
+
interface ICacheStats {
|
|
8
|
+
modelConfigs: number;
|
|
9
|
+
contents: number;
|
|
10
|
+
cache: IBaseCacheStats | undefined;
|
|
11
|
+
}
|
|
12
|
+
interface IMemoryCacheOptions {
|
|
13
|
+
readonly maxSize?: number;
|
|
14
|
+
readonly defaultTTL?: number;
|
|
11
15
|
}
|
|
12
16
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
17
|
+
type LoaderType = 'json' | 'sqlite';
|
|
18
|
+
type RelationType = 'one-to-one' | 'one-to-many';
|
|
19
|
+
type ContentrainStatus = 'publish' | 'draft' | 'private';
|
|
20
|
+
interface ILogger {
|
|
21
|
+
debug: (message: string, context?: Record<string, unknown>) => void;
|
|
22
|
+
info: (message: string, context?: Record<string, unknown>) => void;
|
|
23
|
+
warn: (message: string, context?: Record<string, unknown>) => void;
|
|
24
|
+
error: (message: string, context?: Record<string, unknown>) => void;
|
|
22
25
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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;
|
|
26
|
+
interface IBaseLoader<TData, TResult> {
|
|
27
|
+
load: (modelId: string) => Promise<TResult>;
|
|
28
|
+
resolveRelations: <TRelation>(modelId: string, relationKey: keyof TData, data: TData[]) => Promise<TRelation[]>;
|
|
29
|
+
clearCache: () => Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface IBaseJSONRecord {
|
|
33
|
+
readonly ID: string;
|
|
34
|
+
readonly createdAt: string;
|
|
35
|
+
readonly updatedAt: string;
|
|
36
|
+
readonly status: ContentrainStatus;
|
|
37
|
+
readonly scheduled: boolean;
|
|
38
|
+
readonly _relations?: {
|
|
39
|
+
[key: string]: IBaseJSONRecord | IBaseJSONRecord[];
|
|
48
40
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
41
|
+
}
|
|
42
|
+
interface IJSONLoaderOptions {
|
|
43
|
+
readonly contentDir: string;
|
|
44
|
+
readonly cache?: boolean;
|
|
45
|
+
readonly ttl?: number;
|
|
46
|
+
readonly maxCacheSize?: number;
|
|
47
|
+
readonly defaultLocale?: string;
|
|
48
|
+
readonly modelTTL?: Record<string, number>;
|
|
49
|
+
}
|
|
50
|
+
interface IJSONContent<TData extends IBaseJSONRecord> {
|
|
51
|
+
readonly [locale: string]: TData[];
|
|
52
|
+
}
|
|
53
|
+
interface IJSONContentFile<TData extends IBaseJSONRecord> {
|
|
54
|
+
readonly model: string;
|
|
55
|
+
readonly locale?: string;
|
|
56
|
+
readonly data: TData[];
|
|
57
|
+
}
|
|
58
|
+
interface IJSONModelConfig {
|
|
59
|
+
readonly metadata: {
|
|
60
|
+
readonly modelId: string;
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly type: 'JSON';
|
|
63
|
+
readonly localization: boolean;
|
|
64
|
+
readonly isServerless: boolean;
|
|
65
|
+
readonly createdBy: string;
|
|
56
66
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
readonly fields: Array<{
|
|
68
|
+
readonly name: string;
|
|
69
|
+
readonly fieldId: string;
|
|
70
|
+
readonly modelId: string;
|
|
71
|
+
readonly fieldType: string;
|
|
72
|
+
readonly componentId: string;
|
|
73
|
+
readonly options: {
|
|
74
|
+
readonly reference?: {
|
|
75
|
+
readonly form?: {
|
|
76
|
+
readonly reference?: {
|
|
77
|
+
readonly value: string;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
62
80
|
};
|
|
63
81
|
};
|
|
64
|
-
|
|
82
|
+
readonly validations: Record<string, unknown>;
|
|
83
|
+
}>;
|
|
65
84
|
}
|
|
66
|
-
interface
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
85
|
+
interface IJSONLoaderResult<TData extends IBaseJSONRecord> {
|
|
86
|
+
readonly model: IJSONModelConfig;
|
|
87
|
+
readonly content: IJSONContent<TData>;
|
|
88
|
+
readonly assets?: Array<{
|
|
89
|
+
readonly id: string;
|
|
90
|
+
readonly name: string;
|
|
91
|
+
readonly path: string;
|
|
92
|
+
readonly size: number;
|
|
93
|
+
readonly type: string;
|
|
94
|
+
readonly createdAt: string;
|
|
95
|
+
readonly updatedAt: string;
|
|
96
|
+
}>;
|
|
97
|
+
}
|
|
98
|
+
interface IJSONContentManager {
|
|
99
|
+
loadModelContent: <T extends IBaseJSONRecord>(modelId: string, locale?: string) => Promise<IJSONContentFile<T>>;
|
|
100
|
+
loadAssets: () => Promise<any[]>;
|
|
101
|
+
getModelLocales: (modelId: string) => Promise<string[]>;
|
|
102
|
+
loadModelConfig: (modelId: string) => Promise<IJSONModelConfig>;
|
|
103
|
+
}
|
|
104
|
+
interface IJSONRelationConfig {
|
|
105
|
+
readonly model: string;
|
|
106
|
+
readonly type: 'one-to-one' | 'one-to-many';
|
|
107
|
+
readonly foreignKey: string;
|
|
108
|
+
}
|
|
109
|
+
interface IJSONRelationManager {
|
|
110
|
+
loadRelations: (modelId: string) => Promise<IJSONRelationConfig[]>;
|
|
111
|
+
resolveRelation: <T extends IBaseJSONRecord, R extends IBaseJSONRecord>(modelId: string, relationField: keyof T, data: T[], locale?: string) => Promise<R[]>;
|
|
79
112
|
}
|
|
80
|
-
type ContentrainLocales = string;
|
|
81
113
|
|
|
82
|
-
interface
|
|
83
|
-
|
|
84
|
-
|
|
114
|
+
interface IDBRecord {
|
|
115
|
+
id: string;
|
|
116
|
+
created_at: string;
|
|
117
|
+
updated_at: string;
|
|
118
|
+
status: ContentrainStatus;
|
|
119
|
+
scheduled?: boolean;
|
|
120
|
+
_relations?: {
|
|
121
|
+
[key: string]: IDBRecord | IDBRecord[];
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
interface IDBTranslationRecord extends IDBRecord {
|
|
125
|
+
readonly locale: string;
|
|
126
|
+
readonly [key: string]: unknown;
|
|
127
|
+
}
|
|
128
|
+
interface IDBRelation {
|
|
129
|
+
readonly id: string;
|
|
130
|
+
readonly source_model: string;
|
|
131
|
+
readonly source_id: string;
|
|
132
|
+
readonly target_model: string;
|
|
133
|
+
readonly target_id: string;
|
|
134
|
+
readonly field_id: string;
|
|
135
|
+
readonly type: 'one-to-one' | 'one-to-many';
|
|
136
|
+
readonly created_at: string;
|
|
137
|
+
readonly updated_at: string;
|
|
138
|
+
}
|
|
139
|
+
interface ISQLiteLoaderOptions {
|
|
140
|
+
databasePath: string;
|
|
85
141
|
cache?: boolean;
|
|
86
|
-
ttl?: number;
|
|
87
142
|
maxCacheSize?: number;
|
|
88
|
-
modelTTL?:
|
|
89
|
-
|
|
143
|
+
modelTTL?: number | Record<string, number>;
|
|
144
|
+
defaultLocale?: string;
|
|
145
|
+
sorting?: {
|
|
146
|
+
field: string;
|
|
147
|
+
direction: 'asc' | 'desc';
|
|
90
148
|
};
|
|
91
149
|
}
|
|
92
|
-
interface
|
|
93
|
-
|
|
94
|
-
|
|
150
|
+
interface ISQLiteContent<TData extends IDBRecord> {
|
|
151
|
+
readonly default: TData[];
|
|
152
|
+
readonly translations?: Record<string, IDBTranslationRecord[]>;
|
|
95
153
|
}
|
|
96
|
-
interface
|
|
97
|
-
model:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
size: number;
|
|
105
|
-
alt: string;
|
|
106
|
-
meta: {
|
|
107
|
-
user: {
|
|
108
|
-
name: string;
|
|
109
|
-
email: string;
|
|
110
|
-
avatar: string;
|
|
154
|
+
interface ISQLiteLoaderResult<TData extends IDBRecord> {
|
|
155
|
+
readonly model: {
|
|
156
|
+
readonly metadata: {
|
|
157
|
+
readonly modelId: string;
|
|
158
|
+
readonly name: string;
|
|
159
|
+
readonly type: 'SQLite';
|
|
160
|
+
readonly localization: boolean;
|
|
161
|
+
readonly isServerless: boolean;
|
|
111
162
|
};
|
|
112
|
-
|
|
163
|
+
readonly fields: Array<{
|
|
164
|
+
readonly name: string;
|
|
165
|
+
readonly fieldId: string;
|
|
166
|
+
readonly type: string;
|
|
167
|
+
}>;
|
|
113
168
|
};
|
|
169
|
+
readonly content: ISQLiteContent<TData>;
|
|
114
170
|
}
|
|
115
|
-
interface
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
};
|
|
120
|
-
assets?: AssetMetadata[];
|
|
171
|
+
interface ISQLiteConnection {
|
|
172
|
+
query: <T>(sql: string, params?: unknown[]) => Promise<T[]>;
|
|
173
|
+
get: <T>(sql: string, params?: unknown[]) => Promise<T | undefined>;
|
|
174
|
+
close: () => void;
|
|
121
175
|
}
|
|
122
|
-
interface
|
|
123
|
-
model: string
|
|
124
|
-
|
|
125
|
-
foreignKey: string;
|
|
176
|
+
interface ISQLiteContentManager {
|
|
177
|
+
findById: <T extends IDBRecord>(model: string, id: string) => Promise<T | undefined>;
|
|
178
|
+
findAll: <T extends IDBRecord>(model: string, conditions?: Partial<T>) => Promise<T[]>;
|
|
126
179
|
}
|
|
127
|
-
interface
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
lastCleanup: number;
|
|
180
|
+
interface ISQLiteRelationManager {
|
|
181
|
+
loadRelations: (model: string, sourceIds: string[], fieldId: string) => Promise<IDBRelation[]>;
|
|
182
|
+
loadRelatedContent: <T extends IDBRecord>(relations: IDBRelation[], locale?: string) => Promise<T[]>;
|
|
183
|
+
getRelationTypes: (model: string) => Promise<Record<string, 'one-to-one' | 'one-to-many'>>;
|
|
132
184
|
}
|
|
133
|
-
interface
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
185
|
+
interface ISQLiteTranslationManager {
|
|
186
|
+
hasTranslations: (model: string) => Promise<boolean>;
|
|
187
|
+
loadTranslations: (model: string, ids: string[], locale?: string) => Promise<Record<string, IDBTranslationRecord>>;
|
|
188
|
+
getLocales: (model: string) => Promise<string[]>;
|
|
189
|
+
getMainColumns: (model: string) => Promise<string[]>;
|
|
190
|
+
getTranslationColumns: (model: string) => Promise<string[]>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
declare class SQLiteConnection {
|
|
194
|
+
private readonly db;
|
|
195
|
+
constructor(databasePath: string);
|
|
196
|
+
private setupDatabase;
|
|
197
|
+
query<T>(sql: string, params?: unknown[]): Promise<T[]>;
|
|
198
|
+
get<T>(sql: string, params?: unknown[]): Promise<T | undefined>;
|
|
199
|
+
close(): Promise<void>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
declare class SQLiteContentManager {
|
|
203
|
+
protected readonly databasePath: string;
|
|
204
|
+
protected readonly connection: SQLiteConnection;
|
|
205
|
+
constructor(databasePath: string);
|
|
206
|
+
query<T>(sql: string, params?: unknown[]): Promise<T[]>;
|
|
207
|
+
findById<T extends IDBRecord>(model: string, id: string): Promise<T | undefined>;
|
|
208
|
+
findAll<T extends IDBRecord>(model: string, conditions?: Partial<T>): Promise<T[]>;
|
|
209
|
+
getTableCount(): Promise<number>;
|
|
210
|
+
close(): Promise<void>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
declare class SQLiteRelationManager extends SQLiteContentManager {
|
|
214
|
+
private readonly translationManager;
|
|
215
|
+
constructor(databasePath: string);
|
|
216
|
+
loadRelations(model: string, sourceIds: string[], fieldId: string): Promise<IDBRelation[]>;
|
|
217
|
+
loadRelatedContent<T extends IDBRecord>(relations: IDBRelation[], locale?: string): Promise<T[]>;
|
|
218
|
+
getRelationTypes(model: string): Promise<Record<string, 'one-to-one' | 'one-to-many'>>;
|
|
219
|
+
getRelationFields(model: string): Promise<string[]>;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
declare class SQLiteTranslationManager extends SQLiteContentManager {
|
|
223
|
+
constructor(databasePath: string);
|
|
224
|
+
hasTranslations(model: string): Promise<boolean>;
|
|
225
|
+
loadTranslations(model: string, ids: string[], locale?: string): Promise<Record<string, IDBTranslationRecord>>;
|
|
226
|
+
getLocales(model: string): Promise<string[]>;
|
|
227
|
+
getMainColumns(model: string): Promise<string[]>;
|
|
228
|
+
getTranslationColumns(model: string): Promise<string[]>;
|
|
229
|
+
getAllColumns(model: string): Promise<string[]>;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
declare class SQLiteLoader<TData extends IDBRecord = IDBRecord> {
|
|
233
|
+
private readonly options;
|
|
234
|
+
readonly contentManager: SQLiteContentManager;
|
|
235
|
+
readonly relationManager: SQLiteRelationManager;
|
|
236
|
+
readonly translationManager: SQLiteTranslationManager;
|
|
237
|
+
private readonly cache?;
|
|
238
|
+
private readonly modelTTL;
|
|
239
|
+
private readonly modelConfigs;
|
|
240
|
+
constructor(options: ISQLiteLoaderOptions);
|
|
241
|
+
query<T>(sql: string, params?: unknown[]): Promise<T[]>;
|
|
242
|
+
findById<T extends IDBRecord>(model: string, id: string): Promise<T | undefined>;
|
|
243
|
+
findAll<T extends IDBRecord>(model: string, conditions?: Partial<T>): Promise<T[]>;
|
|
244
|
+
private getCacheKey;
|
|
245
|
+
private getModelTTL;
|
|
246
|
+
private loadWithTranslations;
|
|
247
|
+
private getModelFields;
|
|
248
|
+
private mapSystemFields;
|
|
249
|
+
private mapRelationFields;
|
|
250
|
+
private mapTranslationFields;
|
|
251
|
+
private getFieldType;
|
|
252
|
+
resolveRelations<TRelation extends IDBRecord>(modelId: string, fieldId: string, data: TData[]): Promise<TRelation[]>;
|
|
253
|
+
load(model: string): Promise<ISQLiteLoaderResult<TData>>;
|
|
254
|
+
clearCache(): Promise<void>;
|
|
255
|
+
refreshCache(modelId: string): Promise<void>;
|
|
256
|
+
getCacheStats(): Promise<ICacheStats>;
|
|
257
|
+
close(): Promise<void>;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
interface IQueryExecutor<TData, TInclude extends Include = Include, TOptions extends QueryOptions = QueryOptions> {
|
|
261
|
+
execute: (params: {
|
|
262
|
+
model: string;
|
|
263
|
+
filters?: Filter[];
|
|
264
|
+
includes?: TInclude;
|
|
265
|
+
sorting?: Sort[];
|
|
266
|
+
pagination?: Pagination;
|
|
267
|
+
options?: TOptions;
|
|
268
|
+
}) => Promise<QueryResult<TData>>;
|
|
269
|
+
}
|
|
270
|
+
declare abstract class BaseQueryExecutor<TData, TInclude extends Include = Include, TOptions extends QueryOptions = QueryOptions> implements IQueryExecutor<TData, TInclude, TOptions> {
|
|
271
|
+
protected applyStringOperation(value: string, operator: StringOperator, searchValue: string): boolean;
|
|
272
|
+
protected resolveIncludes(model: string, data: TData[], includes: TInclude, options: TOptions): Promise<TData[]>;
|
|
273
|
+
protected abstract resolveRelation(model: string, field: string, data: TData[], options: TOptions): Promise<TData[]>;
|
|
274
|
+
abstract execute(params: {
|
|
275
|
+
model: string;
|
|
276
|
+
filters?: Filter[];
|
|
277
|
+
includes?: TInclude;
|
|
278
|
+
sorting?: Sort[];
|
|
279
|
+
pagination?: Pagination;
|
|
280
|
+
options?: TOptions;
|
|
281
|
+
}): Promise<QueryResult<TData>>;
|
|
282
|
+
protected applyFilters(data: TData[], filters?: Filter[]): TData[];
|
|
283
|
+
protected applySorting(data: TData[], sorting?: Sort[]): TData[];
|
|
284
|
+
protected applyPagination(data: TData[], limit?: number, offset?: number): TData[];
|
|
285
|
+
protected compareValues(itemValue: any, operator: string, value: any): boolean;
|
|
286
|
+
protected getPaginationInfo(pagination: Pagination | undefined, total: number): QueryResult<TData>['pagination'];
|
|
138
287
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
288
|
+
|
|
289
|
+
declare class SQLiteQueryExecutor<TData extends IDBRecord> extends BaseQueryExecutor<TData, Include, SQLiteOptions> {
|
|
290
|
+
private loader;
|
|
291
|
+
constructor(loader: SQLiteLoader<TData>);
|
|
292
|
+
setLoader(loader: SQLiteLoader<TData>): void;
|
|
293
|
+
protected resolveRelation(model: string, field: string, data: TData[], options: SQLiteOptions): Promise<TData[]>;
|
|
294
|
+
execute(params: {
|
|
295
|
+
model: string;
|
|
296
|
+
filters?: Filter[];
|
|
297
|
+
sorting?: Sort[];
|
|
298
|
+
pagination?: {
|
|
299
|
+
limit?: number;
|
|
300
|
+
offset?: number;
|
|
301
|
+
};
|
|
302
|
+
options?: SQLiteOptions;
|
|
303
|
+
}): Promise<QueryResult<TData>>;
|
|
304
|
+
private buildSQLQuery;
|
|
305
|
+
private buildSQL;
|
|
306
|
+
private addTranslationJoin;
|
|
307
|
+
private buildConditionWithoutParams;
|
|
308
|
+
private getTotal;
|
|
309
|
+
protected getPaginationInfo(pagination: {
|
|
310
|
+
limit?: number;
|
|
311
|
+
offset?: number;
|
|
312
|
+
} | undefined, total: number): {
|
|
313
|
+
limit: number;
|
|
314
|
+
offset: number;
|
|
315
|
+
hasMore: boolean;
|
|
316
|
+
} | undefined;
|
|
317
|
+
private query;
|
|
318
|
+
private mainColumns;
|
|
319
|
+
private translationColumns;
|
|
142
320
|
}
|
|
143
321
|
|
|
144
322
|
type StringOperator = 'eq' | 'ne' | 'contains' | 'startsWith' | 'endsWith';
|
|
145
323
|
type NumericOperator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte';
|
|
146
324
|
type ArrayOperator = 'in' | 'nin';
|
|
147
325
|
type Operator = StringOperator | NumericOperator | ArrayOperator;
|
|
148
|
-
interface Filter
|
|
326
|
+
interface Filter {
|
|
149
327
|
field: string;
|
|
150
328
|
operator: Operator;
|
|
151
|
-
value:
|
|
329
|
+
value: any;
|
|
152
330
|
}
|
|
153
331
|
interface Sort {
|
|
154
332
|
field: string;
|
|
@@ -158,19 +336,13 @@ interface Pagination {
|
|
|
158
336
|
limit?: number;
|
|
159
337
|
offset?: number;
|
|
160
338
|
}
|
|
161
|
-
interface Include {
|
|
162
|
-
[relation: string]: {
|
|
163
|
-
fields?: string[];
|
|
164
|
-
include?: Include;
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
339
|
interface QueryOptions {
|
|
168
340
|
locale?: string;
|
|
169
341
|
cache?: boolean;
|
|
170
342
|
ttl?: number;
|
|
171
343
|
}
|
|
172
|
-
interface QueryResult<
|
|
173
|
-
data:
|
|
344
|
+
interface QueryResult<TData> {
|
|
345
|
+
data: TData[];
|
|
174
346
|
total: number;
|
|
175
347
|
pagination?: {
|
|
176
348
|
limit: number;
|
|
@@ -178,90 +350,186 @@ interface QueryResult<T> {
|
|
|
178
350
|
hasMore: boolean;
|
|
179
351
|
};
|
|
180
352
|
}
|
|
181
|
-
interface
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
353
|
+
interface IncludeOptions {
|
|
354
|
+
relation: string;
|
|
355
|
+
locale?: string;
|
|
356
|
+
}
|
|
357
|
+
type Include = string | IncludeOptions | (string | IncludeOptions)[];
|
|
358
|
+
interface IBaseQueryBuilder<TData> {
|
|
359
|
+
where: (field: keyof TData, operator: Operator, value: any) => this;
|
|
360
|
+
orderBy: (field: keyof TData, direction?: 'asc' | 'desc') => this;
|
|
361
|
+
limit: (count: number) => this;
|
|
362
|
+
offset: (count: number) => this;
|
|
363
|
+
get: () => Promise<QueryResult<TData>>;
|
|
364
|
+
first: () => Promise<TData | null>;
|
|
365
|
+
count: () => Promise<number>;
|
|
366
|
+
cache: (ttl?: number) => this;
|
|
367
|
+
noCache: () => this;
|
|
368
|
+
bypassCache: () => this;
|
|
369
|
+
}
|
|
370
|
+
interface SQLiteOptions {
|
|
371
|
+
includes?: IncludeOptions[];
|
|
372
|
+
locale?: string;
|
|
373
|
+
translations?: boolean;
|
|
374
|
+
}
|
|
375
|
+
type SupportedLocale = string;
|
|
376
|
+
type RelationConfig<T> = {
|
|
377
|
+
[K in keyof T]: T[K] extends {
|
|
378
|
+
_relations?: infer R;
|
|
379
|
+
} ? R : never;
|
|
380
|
+
}[keyof T];
|
|
381
|
+
interface QueryConfig<TModel extends IDBRecord, TLocale extends SupportedLocale = string, TRelations extends Record<string, any> = Record<string, any>> {
|
|
382
|
+
model: TModel;
|
|
383
|
+
locale: TLocale;
|
|
384
|
+
relations: {
|
|
385
|
+
[K in keyof TRelations]: {
|
|
386
|
+
relation: K;
|
|
387
|
+
locale?: TLocale;
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
interface ISQLiteQuery<TModel extends IDBRecord, TLocale extends SupportedLocale = string, TRelations extends Record<string, any> = Record<string, any>> extends IBaseQueryBuilder<TModel> {
|
|
392
|
+
include: <K extends keyof TRelations>(relations: K | K[] | {
|
|
393
|
+
relation: K;
|
|
394
|
+
locale?: TLocale;
|
|
395
|
+
} | Array<{
|
|
396
|
+
relation: K;
|
|
397
|
+
locale?: TLocale;
|
|
398
|
+
}>) => this;
|
|
399
|
+
locale: (code: TLocale) => this;
|
|
400
|
+
setLoader: (loader: SQLiteLoader<TModel>) => this;
|
|
401
|
+
}
|
|
402
|
+
interface SQLQuery {
|
|
403
|
+
select: string[];
|
|
404
|
+
from: string;
|
|
405
|
+
joins: SQLJoin[];
|
|
406
|
+
where: Filter[];
|
|
407
|
+
orderBy: Sort[];
|
|
408
|
+
parameters: unknown[];
|
|
409
|
+
pagination?: Pagination;
|
|
410
|
+
options?: SQLiteOptions;
|
|
411
|
+
}
|
|
412
|
+
interface SQLJoin {
|
|
413
|
+
type: 'LEFT' | 'INNER';
|
|
414
|
+
table: string;
|
|
415
|
+
alias: string;
|
|
416
|
+
conditions: string[];
|
|
417
|
+
}
|
|
418
|
+
interface JSONOptions extends QueryOptions {
|
|
419
|
+
defaultLocale?: string;
|
|
420
|
+
}
|
|
421
|
+
interface IJSONQuery<TData extends IBaseJSONRecord> extends IBaseQueryBuilder<TData> {
|
|
422
|
+
include: (relations: string | string[], reference?: string) => this;
|
|
423
|
+
locale: (code: string, defaultLocale?: string) => this;
|
|
424
|
+
}
|
|
425
|
+
interface SQLiteQueryConfig<TData extends IDBRecord> {
|
|
426
|
+
model: string;
|
|
427
|
+
loader?: SQLiteLoader<TData>;
|
|
428
|
+
options?: SQLiteOptions;
|
|
429
|
+
}
|
|
430
|
+
interface ISQLiteQueryBuilder<TData extends IDBRecord> {
|
|
431
|
+
model: string;
|
|
432
|
+
executor: SQLiteQueryExecutor<TData>;
|
|
433
|
+
options: SQLiteOptions;
|
|
185
434
|
}
|
|
186
435
|
|
|
187
|
-
declare class
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
private
|
|
191
|
-
private
|
|
192
|
-
constructor(
|
|
436
|
+
declare class JSONContentManager {
|
|
437
|
+
protected readonly contentDir: string;
|
|
438
|
+
protected readonly defaultLocale?: string | undefined;
|
|
439
|
+
private readonly modelConfigCache;
|
|
440
|
+
private readonly contentCache;
|
|
441
|
+
constructor(contentDir: string, defaultLocale?: string | undefined);
|
|
442
|
+
private getContentCacheKey;
|
|
443
|
+
loadModelConfig(modelId: string): Promise<IJSONModelConfig>;
|
|
444
|
+
loadModelContent<T extends IBaseJSONRecord>(modelId: string, locale?: string): Promise<IJSONContentFile<T>>;
|
|
445
|
+
loadAssets(): Promise<any[]>;
|
|
446
|
+
getModelLocales(modelId: string): Promise<string[]>;
|
|
447
|
+
clearCache(): Promise<void>;
|
|
448
|
+
getCacheStats(): {
|
|
449
|
+
modelConfigs: number;
|
|
450
|
+
contents: number;
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
declare class JSONLoader<TData extends IBaseJSONRecord> extends JSONContentManager {
|
|
455
|
+
private readonly options;
|
|
456
|
+
private readonly relationManager;
|
|
457
|
+
private readonly cache?;
|
|
458
|
+
private readonly modelConfigs;
|
|
459
|
+
constructor(options: IJSONLoaderOptions);
|
|
193
460
|
private getCacheKey;
|
|
194
461
|
private getModelTTL;
|
|
462
|
+
load(modelId: string): Promise<IJSONLoaderResult<TData>>;
|
|
463
|
+
resolveRelations<TRelation extends IBaseJSONRecord>(modelId: string, relationKey: keyof TData, data: TData[]): Promise<TRelation[]>;
|
|
195
464
|
clearCache(): Promise<void>;
|
|
196
|
-
refreshCache(
|
|
197
|
-
getCacheStats():
|
|
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[]>;
|
|
465
|
+
refreshCache(modelId: string): Promise<void>;
|
|
466
|
+
getCacheStats(): ICacheStats;
|
|
204
467
|
}
|
|
205
468
|
|
|
206
|
-
declare class
|
|
207
|
-
private loader;
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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>>;
|
|
469
|
+
declare class QueryFactory {
|
|
470
|
+
private static loader;
|
|
471
|
+
static setLoader(loader: SQLiteLoader<IDBRecord> | JSONLoader<IBaseJSONRecord>): void;
|
|
472
|
+
static createSQLiteBuilder<TData extends IDBRecord>(model: string, loader?: SQLiteLoader<TData>): ISQLiteQuery<TData>;
|
|
473
|
+
static createJSONBuilder<TData extends IBaseJSONRecord>(model: string, loader?: JSONLoader<TData>): IJSONQuery<TData>;
|
|
474
|
+
static createBuilder<TData extends IDBRecord | IBaseJSONRecord>(model: string, loader?: SQLiteLoader<TData & IDBRecord> | JSONLoader<TData & IBaseJSONRecord>): TData extends IDBRecord ? ISQLiteQuery<TData & IDBRecord> : IJSONQuery<TData & IBaseJSONRecord>;
|
|
226
475
|
}
|
|
227
476
|
|
|
228
|
-
declare
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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;
|
|
477
|
+
declare const loggers: {
|
|
478
|
+
cache: {
|
|
479
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
480
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
481
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
482
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
254
483
|
};
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
484
|
+
query: {
|
|
485
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
486
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
487
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
488
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
489
|
+
};
|
|
490
|
+
loader: {
|
|
491
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
492
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
493
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
494
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
495
|
+
};
|
|
496
|
+
sqlite: {
|
|
497
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
498
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
499
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
500
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
501
|
+
};
|
|
502
|
+
relation: {
|
|
503
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
504
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
505
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
506
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
507
|
+
};
|
|
508
|
+
translation: {
|
|
509
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
510
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
511
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
512
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
513
|
+
};
|
|
514
|
+
executor: {
|
|
515
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
516
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
517
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
518
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
519
|
+
};
|
|
520
|
+
default: {
|
|
521
|
+
debug: (message: string, data?: Record<string, unknown>) => void;
|
|
522
|
+
info: (message: string, data?: Record<string, unknown>) => void;
|
|
523
|
+
warn: (message: string, data?: Record<string, unknown>) => void;
|
|
524
|
+
error: (message: string, data?: Record<string, unknown>) => void;
|
|
525
|
+
};
|
|
526
|
+
};
|
|
259
527
|
|
|
260
528
|
declare class MemoryCache {
|
|
261
529
|
private cache;
|
|
262
530
|
private options;
|
|
263
531
|
private stats;
|
|
264
|
-
constructor(options?:
|
|
532
|
+
constructor(options?: IMemoryCacheOptions);
|
|
265
533
|
private calculateSize;
|
|
266
534
|
set<T>(key: string, data: T, ttl?: number): Promise<void>;
|
|
267
535
|
private findOldestKey;
|
|
@@ -269,23 +537,29 @@ declare class MemoryCache {
|
|
|
269
537
|
delete(key: string): Promise<void>;
|
|
270
538
|
clear(): Promise<void>;
|
|
271
539
|
private cleanupCache;
|
|
272
|
-
getStats():
|
|
540
|
+
getStats(): IBaseCacheStats;
|
|
273
541
|
}
|
|
274
542
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
543
|
+
interface ContentrainSDKOptions {
|
|
544
|
+
contentDir?: string;
|
|
545
|
+
databasePath?: string;
|
|
546
|
+
cache?: boolean;
|
|
547
|
+
ttl?: number;
|
|
548
|
+
maxCacheSize?: number;
|
|
549
|
+
defaultLocale?: string;
|
|
550
|
+
modelTTL?: Record<string, number>;
|
|
551
|
+
logger?: ILogger;
|
|
552
|
+
}
|
|
280
553
|
declare class ContentrainSDK {
|
|
281
|
-
private loader;
|
|
282
|
-
private
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
554
|
+
private static loader;
|
|
555
|
+
private readonly options;
|
|
556
|
+
private readonly type;
|
|
557
|
+
constructor(type: LoaderType, options: ContentrainSDKOptions);
|
|
558
|
+
query<TData extends IDBRecord | IBaseJSONRecord>(model: string): TData extends IDBRecord ? ISQLiteQuery<TData & IDBRecord> : IJSONQuery<TData & IBaseJSONRecord>;
|
|
559
|
+
load<TData extends IDBRecord | IBaseJSONRecord>(model: string): Promise<TData>;
|
|
286
560
|
clearCache(): Promise<void>;
|
|
287
561
|
refreshCache(model: string): Promise<void>;
|
|
288
|
-
getCacheStats():
|
|
562
|
+
getCacheStats(): Promise<ICacheStats>;
|
|
289
563
|
}
|
|
290
564
|
|
|
291
|
-
export { type ArrayOperator, type
|
|
565
|
+
export { type ArrayOperator, ContentrainSDK, type ContentrainSDKOptions, type ContentrainStatus, type Filter, type IBaseJSONRecord, type IBaseLoader, type IBaseQueryBuilder, type IDBRecord, type IDBRelation, type IDBTranslationRecord, type IJSONContent, type IJSONContentFile, type IJSONContentManager, type IJSONLoaderOptions, type IJSONLoaderResult, type IJSONModelConfig, type IJSONQuery, type IJSONRelationConfig, type IJSONRelationManager, type ILogger, type ISQLiteConnection, type ISQLiteContent, type ISQLiteContentManager, type ISQLiteLoaderOptions, type ISQLiteLoaderResult, type ISQLiteQuery, type ISQLiteQueryBuilder, type ISQLiteRelationManager, type ISQLiteTranslationManager, type Include, type IncludeOptions, JSONLoader, type JSONOptions, type LoaderType, MemoryCache, type NumericOperator, type Operator, type Pagination, type QueryConfig, QueryFactory, type QueryOptions, type QueryResult, type RelationConfig, type RelationType, type SQLJoin, type SQLQuery, SQLiteLoader, type SQLiteOptions, type SQLiteQueryConfig, type Sort, type StringOperator, type SupportedLocale, loggers };
|