@inflector/optima 1.1.0 → 1.1.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.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +45 -35
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -147,7 +147,7 @@ declare class OptimaTable<TDef extends Record<string, any> = any> {
|
|
|
147
147
|
*/
|
|
148
148
|
Subscribe(fn: (change: {
|
|
149
149
|
event: "Add" | "AddMany" | "Delete" | "Update";
|
|
150
|
-
data: Partial<Infer<TDef
|
|
150
|
+
data: Partial<Infer<TDef>> | Partial<Infer<TDef>>[];
|
|
151
151
|
time: Date;
|
|
152
152
|
}) => void): Unsubscribe;
|
|
153
153
|
private InitTable;
|
package/dist/index.js
CHANGED
|
@@ -626,24 +626,11 @@ ${valuesBlock}${isReturning ? "\nRETURNING *" : ""};`;
|
|
|
626
626
|
const { sql } = this.BuildUpdate(this.Name, record, data);
|
|
627
627
|
const Result = await (await this.Connection.run(sql)).getRowObjects();
|
|
628
628
|
if (Result.length != 0) {
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
});
|
|
635
|
-
} else {
|
|
636
|
-
const Updated = PotentialyUpdated;
|
|
637
|
-
const UpdatedPromises = Updated.map(
|
|
638
|
-
async (record2) => await this.GetOne().where(record2)
|
|
639
|
-
);
|
|
640
|
-
const UpdatedRecords = await Promise.all(UpdatedPromises);
|
|
641
|
-
this.notifyChange({
|
|
642
|
-
event: "Update",
|
|
643
|
-
data: UpdatedRecords,
|
|
644
|
-
time: /* @__PURE__ */ new Date()
|
|
645
|
-
});
|
|
646
|
-
}
|
|
629
|
+
this.notifyChange({
|
|
630
|
+
event: "Update",
|
|
631
|
+
data: data.returning ? this.FormatOut(Result) : void 0,
|
|
632
|
+
time: /* @__PURE__ */ new Date()
|
|
633
|
+
});
|
|
647
634
|
}
|
|
648
635
|
return data.returning ? this.FormatOut(Result) : this.SafeParse(Result);
|
|
649
636
|
}
|
|
@@ -785,7 +772,7 @@ var OptimaDB = class _OptimaDB {
|
|
|
785
772
|
this.Tables = tables;
|
|
786
773
|
}
|
|
787
774
|
/**
|
|
788
|
-
* Initializes the database, connects, detects schema changes,
|
|
775
|
+
* Initializes the database, connects, detects schema changes,
|
|
789
776
|
* runs migrations, and returns the DB instance.
|
|
790
777
|
*/
|
|
791
778
|
static async Open(name = ":memory:", schema, options) {
|
|
@@ -799,7 +786,10 @@ var OptimaDB = class _OptimaDB {
|
|
|
799
786
|
}
|
|
800
787
|
const existingTables = await (await connection.run(`SELECT * from duckdb_tables`)).getRowObjects();
|
|
801
788
|
const cleanSchema = SchemaMigrator.transformSchema(schema);
|
|
802
|
-
const migrationSQL = SchemaMigrator.generateSQL(
|
|
789
|
+
const migrationSQL = SchemaMigrator.generateSQL(
|
|
790
|
+
existingTables,
|
|
791
|
+
cleanSchema
|
|
792
|
+
);
|
|
803
793
|
if (migrationSQL.length > 0) {
|
|
804
794
|
await connection.run("BEGIN TRANSACTION;");
|
|
805
795
|
try {
|
|
@@ -842,7 +832,7 @@ var OptimaDB = class _OptimaDB {
|
|
|
842
832
|
this.instance.closeSync();
|
|
843
833
|
}
|
|
844
834
|
/**
|
|
845
|
-
* Runs a function within a transaction.
|
|
835
|
+
* Runs a function within a transaction.
|
|
846
836
|
* Re-throws error after rollback so caller handles logic failure.
|
|
847
837
|
*/
|
|
848
838
|
async transaction(fn) {
|
|
@@ -893,12 +883,14 @@ var SchemaMigrator = class {
|
|
|
893
883
|
const steps = [];
|
|
894
884
|
for (const [tableName, fields] of Object.entries(desiredSchema)) {
|
|
895
885
|
if (!currentSchema[tableName]) {
|
|
896
|
-
const fieldDefs = Object.entries(fields).map(
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
886
|
+
const fieldDefs = Object.entries(fields).map(
|
|
887
|
+
([colName, config]) => {
|
|
888
|
+
let def = `"${colName}" ${config.STRUCTType ? config.STRUCTType : config.SQlType}`;
|
|
889
|
+
if (config.primaryKey) def += " PRIMARY KEY";
|
|
890
|
+
if (config.notnull) def += " NOT NULL";
|
|
891
|
+
return def;
|
|
892
|
+
}
|
|
893
|
+
);
|
|
902
894
|
steps.push(`CREATE TABLE ${tableName} (${fieldDefs.join(", ")});`);
|
|
903
895
|
continue;
|
|
904
896
|
}
|
|
@@ -909,24 +901,40 @@ var SchemaMigrator = class {
|
|
|
909
901
|
if (config.notnull) {
|
|
910
902
|
def += ` DEFAULT ${this.getDefaultValueForType(config.SQlType)}`;
|
|
911
903
|
}
|
|
912
|
-
steps.push(
|
|
904
|
+
steps.push(
|
|
905
|
+
`ALTER TABLE ${tableName} ADD COLUMN "${colName}" ${def};`
|
|
906
|
+
);
|
|
913
907
|
if (config.notnull) {
|
|
914
|
-
steps.push(
|
|
908
|
+
steps.push(
|
|
909
|
+
`ALTER TABLE ${tableName} ALTER "${colName}" SET NOT NULL;`
|
|
910
|
+
);
|
|
915
911
|
}
|
|
916
912
|
continue;
|
|
917
913
|
}
|
|
918
914
|
const currentCol = currentTableCols[colName];
|
|
919
915
|
const normCurrentType = currentCol.type.replace(/\s+/g, "").toUpperCase();
|
|
920
916
|
const normNewType = config.SQlType.replace(/\s+/g, "").toUpperCase();
|
|
921
|
-
if (normCurrentType !== normNewType
|
|
922
|
-
|
|
917
|
+
if (normCurrentType !== normNewType) {
|
|
918
|
+
if (normNewType.startsWith("STRUCT") || normNewType.startsWith("LIST")) {
|
|
919
|
+
steps.push(
|
|
920
|
+
`ALTER TABLE ${tableName} ALTER "${colName}" TYPE ${config.STRUCTType};`
|
|
921
|
+
);
|
|
922
|
+
} else {
|
|
923
|
+
steps.push(
|
|
924
|
+
`ALTER TABLE ${tableName} ALTER "${colName}" TYPE ${config.SQlType};`
|
|
925
|
+
);
|
|
926
|
+
}
|
|
923
927
|
}
|
|
924
928
|
const desiredNotNull = !!config.notnull;
|
|
925
929
|
if (desiredNotNull !== currentCol.isNotNull) {
|
|
926
930
|
if (desiredNotNull) {
|
|
927
|
-
steps.push(
|
|
931
|
+
steps.push(
|
|
932
|
+
`ALTER TABLE ${tableName} ALTER "${colName}" SET NOT NULL;`
|
|
933
|
+
);
|
|
928
934
|
} else {
|
|
929
|
-
steps.push(
|
|
935
|
+
steps.push(
|
|
936
|
+
`ALTER TABLE ${tableName} ALTER "${colName}" DROP NOT NULL;`
|
|
937
|
+
);
|
|
930
938
|
}
|
|
931
939
|
}
|
|
932
940
|
}
|
|
@@ -985,9 +993,11 @@ var SchemaMigrator = class {
|
|
|
985
993
|
*/
|
|
986
994
|
static getDefaultValueForType(type) {
|
|
987
995
|
const t = type.toUpperCase();
|
|
988
|
-
if (t.includes("INT") || t.includes("FLOAT") || t.includes("DECIMAL") || t.includes("DOUBLE"))
|
|
996
|
+
if (t.includes("INT") || t.includes("FLOAT") || t.includes("DECIMAL") || t.includes("DOUBLE"))
|
|
997
|
+
return "0";
|
|
989
998
|
if (t.includes("BOOL")) return "FALSE";
|
|
990
|
-
if (t.includes("STRUCT") || t.includes("MAP") || t.includes("LIST"))
|
|
999
|
+
if (t.includes("STRUCT") || t.includes("MAP") || t.includes("LIST"))
|
|
1000
|
+
return "NULL";
|
|
991
1001
|
return "''";
|
|
992
1002
|
}
|
|
993
1003
|
};
|