@conorroberts/utils 0.0.102 → 0.0.103
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/db/migrate.d.mts +9 -2
- package/dist/db/migrate.mjs +9 -2
- package/dist/db/migrate.mjs.map +1 -1
- package/package.json +1 -1
package/dist/db/migrate.d.mts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
import * as drizzle_orm0 from "drizzle-orm";
|
|
1
2
|
import { AnyMySqlTable } from "drizzle-orm/mysql-core";
|
|
3
|
+
import * as node_modules_remeda_dist_ToString_DKW_jYvM0 from "node_modules/remeda/dist/ToString-DKW-jYvM";
|
|
2
4
|
|
|
3
|
-
//#region src/db/
|
|
5
|
+
//#region src/db/types.d.ts
|
|
4
6
|
type DrizzleSchema = Record<string, AnyMySqlTable>;
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/db/filterTables.d.ts
|
|
9
|
+
declare const filterTables: <TSchema extends Record<string, AnyMySqlTable | unknown>>(schema: TSchema) => (string extends node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> ? Record<node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> & string, Required<TSchema>[Exclude<keyof TSchema, symbol>] & drizzle_orm0.Table<drizzle_orm0.TableConfig<drizzle_orm0.Column<any, object, object>>>> : number extends node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> ? Record<node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> & number, (readonly [node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>>, Required<TSchema>[Exclude<keyof TSchema, symbol>] & drizzle_orm0.Table<drizzle_orm0.TableConfig<drizzle_orm0.Column<any, object, object>>>])[][node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> & number][1]> : symbol extends node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> ? Record<node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>> & symbol, Required<TSchema>[Exclude<keyof TSchema, symbol>] & drizzle_orm0.Table<drizzle_orm0.TableConfig<drizzle_orm0.Column<any, object, object>>>> : { [P in node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>>]?: (Extract<readonly [node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>>, Required<TSchema>[Exclude<keyof TSchema, symbol>] & drizzle_orm0.Table<drizzle_orm0.TableConfig<drizzle_orm0.Column<any, object, object>>>], readonly [key: P, value: unknown]> extends never ? readonly [node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>>, Required<TSchema>[Exclude<keyof TSchema, symbol>] & drizzle_orm0.Table<drizzle_orm0.TableConfig<drizzle_orm0.Column<any, object, object>>>] : Extract<readonly [node_modules_remeda_dist_ToString_DKW_jYvM0.ToString<Exclude<keyof TSchema, symbol>>, Required<TSchema>[Exclude<keyof TSchema, symbol>] & drizzle_orm0.Table<drizzle_orm0.TableConfig<drizzle_orm0.Column<any, object, object>>>], readonly [key: P, value: unknown]>)[1] | undefined }) extends infer T ? { [KeyType in keyof T]: T[KeyType] } : never;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/db/migrate.d.ts
|
|
5
12
|
type SchemaTableName<TSchema extends DrizzleSchema> = TSchema[keyof TSchema]["_"]["name"];
|
|
6
13
|
interface MigrateConnectionOptions {
|
|
7
14
|
migrationsFolder: string;
|
|
@@ -23,5 +30,5 @@ type StandardMigrateOptions<TSchema extends DrizzleSchema> = MigrateSharedOption
|
|
|
23
30
|
type MigrateOptions<TSchema extends DrizzleSchema = DrizzleSchema> = CleanMigrateOptions<TSchema> | StandardMigrateOptions<TSchema>;
|
|
24
31
|
declare const runMigrations: <TSchema extends DrizzleSchema>(options: MigrateOptions<TSchema>) => Promise<void>;
|
|
25
32
|
//#endregion
|
|
26
|
-
export { type DrizzleSchema, type MigrateOptions, type SchemaTableName, runMigrations };
|
|
33
|
+
export { type DrizzleSchema, type MigrateOptions, type SchemaTableName, filterTables, runMigrations };
|
|
27
34
|
//# sourceMappingURL=migrate.d.mts.map
|
package/dist/db/migrate.mjs
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
import * as R from "remeda";
|
|
1
2
|
import { consola } from "consola";
|
|
3
|
+
import { isTable, sql } from "drizzle-orm";
|
|
2
4
|
import { DrizzleQueryError } from "drizzle-orm/errors";
|
|
3
|
-
import { sql } from "drizzle-orm";
|
|
4
5
|
import { drizzle } from "drizzle-orm/planetscale-serverless";
|
|
5
6
|
import { migrate } from "drizzle-orm/planetscale-serverless/migrator";
|
|
6
7
|
import { getTableName } from "drizzle-orm/table";
|
|
7
8
|
import { resolve } from "node:path";
|
|
8
9
|
|
|
10
|
+
//#region src/db/filterTables.ts
|
|
11
|
+
const filterTables = (schema) => {
|
|
12
|
+
return R.pipe(schema, R.entries(), R.map(([name, t]) => isTable(t) ? [name, t] : null), R.filter(R.isNonNull), R.fromEntries());
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
9
16
|
//#region src/db/migrate.ts
|
|
10
17
|
const escapeIdentifier = (value) => {
|
|
11
18
|
return `\`${value.replaceAll("`", "``")}\``;
|
|
@@ -113,5 +120,5 @@ const runMigrations = async (options) => {
|
|
|
113
120
|
};
|
|
114
121
|
|
|
115
122
|
//#endregion
|
|
116
|
-
export { runMigrations };
|
|
123
|
+
export { filterTables, runMigrations };
|
|
117
124
|
//# sourceMappingURL=migrate.mjs.map
|
package/dist/db/migrate.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.mjs","names":["ordered: TTableName[]","tableNames: Set<SchemaTableName<TSchema>>","results: DeleteResult[]"],"sources":["../../src/db/migrate.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"migrate.mjs","names":["ordered: TTableName[]","tableNames: Set<SchemaTableName<TSchema>>","results: DeleteResult[]"],"sources":["../../src/db/filterTables.ts","../../src/db/migrate.ts"],"sourcesContent":["import { isTable } from \"drizzle-orm\";\r\nimport type { AnyMySqlTable } from \"drizzle-orm/mysql-core\";\r\nimport * as R from \"remeda\";\r\n\r\nexport const filterTables = <TSchema extends Record<string, AnyMySqlTable | unknown>>(schema: TSchema) => {\r\n return R.pipe(\r\n schema,\r\n R.entries(),\r\n R.map(([name, t]) => (isTable(t) ? ([name, t] as const) : null)),\r\n R.filter(R.isNonNull),\r\n R.fromEntries(),\r\n );\r\n};\r\n","import { consola } from \"consola\";\nimport { sql } from \"drizzle-orm\";\nimport { DrizzleQueryError } from \"drizzle-orm/errors\";\nimport { drizzle } from \"drizzle-orm/planetscale-serverless\";\nimport { migrate } from \"drizzle-orm/planetscale-serverless/migrator\";\nimport { getTableName } from \"drizzle-orm/table\";\nimport { resolve } from \"node:path\";\nimport type { DrizzleSchema } from \"./types\";\n\ntype SchemaTableName<TSchema extends DrizzleSchema> = TSchema[keyof TSchema][\"_\"][\"name\"];\n\ninterface MigrateConnectionOptions {\n migrationsFolder: string;\n host: string;\n username: string;\n password: string;\n}\n\ninterface MigrateSharedOptions<TSchema extends DrizzleSchema> extends MigrateConnectionOptions {\n deleteOrder?: SchemaTableName<TSchema>[];\n}\n\ntype CleanMigrateOptions<TSchema extends DrizzleSchema> = MigrateSharedOptions<TSchema> & {\n clean: true;\n schema: TSchema;\n};\n\ntype StandardMigrateOptions<TSchema extends DrizzleSchema> = MigrateSharedOptions<TSchema> & {\n clean?: false;\n schema?: TSchema;\n};\n\ntype MigrateOptions<TSchema extends DrizzleSchema = DrizzleSchema> =\n | CleanMigrateOptions<TSchema>\n | StandardMigrateOptions<TSchema>;\n\ninterface DeleteResult {\n table: string;\n rowsDeleted: number;\n success: boolean;\n error?: string;\n}\n\nconst escapeIdentifier = (value: string): string => {\n return `\\`${value.replaceAll(\"`\", \"``\")}\\``;\n};\n\nconst parseError = (error: unknown): string => {\n if (error instanceof DrizzleQueryError) {\n if (error.cause instanceof Error) {\n return error.cause.message;\n }\n\n return error.message;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n return String(error);\n};\n\nconst buildDeleteOrder = <TTableName extends string>(tableNames: TTableName[], preferredOrder: TTableName[]) => {\n const seen = new Set<string>();\n const ordered: TTableName[] = [];\n\n for (const tableName of preferredOrder) {\n if (seen.has(tableName)) {\n continue;\n }\n\n seen.add(tableName);\n\n if (tableNames.includes(tableName)) {\n ordered.push(tableName);\n }\n }\n\n for (const tableName of tableNames) {\n if (!seen.has(tableName)) {\n ordered.push(tableName);\n }\n }\n\n return ordered;\n};\n\nconst listSchemaTables = <TSchema extends DrizzleSchema>(schema: TSchema): SchemaTableName<TSchema>[] => {\n const tableNames: Set<SchemaTableName<TSchema>> = new Set();\n const tables = Object.values(schema) as TSchema[keyof TSchema][];\n\n for (const table of tables) {\n const tableName = getTableName(table) as SchemaTableName<TSchema>;\n\n tableNames.add(tableName);\n }\n\n return Array.from(tableNames);\n};\n\nconst deleteTableRows = async (db: ReturnType<typeof drizzle>, tableNames: string[]): Promise<DeleteResult[]> => {\n const results: DeleteResult[] = [];\n\n for (const tableName of tableNames) {\n try {\n const rowsDeleted = await db.$count(sql.raw(tableName));\n\n await db.execute(sql.raw(`DELETE FROM ${tableName}`));\n\n results.push({\n table: tableName,\n rowsDeleted,\n success: true,\n });\n\n const status = rowsDeleted > 0 ? `[OK] Deleted ${rowsDeleted} rows` : \"[SKIP] Empty\";\n consola.log(`${status.padEnd(30)} from ${tableName}`);\n } catch (error) {\n const errorDetails = parseError(error) || String(error);\n\n results.push({\n table: tableName,\n rowsDeleted: 0,\n success: false,\n error: errorDetails,\n });\n\n consola.error(`[FAIL] Failed to delete from ${tableName}: ${errorDetails}`);\n throw error;\n }\n }\n\n return results;\n};\n\nconst dropTables = async (db: ReturnType<typeof drizzle>, tableNames: string[]): Promise<void> => {\n for (const tableName of tableNames) {\n try {\n await db.execute(sql.raw(`DROP TABLE IF EXISTS ${escapeIdentifier(tableName)}`));\n consola.log(`[OK] Dropped table ${tableName}`);\n } catch (error) {\n const errorDetails = parseError(error) || String(error);\n consola.error(`[FAIL] Failed to drop ${tableName}: ${errorDetails}`);\n throw new Error(`Cannot proceed: failed to drop ${tableName}`);\n }\n }\n};\n\nconst cleanDatabase = async (\n db: ReturnType<typeof drizzle>,\n options: CleanMigrateOptions<DrizzleSchema>,\n): Promise<void> => {\n if (process.env.UNSAFE_CONFIRM_DELETE !== \"true\") {\n consola.error('Refusing to clean database. Set UNSAFE_CONFIRM_DELETE=\"true\" to continue.');\n process.exit(1);\n }\n\n const tableNames = listSchemaTables(options.schema);\n\n if (tableNames.length === 0) {\n consola.log(\"Clean requested, but no tables were found in the provided Drizzle schema.\");\n return;\n }\n\n const ordered = buildDeleteOrder(tableNames, options.deleteOrder ?? []);\n\n consola.warn(\"WARNING: This will delete and drop ALL tables from the database.\");\n consola.log(`Database host: ${db.$client.config.host}`);\n\n consola.log(\"\");\n consola.log(\"Deleting table rows in order...\\n\");\n\n const results = await deleteTableRows(db, ordered);\n\n consola.log(\"\\nDropping tables in order...\\n\");\n await dropTables(db, ordered);\n\n const successCount = results.filter((result) => result.success).length;\n const totalRowsDeleted = results.reduce((sum, result) => sum + result.rowsDeleted, 0);\n\n consola.log(\"\");\n consola.log(\"=\".repeat(60));\n consola.log(\"SUMMARY\");\n consola.log(\"=\".repeat(60));\n consola.log(`Deletion phase: ${successCount} tables processed, ${totalRowsDeleted} rows deleted`);\n consola.success(\"Database is now empty and ready for migrations.\");\n};\n\nconst runMigrations = async <TSchema extends DrizzleSchema>(options: MigrateOptions<TSchema>) => {\n const db = drizzle({\n connection: {\n host: options.host,\n username: options.username,\n password: options.password,\n },\n });\n\n if (options.clean) {\n await cleanDatabase(db, options);\n }\n\n consola.log(\"Running database migrations...\");\n await migrate(db, { migrationsFolder: resolve(options.migrationsFolder) });\n consola.log(\"[OK] Migrations completed successfully!\");\n};\n\nexport * from \"./filterTables\";\nexport { runMigrations };\nexport type { DrizzleSchema, MigrateOptions, SchemaTableName };\n"],"mappings":";;;;;;;;;;AAIA,MAAa,gBAAyE,WAAoB;AACxG,QAAO,EAAE,KACP,QACA,EAAE,SAAS,EACX,EAAE,KAAK,CAAC,MAAM,OAAQ,QAAQ,EAAE,GAAI,CAAC,MAAM,EAAE,GAAa,KAAM,EAChE,EAAE,OAAO,EAAE,UAAU,EACrB,EAAE,aAAa,CAChB;;;;;ACgCH,MAAM,oBAAoB,UAA0B;AAClD,QAAO,KAAK,MAAM,WAAW,KAAK,KAAK,CAAC;;AAG1C,MAAM,cAAc,UAA2B;AAC7C,KAAI,iBAAiB,mBAAmB;AACtC,MAAI,MAAM,iBAAiB,MACzB,QAAO,MAAM,MAAM;AAGrB,SAAO,MAAM;;AAGf,KAAI,iBAAiB,MACnB,QAAO,MAAM;AAGf,QAAO,OAAO,MAAM;;AAGtB,MAAM,oBAA+C,YAA0B,mBAAiC;CAC9G,MAAM,uBAAO,IAAI,KAAa;CAC9B,MAAMA,UAAwB,EAAE;AAEhC,MAAK,MAAM,aAAa,gBAAgB;AACtC,MAAI,KAAK,IAAI,UAAU,CACrB;AAGF,OAAK,IAAI,UAAU;AAEnB,MAAI,WAAW,SAAS,UAAU,CAChC,SAAQ,KAAK,UAAU;;AAI3B,MAAK,MAAM,aAAa,WACtB,KAAI,CAAC,KAAK,IAAI,UAAU,CACtB,SAAQ,KAAK,UAAU;AAI3B,QAAO;;AAGT,MAAM,oBAAmD,WAAgD;CACvG,MAAMC,6BAA4C,IAAI,KAAK;CAC3D,MAAM,SAAS,OAAO,OAAO,OAAO;AAEpC,MAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,YAAY,aAAa,MAAM;AAErC,aAAW,IAAI,UAAU;;AAG3B,QAAO,MAAM,KAAK,WAAW;;AAG/B,MAAM,kBAAkB,OAAO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