@type32/tauri-sqlite-orm 0.2.10 → 0.2.11
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.mts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +79 -0
- package/dist/index.mjs +79 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -286,8 +286,21 @@ declare class TauriORM {
|
|
|
286
286
|
private tables;
|
|
287
287
|
constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
|
|
288
288
|
private buildColumnDefinition;
|
|
289
|
+
checkMigration(): Promise<{
|
|
290
|
+
safe: boolean;
|
|
291
|
+
changes: {
|
|
292
|
+
tablesToCreate: string[];
|
|
293
|
+
tablesToRecreate: string[];
|
|
294
|
+
tablesToDrop: string[];
|
|
295
|
+
columnsToAdd: Array<{
|
|
296
|
+
table: string;
|
|
297
|
+
column: string;
|
|
298
|
+
}>;
|
|
299
|
+
};
|
|
300
|
+
}>;
|
|
289
301
|
migrate(options?: {
|
|
290
302
|
performDestructiveActions?: boolean;
|
|
303
|
+
dryRun?: boolean;
|
|
291
304
|
}): Promise<void>;
|
|
292
305
|
private canAddColumnWithAlter;
|
|
293
306
|
private hasColumnDefinitionChanged;
|
package/dist/index.d.ts
CHANGED
|
@@ -286,8 +286,21 @@ declare class TauriORM {
|
|
|
286
286
|
private tables;
|
|
287
287
|
constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
|
|
288
288
|
private buildColumnDefinition;
|
|
289
|
+
checkMigration(): Promise<{
|
|
290
|
+
safe: boolean;
|
|
291
|
+
changes: {
|
|
292
|
+
tablesToCreate: string[];
|
|
293
|
+
tablesToRecreate: string[];
|
|
294
|
+
tablesToDrop: string[];
|
|
295
|
+
columnsToAdd: Array<{
|
|
296
|
+
table: string;
|
|
297
|
+
column: string;
|
|
298
|
+
}>;
|
|
299
|
+
};
|
|
300
|
+
}>;
|
|
289
301
|
migrate(options?: {
|
|
290
302
|
performDestructiveActions?: boolean;
|
|
303
|
+
dryRun?: boolean;
|
|
291
304
|
}): Promise<void>;
|
|
292
305
|
private canAddColumnWithAlter;
|
|
293
306
|
private hasColumnDefinitionChanged;
|
package/dist/index.js
CHANGED
|
@@ -1258,7 +1258,86 @@ var TauriORM = class {
|
|
|
1258
1258
|
}
|
|
1259
1259
|
return sql2;
|
|
1260
1260
|
}
|
|
1261
|
+
async checkMigration() {
|
|
1262
|
+
const dbTables = await this.db.select(
|
|
1263
|
+
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`
|
|
1264
|
+
);
|
|
1265
|
+
const dbTableNames = new Set(dbTables.map((t) => t.name));
|
|
1266
|
+
const schemaTableNames = new Set(Array.from(this.tables.keys()));
|
|
1267
|
+
const changes = {
|
|
1268
|
+
tablesToCreate: [],
|
|
1269
|
+
tablesToRecreate: [],
|
|
1270
|
+
tablesToDrop: [],
|
|
1271
|
+
columnsToAdd: []
|
|
1272
|
+
};
|
|
1273
|
+
for (const tableName of schemaTableNames) {
|
|
1274
|
+
if (!dbTableNames.has(tableName)) {
|
|
1275
|
+
changes.tablesToCreate.push(tableName);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
for (const tableName of dbTableNames) {
|
|
1279
|
+
if (!schemaTableNames.has(tableName)) {
|
|
1280
|
+
changes.tablesToDrop.push(tableName);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
for (const table of this.tables.values()) {
|
|
1284
|
+
const tableName = table._.name;
|
|
1285
|
+
if (!dbTableNames.has(tableName)) continue;
|
|
1286
|
+
const existingTableInfo = await this.db.select(`PRAGMA table_info('${tableName}')`);
|
|
1287
|
+
const existingIndexes = await this.db.select(`PRAGMA index_list('${tableName}')`);
|
|
1288
|
+
const uniqueColumns = /* @__PURE__ */ new Set();
|
|
1289
|
+
for (const index of existingIndexes) {
|
|
1290
|
+
if (index.unique === 1 && index.origin === "u") {
|
|
1291
|
+
const indexInfo = await this.db.select(`PRAGMA index_info('${index.name}')`);
|
|
1292
|
+
if (indexInfo.length === 1) {
|
|
1293
|
+
uniqueColumns.add(indexInfo[0].name);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
const existingColumns = new Map(existingTableInfo.map((c) => [c.name, c]));
|
|
1298
|
+
const schemaColumns = table._.columns;
|
|
1299
|
+
let needsRecreate = false;
|
|
1300
|
+
for (const [colName, column] of Object.entries(schemaColumns)) {
|
|
1301
|
+
const existing = existingColumns.get(colName);
|
|
1302
|
+
if (!existing) {
|
|
1303
|
+
if (!this.canAddColumnWithAlter(column)) {
|
|
1304
|
+
needsRecreate = true;
|
|
1305
|
+
break;
|
|
1306
|
+
}
|
|
1307
|
+
changes.columnsToAdd.push({ table: tableName, column: colName });
|
|
1308
|
+
} else {
|
|
1309
|
+
const hasUniqueInDB = uniqueColumns.has(colName);
|
|
1310
|
+
const wantsUnique = !!column.options.unique;
|
|
1311
|
+
if (hasUniqueInDB !== wantsUnique || this.hasColumnDefinitionChanged(column, existing)) {
|
|
1312
|
+
needsRecreate = true;
|
|
1313
|
+
break;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
for (const existingCol of existingColumns.keys()) {
|
|
1318
|
+
if (!schemaColumns[existingCol]) {
|
|
1319
|
+
needsRecreate = true;
|
|
1320
|
+
break;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
if (needsRecreate) {
|
|
1324
|
+
changes.tablesToRecreate.push(tableName);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
const safe = changes.tablesToRecreate.length === 0 && changes.tablesToDrop.length === 0;
|
|
1328
|
+
return { safe, changes };
|
|
1329
|
+
}
|
|
1261
1330
|
async migrate(options) {
|
|
1331
|
+
if (options?.dryRun) {
|
|
1332
|
+
const check = await this.checkMigration();
|
|
1333
|
+
console.log("[Tauri-ORM] Migration Preview (Dry Run):");
|
|
1334
|
+
console.log(" Tables to create:", check.changes.tablesToCreate);
|
|
1335
|
+
console.log(" Tables to recreate (DESTRUCTIVE):", check.changes.tablesToRecreate);
|
|
1336
|
+
console.log(" Tables to drop:", check.changes.tablesToDrop);
|
|
1337
|
+
console.log(" Columns to add:", check.changes.columnsToAdd);
|
|
1338
|
+
console.log(" Safe migration:", check.safe);
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1262
1341
|
const dbTables = await this.db.select(
|
|
1263
1342
|
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`
|
|
1264
1343
|
);
|
package/dist/index.mjs
CHANGED
|
@@ -1159,7 +1159,86 @@ var TauriORM = class {
|
|
|
1159
1159
|
}
|
|
1160
1160
|
return sql2;
|
|
1161
1161
|
}
|
|
1162
|
+
async checkMigration() {
|
|
1163
|
+
const dbTables = await this.db.select(
|
|
1164
|
+
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`
|
|
1165
|
+
);
|
|
1166
|
+
const dbTableNames = new Set(dbTables.map((t) => t.name));
|
|
1167
|
+
const schemaTableNames = new Set(Array.from(this.tables.keys()));
|
|
1168
|
+
const changes = {
|
|
1169
|
+
tablesToCreate: [],
|
|
1170
|
+
tablesToRecreate: [],
|
|
1171
|
+
tablesToDrop: [],
|
|
1172
|
+
columnsToAdd: []
|
|
1173
|
+
};
|
|
1174
|
+
for (const tableName of schemaTableNames) {
|
|
1175
|
+
if (!dbTableNames.has(tableName)) {
|
|
1176
|
+
changes.tablesToCreate.push(tableName);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
for (const tableName of dbTableNames) {
|
|
1180
|
+
if (!schemaTableNames.has(tableName)) {
|
|
1181
|
+
changes.tablesToDrop.push(tableName);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
for (const table of this.tables.values()) {
|
|
1185
|
+
const tableName = table._.name;
|
|
1186
|
+
if (!dbTableNames.has(tableName)) continue;
|
|
1187
|
+
const existingTableInfo = await this.db.select(`PRAGMA table_info('${tableName}')`);
|
|
1188
|
+
const existingIndexes = await this.db.select(`PRAGMA index_list('${tableName}')`);
|
|
1189
|
+
const uniqueColumns = /* @__PURE__ */ new Set();
|
|
1190
|
+
for (const index of existingIndexes) {
|
|
1191
|
+
if (index.unique === 1 && index.origin === "u") {
|
|
1192
|
+
const indexInfo = await this.db.select(`PRAGMA index_info('${index.name}')`);
|
|
1193
|
+
if (indexInfo.length === 1) {
|
|
1194
|
+
uniqueColumns.add(indexInfo[0].name);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
const existingColumns = new Map(existingTableInfo.map((c) => [c.name, c]));
|
|
1199
|
+
const schemaColumns = table._.columns;
|
|
1200
|
+
let needsRecreate = false;
|
|
1201
|
+
for (const [colName, column] of Object.entries(schemaColumns)) {
|
|
1202
|
+
const existing = existingColumns.get(colName);
|
|
1203
|
+
if (!existing) {
|
|
1204
|
+
if (!this.canAddColumnWithAlter(column)) {
|
|
1205
|
+
needsRecreate = true;
|
|
1206
|
+
break;
|
|
1207
|
+
}
|
|
1208
|
+
changes.columnsToAdd.push({ table: tableName, column: colName });
|
|
1209
|
+
} else {
|
|
1210
|
+
const hasUniqueInDB = uniqueColumns.has(colName);
|
|
1211
|
+
const wantsUnique = !!column.options.unique;
|
|
1212
|
+
if (hasUniqueInDB !== wantsUnique || this.hasColumnDefinitionChanged(column, existing)) {
|
|
1213
|
+
needsRecreate = true;
|
|
1214
|
+
break;
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
for (const existingCol of existingColumns.keys()) {
|
|
1219
|
+
if (!schemaColumns[existingCol]) {
|
|
1220
|
+
needsRecreate = true;
|
|
1221
|
+
break;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
if (needsRecreate) {
|
|
1225
|
+
changes.tablesToRecreate.push(tableName);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
const safe = changes.tablesToRecreate.length === 0 && changes.tablesToDrop.length === 0;
|
|
1229
|
+
return { safe, changes };
|
|
1230
|
+
}
|
|
1162
1231
|
async migrate(options) {
|
|
1232
|
+
if (options?.dryRun) {
|
|
1233
|
+
const check = await this.checkMigration();
|
|
1234
|
+
console.log("[Tauri-ORM] Migration Preview (Dry Run):");
|
|
1235
|
+
console.log(" Tables to create:", check.changes.tablesToCreate);
|
|
1236
|
+
console.log(" Tables to recreate (DESTRUCTIVE):", check.changes.tablesToRecreate);
|
|
1237
|
+
console.log(" Tables to drop:", check.changes.tablesToDrop);
|
|
1238
|
+
console.log(" Columns to add:", check.changes.columnsToAdd);
|
|
1239
|
+
console.log(" Safe migration:", check.safe);
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1163
1242
|
const dbTables = await this.db.select(
|
|
1164
1243
|
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`
|
|
1165
1244
|
);
|