@ooneex/migrations 0.0.1 → 0.4.0
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 +6 -3
- package/dist/index.js +12 -9
- package/dist/index.js.map +5 -5
- package/package.json +13 -7
- package/dist/ooneex-migrations-0.0.1.tgz +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -7,8 +7,11 @@ interface IMigration {
|
|
|
7
7
|
up: (tx: TransactionSQL, sql: SQL2) => Promise<void>;
|
|
8
8
|
down: (tx: TransactionSQL, sql: SQL2) => Promise<void>;
|
|
9
9
|
getVersion: () => string;
|
|
10
|
+
getDependencies: () => Promise<MigrationClassType[]> | MigrationClassType[];
|
|
10
11
|
}
|
|
11
|
-
declare const
|
|
12
|
+
declare const decorator: {
|
|
13
|
+
migration: (scope?: EContainerScope) => (target: MigrationClassType) => void;
|
|
14
|
+
};
|
|
12
15
|
/**
|
|
13
16
|
* Generates a migration version based on the current timestamp.
|
|
14
17
|
*
|
|
@@ -31,9 +34,9 @@ declare const generateMigrationVersion: () => string;
|
|
|
31
34
|
declare const getMigrations: () => IMigration[];
|
|
32
35
|
declare const migrationCreate: (config?: {
|
|
33
36
|
dir?: string;
|
|
34
|
-
}) => Promise<
|
|
37
|
+
}) => Promise<string>;
|
|
35
38
|
declare const migrationUp: (config?: {
|
|
36
39
|
databaseUrl?: string;
|
|
37
40
|
tableName?: string;
|
|
38
41
|
}) => Promise<void>;
|
|
39
|
-
export { migrationUp, migrationCreate,
|
|
42
|
+
export { migrationUp, migrationCreate, getMigrations, generateMigrationVersion, decorator, createMigrationTable, MigrationClassType, IMigration };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
2
|
+
var c=async(t,r)=>{await t.begin(async(o)=>{await o`CREATE TABLE IF NOT EXISTS ${t(r)} (id VARCHAR(20) PRIMARY KEY)`})};import{container as y,EContainerScope as I}from"@ooneex/container";var m=[];var T={migration:(t=I.Singleton)=>{return(r)=>{y.add(r,t),m.push(r)}}};var g=()=>{let t=new Date,r=t.getFullYear().toString(),o=(t.getMonth()+1).toString().padStart(2,"0"),i=t.getDate().toString().padStart(2,"0"),n=t.getHours().toString().padStart(2,"0"),e=t.getMinutes().toString().padStart(2,"0"),s=t.getSeconds().toString().padStart(2,"0"),a=t.getMilliseconds().toString().padStart(3,"0");return`${r}${o}${i}${n}${e}${s}${a}`};import{container as C}from"@ooneex/container";var M=()=>{return m.map((r)=>{return C.get(r)}).sort((r,o)=>Number(r.getVersion())-Number(o.getVersion()))};import{join as d}from"path";var{Glob:w}=globalThis.Bun;var u=`import { decorator, type IMigration, type MigrationClassType } from '@ooneex/migrations';
|
|
3
3
|
import type { TransactionSQL } from 'bun';
|
|
4
4
|
|
|
5
|
-
@migration()
|
|
5
|
+
@decorator.migration()
|
|
6
6
|
export class {{ name }} implements IMigration {
|
|
7
7
|
public async up(tx: TransactionSQL): Promise<void> {
|
|
8
8
|
// await tx\`...\`;
|
|
@@ -15,13 +15,16 @@ export class {{ name }} implements IMigration {
|
|
|
15
15
|
public getVersion(): string {
|
|
16
16
|
return '{{ version }}';
|
|
17
17
|
}
|
|
18
|
+
|
|
19
|
+
public getDependencies(): MigrationClassType[] {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
18
22
|
}
|
|
19
|
-
`;var
|
|
23
|
+
`;var A=async(t)=>{let r=g(),o=`Migration${r}`,i=t?.dir||"migrations";await Bun.write(d(process.cwd(),i,`${o}.ts`),u.replaceAll("{{ name }}",o).replaceAll("{{ version }}",r));let n=[],e=new w("**/Migration*.ts");for await(let s of e.scan(i)){let a=s.replace(/\.ts$/,"");n.push(`export { ${a} } from './${a}';`)}return await Bun.write(d(process.cwd(),i,"migrations.ts"),`${n.sort().join(`
|
|
20
24
|
`)}
|
|
21
|
-
`),
|
|
22
|
-
`)
|
|
23
|
-
`)
|
|
24
|
-
`)
|
|
25
|
-
`),i.error(p),process.exit(1)}}};export{R as migrationUp,C as migrationCreate,l as migration,d as getMigrations,M as generateMigrationVersion,g as createMigrationTable};
|
|
25
|
+
`),d(i,`${o}.ts`)};import{container as l}from"@ooneex/container";import{TerminalLogger as v}from"@ooneex/logger";var{SQL:R}=globalThis.Bun;var f=async(t,r,o)=>{let i=await t.getDependencies();for(let n of i){let e=l.get(n);await f(e,r,o)}await t.up(r,o)},Y=async(t)=>{let r=t?.tableName||"migrations",o=new R({url:t?.databaseUrl||Bun.env.DATABASE_URL,max:20,idleTimeout:30,maxLifetime:0,connectionTimeout:30}),i=new v,n=M();if(n.length===0)i.info(`No migrations found
|
|
26
|
+
`),process.exit(0);await c(o,r);for(let e of n){let s=e.getVersion();if((await o`SELECT * FROM ${o(r)} WHERE id = ${s}`).length>0)continue;let S=s;try{await o.begin(async(p)=>{await f(e,p,o),await p`INSERT INTO ${o(r)} (id) VALUES (${s})`,i.success(`Migration ${S} completed
|
|
27
|
+
`)})}catch(p){i.error(`Migration ${S} failed
|
|
28
|
+
`),i.error(p),process.exit(1)}}};export{Y as migrationUp,A as migrationCreate,M as getMigrations,g as generateMigrationVersion,T as decorator,c as createMigrationTable};
|
|
26
29
|
|
|
27
|
-
//# debugId=
|
|
30
|
+
//# debugId=ED3D772F46B003EE64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
"sources": ["src/createMigrationTable.ts", "src/decorators.ts", "src/container.ts", "src/generateMigrationVersion.ts", "src/getMigrations.ts", "src/migrationCreate.ts", "src/migrationUp.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import type { SQL } from \"bun\";\n\nexport const createMigrationTable = async (sql: SQL, tableName: string): Promise<void> => {\n await sql.begin(async (tx) => {\n await tx`CREATE TABLE IF NOT EXISTS ${sql(tableName)} (id VARCHAR(20) PRIMARY KEY)`;\n });\n};\n",
|
|
6
|
-
"import {
|
|
6
|
+
"import { container, EContainerScope } from \"@ooneex/container\";\nimport { MIGRATIONS_CONTAINER } from \"./container\";\nimport type { MigrationClassType } from \"./types\";\n\nexport const decorator = {\n migration: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: MigrationClassType): void => {\n container.add(target, scope);\n MIGRATIONS_CONTAINER.push(target);\n };\n },\n};\n",
|
|
7
7
|
"import type { MigrationClassType } from \"./types\";\n\nexport const MIGRATIONS_CONTAINER: MigrationClassType[] = [];\n",
|
|
8
8
|
"/**\n * Generates a migration version based on the current timestamp.\n *\n * Format: YYYYMMDDHHMMSSMMM\n * - YYYY: 4-digit year (e.g., 2025)\n * - MM: 2-digit month (01-12)\n * - DD: 2-digit day (01-31)\n * - HH: 2-digit hour in 24-hour format (00-23)\n * - MM: 2-digit minutes (00-59)\n * - SS: 2-digit seconds (00-59)\n * - MMM: 3-digit milliseconds (000-999)\n *\n * @returns {string} A migration version string (e.g., \"20250630210755123\")\n *\n * @example\n * const version = generateMigrationVersion();\n * console.log(version); // \"20250630210755123\"\n */\nexport const generateMigrationVersion = (): string => {\n const now = new Date();\n\n const year = now.getFullYear().toString();\n const month = (now.getMonth() + 1).toString().padStart(2, \"0\");\n const date = now.getDate().toString().padStart(2, \"0\");\n const hour = now.getHours().toString().padStart(2, \"0\");\n const minutes = now.getMinutes().toString().padStart(2, \"0\");\n const seconds = now.getSeconds().toString().padStart(2, \"0\");\n const milliseconds = now.getMilliseconds().toString().padStart(3, \"0\");\n\n return `${year}${month}${date}${hour}${minutes}${seconds}${milliseconds}`;\n};\n",
|
|
9
9
|
"import { container } from \"@ooneex/container\";\nimport { MIGRATIONS_CONTAINER } from \"./container\";\nimport type { IMigration } from \"./types\";\n\nexport const getMigrations = (): IMigration[] => {\n const migrationInstances = MIGRATIONS_CONTAINER.map((MigrationClass) => {\n return container.get(MigrationClass);\n });\n\n return migrationInstances.sort((a, b) => Number(a.getVersion()) - Number(b.getVersion()));\n};\n",
|
|
10
|
-
"import { join } from \"node:path\";\nimport {
|
|
11
|
-
"import type { IException } from \"@ooneex/exception\";\nimport { TerminalLogger } from \"@ooneex/logger\";\nimport { SQL } from \"bun\";\nimport { createMigrationTable } from \"./createMigrationTable\";\nimport { getMigrations } from \"./getMigrations\";\n\nexport const migrationUp = async (config?: { databaseUrl?: string; tableName?: string }): Promise<void> => {\n const tableName = config?.tableName || \"migrations\";\n\n const sql = new SQL({\n url: config?.databaseUrl || Bun.env.DATABASE_URL,\n\n // Connection pool settings\n max: 20, // Maximum connections in pool\n idleTimeout: 30, // Close idle connections after 30s\n maxLifetime: 0, // Connection lifetime in seconds (0 = forever)\n connectionTimeout: 30, // Timeout when establishing new connections\n });\n\n const logger = new TerminalLogger();\n const migrations = getMigrations();\n\n if (migrations.length === 0) {\n logger.info(\"No migrations found\\n\");\n process.exit(0);\n }\n\n await createMigrationTable(sql, tableName);\n\n for (const migration of migrations) {\n const id = migration.getVersion();\n\n const entities = await sql`SELECT * FROM ${sql(tableName)} WHERE id = ${id}`;\n\n if (entities.length > 0) {\n continue;\n }\n\n const migrationName = id;\n\n try {\n await sql.begin(async (tx) => {\n await migration
|
|
10
|
+
"import { join } from \"node:path\";\nimport { Glob } from \"bun\";\nimport { generateMigrationVersion } from \"./generateMigrationVersion\";\nimport content from \"./migration.txt\";\n\nexport const migrationCreate = async (config?: { dir?: string }): Promise<string> => {\n const version = generateMigrationVersion();\n const name = `Migration${version}`;\n const migrationsDir = config?.dir || \"migrations\";\n\n await Bun.write(\n join(process.cwd(), migrationsDir, `${name}.ts`),\n content.replaceAll(\"{{ name }}\", name).replaceAll(\"{{ version }}\", version),\n );\n\n const imports: string[] = [];\n const glob = new Glob(\"**/Migration*.ts\");\n for await (const file of glob.scan(migrationsDir)) {\n const name = file.replace(/\\.ts$/, \"\");\n imports.push(`export { ${name} } from './${name}';`);\n }\n\n await Bun.write(join(process.cwd(), migrationsDir, \"migrations.ts\"), `${imports.sort().join(\"\\n\")}\\n`);\n\n return join(migrationsDir, `${name}.ts`);\n};\n",
|
|
11
|
+
"import { container } from \"@ooneex/container\";\nimport type { IException } from \"@ooneex/exception\";\nimport { TerminalLogger } from \"@ooneex/logger\";\nimport { SQL } from \"bun\";\nimport { createMigrationTable } from \"./createMigrationTable\";\nimport { getMigrations } from \"./getMigrations\";\nimport type { IMigration } from \"./types\";\n\n// biome-ignore lint/suspicious/noExplicitAny: trust me\nconst run = async (migration: IMigration, tx: any, sql: SQL): Promise<void> => {\n const dependencies = await migration.getDependencies();\n\n for (const dependency of dependencies) {\n const dep = container.get(dependency);\n await run(dep, tx, sql);\n }\n\n await migration.up(tx, sql);\n};\n\nexport const migrationUp = async (config?: { databaseUrl?: string; tableName?: string }): Promise<void> => {\n const tableName = config?.tableName || \"migrations\";\n\n const sql = new SQL({\n url: config?.databaseUrl || Bun.env.DATABASE_URL,\n\n // Connection pool settings\n max: 20, // Maximum connections in pool\n idleTimeout: 30, // Close idle connections after 30s\n maxLifetime: 0, // Connection lifetime in seconds (0 = forever)\n connectionTimeout: 30, // Timeout when establishing new connections\n });\n\n const logger = new TerminalLogger();\n const migrations = getMigrations();\n\n if (migrations.length === 0) {\n logger.info(\"No migrations found\\n\");\n process.exit(0);\n }\n\n await createMigrationTable(sql, tableName);\n\n for (const migration of migrations) {\n const id = migration.getVersion();\n\n const entities = await sql`SELECT * FROM ${sql(tableName)} WHERE id = ${id}`;\n\n if (entities.length > 0) {\n continue;\n }\n\n const migrationName = id;\n\n try {\n await sql.begin(async (tx) => {\n await run(migration, tx, sql);\n await tx`INSERT INTO ${sql(tableName)} (id) VALUES (${id})`;\n logger.success(`Migration ${migrationName} completed\\n`);\n });\n } catch (error: unknown) {\n logger.error(`Migration ${migrationName} failed\\n`);\n logger.error(error as IException);\n process.exit(1);\n }\n }\n};\n"
|
|
12
12
|
],
|
|
13
|
-
"mappings": ";AAEO,IAAM,EAAuB,MAAO,EAAU,IAAqC,CACxF,MAAM,EAAI,MAAM,MAAO,IAAO,CAC5B,KAAM,gCAAgC,EAAI,CAAS,iCACpD,GCLH,
|
|
14
|
-
"debugId": "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AAM,EAAI,EAAW,EAAI,CAAG,EAC5B,KAAM,iBAAiB,EAAI,CAAS,kBAAkB,KACtD,EAAO,QAAQ,aAAa;AAAA,CAA2B,EACxD,EACD,MAAO,EAAgB,CACvB,EAAO,MAAM,aAAa;AAAA,CAAwB,EAClD,EAAO,MAAM,CAAmB,EAChC,QAAQ,KAAK,CAAC",
|
|
14
|
+
"debugId": "ED3D772F46B003EE64756E2164756E21",
|
|
15
15
|
"names": []
|
|
16
16
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/migrations",
|
|
3
|
-
"description": "",
|
|
4
|
-
"version": "0.0
|
|
3
|
+
"description": "Database migration runner framework with logging and container integration",
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -25,16 +25,22 @@
|
|
|
25
25
|
"test": "bun test tests",
|
|
26
26
|
"build": "bunup",
|
|
27
27
|
"lint": "tsgo --noEmit && bunx biome lint",
|
|
28
|
-
"publish
|
|
29
|
-
"publish:pack": "bun pm pack --destination ./dist",
|
|
30
|
-
"publish:dry": "bun publish --dry-run"
|
|
28
|
+
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
31
29
|
},
|
|
32
30
|
"dependencies": {
|
|
33
|
-
"@ooneex/container": "0.0.
|
|
31
|
+
"@ooneex/container": "0.0.2",
|
|
34
32
|
"@ooneex/logger": "0.0.1"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
35
|
"@ooneex/exception": "0.0.1"
|
|
38
36
|
},
|
|
39
|
-
"
|
|
37
|
+
"keywords": [
|
|
38
|
+
"bun",
|
|
39
|
+
"database",
|
|
40
|
+
"migrations",
|
|
41
|
+
"ooneex",
|
|
42
|
+
"schema",
|
|
43
|
+
"typescript",
|
|
44
|
+
"versioning"
|
|
45
|
+
]
|
|
40
46
|
}
|
|
Binary file
|