@inflector/optima 1.1.0 → 1.1.1

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 +40 -17
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -785,7 +785,7 @@ var OptimaDB = class _OptimaDB {
785
785
  this.Tables = tables;
786
786
  }
787
787
  /**
788
- * Initializes the database, connects, detects schema changes,
788
+ * Initializes the database, connects, detects schema changes,
789
789
  * runs migrations, and returns the DB instance.
790
790
  */
791
791
  static async Open(name = ":memory:", schema, options) {
@@ -799,7 +799,10 @@ var OptimaDB = class _OptimaDB {
799
799
  }
800
800
  const existingTables = await (await connection.run(`SELECT * from duckdb_tables`)).getRowObjects();
801
801
  const cleanSchema = SchemaMigrator.transformSchema(schema);
802
- const migrationSQL = SchemaMigrator.generateSQL(existingTables, cleanSchema);
802
+ const migrationSQL = SchemaMigrator.generateSQL(
803
+ existingTables,
804
+ cleanSchema
805
+ );
803
806
  if (migrationSQL.length > 0) {
804
807
  await connection.run("BEGIN TRANSACTION;");
805
808
  try {
@@ -842,7 +845,7 @@ var OptimaDB = class _OptimaDB {
842
845
  this.instance.closeSync();
843
846
  }
844
847
  /**
845
- * Runs a function within a transaction.
848
+ * Runs a function within a transaction.
846
849
  * Re-throws error after rollback so caller handles logic failure.
847
850
  */
848
851
  async transaction(fn) {
@@ -893,12 +896,14 @@ var SchemaMigrator = class {
893
896
  const steps = [];
894
897
  for (const [tableName, fields] of Object.entries(desiredSchema)) {
895
898
  if (!currentSchema[tableName]) {
896
- const fieldDefs = Object.entries(fields).map(([colName, config]) => {
897
- let def = `"${colName}" ${config.STRUCTType ? config.STRUCTType : config.SQlType}`;
898
- if (config.primaryKey) def += " PRIMARY KEY";
899
- if (config.notnull) def += " NOT NULL";
900
- return def;
901
- });
899
+ const fieldDefs = Object.entries(fields).map(
900
+ ([colName, config]) => {
901
+ let def = `"${colName}" ${config.STRUCTType ? config.STRUCTType : config.SQlType}`;
902
+ if (config.primaryKey) def += " PRIMARY KEY";
903
+ if (config.notnull) def += " NOT NULL";
904
+ return def;
905
+ }
906
+ );
902
907
  steps.push(`CREATE TABLE ${tableName} (${fieldDefs.join(", ")});`);
903
908
  continue;
904
909
  }
@@ -909,24 +914,40 @@ var SchemaMigrator = class {
909
914
  if (config.notnull) {
910
915
  def += ` DEFAULT ${this.getDefaultValueForType(config.SQlType)}`;
911
916
  }
912
- steps.push(`ALTER TABLE ${tableName} ADD COLUMN "${colName}" ${def};`);
917
+ steps.push(
918
+ `ALTER TABLE ${tableName} ADD COLUMN "${colName}" ${def};`
919
+ );
913
920
  if (config.notnull) {
914
- steps.push(`ALTER TABLE ${tableName} ALTER "${colName}" SET NOT NULL;`);
921
+ steps.push(
922
+ `ALTER TABLE ${tableName} ALTER "${colName}" SET NOT NULL;`
923
+ );
915
924
  }
916
925
  continue;
917
926
  }
918
927
  const currentCol = currentTableCols[colName];
919
928
  const normCurrentType = currentCol.type.replace(/\s+/g, "").toUpperCase();
920
929
  const normNewType = config.SQlType.replace(/\s+/g, "").toUpperCase();
921
- if (normCurrentType !== normNewType && !normNewType.startsWith("STRUCT")) {
922
- steps.push(`ALTER TABLE ${tableName} ALTER "${colName}" TYPE ${config.SQlType};`);
930
+ if (normCurrentType !== normNewType) {
931
+ if (normNewType.startsWith("STRUCT") || normNewType.startsWith("LIST")) {
932
+ steps.push(
933
+ `ALTER TABLE ${tableName} ALTER "${colName}" TYPE ${config.STRUCTType};`
934
+ );
935
+ } else {
936
+ steps.push(
937
+ `ALTER TABLE ${tableName} ALTER "${colName}" TYPE ${config.SQlType};`
938
+ );
939
+ }
923
940
  }
924
941
  const desiredNotNull = !!config.notnull;
925
942
  if (desiredNotNull !== currentCol.isNotNull) {
926
943
  if (desiredNotNull) {
927
- steps.push(`ALTER TABLE ${tableName} ALTER "${colName}" SET NOT NULL;`);
944
+ steps.push(
945
+ `ALTER TABLE ${tableName} ALTER "${colName}" SET NOT NULL;`
946
+ );
928
947
  } else {
929
- steps.push(`ALTER TABLE ${tableName} ALTER "${colName}" DROP NOT NULL;`);
948
+ steps.push(
949
+ `ALTER TABLE ${tableName} ALTER "${colName}" DROP NOT NULL;`
950
+ );
930
951
  }
931
952
  }
932
953
  }
@@ -985,9 +1006,11 @@ var SchemaMigrator = class {
985
1006
  */
986
1007
  static getDefaultValueForType(type) {
987
1008
  const t = type.toUpperCase();
988
- if (t.includes("INT") || t.includes("FLOAT") || t.includes("DECIMAL") || t.includes("DOUBLE")) return "0";
1009
+ if (t.includes("INT") || t.includes("FLOAT") || t.includes("DECIMAL") || t.includes("DOUBLE"))
1010
+ return "0";
989
1011
  if (t.includes("BOOL")) return "FALSE";
990
- if (t.includes("STRUCT") || t.includes("MAP") || t.includes("LIST")) return "NULL";
1012
+ if (t.includes("STRUCT") || t.includes("MAP") || t.includes("LIST"))
1013
+ return "NULL";
991
1014
  return "''";
992
1015
  }
993
1016
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflector/optima",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "author": "Inflector",
6
6
  "main": "./dist/index.js",