@travetto/model-mysql 8.0.7 → 8.0.8

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/dialect.ts +29 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mysql",
3
- "version": "8.0.7",
3
+ "version": "8.0.8",
4
4
  "description": "MySQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
5
5
  "keywords": [
6
6
  "data-modeling",
@@ -34,8 +34,8 @@
34
34
  "@travetto/config": "^8.0.3",
35
35
  "@travetto/context": "^8.0.1",
36
36
  "@travetto/model": "^8.0.4",
37
- "@travetto/model-query": "^8.0.7",
38
- "@travetto/model-sql": "^8.0.7",
37
+ "@travetto/model-query": "^8.0.8",
38
+ "@travetto/model-sql": "^8.0.8",
39
39
  "mysql2": "^3.24.4"
40
40
  },
41
41
  "peerDependencies": {
package/src/dialect.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ModelType } from '@travetto/model';
1
2
  import { AbstractANSI99Dialect, type JSONSqlPathMode, type ResolvedPathContext, type TableContext } from '@travetto/model-sql';
2
3
  import { type Class, castTo, JSONUtil } from '@travetto/runtime';
3
4
  import type { SchemaFieldConfig } from '@travetto/schema';
@@ -135,6 +136,34 @@ export class MysqlDialect extends AbstractANSI99Dialect {
135
136
  return sqlPath;
136
137
  }
137
138
 
139
+ buildArrayFacet<T extends ModelType>(
140
+ tableContext: TableContext<T>,
141
+ resolvedContext: ResolvedPathContext,
142
+ whereSQL?: string,
143
+ limit?: number,
144
+ offset?: number
145
+ ): string {
146
+ const columnName = this.escapeIdentifier(resolvedContext.arrayPath?.[0] ?? resolvedContext.sqlPath);
147
+ const arrayPathPart =
148
+ resolvedContext.arrayPath && resolvedContext.arrayPath.length > 1
149
+ ? `$.${this.formatJsonPath(resolvedContext.arrayPath.slice(1))}[*]`
150
+ : '$[*]';
151
+ const elementPath =
152
+ resolvedContext.subPath && resolvedContext.subPath.length > 0 ? `$.${this.formatJsonPath(resolvedContext.subPath)}` : '$';
153
+
154
+ const countClause = this.castColumn?.('COUNT(*)', Number) ?? 'COUNT(*)';
155
+
156
+ return `
157
+ SELECT jsonTable.value AS ${this.escapeIdentifier('key')}, ${countClause} AS ${this.escapeIdentifier('count')}
158
+ FROM ${this.escapeIdentifier(tableContext.tableName)}, JSON_TABLE(${columnName}, '${arrayPathPart}' COLUMNS (value VARCHAR(1024) PATH '${elementPath}')) AS jsonTable
159
+ WHERE jsonTable.value IS NOT NULL
160
+ ${whereSQL ? `AND ${whereSQL}` : ''}
161
+ GROUP BY jsonTable.value
162
+ ORDER BY ${this.escapeIdentifier('count')} DESC
163
+ ${limit !== undefined ? `LIMIT ${limit}` : ''}
164
+ ${offset !== undefined ? `OFFSET ${offset}` : ''};`;
165
+ }
166
+
138
167
  override getUpsertSQL(
139
168
  context: TableContext,
140
169
  columns: string[],