brizzle 0.2.7 → 0.2.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/dist/index.js +24 -2
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -506,6 +506,24 @@ function getTimestampColumns(dialect, noTimestamps = false) {
506
506
  .$defaultFn(() => new Date())`;
507
507
  }
508
508
  }
509
+ function extractImportsFromSchema(content) {
510
+ const importMatch = content.match(/import\s*\{([^}]+)\}\s*from\s*["']drizzle-orm\/[^"']+["']/);
511
+ if (!importMatch) {
512
+ return [];
513
+ }
514
+ return importMatch[1].split(",").map((s) => s.trim()).filter((s) => s.length > 0);
515
+ }
516
+ function updateSchemaImports(content, newImports, dialect) {
517
+ const existingImports = extractImportsFromSchema(content);
518
+ const mergedImports = Array.from(/* @__PURE__ */ new Set([...existingImports, ...newImports]));
519
+ const drizzleImport = getDrizzleImport(dialect);
520
+ const newImportLine = `import { ${mergedImports.join(", ")} } from "${drizzleImport}";`;
521
+ const importRegex = /import\s*\{[^}]+\}\s*from\s*["']drizzle-orm\/[^"']+["'];?/;
522
+ if (importRegex.test(content)) {
523
+ return content.replace(importRegex, newImportLine);
524
+ }
525
+ return newImportLine + "\n" + content;
526
+ }
509
527
  function getRequiredImports(fields, dialect, options = {}) {
510
528
  const types = /* @__PURE__ */ new Set();
511
529
  types.add(getTableFunction(dialect));
@@ -782,17 +800,21 @@ function generateEnumField(field, columnName, dialect) {
782
800
  }
783
801
  function appendToSchema(schemaPath, modelName, tableName, fields, dialect, options) {
784
802
  const existingContent = readFile(schemaPath);
803
+ const newImports = getRequiredImports(fields, dialect, options);
804
+ const updatedContent = updateSchemaImports(existingContent, newImports, dialect);
785
805
  const enumDefinitions = generateEnumDefinitions(fields, dialect);
786
806
  const tableDefinition = generateTableDefinition(modelName, tableName, fields, dialect, options);
787
- const newContent = existingContent + enumDefinitions + "\n" + tableDefinition + "\n";
807
+ const newContent = updatedContent + enumDefinitions + "\n" + tableDefinition + "\n";
788
808
  writeFile(schemaPath, newContent, { force: true, dryRun: options.dryRun });
789
809
  }
790
810
  function replaceInSchema(schemaPath, modelName, tableName, fields, dialect, options) {
791
811
  const existingContent = readFile(schemaPath);
792
812
  const cleanedContent = removeModelFromSchemaContent(existingContent, tableName);
813
+ const newImports = getRequiredImports(fields, dialect, options);
814
+ const updatedContent = updateSchemaImports(cleanedContent, newImports, dialect);
793
815
  const enumDefinitions = generateEnumDefinitions(fields, dialect);
794
816
  const tableDefinition = generateTableDefinition(modelName, tableName, fields, dialect, options);
795
- const newContent = cleanedContent.trimEnd() + "\n" + enumDefinitions + "\n" + tableDefinition + "\n";
817
+ const newContent = updatedContent.trimEnd() + "\n" + enumDefinitions + "\n" + tableDefinition + "\n";
796
818
  writeFile(schemaPath, newContent, { force: true, dryRun: options.dryRun });
797
819
  }
798
820
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brizzle",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "Rails-like generators for Next.js + Drizzle ORM projects",
5
5
  "type": "module",
6
6
  "bin": {