@izumisy/kyrage 0.1.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/LICENSE +21 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +4 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 IzumiSy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{Migrator as W}from"kysely";function E(t){let{current:n,ideal:e}=t,a=Object.keys(n),o=Object.keys(e),s=o.filter(i=>!a.includes(i)).map(i=>({table:i,columns:e[i]})),l=a.filter(i=>!o.includes(i)),f=a.reduce((i,g)=>{if(!o.includes(g))return i;let u=n[g],m=e[g],y=Object.keys(u),b=Object.keys(m),v=b.filter(c=>!y.includes(c)).map(c=>({column:c,attributes:m[c]})),x=y.filter(c=>!b.includes(c)).map(c=>({column:c,attributes:u[c]})),D=y.filter(c=>b.includes(c)&&(u[c].type!==m[c].type||u[c].notNull!==m[c].notNull)).map(c=>({column:c,before:u[c],after:m[c]}));return(v.length>0||x.length>0||D.length>0)&&i.push({table:g,addedColumns:v,removedColumns:x,changedColumns:D}),i},[]);return{addedTables:s,removedTables:l,changedTables:f}}import{isColumnDataType as R}from"kysely";import{readdir as A,readFile as q}from"fs/promises";import{join as J}from"path";import{z as r}from"zod";var S=r.record(r.string(),r.record(r.string(),r.object({type:r.string(),primaryKey:r.boolean().optional().default(!1),notNull:r.boolean().optional().default(!1),unique:r.boolean().optional().default(!1)}))),K=r.record(r.string(),r.object({table:r.string(),columns:r.array(r.string()),unique:r.boolean().optional()})),F=r.enum(["postgres","mysql","sqlite","mssql"]),P=r.object({dialect:F,connectionString:r.string()}),j=r.object({database:P,tables:S,indexes:K.optional()}),k=r.object({id:r.string(),version:r.string(),diff:r.any()});var p="migrations",T=async()=>{try{let n=(await A(p)).filter(e=>e.endsWith(".json")).map(async e=>k.parse(JSON.parse(await q(J(p,e),"utf-8"))));return await Promise.all(n)}catch(t){if(t instanceof Object&&"code"in t&&t.code==="ENOENT")return[];throw t}},$=t=>({getMigrations:async()=>{let n=await T(),e={};return n.forEach(a=>{e[a.id]={up:async()=>{await _(t.db,a.diff)}}}),e}});async function _(t,n){for(let e of n.addedTables){let a=t.schema.createTable(e.table);for(let[o,s]of Object.entries(e.columns)){let l=s.type;h(l),a=a.addColumn(o,l,f=>{let i=f;return s.notNull&&(i=i.notNull()),s.primaryKey&&(i=i.primaryKey()),s.unique&&(i=i.unique()),i})}await a.execute()}for(let e of n.removedTables)await t.schema.dropTable(e).execute();for(let e of n.changedTables){for(let a of e.addedColumns){let o=a.attributes.type;h(o),await t.schema.alterTable(e.table).addColumn(a.column,o,s=>{let l=s;return a.attributes.notNull&&(l=l.notNull()),a.attributes.primaryKey&&(l=l.primaryKey()),a.attributes.unique&&(l=l.unique()),l}).execute()}for(let a of e.removedColumns)await t.schema.alterTable(e.table).dropColumn(a.column).execute();for(let a of e.changedColumns){if(a.before.type!==a.after.type){let o=a.after.type;h(o),await t.schema.alterTable(e.table).alterColumn(a.column,s=>s.setDataType(o)).execute()}a.after.notNull!==a.before.notNull&&(a.after.notNull?await t.schema.alterTable(e.table).alterColumn(a.column,o=>o.setNotNull()).execute():await t.schema.alterTable(e.table).alterColumn(a.column,o=>o.dropNotNull()).execute())}}}var h=t=>{if(!R(t))throw new Error(`Unsupported data type: ${t}`)};import{defineCommand as N,runMain as B}from"citty";import{Kysely as V,PostgresDialect as z}from"kysely";import{Pool as I}from"pg";var C=async t=>{let n=()=>{switch(t.database.dialect){case"postgres":return new z({pool:new I({connectionString:t.database.connectionString})});default:throw new Error(`Unsupported dialect: ${t.database.dialect}`)}};return{db:new V({dialect:n()})}};var M="0.1.0";import{createConsola as H}from"consola";import{loadConfig as G}from"c12";var w=async()=>{let t=await G({name:"kyrage"});return j.parse(t.config)};import{writeFile as Q,mkdir as X}from"fs/promises";var d=H({stdout:process.stderr}),Y=async t=>{let n=await t.selectFrom("kysely_migration").select(["name","timestamp"]).$narrowType().execute().catch(()=>[]);return n.length===0?[]:(await T()).filter(o=>!n.some(s=>s.name===o.id))},Z=N({meta:{name:"generate",description:"Generate migration files based on the current schema"},args:{apply:{type:"boolean",description:"Apply the migration after generating it",default:!1},"ignore-pending":{type:"boolean",description:"Ignore pending migrations and generate a new one",default:!1}},run:async t=>{try{let n=await w(),{db:e}=await C({database:n.database});if(!t.args["ignore-pending"]){let s=await Y(e);if(s.length>0){d.warn([`There are pending migrations: ${s.map(l=>l.id).join(", ")}`,"Please apply them first before generating a new migration.","Otherwise, use --ignore-pending to skip this check."].join(`
|
|
2
|
+
`)),await e.destroy();return}}let a=await ne({db:e,config:n});if(!a){d.info("No changes detected, no migration needed."),await e.destroy();return}te(a.diff);let o=`${p}/${a.id}.json`;await X(p,{recursive:!0}),await Q(o,JSON.stringify(a,null,2)),d.success(`Migration file generated: ${o}`),t.args.apply&&await O({db:e,dryRun:!1}),await e.destroy()}catch(n){d.error(n),process.exit(1)}}}),ee=N({meta:{name:"apply",description:"Run migrations to sync database schema"},args:{"dry-run":{name:"dryRun",type:"boolean",description:"Run the migration in dry-run mode",default:!1}},run:async t=>{try{let n=await w(),{db:e}=await C({database:n.database});await O({db:e,dryRun:t.args["dry-run"]}),await e.destroy()}catch(n){d.error(n),process.exit(1)}}}),O=async t=>{let{db:n}=t,e=new W({db:n,provider:$({db:n,migrationDirName:p})}),{results:a,error:o}=await e.migrateToLatest();a&&a.length>0?a.forEach(s=>{s.status==="Error"?d.error(`Migration failed: ${s.migrationName}`):s.status==="Success"&&d.success(`Migration applied: ${s.migrationName}`)}):d.info("No migrations to run"),o&&(d.error(`Migration error: ${o}`),process.exit(1))},te=t=>{t.addedTables.length>0&&t.addedTables.forEach(n=>{d.log(`-- create_table: ${n.table}`),Object.entries(n.columns).forEach(([e,a])=>{d.log(` -> column: ${e} (${JSON.stringify(a)})`)})}),t.removedTables.length>0&&t.removedTables.forEach(n=>{d.log(`-- remove_table: ${n}`)}),t.changedTables.length>0&&t.changedTables.forEach(n=>{n.addedColumns.forEach(e=>{d.log([`-- add_column: ${n.table}.${e.column}`,` -> to: ${JSON.stringify(e.attributes)}`].join(`
|
|
3
|
+
`))}),n.removedColumns.forEach(e=>{d.log(`-- remove_column: ${n.table}.${e.column})`)}),n.changedColumns.forEach(e=>{d.log([`-- change_column: ${n.table}.${e.column}`,` -> from: ${JSON.stringify(e.before)}`,` -> to: ${JSON.stringify(e.after)}`].join(`
|
|
4
|
+
`))})})},ne=async t=>{let{db:n,config:e}=t,o=(await n.introspection.getTables()).reduce((i,g)=>(i[g.name]=(g.columns??[]).reduce((u,m)=>(u[m.name]={type:m.dataType,notNull:!m.isNullable},u),{}),i),{}),s=Object.fromEntries(Object.entries(e.tables).map(([i,g])=>[i,Object.fromEntries(Object.entries(g).map(([u,m])=>[u,{type:m.type,notNull:m.notNull,primaryKey:m.primaryKey,unique:m.unique}]))])),l=E({current:o,ideal:s});return l.addedTables.length===0&&l.removedTables.length===0&&l.changedTables.length===0?null:{version:"1",id:Date.now()+"",diff:l}},ae=N({meta:{name:"kyrage",version:M,description:"Kysely migration CLI with declarative schema"},subCommands:{apply:ee,generate:Z}});B(ae);
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@izumisy/kyrage",
|
|
3
|
+
"private": false,
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"description": "Declative schema manager for kysely migration",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"bin": {
|
|
11
|
+
"kyrage": "./dist/main.js"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"c12": "^3.1.0",
|
|
15
|
+
"citty": "^0.1.6",
|
|
16
|
+
"consola": "^3.4.2",
|
|
17
|
+
"kysely": "^0.28.3",
|
|
18
|
+
"kysely-codegen": "^0.18.5",
|
|
19
|
+
"pg": "^8.16.3",
|
|
20
|
+
"zod": "^4.0.14"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@swc/core": "^1.13.3",
|
|
24
|
+
"@types/node": "^24.1.0",
|
|
25
|
+
"@types/pg": "^8.15.5",
|
|
26
|
+
"prettier": "^3.6.2",
|
|
27
|
+
"tsup": "^8.5.0",
|
|
28
|
+
"typescript": "^5.8.3",
|
|
29
|
+
"vitest": "^3.2.4"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "NODE_ENV=development tsup --watch",
|
|
33
|
+
"build": "tsup",
|
|
34
|
+
"type-check": "tsc --noEmit --incremental --pretty",
|
|
35
|
+
"test": "vitest run"
|
|
36
|
+
}
|
|
37
|
+
}
|