metal-orm 1.0.50 → 1.0.51

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.cjs CHANGED
@@ -379,6 +379,10 @@ var col = {
379
379
  * @returns ColumnDef with VARCHAR type
380
380
  */
381
381
  varchar: (length2) => ({ name: "", type: "VARCHAR", args: [length2] }),
382
+ /**
383
+ * Creates a text column definition
384
+ */
385
+ text: () => ({ name: "", type: "TEXT" }),
382
386
  /**
383
387
  * Creates a fixed precision decimal column definition
384
388
  */
@@ -6887,6 +6891,29 @@ var postgresIntrospector = {
6887
6891
  if (!trimmed) return;
6888
6892
  columnComments.set(key, trimmed);
6889
6893
  });
6894
+ const tableCommentRows = await queryRows(
6895
+ ctx.executor,
6896
+ `
6897
+ SELECT
6898
+ ns.nspname AS table_schema,
6899
+ cls.relname AS table_name,
6900
+ pg_catalog.obj_description(cls.oid) AS description
6901
+ FROM pg_catalog.pg_class cls
6902
+ JOIN pg_catalog.pg_namespace ns ON ns.oid = cls.relnamespace
6903
+ WHERE ns.nspname = $1
6904
+ AND cls.relkind IN ('r', 'p')
6905
+ `,
6906
+ [schema]
6907
+ );
6908
+ const tableComments = /* @__PURE__ */ new Map();
6909
+ tableCommentRows.forEach((r) => {
6910
+ if (!shouldIncludeTable(r.table_name, options)) return;
6911
+ if (!r.description) return;
6912
+ const key = `${r.table_schema}.${r.table_name}`;
6913
+ const trimmed = r.description.trim();
6914
+ if (!trimmed) return;
6915
+ tableComments.set(key, trimmed);
6916
+ });
6890
6917
  const qbPk = new SelectQueryBuilder(PgKeyColumnUsage).select({
6891
6918
  table_schema: PgKeyColumnUsage.columns.table_schema,
6892
6919
  table_name: PgKeyColumnUsage.columns.table_name,
@@ -7027,7 +7054,8 @@ var postgresIntrospector = {
7027
7054
  schema: r.table_schema,
7028
7055
  columns: [],
7029
7056
  primaryKey: pkMap.get(key) || [],
7030
- indexes: []
7057
+ indexes: [],
7058
+ comment: tableComments.get(key)
7031
7059
  });
7032
7060
  }
7033
7061
  const cols = tablesByKey.get(key);
@@ -7412,6 +7440,53 @@ var runPragma = async (name, table, alias, columnAliases, ctx) => {
7412
7440
  const query = buildPragmaQuery(name, table, alias, columnAliases);
7413
7441
  return await runSelectNode(query, ctx);
7414
7442
  };
7443
+ var loadSqliteSchemaComments = async (ctx) => {
7444
+ const tableComments = /* @__PURE__ */ new Map();
7445
+ const columnComments = /* @__PURE__ */ new Map();
7446
+ const tableExists = await queryRows(
7447
+ ctx.executor,
7448
+ `SELECT name FROM sqlite_master WHERE type='table' AND name='schema_comments' LIMIT 1`
7449
+ );
7450
+ if (!tableExists.length) {
7451
+ return { tableComments, columnComments };
7452
+ }
7453
+ const commentRows = await queryRows(
7454
+ ctx.executor,
7455
+ `SELECT object_type, schema_name, table_name, column_name, comment FROM schema_comments`
7456
+ );
7457
+ for (const row of commentRows) {
7458
+ const objectType = typeof row.object_type === "string" ? row.object_type.toLowerCase() : "";
7459
+ const tableName = typeof row.table_name === "string" ? row.table_name : "";
7460
+ if (!tableName) continue;
7461
+ const columnName = typeof row.column_name === "string" ? row.column_name : "";
7462
+ const schemaName = typeof row.schema_name === "string" ? row.schema_name : "";
7463
+ const rawComment = row.comment;
7464
+ if (rawComment == null) continue;
7465
+ const commentText = String(rawComment).trim();
7466
+ if (!commentText) continue;
7467
+ const addTableComment = () => {
7468
+ tableComments.set(tableName, commentText);
7469
+ if (schemaName) {
7470
+ tableComments.set(`${schemaName}.${tableName}`, commentText);
7471
+ }
7472
+ };
7473
+ const addColumnComment = () => {
7474
+ columnComments.set(`${tableName}.${columnName}`, commentText);
7475
+ if (schemaName) {
7476
+ columnComments.set(`${schemaName}.${tableName}.${columnName}`, commentText);
7477
+ }
7478
+ };
7479
+ if (objectType === "table") {
7480
+ addTableComment();
7481
+ } else if (objectType === "column" && columnName) {
7482
+ addColumnComment();
7483
+ }
7484
+ }
7485
+ return {
7486
+ tableComments,
7487
+ columnComments
7488
+ };
7489
+ };
7415
7490
  var sqliteIntrospector = {
7416
7491
  async introspect(ctx, options) {
7417
7492
  const alias = "sqlite_master";
@@ -7425,6 +7500,7 @@ var sqliteIntrospector = {
7425
7500
  notLike(columnNode2(alias, "name"), "sqlite_%")
7426
7501
  )
7427
7502
  };
7503
+ const { tableComments, columnComments } = await loadSqliteSchemaComments(ctx);
7428
7504
  const tableRows = await runSelectNode(tablesQuery, ctx);
7429
7505
  const tables = [];
7430
7506
  for (const row of tableRows) {
@@ -7451,15 +7527,26 @@ var sqliteIntrospector = {
7451
7527
  ["seq", "name", "unique"],
7452
7528
  ctx
7453
7529
  );
7454
- const tableEntry = { name: tableName, columns: [], primaryKey: [], indexes: [] };
7530
+ const tableEntry = {
7531
+ name: tableName,
7532
+ columns: [],
7533
+ primaryKey: [],
7534
+ indexes: [],
7535
+ comment: tableComments.get(tableName)
7536
+ };
7455
7537
  tableInfo.forEach((info) => {
7456
- tableEntry.columns.push({
7538
+ const column = {
7457
7539
  name: info.name,
7458
7540
  type: info.type,
7459
7541
  notNull: info.notnull === 1,
7460
7542
  default: info.dflt_value ?? void 0,
7461
7543
  autoIncrement: false
7462
- });
7544
+ };
7545
+ const columnComment = columnComments.get(`${tableName}.${info.name}`);
7546
+ if (columnComment) {
7547
+ column.comment = columnComment;
7548
+ }
7549
+ tableEntry.columns.push(column);
7463
7550
  if (info.pk && info.pk > 0) {
7464
7551
  tableEntry.primaryKey = tableEntry.primaryKey || [];
7465
7552
  tableEntry.primaryKey.push(info.name);
@@ -7731,6 +7818,60 @@ var mssqlIntrospector = {
7731
7818
  async introspect(ctx, options) {
7732
7819
  const schema = options.schema;
7733
7820
  const schemaCondition = schema ? eq(columnNode3("sch", "name"), schema) : void 0;
7821
+ const schemaFilter = schema ? "AND sch.name = @p1" : "";
7822
+ const schemaParams = schema ? [schema] : [];
7823
+ const tableCommentRows = await queryRows(
7824
+ ctx.executor,
7825
+ `
7826
+ SELECT
7827
+ sch.name AS table_schema,
7828
+ t.name AS table_name,
7829
+ CONVERT(nvarchar(4000), ep.value) AS comment
7830
+ FROM sys.extended_properties ep
7831
+ JOIN sys.tables t ON t.object_id = ep.major_id
7832
+ JOIN sys.schemas sch ON sch.schema_id = t.schema_id
7833
+ WHERE ep.class = 1
7834
+ AND ep.minor_id = 0
7835
+ AND ep.name = 'MS_Description'
7836
+ ${schemaFilter}
7837
+ `,
7838
+ schemaParams
7839
+ );
7840
+ const columnCommentRows = await queryRows(
7841
+ ctx.executor,
7842
+ `
7843
+ SELECT
7844
+ sch.name AS table_schema,
7845
+ t.name AS table_name,
7846
+ col.name AS column_name,
7847
+ CONVERT(nvarchar(4000), ep.value) AS comment
7848
+ FROM sys.extended_properties ep
7849
+ JOIN sys.columns col ON col.object_id = ep.major_id AND col.column_id = ep.minor_id
7850
+ JOIN sys.tables t ON t.object_id = col.object_id
7851
+ JOIN sys.schemas sch ON sch.schema_id = t.schema_id
7852
+ WHERE ep.class = 1
7853
+ AND ep.minor_id > 0
7854
+ AND ep.name = 'MS_Description'
7855
+ ${schemaFilter}
7856
+ `,
7857
+ schemaParams
7858
+ );
7859
+ const tableComments = /* @__PURE__ */ new Map();
7860
+ tableCommentRows.forEach((r) => {
7861
+ if (!shouldIncludeTable(r.table_name, options)) return;
7862
+ if (!r.comment) return;
7863
+ const trimmed = r.comment.trim();
7864
+ if (!trimmed) return;
7865
+ tableComments.set(`${r.table_schema}.${r.table_name}`, trimmed);
7866
+ });
7867
+ const columnComments = /* @__PURE__ */ new Map();
7868
+ columnCommentRows.forEach((r) => {
7869
+ if (!shouldIncludeTable(r.table_name, options)) return;
7870
+ if (!r.comment) return;
7871
+ const trimmed = r.comment.trim();
7872
+ if (!trimmed) return;
7873
+ columnComments.set(`${r.table_schema}.${r.table_name}.${r.column_name}`, trimmed);
7874
+ });
7734
7875
  const dataTypeExpression = buildMssqlDataType(
7735
7876
  { table: "ty", name: "name" },
7736
7877
  { table: "c", name: "max_length" },
@@ -8036,7 +8177,8 @@ var mssqlIntrospector = {
8036
8177
  schema: r.table_schema,
8037
8178
  columns: [],
8038
8179
  primaryKey: pkMap.get(key) || [],
8039
- indexes: []
8180
+ indexes: [],
8181
+ comment: tableComments.get(key)
8040
8182
  });
8041
8183
  }
8042
8184
  const table = tablesByKey.get(key);
@@ -8047,6 +8189,10 @@ var mssqlIntrospector = {
8047
8189
  default: r.column_default ?? void 0,
8048
8190
  autoIncrement: !!r.is_identity
8049
8191
  };
8192
+ const columnComment = columnComments.get(`${key}.${r.column_name}`);
8193
+ if (columnComment) {
8194
+ column.comment = columnComment;
8195
+ }
8050
8196
  const fk = fkMap.get(`${key}.${r.column_name}`)?.[0];
8051
8197
  if (fk) {
8052
8198
  column.references = {