@ooneex/migrations 0.13.0 → 0.14.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.js +7 -125
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,51 +1,5 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
|
|
3
|
-
var createMigrationTable = async (sql, tableName) => {
|
|
4
|
-
await sql.begin(async (tx) => {
|
|
5
|
-
await tx`CREATE TABLE IF NOT EXISTS ${sql(tableName)} (id VARCHAR(20) PRIMARY KEY)`;
|
|
6
|
-
});
|
|
7
|
-
};
|
|
8
|
-
// src/decorators.ts
|
|
9
|
-
import { container, EContainerScope } from "@ooneex/container";
|
|
10
|
-
|
|
11
|
-
// src/container.ts
|
|
12
|
-
var MIGRATIONS_CONTAINER = [];
|
|
13
|
-
|
|
14
|
-
// src/decorators.ts
|
|
15
|
-
var decorator = {
|
|
16
|
-
migration: (scope = EContainerScope.Singleton) => {
|
|
17
|
-
return (target) => {
|
|
18
|
-
container.add(target, scope);
|
|
19
|
-
MIGRATIONS_CONTAINER.push(target);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
// src/generateMigrationVersion.ts
|
|
24
|
-
var generateMigrationVersion = () => {
|
|
25
|
-
const now = new Date;
|
|
26
|
-
const year = now.getFullYear().toString();
|
|
27
|
-
const month = (now.getMonth() + 1).toString().padStart(2, "0");
|
|
28
|
-
const date = now.getDate().toString().padStart(2, "0");
|
|
29
|
-
const hour = now.getHours().toString().padStart(2, "0");
|
|
30
|
-
const minutes = now.getMinutes().toString().padStart(2, "0");
|
|
31
|
-
const seconds = now.getSeconds().toString().padStart(2, "0");
|
|
32
|
-
const milliseconds = now.getMilliseconds().toString().padStart(3, "0");
|
|
33
|
-
return `${year}${month}${date}${hour}${minutes}${seconds}${milliseconds}`;
|
|
34
|
-
};
|
|
35
|
-
// src/getMigrations.ts
|
|
36
|
-
import { container as container2 } from "@ooneex/container";
|
|
37
|
-
var getMigrations = () => {
|
|
38
|
-
const migrationInstances = MIGRATIONS_CONTAINER.map((MigrationClass) => {
|
|
39
|
-
return container2.get(MigrationClass);
|
|
40
|
-
});
|
|
41
|
-
return migrationInstances.sort((a, b) => Number(a.getVersion()) - Number(b.getVersion()));
|
|
42
|
-
};
|
|
43
|
-
// src/migrationCreate.ts
|
|
44
|
-
import { join } from "path";
|
|
45
|
-
var {Glob } = globalThis.Bun;
|
|
46
|
-
|
|
47
|
-
// src/migration.txt
|
|
48
|
-
var migration_default = `import { decorator, type IMigration, type MigrationClassType } from '@ooneex/migrations';
|
|
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';
|
|
49
3
|
import type { TransactionSQL } from 'bun';
|
|
50
4
|
|
|
51
5
|
@decorator.migration()
|
|
@@ -66,83 +20,11 @@ export class {{ name }} implements IMigration {
|
|
|
66
20
|
return [];
|
|
67
21
|
}
|
|
68
22
|
}
|
|
69
|
-
`;
|
|
70
|
-
|
|
71
|
-
// src/migrationCreate.ts
|
|
72
|
-
var migrationCreate = async (config) => {
|
|
73
|
-
const version = generateMigrationVersion();
|
|
74
|
-
const name = `Migration${version}`;
|
|
75
|
-
const migrationsDir = config?.dir || "migrations";
|
|
76
|
-
await Bun.write(join(process.cwd(), migrationsDir, `${name}.ts`), migration_default.replaceAll("{{ name }}", name).replaceAll("{{ version }}", version));
|
|
77
|
-
const imports = [];
|
|
78
|
-
const glob = new Glob("**/Migration*.ts");
|
|
79
|
-
for await (const file of glob.scan(migrationsDir)) {
|
|
80
|
-
const name2 = file.replace(/\.ts$/, "");
|
|
81
|
-
imports.push(`export { ${name2} } from './${name2}';`);
|
|
82
|
-
}
|
|
83
|
-
await Bun.write(join(process.cwd(), migrationsDir, "migrations.ts"), `${imports.sort().join(`
|
|
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(`
|
|
84
24
|
`)}
|
|
85
|
-
`);
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
import { container as container3 } from "@ooneex/container";
|
|
90
|
-
import { TerminalLogger } from "@ooneex/logger";
|
|
91
|
-
var {SQL } = globalThis.Bun;
|
|
92
|
-
var run = async (migration, tx, sql) => {
|
|
93
|
-
const dependencies = await migration.getDependencies();
|
|
94
|
-
for (const dependency of dependencies) {
|
|
95
|
-
const dep = container3.get(dependency);
|
|
96
|
-
await run(dep, tx, sql);
|
|
97
|
-
}
|
|
98
|
-
await migration.up(tx, sql);
|
|
99
|
-
};
|
|
100
|
-
var migrationUp = async (config) => {
|
|
101
|
-
const tableName = config?.tableName || "migrations";
|
|
102
|
-
const sql = new SQL({
|
|
103
|
-
url: config?.databaseUrl || Bun.env.DATABASE_URL,
|
|
104
|
-
max: 20,
|
|
105
|
-
idleTimeout: 30,
|
|
106
|
-
maxLifetime: 0,
|
|
107
|
-
connectionTimeout: 30
|
|
108
|
-
});
|
|
109
|
-
const logger = new TerminalLogger;
|
|
110
|
-
const migrations = getMigrations();
|
|
111
|
-
if (migrations.length === 0) {
|
|
112
|
-
logger.info(`No migrations found
|
|
113
|
-
`);
|
|
114
|
-
process.exit(0);
|
|
115
|
-
}
|
|
116
|
-
await createMigrationTable(sql, tableName);
|
|
117
|
-
for (const migration of migrations) {
|
|
118
|
-
const id = migration.getVersion();
|
|
119
|
-
const entities = await sql`SELECT * FROM ${sql(tableName)} WHERE id = ${id}`;
|
|
120
|
-
if (entities.length > 0) {
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
const migrationName = id;
|
|
124
|
-
try {
|
|
125
|
-
await sql.begin(async (tx) => {
|
|
126
|
-
await run(migration, tx, sql);
|
|
127
|
-
await tx`INSERT INTO ${sql(tableName)} (id) VALUES (${id})`;
|
|
128
|
-
logger.success(`Migration ${migrationName} completed
|
|
129
|
-
`);
|
|
130
|
-
});
|
|
131
|
-
} catch (error) {
|
|
132
|
-
logger.error(`Migration ${migrationName} failed
|
|
133
|
-
`);
|
|
134
|
-
logger.error(error);
|
|
135
|
-
process.exit(1);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
export {
|
|
140
|
-
migrationUp,
|
|
141
|
-
migrationCreate,
|
|
142
|
-
getMigrations,
|
|
143
|
-
generateMigrationVersion,
|
|
144
|
-
decorator,
|
|
145
|
-
createMigrationTable
|
|
146
|
-
};
|
|
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};
|
|
147
29
|
|
|
148
|
-
//# debugId=
|
|
30
|
+
//# debugId=ED3D772F46B003EE64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
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
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": "
|
|
14
|
-
"debugId": "
|
|
13
|
+
"mappings": ";AAEO,IAAM,EAAuB,MAAO,EAAU,IAAqC,CACxF,MAAM,EAAI,MAAM,MAAO,IAAO,CAC5B,KAAM,gCAAgC,EAAI,CAAS,iCACpD,GCLH,oBAAS,qBAAW,0BCEb,IAAM,EAA6C,CAAC,EDEpD,IAAM,EAAY,CACvB,UAAW,CAAC,EAAyB,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