@h3ravel/arquebus 0.2.5 → 0.2.6
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/bin/index.cjs +1 -1
- package/bin/index.js +1 -1
- package/dist/migrations/index.cjs +1 -0
- package/dist/migrations/index.d.cts +1281 -0
- package/dist/migrations/index.d.ts +1281 -0
- package/dist/migrations/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t from"chalk";import e from"fs/promises";import i,{dirname as s}from"path";import{existsSync as r,readFileSync as a}from"fs";import n from"dayjs";import{fileURLToPath as o}from"url";var c=Object.defineProperty,h=(t,e)=>{for(var i in e)c(t,i,{get:e[i],enumerable:!0})},u={};h(u,{Migration:()=>l,default:()=>p});var g=class{},l=class extends g{connection;withinTransaction=!0;getConnection(){return this.connection}},p=l,m={};async function y(t){const i=await e.readdir(t),s=[];for(const r of i){const i=`${t}/${r}`,a=await e.stat(i);if(a.isFile())s.push(i);else if(a.isDirectory()){const t=await y(i);s.push(...t)}}return s}h(m,{default:()=>w});var w=class{events=null;repository;files;resolver;connection;paths=[];output=null;constructor(t,e=null,i=null,s=null){this.repository=t,this.files=i,this.resolver=e,this.events=s}async run(t=[],e={}){const i=await this.getMigrationFiles(t),s=await this.repository.getRan(),r=this.pendingMigrations(i,s);return await this.runPending(r,e),r}pendingMigrations(t,e){return Object.values(t).filter(t=>!e.includes(this.getMigrationName(t)))}async runPending(t,e={}){if(0===t.length)return void this.write("Nothing to migrate");let i=await this.repository.getNextBatchNumber();const s=e.pretend||!1,r=e.step||!1;this.write("Running migrations.");for(const e of t)await this.runUp(e,i,s),r&&i++}async runUp(t,e,i){const s=await this.resolvePath(t),r=this.getMigrationName(t);await this.writeTask(r,()=>this.runMigration(s,"up")),await this.repository.log(r,e)}async runDown(t,e,i){const s=await this.resolvePath(t),r=this.getMigrationName(t);await this.writeTask(r,()=>this.runMigration(s,"down")),await this.repository.delete(e)}async rollback(t=[],e={}){const i=await this.getMigrationsForRollback(e);return 0===i.length?(this.write("Nothing to rollback."),[]):await this.rollbackMigrations(i,t,e)}async getMigrationsForRollback(t){return t.step&&t.step>0?await this.repository.getMigrations(t.step):t.batch&&t.batch>0?await this.repository.getMigrationsByBatch(t.batch):await this.repository.getLast()}async rollbackMigrations(e,i,s){const r=[],a=await this.getMigrationFiles(i);this.write("Rolling back migrations.");for(const i of e){const e=a[i.migration];e?(r.push(e),await this.runDown(e,i,s.pretend||!1)):this.writeTwoColumns(i.migration,t.yellow("Migration not found"))}return r}reset(t=[],e=!1){return this.repository.getRan().then(t=>t.reverse()),[]}resetMigrations(t,e,i=!1){return this.rollbackMigrations(t,e,{pretend:i})}async runMigration(t,e){const i=this.resolveConnection(t.getConnection()),s=async i=>{"function"==typeof t[e]&&await this.runMethod(i,t,e)};t.withinTransaction?await i.transaction(s):await s(i)}async runMethod(t,e,i){await(e[i]?.(t.schema,t))}async resolvePath(t){try{return new((await import(t)).default)}catch{}return new class{}}getMigrationClass(t){return t.split("_").slice(4).map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join("")}async getMigrationFiles(t){const e=[];for(const i of t)i.endsWith(".js")||i.endsWith(".ts")?e.push(i):e.push(...await y(i));return e.filter(Boolean).reduce((t,e)=>(t[this.getMigrationName(e)]=e,t),{})}getMigrationName(t){return i.basename(t).replace(".js","")}path(t){this.paths=Array.from(new Set([...this.paths,t]))}getPaths(){return this.paths}getConnection(){return this.connection}resolveConnection(t){return this.resolver.connection(t||this.connection)}getRepository(){return this.repository}repositoryExists(){return this.repository.repositoryExists()}async hasRunAnyMigrations(){const t=await this.repository.getRan();return await this.repositoryExists()&&t.length>0}deleteRepository(){this.repository.deleteRepository()}setOutput(t){return this.output=t,this}write(...t){this.output&&console.log(...t)}writeTwoColumns(e,...i){const s=i.join(" "),r=/\x1b\[\d+m/g,a=Math.min(process.stdout.columns,100),n=Math.max(a-e.replace(r,"").length-s.replace(r,"").length-10,0);this.write(e,t.gray(".".repeat(n)),s)}async writeTask(e,i){const s=process.hrtime();let r=!1;try{r=await(i||(()=>!0))()}finally{const i=process.hrtime(s),a=(1e9*i[0]+i[1])/1e6;this.writeTwoColumns(t.green(e),t.gray(`${Math.floor(a)}ms`),!1!==r?t.green("✔"):t.red("✘"))}}},b={};h(b,{default:()=>d});var d=class{constructor(t,e="js"){this.customStubPath=t,this.type=e}postCreate=[];async create(t,s,r,a=!1){const n=this.getStub(r,a),o=this.getPath(t,s);return await this.ensureDirectoryExists(i.dirname(o)),await e.writeFile(o,this.populateStub(n,r)),await this.firePostCreateHooks(r,o),o}async publish(t,s){const r=await e.readdir(this.customStubPath??"");await this.ensureDirectoryExists(t);for(const a of r){const r=i.join(this.customStubPath??"",a),n=i.join(t,a);await e.copyFile(r,n),s&&s(a,r,n)}}getStub(t,e=!1){let s;if(t)if(e){const t=i.join(this.customStubPath??"",`migration.create-${this.type}.stub`);s=r(t)?t:this.stubPath(`/migration.create-${this.type}.stub`)}else{const t=i.join(this.customStubPath??"",`migration.update-${this.type}.stub`);s=r(t)?t:this.stubPath(`/migration.update-${this.type}.stub`)}else{const t=i.join(this.customStubPath??"",`migration-${this.type}.stub`);console.log("\n",t,"---"),s=r(t)?t:this.stubPath(`/migration-${this.type}.stub`)}return a(s,"utf-8")}populateStub(t,e){return null!==e&&(t=t.replace(/DummyTable|{{\s*table\s*}}/g,e)),t}getClassName(t){return t.replace(/_+([a-z])/g,(t,e)=>e.toUpperCase())}getPath(t,e){const s=n().format("YYYY_MM_DD_HHmmss");return i.join(e,`${s}_${t}.${this.type}`)}async firePostCreateHooks(t,e){for(const i of this.postCreate)await i(t,e)}afterCreate(t){this.postCreate.push(t)}async ensureDirectoryExists(t){await e.mkdir(t,{recursive:!0})}stubPath(t=""){const e=this.getDirname(import.meta);return i.join(e,"stubs",t)}getDirname(t){if("undefined"!=typeof __dirname)return __dirname;if(t&&t.url)return s(o(t.url));throw new Error("Unable to determine dirname")}},f={};h(f,{default:()=>M});var M=class{resolver;table;connection=null;constructor(t,e){this.resolver=t,this.table=e}async getRan(){return await this.getTable().orderBy("batch","asc").orderBy("migration","asc").pluck("migration")}async getMigrations(t){const e=this.getTable().where("batch",">=","1");return await e.orderBy("batch","desc").orderBy("migration","desc").take(t).get()}async getMigrationsByBatch(t){return await this.getTable().where("batch",t).orderBy("migration","desc").get()}async getLast(){const t=this.getTable().where("batch",await this.getLastBatchNumber());return await t.orderBy("migration","desc").get()}async getMigrationBatches(){const t=await this.getTable().select("batch","migration").orderBy("batch","asc").orderBy("migration","asc").get(),e={};return t.map(t=>{e[t.migration]=t.batch}),e}async log(t,e){await this.getTable().insert({migration:t,batch:e})}async delete(t){await this.getTable().where("migration",t.migration).delete()}async getNextBatchNumber(){return await this.getLastBatchNumber()+1}async getLastBatchNumber(){return await this.getTable().max("batch")}async createRepository(){const t=this.getConnection().schema;await t.createTable(this.table,function(t){t.increments("id"),t.string("migration"),t.integer("batch")})}repositoryExists(){return this.getConnection().schema.hasTable(this.table)}async deleteRepository(){const t=this.getConnection().schema;await t.drop(this.table)}getTable(){return this.getConnection().table(this.table)}getConnection(){return this.resolver.connection(this.connection)}setSource(t){this.connection=t}};export{u as Migration,b as MigrationCreator,f as MigrationRepository,m as Migrator};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/arquebus",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Arquebus ORM is a Beautiful, expressive ORM inspired by Laravel's Eloquent, designed for TypeScript applications and for the H3ravel Framework.",
|
|
5
5
|
"homepage": "https://h3ravel.net/arquebus",
|
|
6
6
|
"bin": {
|