kysely-gen 0.12.0 → 0.13.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.
Files changed (2) hide show
  1. package/dist/cli.js +93 -19
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -109898,6 +109898,44 @@ function parseMssqlOrChain(definition) {
109898
109898
  }
109899
109899
  return values.length > 0 ? values : null;
109900
109900
  }
109901
+ var MYSQL_IN_REGEX = /^[\s(]*`?(\w+)`?\s+IN\s*\(([^)]+)\)/i;
109902
+ function parseMysqlCheckConstraint(definition) {
109903
+ if (!definition || definition.trim() === "")
109904
+ return null;
109905
+ const match = definition.match(MYSQL_IN_REGEX);
109906
+ if (!match)
109907
+ return null;
109908
+ const columnName = match[1];
109909
+ const valuesPart = match[2];
109910
+ if (!valuesPart || valuesPart.trim() === "")
109911
+ return null;
109912
+ const numericValues = parseNumericArray(valuesPart);
109913
+ if (numericValues !== null) {
109914
+ if (isBooleanPattern(numericValues)) {
109915
+ return { columnName, constraint: { type: "boolean" } };
109916
+ }
109917
+ return { columnName, constraint: { type: "number", values: numericValues } };
109918
+ }
109919
+ const stringValues = parseMysqlStringArray(valuesPart);
109920
+ if (stringValues !== null && stringValues.length > 0) {
109921
+ return { columnName, constraint: { type: "string", values: stringValues } };
109922
+ }
109923
+ return null;
109924
+ }
109925
+ function parseMysqlStringArray(arrayContent) {
109926
+ const values = [];
109927
+ const mysqlValueRegex = /_\w+\\'([^'\\]*)\\'/g;
109928
+ let match;
109929
+ while ((match = mysqlValueRegex.exec(arrayContent)) !== null) {
109930
+ let value = match[1];
109931
+ value = value.replace(/''/g, "'");
109932
+ values.push(value);
109933
+ }
109934
+ if (values.length > 0) {
109935
+ return values;
109936
+ }
109937
+ return parseStringArray(arrayContent);
109938
+ }
109901
109939
 
109902
109940
  // src/dialects/postgres/introspect.ts
109903
109941
  async function introspectPostgres(db, options) {
@@ -110441,11 +110479,22 @@ function isEnumType(columnType) {
110441
110479
 
110442
110480
  // src/dialects/mysql/introspect.ts
110443
110481
  async function introspectMysql(db, options) {
110444
- const [baseTables, views] = await Promise.all([
110482
+ const [baseTables, views, checkConstraints] = await Promise.all([
110445
110483
  introspectTables2(db, options.schemas),
110446
- introspectViews2(db, options.schemas)
110484
+ introspectViews2(db, options.schemas),
110485
+ introspectCheckConstraints2(db, options.schemas)
110447
110486
  ]);
110448
- const tables = [...baseTables, ...views];
110487
+ const tables = [...baseTables, ...views].map((table) => ({
110488
+ ...table,
110489
+ columns: table.columns.map((column) => {
110490
+ const key = `${table.schema}.${table.name}.${column.name}`;
110491
+ const checkConstraint = checkConstraints.get(key);
110492
+ return {
110493
+ ...column,
110494
+ ...checkConstraint && { checkConstraint }
110495
+ };
110496
+ })
110497
+ }));
110449
110498
  const enums = extractEnums(tables);
110450
110499
  return {
110451
110500
  tables,
@@ -110560,6 +110609,31 @@ function extractEnums(tables) {
110560
110609
  }
110561
110610
  return Array.from(enumMap.values());
110562
110611
  }
110612
+ async function introspectCheckConstraints2(db, schemas) {
110613
+ const rawConstraints = await sql`
110614
+ SELECT
110615
+ tc.TABLE_SCHEMA AS CONSTRAINT_SCHEMA,
110616
+ tc.TABLE_NAME,
110617
+ cc.CHECK_CLAUSE
110618
+ FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
110619
+ JOIN INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc
110620
+ ON tc.CONSTRAINT_SCHEMA = cc.CONSTRAINT_SCHEMA
110621
+ AND tc.CONSTRAINT_NAME = cc.CONSTRAINT_NAME
110622
+ WHERE tc.CONSTRAINT_TYPE = 'CHECK'
110623
+ AND tc.TABLE_SCHEMA IN (${sql.join(schemas.map((s) => sql`${s}`))})
110624
+ `.execute(db);
110625
+ const constraintMap = new Map;
110626
+ for (const row of rawConstraints.rows) {
110627
+ const parsed = parseMysqlCheckConstraint(row.CHECK_CLAUSE);
110628
+ if (!parsed)
110629
+ continue;
110630
+ const key = `${row.CONSTRAINT_SCHEMA}.${row.TABLE_NAME}.${parsed.columnName}`;
110631
+ if (constraintMap.has(key))
110632
+ continue;
110633
+ constraintMap.set(key, parsed.constraint);
110634
+ }
110635
+ return constraintMap;
110636
+ }
110563
110637
 
110564
110638
  // src/dialects/mysql/type-mapper.ts
110565
110639
  function createColumnType(selectType, insertType, updateType) {
@@ -110736,7 +110810,7 @@ function parseSqliteTableDDL(sql2) {
110736
110810
 
110737
110811
  // src/dialects/sqlite/introspect.ts
110738
110812
  async function introspectSqlite(db, _options) {
110739
- const checkConstraints = await introspectCheckConstraints2(db);
110813
+ const checkConstraints = await introspectCheckConstraints3(db);
110740
110814
  const [baseTables, views] = await Promise.all([
110741
110815
  introspectTables3(db, checkConstraints),
110742
110816
  introspectViews3(db)
@@ -110809,7 +110883,7 @@ function normalizeDataType2(type) {
110809
110883
  }
110810
110884
  return lowerType;
110811
110885
  }
110812
- async function introspectCheckConstraints2(db) {
110886
+ async function introspectCheckConstraints3(db) {
110813
110887
  const result = await sql`
110814
110888
  SELECT name, sql FROM sqlite_master
110815
110889
  WHERE type = 'table'
@@ -110910,7 +110984,7 @@ async function introspectMssql(db, options) {
110910
110984
  const [baseTables, views, checkConstraints] = await Promise.all([
110911
110985
  introspectTables4(db, options.schemas),
110912
110986
  introspectViews4(db, options.schemas),
110913
- introspectCheckConstraints3(db, options.schemas)
110987
+ introspectCheckConstraints4(db, options.schemas)
110914
110988
  ]);
110915
110989
  const tables = [...baseTables, ...views].map((table) => ({
110916
110990
  ...table,
@@ -110998,7 +111072,7 @@ function buildTableMetadata2(rows, isView) {
110998
111072
  }
110999
111073
  return Array.from(tableMap.values());
111000
111074
  }
111001
- async function introspectCheckConstraints3(db, schemas) {
111075
+ async function introspectCheckConstraints4(db, schemas) {
111002
111076
  const rawConstraints = await sql`
111003
111077
  SELECT
111004
111078
  s.name AS schema_name,
@@ -111644,21 +111718,16 @@ function transformColumnToZod(column, enums, enumResolver, mode, options) {
111644
111718
  }
111645
111719
  } else if (column.checkConstraint) {
111646
111720
  if (column.checkConstraint.type === "boolean") {
111647
- const unionSchema = {
111648
- kind: "zod-union",
111649
- schemas: [
111650
- { kind: "zod-literal", value: 0 },
111651
- { kind: "zod-literal", value: 1 }
111652
- ]
111653
- };
111654
111721
  if (options?.noBooleanCoerce) {
111655
- schema = unionSchema;
111656
- } else {
111657
111722
  schema = {
111658
- kind: "zod-transform",
111659
- schema: unionSchema,
111660
- transformFn: "v => v === 1"
111723
+ kind: "zod-union",
111724
+ schemas: [
111725
+ { kind: "zod-literal", value: 0 },
111726
+ { kind: "zod-literal", value: 1 }
111727
+ ]
111661
111728
  };
111729
+ } else {
111730
+ schema = { kind: "zod-coerce", method: "boolean" };
111662
111731
  }
111663
111732
  } else if (column.checkConstraint.type === "string") {
111664
111733
  schema = { kind: "zod-enum", values: column.checkConstraint.values };
@@ -111820,6 +111889,8 @@ function serializeZodSchema(node) {
111820
111889
  return serializeZodCustom(node);
111821
111890
  case "zod-transform":
111822
111891
  return serializeZodTransform(node);
111892
+ case "zod-coerce":
111893
+ return serializeZodCoerce(node);
111823
111894
  }
111824
111895
  }
111825
111896
  function serializeZodPrimitive(node) {
@@ -111872,6 +111943,9 @@ function serializeZodCustom(node) {
111872
111943
  function serializeZodTransform(node) {
111873
111944
  return `${serializeZodSchema(node.schema)}.transform(${node.transformFn})`;
111874
111945
  }
111946
+ function serializeZodCoerce(node) {
111947
+ return `z.coerce.${node.method}()`;
111948
+ }
111875
111949
  function serializeZodDeclaration(node) {
111876
111950
  switch (node.kind) {
111877
111951
  case "zod-import":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kysely-gen",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Database type generator for Kysely - Supports PostgreSQL, MySQL, SQLite, and MSSQL",
5
5
  "type": "module",
6
6
  "license": "MIT",