@lobomfz/db 0.5.0 → 0.5.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/README.md +4 -1
- package/package.json +1 -1
- package/src/migration/diff.ts +14 -2
- package/src/migration/introspect.ts +4 -0
- package/src/migration/types.ts +1 -0
- package/src/schema-compiler.ts +2 -1
- package/src/types.ts +1 -0
package/README.md
CHANGED
|
@@ -36,7 +36,10 @@ const db = await Database.open({
|
|
|
36
36
|
}),
|
|
37
37
|
},
|
|
38
38
|
indexes: {
|
|
39
|
-
posts: [
|
|
39
|
+
posts: [
|
|
40
|
+
{ columns: ["user_id", "status"] },
|
|
41
|
+
{ columns: ["title"], unique: true, where: "status = 'published'" },
|
|
42
|
+
],
|
|
40
43
|
},
|
|
41
44
|
},
|
|
42
45
|
pragmas: {
|
package/package.json
CHANGED
package/src/migration/diff.ts
CHANGED
|
@@ -274,11 +274,23 @@ export class Differ {
|
|
|
274
274
|
continue;
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
const existingNames = new Set(existingTable.indexes.map((i) => i.name));
|
|
278
277
|
const desiredNames = new Set(tableIndexes.map((i) => i.name));
|
|
279
278
|
|
|
280
279
|
for (const idx of tableIndexes) {
|
|
281
|
-
|
|
280
|
+
const existingIndex = existingTable.indexes.find((existing) => existing.name === idx.name);
|
|
281
|
+
|
|
282
|
+
if (!existingIndex) {
|
|
283
|
+
this.ops.push({
|
|
284
|
+
type: "CreateIndex",
|
|
285
|
+
table: table.name,
|
|
286
|
+
columns: idx.columns,
|
|
287
|
+
sql: idx.sql,
|
|
288
|
+
});
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (normalizeSql(existingIndex.sql) !== normalizeSql(idx.sql)) {
|
|
293
|
+
this.ops.push({ type: "DropIndex", index: idx.name });
|
|
282
294
|
this.ops.push({
|
|
283
295
|
type: "CreateIndex",
|
|
284
296
|
table: table.name,
|
|
@@ -220,11 +220,15 @@ export class Introspector {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
const idxCols = this.db.prepare(`PRAGMA index_info("${idx.name}")`).all() as IndexInfoRow[];
|
|
223
|
+
const master = this.db
|
|
224
|
+
.prepare("SELECT name, sql FROM sqlite_master WHERE type = 'index' AND name = ?")
|
|
225
|
+
.get(idx.name) as MasterRow;
|
|
223
226
|
|
|
224
227
|
indexes.push({
|
|
225
228
|
name: idx.name,
|
|
226
229
|
columns: idxCols.map((c) => c.name),
|
|
227
230
|
unique: idx.unique === 1,
|
|
231
|
+
sql: master.sql!,
|
|
228
232
|
});
|
|
229
233
|
}
|
|
230
234
|
|
package/src/migration/types.ts
CHANGED
package/src/schema-compiler.ts
CHANGED
|
@@ -306,8 +306,9 @@ export class SchemaCompiler {
|
|
|
306
306
|
const indexName = this.generateIndexName(tableName, indexDef.columns, indexDef.unique ?? false);
|
|
307
307
|
const unique = indexDef.unique ? "UNIQUE " : "";
|
|
308
308
|
const columns = indexDef.columns.map((c) => `"${c}"`).join(", ");
|
|
309
|
+
const where = indexDef.where ? ` WHERE ${indexDef.where}` : "";
|
|
309
310
|
|
|
310
|
-
return `CREATE ${unique}INDEX "${indexName}" ON "${tableName}" (${columns})`;
|
|
311
|
+
return `CREATE ${unique}INDEX "${indexName}" ON "${tableName}" (${columns})${where}`;
|
|
311
312
|
}
|
|
312
313
|
|
|
313
314
|
private buildTableInfo(props: Prop[]): FtsTableInfo {
|
package/src/types.ts
CHANGED
|
@@ -71,6 +71,7 @@ type VectorColumnName<T extends SchemaRecord, K extends keyof T> = {
|
|
|
71
71
|
export type IndexDefinition<Columns extends string = string> = {
|
|
72
72
|
columns: Columns[];
|
|
73
73
|
unique?: boolean;
|
|
74
|
+
where?: string;
|
|
74
75
|
};
|
|
75
76
|
|
|
76
77
|
export type IndexesConfig<T extends SchemaRecord> = {
|