mutano 3.7.0 → 3.7.2

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/dist/main.js +130 -1
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -16189,9 +16189,138 @@ function extractSqlEntities(config) {
16189
16189
  for (const match2 of viewMatches) {
16190
16190
  const viewName = match2[1];
16191
16191
  views.push(viewName);
16192
+ const viewColumns = parseViewColumns(sqlContent, match2.index + match2[0].length, tableDefinitions);
16193
+ if (viewColumns.length > 0) {
16194
+ tableDefinitions.set(viewName, {
16195
+ name: viewName,
16196
+ isView: true,
16197
+ columns: viewColumns
16198
+ });
16199
+ }
16192
16200
  }
16193
16201
  return { tables, views, tableDefinitions };
16194
16202
  }
16203
+ function parseViewColumns(sqlContent, startPos, tableDefinitions) {
16204
+ const columns = [];
16205
+ const remainingContent = sqlContent.substring(startPos);
16206
+ const asSelectMatch = remainingContent.match(/\s*AS\s+SELECT\s+/i);
16207
+ if (!asSelectMatch) return columns;
16208
+ const selectStart = asSelectMatch.index + asSelectMatch[0].length;
16209
+ const selectContent = remainingContent.substring(selectStart);
16210
+ const fromMatch = selectContent.match(/\sFROM\s/i);
16211
+ const selectClause = fromMatch ? selectContent.substring(0, fromMatch.index) : selectContent;
16212
+ const columnExprs = [];
16213
+ let current = "";
16214
+ let parenDepth = 0;
16215
+ for (let i = 0; i < selectClause.length; i++) {
16216
+ const char = selectClause[i];
16217
+ if (char === "(") parenDepth++;
16218
+ if (char === ")") parenDepth--;
16219
+ if (char === "," && parenDepth === 0) {
16220
+ columnExprs.push(current.trim());
16221
+ current = "";
16222
+ } else {
16223
+ current += char;
16224
+ }
16225
+ }
16226
+ if (current.trim()) {
16227
+ columnExprs.push(current.trim());
16228
+ }
16229
+ const prefixToTable = {
16230
+ "user_": ["users_data", "ud"],
16231
+ "company_": ["rise_entities", "company"],
16232
+ "team_": ["rise_entities", "team"],
16233
+ "user_relationship_": ["rise_entities", "user_rel"],
16234
+ "user_entity_": ["rise_entities", "user_entity"]
16235
+ };
16236
+ for (const expr of columnExprs) {
16237
+ if (!expr) continue;
16238
+ const aliasMatch = expr.match(/\s+AS\s+[`"]?([^`"\s,]+)[`"]?\s*$/i);
16239
+ if (aliasMatch) {
16240
+ const name = aliasMatch[1];
16241
+ const sourceRefMatch = expr.match(/^(?:[`"]?([\w_]+)[`"]?\.)?[`"]?([\w_]+)[`"]?\s+AS/i);
16242
+ const sourceTable = sourceRefMatch?.[1];
16243
+ const sourceCol = sourceRefMatch?.[2];
16244
+ let type = "varchar(191)";
16245
+ let nullable = true;
16246
+ let foundType = false;
16247
+ if (sourceTable && sourceCol) {
16248
+ const table = tableDefinitions.get(sourceTable);
16249
+ if (table) {
16250
+ const col = table.columns.find((c) => c.name === sourceCol);
16251
+ if (col) {
16252
+ type = col.type;
16253
+ nullable = col.nullable;
16254
+ foundType = true;
16255
+ }
16256
+ }
16257
+ }
16258
+ if (!foundType && sourceCol) {
16259
+ for (const [prefix, tableNames] of Object.entries(prefixToTable)) {
16260
+ if (name.startsWith(prefix)) {
16261
+ for (const tableName of tableNames) {
16262
+ const table = tableDefinitions.get(tableName);
16263
+ if (table) {
16264
+ const col = table.columns.find((c) => c.name === sourceCol);
16265
+ if (col) {
16266
+ type = col.type;
16267
+ nullable = col.nullable;
16268
+ foundType = true;
16269
+ break;
16270
+ }
16271
+ }
16272
+ }
16273
+ if (foundType) break;
16274
+ }
16275
+ }
16276
+ }
16277
+ let comment = "";
16278
+ if (name === "user_rise_account") {
16279
+ comment = "@kysely(UserRiseAccount)";
16280
+ } else {
16281
+ const nanoidMatch = name.match(/^(\w+)_nanoid$/);
16282
+ if (nanoidMatch) {
16283
+ const prefix = nanoidMatch[1];
16284
+ const brandMap = {
16285
+ "user": "UserNanoid",
16286
+ "company": "CompanyNanoid",
16287
+ "team": "TeamNanoid",
16288
+ "document": "DocumentNanoid",
16289
+ "template": "TemplateNanoid",
16290
+ "entity": "EntityNanoid",
16291
+ "payment": "PaymentNanoid",
16292
+ "transaction": "TransactionNanoid"
16293
+ };
16294
+ const brand = brandMap[prefix];
16295
+ if (brand) {
16296
+ comment = `@kysely(${brand})`;
16297
+ }
16298
+ }
16299
+ }
16300
+ columns.push({
16301
+ name,
16302
+ type,
16303
+ nullable,
16304
+ defaultValue: null,
16305
+ extra: "",
16306
+ comment
16307
+ });
16308
+ } else {
16309
+ const colNameMatch = expr.match(/^(?:.*\.)?[`"]?([^`"\s,()]+)[`"]?\s*$/i);
16310
+ if (colNameMatch) {
16311
+ columns.push({
16312
+ name: colNameMatch[1],
16313
+ type: "varchar(191)",
16314
+ nullable: true,
16315
+ defaultValue: null,
16316
+ extra: "",
16317
+ comment: ""
16318
+ });
16319
+ }
16320
+ }
16321
+ }
16322
+ return columns;
16323
+ }
16195
16324
  function parseColumns(columnSection) {
16196
16325
  const columns = [];
16197
16326
  const lines = columnSection.split("\n");
@@ -16286,7 +16415,7 @@ function extractSqlColumnDescriptions(config, tableName, tableDefinitions) {
16286
16415
  const table = tableDefinitions.get(tableName);
16287
16416
  if (!table) return [];
16288
16417
  return table.columns.map((col) => {
16289
- const enumMatch = col.type.match(/enum\s*\(([^)]+)\)/i);
16418
+ const enumMatch = col.type.match(/enum\s*\(([\s\S]+)\)/i);
16290
16419
  const enumOptions = enumMatch ? enumMatch[1].match(/'([^']+)'/g)?.map((s) => s.slice(1, -1)) : void 0;
16291
16420
  const dataType = col.type.split("(")[0].toLowerCase();
16292
16421
  return {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mutano",
3
3
  "type": "module",
4
- "version": "3.7.0",
4
+ "version": "3.7.2",
5
5
  "description": "Converts Prisma/MySQL/PostgreSQL/SQLite schemas to Zod/TS/Kysely interfaces",
6
6
  "author": "Alisson Cavalcante Agiani <thelinuxlich@gmail.com>",
7
7
  "license": "MIT",