linkgress-orm 0.0.3 → 0.1.1
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 +3 -3
- package/dist/entity/db-column.d.ts +38 -1
- package/dist/entity/db-column.d.ts.map +1 -1
- package/dist/entity/db-column.js.map +1 -1
- package/dist/entity/db-context.d.ts +429 -50
- package/dist/entity/db-context.d.ts.map +1 -1
- package/dist/entity/db-context.js +884 -203
- package/dist/entity/db-context.js.map +1 -1
- package/dist/entity/entity-base.d.ts +8 -0
- package/dist/entity/entity-base.d.ts.map +1 -1
- package/dist/entity/entity-base.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/query/collection-strategy.factory.d.ts.map +1 -1
- package/dist/query/collection-strategy.factory.js +7 -3
- package/dist/query/collection-strategy.factory.js.map +1 -1
- package/dist/query/collection-strategy.interface.d.ts +12 -6
- package/dist/query/collection-strategy.interface.d.ts.map +1 -1
- package/dist/query/conditions.d.ts +134 -23
- package/dist/query/conditions.d.ts.map +1 -1
- package/dist/query/conditions.js +58 -0
- package/dist/query/conditions.js.map +1 -1
- package/dist/query/cte-builder.d.ts +24 -5
- package/dist/query/cte-builder.d.ts.map +1 -1
- package/dist/query/cte-builder.js +45 -7
- package/dist/query/cte-builder.js.map +1 -1
- package/dist/query/grouped-query.d.ts +196 -8
- package/dist/query/grouped-query.d.ts.map +1 -1
- package/dist/query/grouped-query.js +586 -54
- package/dist/query/grouped-query.js.map +1 -1
- package/dist/query/join-builder.d.ts +5 -4
- package/dist/query/join-builder.d.ts.map +1 -1
- package/dist/query/join-builder.js +21 -47
- package/dist/query/join-builder.js.map +1 -1
- package/dist/query/query-builder.d.ts +118 -20
- package/dist/query/query-builder.d.ts.map +1 -1
- package/dist/query/query-builder.js +511 -280
- package/dist/query/query-builder.js.map +1 -1
- package/dist/query/query-utils.d.ts +45 -0
- package/dist/query/query-utils.d.ts.map +1 -0
- package/dist/query/query-utils.js +103 -0
- package/dist/query/query-utils.js.map +1 -0
- package/dist/query/sql-utils.d.ts +83 -0
- package/dist/query/sql-utils.d.ts.map +1 -0
- package/dist/query/sql-utils.js +218 -0
- package/dist/query/sql-utils.js.map +1 -0
- package/dist/query/strategies/cte-collection-strategy.d.ts +85 -0
- package/dist/query/strategies/cte-collection-strategy.d.ts.map +1 -0
- package/dist/query/strategies/cte-collection-strategy.js +338 -0
- package/dist/query/strategies/cte-collection-strategy.js.map +1 -0
- package/dist/query/strategies/lateral-collection-strategy.d.ts +59 -0
- package/dist/query/strategies/lateral-collection-strategy.d.ts.map +1 -0
- package/dist/query/strategies/lateral-collection-strategy.js +243 -0
- package/dist/query/strategies/lateral-collection-strategy.js.map +1 -0
- package/dist/query/strategies/temptable-collection-strategy.d.ts +21 -0
- package/dist/query/strategies/temptable-collection-strategy.d.ts.map +1 -1
- package/dist/query/strategies/temptable-collection-strategy.js +160 -38
- package/dist/query/strategies/temptable-collection-strategy.js.map +1 -1
- package/dist/query/subquery.d.ts +24 -1
- package/dist/query/subquery.d.ts.map +1 -1
- package/dist/query/subquery.js +38 -2
- package/dist/query/subquery.js.map +1 -1
- package/dist/schema/table-builder.d.ts +16 -0
- package/dist/schema/table-builder.d.ts.map +1 -1
- package/dist/schema/table-builder.js +23 -1
- package/dist/schema/table-builder.js.map +1 -1
- package/package.json +1 -1
- package/dist/query/strategies/jsonb-collection-strategy.d.ts +0 -51
- package/dist/query/strategies/jsonb-collection-strategy.d.ts.map +0 -1
- package/dist/query/strategies/jsonb-collection-strategy.js +0 -210
- package/dist/query/strategies/jsonb-collection-strategy.js.map +0 -1
|
@@ -72,14 +72,36 @@ class TableBuilder {
|
|
|
72
72
|
* Build the final table schema
|
|
73
73
|
*/
|
|
74
74
|
build() {
|
|
75
|
-
|
|
75
|
+
// Return cached schema if available
|
|
76
|
+
if (this._cachedSchema) {
|
|
77
|
+
return this._cachedSchema;
|
|
78
|
+
}
|
|
79
|
+
// Performance: Pre-compute column name map once during build
|
|
80
|
+
const columnNameMap = new Map();
|
|
81
|
+
for (const [propName, colBuilder] of Object.entries(this.columnDefs)) {
|
|
82
|
+
columnNameMap.set(propName, colBuilder.build().name);
|
|
83
|
+
}
|
|
84
|
+
// Performance: Pre-compute relation entries array
|
|
85
|
+
const relationEntries = Object.entries(this.relationDefs);
|
|
86
|
+
// Performance: Pre-build and cache target schemas for all relations
|
|
87
|
+
const relationSchemaCache = new Map();
|
|
88
|
+
for (const [relName, relConfig] of relationEntries) {
|
|
89
|
+
if (relConfig.targetTableBuilder) {
|
|
90
|
+
relationSchemaCache.set(relName, relConfig.targetTableBuilder.build());
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this._cachedSchema = {
|
|
76
94
|
name: this.tableName,
|
|
77
95
|
schema: this.schemaName,
|
|
78
96
|
columns: this.columnDefs,
|
|
79
97
|
relations: this.relationDefs,
|
|
80
98
|
indexes: this.indexDefs,
|
|
81
99
|
foreignKeys: this.foreignKeyDefs,
|
|
100
|
+
columnNameMap,
|
|
101
|
+
relationEntries,
|
|
102
|
+
relationSchemaCache,
|
|
82
103
|
};
|
|
104
|
+
return this._cachedSchema;
|
|
83
105
|
}
|
|
84
106
|
/**
|
|
85
107
|
* Get table name
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table-builder.js","sourceRoot":"","sources":["../../src/schema/table-builder.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"table-builder.js","sourceRoot":"","sources":["../../src/schema/table-builder.ts"],"names":[],"mappings":";;;AAmRA,sBAKC;AAxRD,qDAA+D;AAC/D,6CAUsB;AAwGtB;;GAEG;AACH,MAAa,YAAY;IASvB,YAAY,IAAY,EAAE,MAAe,EAAE,OAA2B,EAAE,WAAoC,EAAE,UAAmB;QALzH,eAAU,GAAkC,EAAE,CAAC;QAC/C,iBAAY,GAAmC,EAAE,CAAC;QAClD,cAAS,GAAsB,E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UAAU,CAAC,YAAY,CAAC,CAAC;IAC5D,CAAC;CACF;AAvJD,oCAuJC;AAED;;;GAGG;AACH,SAAgB,KAAK,CACnB,IAAY,EACZ,MAAe;IAEf,OAAO,IAAI,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { ICollectionStrategy, CollectionStrategyType, CollectionAggregationConfig, CollectionAggregationResult } from '../collection-strategy.interface';
|
|
2
|
-
import { QueryContext } from '../query-builder';
|
|
3
|
-
/**
|
|
4
|
-
* JSONB-based collection strategy using CTEs
|
|
5
|
-
*
|
|
6
|
-
* This is the current/default strategy that uses PostgreSQL CTEs with jsonb_agg
|
|
7
|
-
* to aggregate related records into JSONB arrays.
|
|
8
|
-
*
|
|
9
|
-
* Benefits:
|
|
10
|
-
* - Single query execution
|
|
11
|
-
* - No temp table management
|
|
12
|
-
* - Works well for moderate data sizes
|
|
13
|
-
*
|
|
14
|
-
* SQL Pattern:
|
|
15
|
-
* ```sql
|
|
16
|
-
* WITH "cte_0" AS (
|
|
17
|
-
* SELECT
|
|
18
|
-
* "user_id" as parent_id,
|
|
19
|
-
* jsonb_agg(
|
|
20
|
-
* jsonb_build_object('id', "id", 'title', "title")
|
|
21
|
-
* ORDER BY "views" DESC
|
|
22
|
-
* ) as data
|
|
23
|
-
* FROM (
|
|
24
|
-
* SELECT "user_id", "id", "title", "views"
|
|
25
|
-
* FROM "posts"
|
|
26
|
-
* WHERE "views" > $1
|
|
27
|
-
* ORDER BY "views" DESC
|
|
28
|
-
* ) sub
|
|
29
|
-
* GROUP BY "user_id"
|
|
30
|
-
* )
|
|
31
|
-
* SELECT ... COALESCE("cte_0".data, '[]'::jsonb) as "posts" ...
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export declare class JsonbCollectionStrategy implements ICollectionStrategy {
|
|
35
|
-
getType(): CollectionStrategyType;
|
|
36
|
-
requiresParentIds(): boolean;
|
|
37
|
-
buildAggregation(config: CollectionAggregationConfig, context: QueryContext): CollectionAggregationResult;
|
|
38
|
-
/**
|
|
39
|
-
* Build JSONB aggregation CTE
|
|
40
|
-
*/
|
|
41
|
-
private buildJsonbAggregation;
|
|
42
|
-
/**
|
|
43
|
-
* Build array aggregation CTE (for toNumberList/toStringList)
|
|
44
|
-
*/
|
|
45
|
-
private buildArrayAggregation;
|
|
46
|
-
/**
|
|
47
|
-
* Build scalar aggregation CTE (COUNT, MIN, MAX, SUM)
|
|
48
|
-
*/
|
|
49
|
-
private buildScalarAggregation;
|
|
50
|
-
}
|
|
51
|
-
//# sourceMappingURL=jsonb-collection-strategy.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsonb-collection-strategy.d.ts","sourceRoot":"","sources":["../../../src/query/strategies/jsonb-collection-strategy.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,2BAA2B,EAC3B,2BAA2B,EAC5B,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,uBAAwB,YAAW,mBAAmB;IACjE,OAAO,IAAI,sBAAsB;IAIjC,iBAAiB,IAAI,OAAO;IAK5B,gBAAgB,CACd,MAAM,EAAE,2BAA2B,EACnC,OAAO,EAAE,YAAY,GACpB,2BAA2B;IA0C9B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAgE7B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAsD7B;;OAEG;IACH,OAAO,CAAC,sBAAsB;CAuC/B"}
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JsonbCollectionStrategy = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* JSONB-based collection strategy using CTEs
|
|
6
|
-
*
|
|
7
|
-
* This is the current/default strategy that uses PostgreSQL CTEs with jsonb_agg
|
|
8
|
-
* to aggregate related records into JSONB arrays.
|
|
9
|
-
*
|
|
10
|
-
* Benefits:
|
|
11
|
-
* - Single query execution
|
|
12
|
-
* - No temp table management
|
|
13
|
-
* - Works well for moderate data sizes
|
|
14
|
-
*
|
|
15
|
-
* SQL Pattern:
|
|
16
|
-
* ```sql
|
|
17
|
-
* WITH "cte_0" AS (
|
|
18
|
-
* SELECT
|
|
19
|
-
* "user_id" as parent_id,
|
|
20
|
-
* jsonb_agg(
|
|
21
|
-
* jsonb_build_object('id', "id", 'title', "title")
|
|
22
|
-
* ORDER BY "views" DESC
|
|
23
|
-
* ) as data
|
|
24
|
-
* FROM (
|
|
25
|
-
* SELECT "user_id", "id", "title", "views"
|
|
26
|
-
* FROM "posts"
|
|
27
|
-
* WHERE "views" > $1
|
|
28
|
-
* ORDER BY "views" DESC
|
|
29
|
-
* ) sub
|
|
30
|
-
* GROUP BY "user_id"
|
|
31
|
-
* )
|
|
32
|
-
* SELECT ... COALESCE("cte_0".data, '[]'::jsonb) as "posts" ...
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
class JsonbCollectionStrategy {
|
|
36
|
-
getType() {
|
|
37
|
-
return 'jsonb';
|
|
38
|
-
}
|
|
39
|
-
requiresParentIds() {
|
|
40
|
-
// JSONB strategy doesn't need parent IDs upfront - it aggregates for all parents
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
buildAggregation(config, context) {
|
|
44
|
-
const cteName = `cte_${config.counter}`;
|
|
45
|
-
let cteSQL;
|
|
46
|
-
let selectExpression;
|
|
47
|
-
switch (config.aggregationType) {
|
|
48
|
-
case 'jsonb':
|
|
49
|
-
cteSQL = this.buildJsonbAggregation(config, cteName, context);
|
|
50
|
-
selectExpression = `COALESCE("${cteName}".data, ${config.defaultValue})`;
|
|
51
|
-
break;
|
|
52
|
-
case 'array':
|
|
53
|
-
cteSQL = this.buildArrayAggregation(config, cteName, context);
|
|
54
|
-
selectExpression = `COALESCE("${cteName}".data, ${config.defaultValue})`;
|
|
55
|
-
break;
|
|
56
|
-
case 'count':
|
|
57
|
-
case 'min':
|
|
58
|
-
case 'max':
|
|
59
|
-
case 'sum':
|
|
60
|
-
cteSQL = this.buildScalarAggregation(config, cteName, context);
|
|
61
|
-
selectExpression = `COALESCE("${cteName}".data, ${config.defaultValue})`;
|
|
62
|
-
break;
|
|
63
|
-
default:
|
|
64
|
-
throw new Error(`Unknown aggregation type: ${config.aggregationType}`);
|
|
65
|
-
}
|
|
66
|
-
// Store CTE in context
|
|
67
|
-
context.ctes.set(cteName, { sql: cteSQL, params: [] });
|
|
68
|
-
return {
|
|
69
|
-
sql: cteSQL,
|
|
70
|
-
params: context.allParams,
|
|
71
|
-
tableName: cteName,
|
|
72
|
-
joinClause: `LEFT JOIN "${cteName}" ON "${config.sourceTable}"."id" = "${cteName}".parent_id`,
|
|
73
|
-
selectExpression,
|
|
74
|
-
isCTE: true,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Build JSONB aggregation CTE
|
|
79
|
-
*/
|
|
80
|
-
buildJsonbAggregation(config, cteName, context) {
|
|
81
|
-
const { selectedFields, targetTable, foreignKey, whereClause, orderByClause, limitValue, offsetValue, isDistinct } = config;
|
|
82
|
-
// Build the subquery SELECT fields
|
|
83
|
-
const allSelectFields = [
|
|
84
|
-
`"${foreignKey}" as "__fk_${foreignKey}"`,
|
|
85
|
-
...selectedFields.map(f => {
|
|
86
|
-
// Always add alias if expression doesn't already match the quoted alias
|
|
87
|
-
if (f.expression !== `"${f.alias}"`) {
|
|
88
|
-
return `${f.expression} as "${f.alias}"`;
|
|
89
|
-
}
|
|
90
|
-
return f.expression;
|
|
91
|
-
}),
|
|
92
|
-
];
|
|
93
|
-
// Build the JSONB fields for jsonb_build_object using the aliases from the subquery
|
|
94
|
-
const jsonbFields = selectedFields
|
|
95
|
-
.map(f => `'${f.alias}', "${f.alias}"`)
|
|
96
|
-
.join(', ');
|
|
97
|
-
// Build WHERE clause
|
|
98
|
-
const whereSQL = whereClause ? `WHERE ${whereClause}` : '';
|
|
99
|
-
// Build ORDER BY clause
|
|
100
|
-
const orderBySQL = orderByClause ? `ORDER BY ${orderByClause}` : '';
|
|
101
|
-
// Build LIMIT/OFFSET
|
|
102
|
-
let limitOffsetClause = '';
|
|
103
|
-
if (limitValue !== undefined) {
|
|
104
|
-
limitOffsetClause = `LIMIT ${limitValue}`;
|
|
105
|
-
}
|
|
106
|
-
if (offsetValue !== undefined) {
|
|
107
|
-
limitOffsetClause += ` OFFSET ${offsetValue}`;
|
|
108
|
-
}
|
|
109
|
-
// Build DISTINCT clause
|
|
110
|
-
const distinctClause = isDistinct ? 'DISTINCT ' : '';
|
|
111
|
-
// Build the jsonb_agg ORDER BY clause
|
|
112
|
-
const jsonbAggOrderBy = orderByClause ? ` ORDER BY ${orderByClause}` : '';
|
|
113
|
-
const cteSQL = `
|
|
114
|
-
SELECT
|
|
115
|
-
"__fk_${foreignKey}" as parent_id,
|
|
116
|
-
jsonb_agg(
|
|
117
|
-
jsonb_build_object(${jsonbFields})${jsonbAggOrderBy}
|
|
118
|
-
) as data
|
|
119
|
-
FROM (
|
|
120
|
-
SELECT ${distinctClause}${allSelectFields.join(', ')}
|
|
121
|
-
FROM "${targetTable}"
|
|
122
|
-
${whereSQL}
|
|
123
|
-
${orderBySQL}
|
|
124
|
-
${limitOffsetClause}
|
|
125
|
-
) sub
|
|
126
|
-
GROUP BY "__fk_${foreignKey}"
|
|
127
|
-
`.trim();
|
|
128
|
-
return cteSQL;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Build array aggregation CTE (for toNumberList/toStringList)
|
|
132
|
-
*/
|
|
133
|
-
buildArrayAggregation(config, cteName, context) {
|
|
134
|
-
const { arrayField, targetTable, foreignKey, whereClause, orderByClause, limitValue, offsetValue, isDistinct } = config;
|
|
135
|
-
if (!arrayField) {
|
|
136
|
-
throw new Error('arrayField is required for array aggregation');
|
|
137
|
-
}
|
|
138
|
-
// Build WHERE clause
|
|
139
|
-
const whereSQL = whereClause ? `WHERE ${whereClause}` : '';
|
|
140
|
-
// Build ORDER BY clause
|
|
141
|
-
const orderBySQL = orderByClause ? `ORDER BY ${orderByClause}` : '';
|
|
142
|
-
// Build LIMIT/OFFSET
|
|
143
|
-
let limitOffsetClause = '';
|
|
144
|
-
if (limitValue !== undefined) {
|
|
145
|
-
limitOffsetClause = `LIMIT ${limitValue}`;
|
|
146
|
-
}
|
|
147
|
-
if (offsetValue !== undefined) {
|
|
148
|
-
limitOffsetClause += ` OFFSET ${offsetValue}`;
|
|
149
|
-
}
|
|
150
|
-
// Build DISTINCT clause
|
|
151
|
-
const distinctClause = isDistinct ? 'DISTINCT ' : '';
|
|
152
|
-
// Build the array_agg ORDER BY clause
|
|
153
|
-
const arrayAggOrderBy = orderByClause ? ` ORDER BY ${orderByClause}` : '';
|
|
154
|
-
const cteSQL = `
|
|
155
|
-
SELECT
|
|
156
|
-
"__fk_${foreignKey}" as parent_id,
|
|
157
|
-
array_agg(
|
|
158
|
-
"${arrayField}"${arrayAggOrderBy}
|
|
159
|
-
) as data
|
|
160
|
-
FROM (
|
|
161
|
-
SELECT ${distinctClause}"__fk_${foreignKey}", "${arrayField}"
|
|
162
|
-
FROM (
|
|
163
|
-
SELECT "${foreignKey}" as "__fk_${foreignKey}", "${arrayField}"
|
|
164
|
-
FROM "${targetTable}"
|
|
165
|
-
${whereSQL}
|
|
166
|
-
${orderBySQL}
|
|
167
|
-
${limitOffsetClause}
|
|
168
|
-
) inner_sub
|
|
169
|
-
) sub
|
|
170
|
-
GROUP BY "__fk_${foreignKey}"
|
|
171
|
-
`.trim();
|
|
172
|
-
return cteSQL;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Build scalar aggregation CTE (COUNT, MIN, MAX, SUM)
|
|
176
|
-
*/
|
|
177
|
-
buildScalarAggregation(config, cteName, context) {
|
|
178
|
-
const { aggregationType, aggregateField, targetTable, foreignKey, whereClause } = config;
|
|
179
|
-
// Build WHERE clause
|
|
180
|
-
const whereSQL = whereClause ? `WHERE ${whereClause}` : '';
|
|
181
|
-
// Build aggregation expression
|
|
182
|
-
let aggregateExpression;
|
|
183
|
-
switch (aggregationType) {
|
|
184
|
-
case 'count':
|
|
185
|
-
aggregateExpression = 'COUNT(*)';
|
|
186
|
-
break;
|
|
187
|
-
case 'min':
|
|
188
|
-
case 'max':
|
|
189
|
-
case 'sum':
|
|
190
|
-
if (!aggregateField) {
|
|
191
|
-
throw new Error(`${aggregationType.toUpperCase()} requires an aggregate field`);
|
|
192
|
-
}
|
|
193
|
-
aggregateExpression = `${aggregationType.toUpperCase()}("${aggregateField}")`;
|
|
194
|
-
break;
|
|
195
|
-
default:
|
|
196
|
-
throw new Error(`Unknown aggregation type: ${aggregationType}`);
|
|
197
|
-
}
|
|
198
|
-
const cteSQL = `
|
|
199
|
-
SELECT
|
|
200
|
-
"${foreignKey}" as parent_id,
|
|
201
|
-
${aggregateExpression} as data
|
|
202
|
-
FROM "${targetTable}"
|
|
203
|
-
${whereSQL}
|
|
204
|
-
GROUP BY "${foreignKey}"
|
|
205
|
-
`.trim();
|
|
206
|
-
return cteSQL;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
exports.JsonbCollectionStrategy = JsonbCollectionStrategy;
|
|
210
|
-
//# sourceMappingURL=jsonb-collection-strategy.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsonb-collection-strategy.js","sourceRoot":"","sources":["../../../src/query/strategies/jsonb-collection-strategy.ts"],"names":[],"mappings":";;;AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAa,uBAAuB;IAClC,OAAO;QACL,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,iBAAiB;QACf,iFAAiF;QACjF,OAAO,KAAK,CAAC;IACf,CAAC;I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