@coreframe/db 0.1.6 → 0.1.8
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/dev-supervisor-client.d.ts +11 -0
- package/dist/dev-supervisor-client.js +1 -0
- package/dist/dev-supervisor-process.d.ts +1 -0
- package/dist/dev-supervisor-process.js +2 -0
- package/dist/dev-supervisor.d.ts +12 -0
- package/dist/dev-supervisor.js +1 -0
- package/dist/drizzle-kit.js +1 -1
- package/dist/ensure.js +1 -1
- package/dist/environment.js +1 -1
- package/dist/index.js +1 -1
- package/dist/local-postgres.js +1 -1
- package/dist/local-turso.d.ts +2 -2
- package/dist/local-turso.js +1 -1
- package/dist/migrate-held.js +1 -1
- package/dist/migrate.js +1 -1
- package/dist/reset.js +5 -5
- package/dist/schema-push.js +1 -1
- package/dist/seed.js +1 -1
- package/package.json +7 -3
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/dev-supervisor-client.d.ts
|
|
2
|
+
/** Starts or connects to the detached development supervisor for a Coreframe project directory. */
|
|
3
|
+
declare function connectDevSupervisor({
|
|
4
|
+
cwd,
|
|
5
|
+
env
|
|
6
|
+
}?: {
|
|
7
|
+
cwd?: string;
|
|
8
|
+
env?: NodeJS.ProcessEnv;
|
|
9
|
+
}): Promise<import("@coreframe/dev-supervisor").DevSupervisorClient>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { connectDevSupervisor };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{connectDevSupervisor as e}from"@coreframe/dev-supervisor";import{access as t}from"node:fs/promises";import{fileURLToPath as n}from"node:url";async function r({cwd:t=process.cwd(),env:n=process.env}={}){return e({cwd:t,env:n,supervisorEntry:await i()})}async function i(){let e=n(new URL(`./dev-supervisor-process.js`,import.meta.url));try{return await t(e),e}catch{return n(new URL(`./dev-supervisor-process.ts`,import.meta.url))}}export{r as connectDevSupervisor};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { connectDevSupervisor } from "./dev-supervisor-client.js";
|
|
2
|
+
export * from "@coreframe/dev-supervisor";
|
|
3
|
+
|
|
4
|
+
//#region src/dev-supervisor.d.ts
|
|
5
|
+
/** Runs the detached supervisor process with the Coreframe database sidecar implementations. */
|
|
6
|
+
declare function runDevSupervisorProcess({
|
|
7
|
+
cwd
|
|
8
|
+
}?: {
|
|
9
|
+
cwd?: string;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { connectDevSupervisor, runDevSupervisorProcess };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{connectDevSupervisor as e}from"./dev-supervisor-client.js";import{startProjectPostgres as t}from"./local-postgres.js";import{startProjectTurso as n}from"./local-turso.js";import{runDevSupervisorProcess as r}from"@coreframe/dev-supervisor";export*from"@coreframe/dev-supervisor";function i({cwd:e=process.cwd()}={}){return r({cwd:e,startPostgres:t,startSqlite:n})}export{e as connectDevSupervisor,i as runDevSupervisorProcess};
|
package/dist/drizzle-kit.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{startProjectPostgres as e}from"./local-postgres.js";import{startProjectTurso as t}from"./local-turso.js";import n from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import r from"node:
|
|
1
|
+
import{startProjectPostgres as e}from"./local-postgres.js";import{startProjectTurso as t}from"./local-turso.js";import n from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import r from"node:process";import i from"node:path";import{readProjectConfig as a}from"@coreframe/config";import o from"nano-spawn";const s=r.platform===`win32`?`drizzle-kit.cmd`:`drizzle-kit`;async function c(i,o=[],{cwd:s=r.cwd(),env:c=r.env}={}){try{var u=n();let r=await a({cwd:s});if(r.database?.dialect===`sqlite`)try{var d=n();await l(o,i,s,d.a(await t({config:r,cwd:s,env:c,useSupervisorLease:!0})).env);return}catch(e){d.e=e}finally{await d.d()}await l(o,i,s,u.a(await e({config:r,cwd:s,env:c,useSupervisorLease:!0})).env)}catch(e){u.e=e}finally{await u.d()}}async function l(e,t,n,r){let a=r.PATH??r.Path??``;await o(s,[t,...e],{cwd:n,env:{...r,PATH:i.resolve(n,`node_modules`,`.bin`)+i.delimiter+a},stdio:`inherit`})}export{c as runDrizzleKitCommand};
|
package/dist/ensure.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createProjectSqliteDrizzleDatabase as e}from"./project.js";import{createLocalPostgresDb as t,startProjectPostgres as n}from"./local-postgres.js";import{startProjectTurso as r}from"./local-turso.js";import{SEED_EMAILS as i,SEED_IDS as a}from"./config.js";import o from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{migrateLocalDb as s}from"./migrate.js";import{setupLocalDb as c}from"./setup.js";import{importProjectModule as l,readProjectConfig as u}from"@coreframe/config";import{eq as d}from"drizzle-orm";async function f(e){return{schema:await l(`src/db/schema.ts`,e)}}async function p(e,{users:t}){let[n]=await e.select({id:t.id}).from(t).where(d(t.email,i.adminUser)).limit(1);return n?.id===a.adminUser}async function m({config:i,cwd:a=process.cwd(),env:s=process.env}={}){try{var c=o();let d=i??await u({cwd:a}),p=await f(a);if(d.database?.dialect===`sqlite`)try{var l=o();let t=l.a(await r({config:d,cwd:a,env:s,useSupervisorLease:!0}));await h({config:d,cwd:a,db:await e({cwd:a,env:t.env}),env:t.env,modules:p});return}catch(e){l.e=e}finally{await l.d()}let m=c.a(await n({config:d,cwd:a,env:s,useSupervisorLease:!0}));await h({config:d,cwd:a,db:await t({cwd:a,env:m.env}),env:m.env,modules:p})}catch(e){c.e=e}finally{await c.d()}}async function h({config:e,cwd:t,db:n,env:r,modules:i}){try{await g(n,i.schema)?(await s({config:e,cwd:t,env:r}),console.log(`Local database already set up`)):(console.log(`Local database is missing or unseeded; running setup`),await c(`default`,{config:e,cwd:t,env:r}))}finally{await _(n)}}async function g(e,t){try{return await p(e,t)}catch{return!1}}async function _(e){await e.$client?.close?.(),await e.$client?.end?.()}export{m as ensureLocalDb};
|
package/dist/environment.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{access as
|
|
1
|
+
import e from"node:process";import{access as t,mkdtemp as n,readFile as r,rm as i,stat as a}from"node:fs/promises";import{tmpdir as o}from"node:os";const s=e.kill.bind(e),c={access:t,mkdtemp:n,readFile:r,rm:i,stat:a},l={kill:s,tmpdir:o};e.argv,e.cwd.bind(e),e.env,e.platform;export{c as fs,l as os};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createDatabaseAdapter as e,normalizeQueryResult as t}from"./client.js";import{createProjectPostgresDatabaseAdapter as n,createProjectPostgresDrizzleDatabase as r,createProjectSqliteDatabaseAdapter as i,createProjectSqliteDrizzleDatabase as a}from"./project.js";import{LOCAL_POSTGRES_ENV_FLAG as o,ProjectPostgresAlreadyRunningError as s,createLocalPostgresDatabaseAdapter as c,createLocalPostgresDb as l,createLocalPostgresEnv as u,getLocalPostgresConnectionString as d,startProjectPostgres as f,startTemporaryProjectPostgres as p,stopProjectPostgres as m}from"./local-postgres.js";import{LOCAL_TURSO_ENV_FLAG as h,createLocalTursoEnv as g,startProjectTurso as _,startTemporaryProjectTurso as v}from"./local-turso.js";import{applySeed as y}from"./apply-seed.js";import{HELD_MIGRATIONS_FOLDER as b,MIGRATIONS_FOLDER as x,NOW as S,SEED_EMAILS as C,SEED_IDS as w,generatedUserId as T,getSeedProfile as E}from"./config.js";import{collectDeployMigrationFiles as D,collectHeldMigrationFiles as O,formatMigrationList as k,formatMigrationScanFailures as A,scanDeployMigrationFiles as j,scanMigrationSql as M}from"./migrations.js";import{checkDeployMigrations as N}from"./check-migrations.js";import{runDrizzleKitCommand as P}from"./drizzle-kit.js";import{migrateLocalDb as F}from"./migrate.js";import{inferSchemaDialect as I,planLocalSchemaPush as L}from"./schema-push.js";import{resetLocalDb as R}from"./reset.js";import{runProjectSeed as z,seedLocalDb as B}from"./seed.js";import{setupLocalDb as V}from"./setup.js";import{ensureLocalDb as H}from"./ensure.js";import{formatDbErrorMessage as U}from"./error.js";import{heldMigrationInsertQuery as W,heldMigrationSelectQuery as G,migrateHeldMigrations as K,validateHeldMigrationHeader as q}from"./migrate-held.js";import{appliedMigrationsQuery as J,filterPendingMigrations as Y,readAppliedMigrationNames as X}from"./pending-migrations.js";import{checkSchemaPush as Z}from"./schema-check.js";export{b as HELD_MIGRATIONS_FOLDER,o as LOCAL_POSTGRES_ENV_FLAG,h as LOCAL_TURSO_ENV_FLAG,x as MIGRATIONS_FOLDER,S as NOW,s as ProjectPostgresAlreadyRunningError,C as SEED_EMAILS,w as SEED_IDS,J as appliedMigrationsQuery,y as applySeed,N as checkDeployMigrations,Z as checkSchemaPush,D as collectDeployMigrationFiles,O as collectHeldMigrationFiles,e as createDatabaseAdapter,c as createLocalPostgresDatabaseAdapter,l as createLocalPostgresDb,u as createLocalPostgresEnv,g as createLocalTursoEnv,n as createProjectPostgresDatabaseAdapter,r as createProjectPostgresDrizzleDatabase,i as createProjectSqliteDatabaseAdapter,a as createProjectSqliteDrizzleDatabase,H as ensureLocalDb,Y as filterPendingMigrations,U as formatDbErrorMessage,k as formatMigrationList,A as formatMigrationScanFailures,T as generatedUserId,d as getLocalPostgresConnectionString,E as getSeedProfile,W as heldMigrationInsertQuery,G as heldMigrationSelectQuery,I as inferSchemaDialect,K as migrateHeldMigrations,F as migrateLocalDb,t as normalizeQueryResult,L as planLocalSchemaPush,X as readAppliedMigrationNames,R as resetLocalDb,P as runDrizzleKitCommand,z as runProjectSeed,j as scanDeployMigrationFiles,M as scanMigrationSql,B as seedLocalDb,V as setupLocalDb,f as startProjectPostgres,_ as startProjectTurso,p as startTemporaryProjectPostgres,v as startTemporaryProjectTurso,m as stopProjectPostgres,q as validateHeldMigrationHeader};
|
package/dist/local-postgres.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{connectDevSupervisor as e}from"./dev-supervisor-client.js";import{createDatabaseAdapter as t}from"./client.js";import{fs as n,os as r}from"./environment.js";import{createProjectPostgresDrizzleDatabase as i}from"./project.js";import a from"node:path";import{readProjectConfig as o}from"@coreframe/config";import{startPostgres as s}from"local-postgres";import{PostgresDataDirInUseError as c,stopPostgresDataDir as l}from"local-postgres/core";const u=`COREFRAME_LOCAL_POSTGRES`,d=`coreframe`,f=2e3,p=4e3,m=new Map,h={name:`coreframe`,password:`coreframe`};var g=class extends Error{dataDir;pid;constructor(e,t,n){super(`PostgreSQL is already running for this project (PID ${t}). Stop the other top-level \`coreframe dev\` process before starting another.`,n),this.name=`ProjectPostgresAlreadyRunningError`,this.dataDir=e,this.pid=t}};async function _({config:t,cwd:r=process.cwd(),env:i=process.env,failIfRunning:c=!1,postgresOutput:l,useSupervisorLease:u=!1}={}){if(i.COREFRAME_LOCAL_POSTGRES===`1`)return D(i);let p=t??await o({cwd:r});if((p.database?.dialect??`postgres`)!==`postgres`)return D(i);if(u&&i.COREFRAME_DEV_SUPERVISOR!==`0`){let t=await e({cwd:r,env:i});try{let e=await t.acquirePostgres({config:p,env:i});return O(t,e.leaseId,e.env)}catch(e){throw t.close(),e}}let m=a.resolve(r,`.coreframe/postgres`),_=await k(m);if(_){if(c)throw new g(m,_.pid);return D(A(i,m,_))}let v=a.join(m,`postgres.log`);await n.rm(v,{force:!0});let y;try{y=await s({dataDir:m,database:d,postgresOutput:l??{filePath:v},postgres:{strategy:`prefer-local`},stopTimeoutMs:f,superuser:h})}catch(e){throw await j(e,l?void 0:v)}return C(i,y)}async function v({dataDir:e,pid:t}){let n=`${e}\0${t}`,r=m.get(n);if(r){await r;return}let i=T(e,t);m.set(n,i);try{await i}finally{m.get(n)===i&&m.delete(n)}}async function y({config:e,cwd:t=process.cwd(),env:i=process.env}={}){let c=(e??await o({cwd:t})).database?.dialect??`postgres`;if(c!==`postgres`)throw Error(`Temporary schema push checks require PostgreSQL, but this project uses ${c}.`);let l=await n.mkdtemp(a.join(r.tmpdir(),`coreframe-schema-check-`));try{let e=C(i,await s({dataDir:l,database:d,postgresOutput:`ignore`,postgres:{strategy:`prefer-local`},stopTimeoutMs:f,superuser:h}));return{env:e.env,async[Symbol.asyncDispose](){await e[Symbol.asyncDispose](),await n.rm(l,{force:!0,recursive:!0})}}}catch(e){throw await E(l,e),e}}function b(e,t){return{...e,...t,[u]:`1`}}async function x({cwd:e=process.cwd(),env:t=process.env}={}){let n=N(t);return i({cwd:e,env:{...t,DATABASE_URL:n}})}async function S({cwd:e=process.cwd(),env:n=process.env}={}){let r=await x({cwd:e,env:n});return t({database:r,dialect:`postgres`,close:()=>r.$client.end()})}function C(e,t){let n;return{env:b(e,t.env),async[Symbol.asyncDispose](){n??=w(t),await n}}}async function w(e){let t;try{await e[Symbol.asyncDispose]()}catch(e){t=e}if(e.pid===void 0){if(t)throw t;return}try{await l({dataDir:e.dataDir,expectedPid:e.pid,mode:`immediate`,timeoutMs:p})}catch(n){throw t?AggregateError([t,n],`Failed to stop PostgreSQL process ${e.pid}.`):n}}async function T(e,t){try{await l({dataDir:e,expectedPid:t,mode:`fast`,timeoutMs:p})}catch(n){try{await l({dataDir:e,expectedPid:t,mode:`immediate`,timeoutMs:p})}catch(e){throw AggregateError([n,e],`Failed to stop PostgreSQL process ${t}.`)}}}async function E(e,t){try{await l({dataDir:e,mode:`immediate`,timeoutMs:p})}catch(n){throw AggregateError([t,n],`Temporary PostgreSQL failed to start and could not be stopped for ${e}.`)}await n.rm(e,{force:!0,recursive:!0})}function D(e){return{env:e,async[Symbol.asyncDispose](){}}}function O(e,t,n){let r;return{env:n,async[Symbol.asyncDispose](){r??=(async()=>{try{await e.release(t)}finally{e.close()}})(),await r}}}async function k(e){let t;try{t=await n.readFile(a.join(e,`postmaster.pid`),`utf8`)}catch{return}let[i,,,o,,s]=t.split(/\r?\n/),c=Number(i);if(!Number.isSafeInteger(c)||c<=0)return;try{r.kill(c,0)}catch(e){if(!M(e)||e.code!==`EPERM`)return}let l=Number(o);if(!Number.isSafeInteger(l)||l<=0||l>65535||!s)throw Error(`Cannot read connection details for PostgreSQL process ${c} from ${e}.`);return{host:s,pid:c,port:l}}function A(e,t,{host:n,port:r}){let i=new URL(`postgresql://${n}:${r}/${d}`);return i.username=h.name,i.password=h.password,b(e,{DATABASE_URL:i.href,PGDATA:t,PGDATABASE:d,PGHOST:n,PGPASSWORD:h.password,PGPORT:String(r),PGUSER:h.name})}async function j(e,t){if(e instanceof c)return new g(e.dataDir,e.pid,{cause:e});let r=e instanceof Error?e.message:String(e),i;if(t)try{i=(await n.readFile(t,`utf8`)).trim()||void 0}catch{}let a=i?`\n\nPostgreSQL output:\n${i}`:``,o=t?`\n\nPostgreSQL log: ${t}`:``;return Error(`Local PostgreSQL failed to start: ${r}${a}${o}`,{cause:e})}function M(e){return e instanceof Error}function N(e=process.env){let t=e.DATABASE_URL||P(e);if(!t)throw Error(`DATABASE_URL or PGDATABASE, PGHOST, PGPORT, and PGUSER are required.`);return t}function P(e){let{PGDATABASE:t,PGHOST:n,PGPASSWORD:r,PGPORT:i,PGUSER:a}=e;if(!t||!n||!i||!a)return;let o=new URL(`postgresql://${n}:${i}/${t}`);return o.username=a,r&&(o.password=r),o.href}export{u as LOCAL_POSTGRES_ENV_FLAG,g as ProjectPostgresAlreadyRunningError,S as createLocalPostgresDatabaseAdapter,x as createLocalPostgresDb,b as createLocalPostgresEnv,N as getLocalPostgresConnectionString,_ as startProjectPostgres,y as startTemporaryProjectPostgres,v as stopProjectPostgres};
|
package/dist/local-turso.d.ts
CHANGED
|
@@ -18,8 +18,8 @@ type ProjectTursoSidecar = {
|
|
|
18
18
|
/**
|
|
19
19
|
* Starts a project-scoped local libSQL server backed by `.coreframe/turso.db`.
|
|
20
20
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* The development supervisor owns and reuses the process through a lease. Callers may explicitly
|
|
22
|
+
* disable supervisor use to own a short-lived server directly.
|
|
23
23
|
*/
|
|
24
24
|
declare function startProjectTurso({
|
|
25
25
|
config,
|
package/dist/local-turso.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mkdir as
|
|
1
|
+
import{connectDevSupervisor as e}from"./dev-supervisor-client.js";import t from"node:process";import{mkdir as n,mkdtemp as r,rm as i}from"node:fs/promises";import a from"node:path";import{readProjectConfig as o}from"@coreframe/config";import{tmpdir as s}from"node:os";import{spawn as c}from"node:child_process";import{createConnection as l,createServer as u}from"node:net";const d=`COREFRAME_LOCAL_TURSO`,f=t.platform===`win32`?`turso.cmd`:`turso`;async function p({config:n,cwd:r=t.cwd(),databaseFile:i=a.resolve(r,`.coreframe/turso.db`),env:s=t.env,tursoOutput:c,useSupervisorLease:l=!1}={}){if(s.COREFRAME_LOCAL_TURSO===`1`)return _(s);let u=n??await o({cwd:r});if((u.database?.dialect??`postgres`)!==`sqlite`)return _(s);if(l&&s.COREFRAME_DEV_SUPERVISOR!==`0`){let t=await e({cwd:r,env:s});try{let e=await t.acquireSqlite({config:u,env:s});return v(t,e.leaseId,e.env)}catch(e){throw t.close(),e}}return g({cwd:r,databaseFile:i,env:s,tursoOutput:c})}async function m({config:e,cwd:n=t.cwd(),env:c=t.env}={}){let l=(e??await o({cwd:n})).database?.dialect??`postgres`;if(l!==`sqlite`)throw Error(`Temporary schema push checks require SQLite/libSQL, but this project uses ${l}.`);let u=await r(a.join(s(),`coreframe-schema-check-`));try{let e=await g({cwd:n,databaseFile:a.join(u,`turso.db`),env:c});return{env:e.env,async[Symbol.asyncDispose](){await e[Symbol.asyncDispose](),await i(u,{force:!0,recursive:!0})}}}catch(e){throw await i(u,{force:!0,recursive:!0}),e}}function h(e,t){let n=`http://127.0.0.1:${t}`;return{...e,DATABASE_URL:n,TURSO_AUTH_TOKEN:void 0,TURSO_DATABASE_URL:n,[d]:`1`}}async function g({cwd:e,databaseFile:t,env:r,tursoOutput:i}){await n(a.dirname(t),{recursive:!0});let o=await C(),s=h(r,o),l=c(f,[`dev`,`--port`,String(o),`--db-file`,t],{cwd:e,env:w(s),stdio:[`ignore`,`pipe`,`pipe`]});b(l,i);try{await x(l,o)}catch(e){throw await S(l),e}return y(s,l)}function _(e){return{env:e,async[Symbol.asyncDispose](){}}}function v(e,t,n){let r;return{env:n,async[Symbol.asyncDispose](){r??=(async()=>{try{await e.release(t)}finally{e.close()}})(),await r}}}function y(e,t){let n;return{env:e,async[Symbol.asyncDispose](){n??=S(t),await n}}}function b(e,t){t&&(e.stdout.on(`data`,e=>t.write(e)),e.stderr.on(`data`,e=>t.write(e)))}async function x(e,t){await new Promise((n,r)=>{let i=!1,a,o=setTimeout(()=>s(Error(`Turso did not start listening on port ${t} within 10 seconds.`)),1e4),s=t=>{i||(i=!0,clearTimeout(o),a&&clearTimeout(a),e.off(`error`,c),e.off(`exit`,u),t?r(t):n())},c=e=>s(e),u=(e,t)=>s(Error(`Turso exited before it started listening${e===null?``:` (code ${e})`}${t?` (${t})`:``}.`)),d=()=>{if(i)return;let e=l({host:`127.0.0.1`,port:t});e.once(`connect`,()=>{e.destroy(),s()}),e.once(`error`,()=>{e.destroy(),i||(a=setTimeout(d,50),a.unref())})};e.once(`error`,c),e.once(`exit`,u),d()})}async function S(e){e.exitCode!==null||e.signalCode!==null||await new Promise(t=>{let n=!1,r,i=()=>{n||(n=!0,r&&clearTimeout(r),e.off(`close`,i),e.off(`error`,i),e.off(`exit`,i),t())};r=setTimeout(()=>{e.kill(`SIGKILL`),i()},5e3),r.unref(),e.once(`close`,i),e.once(`error`,i),e.once(`exit`,i),!e.kill(`SIGTERM`)&&(e.exitCode!==null||e.signalCode!==null)&&i()})}async function C(){let e=u();try{await new Promise((t,n)=>{e.once(`error`,n),e.listen(0,`127.0.0.1`,t)});let t=e.address();if(!t||typeof t==`string`)throw Error(`Could not allocate an available TCP port for Turso.`);return t.port}finally{e.listening&&await new Promise((t,n)=>{e.close(e=>e?n(e):t())})}}function w(e){return Object.fromEntries(Object.entries(e).filter(e=>e[1]!==void 0))}export{d as LOCAL_TURSO_ENV_FLAG,h as createLocalTursoEnv,p as startProjectTurso,m as startTemporaryProjectTurso};
|
package/dist/migrate-held.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createProjectPostgresDatabaseAdapter as e,createProjectSqliteDatabaseAdapter as t}from"./project.js";import{createLocalPostgresDatabaseAdapter as n,startProjectPostgres as r}from"./local-postgres.js";import{collectHeldMigrationFiles as i,formatMigrationList as a}from"./migrations.js";import o from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{readFile as s}from"node:fs/promises";import{readProjectConfig as c}from"@coreframe/config";import{createHash as l}from"node:crypto";const u=[`-- Held migration:`,`-- Safe after:`,`-- Verification:`];async function d({cwd:s=process.cwd(),env:l=process.env,target:u=`deployment`}={}){let d=await i(s);if(console.log(`Held migration files:`),console.log(a(d)),d.length===0){console.log(`No held migrations to run.`);return}if(u===`local`)try{var p=o();let e=p.a(await r({cwd:s,env:l,useSupervisorLease:!0}));if(e.env.COREFRAME_LOCAL_POSTGRES!==`1`)throw Error(`Held migrations with --local require a Coreframe-managed PostgreSQL database.`);await f(()=>n({cwd:s,env:e.env}),d);return}catch(e){p.e=e}finally{await p.d()}let m=(await c({cwd:s})).database?.dialect??`postgres`;switch(m){case`postgres`:await f(()=>e({cwd:s,env:l}),d);return;case`sqlite`:await f(()=>t({cwd:s,env:l}),d);return;default:throw Error(`Unsupported database dialect "${m}".`)}}async function f(e,t){let n=await e();try{await p(n,t)}finally{await n.close?.()}}async function p(e,t){await g(e);for(let n of t){let t=await s(n.path,`utf8`);m(t,n.relativePath);let r=h(t),i=await e.execute(v(e.dialect,n.id));if(i.rows.length===1){if(i.rows[0]?.sha256!==r)throw Error(`Held migration ${n.relativePath} changed after it was applied.`);console.log(`Skipping already-applied held migration ${n.relativePath}`);continue}console.log(`Applying held migration ${n.relativePath}`),await x(e,async i=>{await i.execute(t),await i.execute(y(e.dialect,n.id,r))})}}function m(e,t){let n=u.filter(t=>!e.includes(t));if(n.length>0)throw Error(`${t} is missing held migration header fields: ${n.join(`, `)}`)}function h(e){return l(`sha256`).update(e).digest(`hex`)}async function g(e){for(let t of _(e.dialect))await e.execute(t)}function _(e){switch(e){case`postgres`:return[`create schema if not exists drizzle`,`create table if not exists drizzle.__held_migrations (
|
|
2
2
|
id text primary key,
|
|
3
3
|
sha256 text not null,
|
|
4
4
|
applied_at timestamptz not null default now()
|
package/dist/migrate.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createProjectSqliteDrizzleDatabase as e}from"./project.js";import{createLocalPostgresDb as t,startProjectPostgres as n}from"./local-postgres.js";import{startProjectTurso as r}from"./local-turso.js";import{MIGRATIONS_FOLDER as i}from"./config.js";import a from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{access as o}from"node:fs/promises";import{resolve as s}from"node:path";import{readProjectConfig as c}from"@coreframe/config";import{migrate as l}from"drizzle-orm/libsql/migrator";import{migrate as u}from"drizzle-orm/node-postgres/migrator";async function d({config:d,cwd:f=process.cwd(),env:p=process.env}={}){try{var m=a();let g=d??await c({cwd:f}),_=s(f,i);if(await o(_),g.database?.dialect===`sqlite`)try{var h=a();let t=await e({cwd:f,env:h.a(await r({config:g,cwd:f,env:p,useSupervisorLease:!0})).env});try{await l(t,{migrationsFolder:_}),console.log(`Applied local migrations from ${_}`)}finally{await t.$client?.close?.()}return}catch(e){h.e=e}finally{await h.d()}let v=await t({cwd:f,env:m.a(await n({config:g,cwd:f,env:p,useSupervisorLease:!0})).env});try{await u(v,{migrationsFolder:_}),console.log(`Applied local migrations from ${_}`)}finally{await v.$client.end()}}catch(e){m.e=e}finally{await m.d()}}export{d as migrateLocalDb};
|
package/dist/reset.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createProjectSqliteDrizzleDatabase as e}from"./project.js";import{createLocalPostgresDb as t,startProjectPostgres as n}from"./local-postgres.js";import{startProjectTurso as r}from"./local-turso.js";import{collectDeployMigrationFiles as i}from"./migrations.js";import a from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{inferSchemaDialect as o,planLocalSchemaPush as s}from"./schema-push.js";import{readFile as c}from"node:fs/promises";import{importProjectModule as l,readProjectConfig as u}from"@coreframe/config";import{sql as d}from"drizzle-orm";import{createHash as f}from"node:crypto";async function p({config:e,cwd:c=process.cwd(),env:f=process.env}={}){try{var p=a();let _=e??await u({cwd:c}),v=await l(`src/db/schema.ts`,c),y=o(v),b=_.database?.dialect??`postgres`;if(y!==b)throw Error(`Configured ${b} database does not match the ${y} schema in src/db/schema.ts.`);if(b===`sqlite`)try{var h=a();await m({cwd:c,env:h.a(await r({config:_,cwd:c,env:f,useSupervisorLease:!0})).env,schema:v});return}catch(e){h.e=e}finally{await h.d()}let x=await t({cwd:c,env:p.a(await n({config:_,cwd:c,env:f,useSupervisorLease:!0})).env});try{await x.transaction(async e=>{await e.execute(d`drop schema if exists public cascade`),await e.execute(d`drop schema if exists drizzle cascade`),await e.execute(d`create schema public`)}),await(await s(`postgres`,v,x)).apply(),await g(x,await i(c)),console.log(`Reset local PostgreSQL schema from the current Drizzle schema`)}finally{await x.$client.end()}}catch(e){p.e=e}finally{await p.d()}}async function m({cwd:t,env:n,schema:r}){let a=await e({cwd:t,env:n}),o=!1;try{let e=await a.all(d`
|
|
2
2
|
select name, type
|
|
3
3
|
from sqlite_schema
|
|
4
4
|
where type in ('table', 'index', 'trigger', 'view') and name not like 'sqlite_%'
|
|
5
|
-
`);await a.run(
|
|
5
|
+
`);await a.run(d`pragma foreign_keys = off`),o=!0,await a.transaction(async t=>{for(let n of[`trigger`,`view`,`index`,`table`])for(let r of e.filter(e=>e.type===n))await t.run(d`drop ${d.raw(n)} if exists ${d.identifier(r.name)}`)}),await(await s(`sqlite`,r,a)).apply(),await _(a,await i(t)),console.log(`Reset local SQLite/libSQL schema from the current Drizzle schema`)}finally{o&&await a.run(d`pragma foreign_keys = on`).catch(()=>void 0),await a.$client?.close?.()}}async function h(e){return Promise.all(e.map(async e=>({migration:e,hash:f(`sha256`).update(await c(e.path)).digest(`hex`),createdAt:v(e.id)})))}async function g(e,t){let n=await h(t);await e.transaction(async e=>{await e.execute(d`create schema if not exists drizzle`),await e.execute(d`
|
|
6
6
|
create table if not exists drizzle.__drizzle_migrations (
|
|
7
7
|
id serial primary key,
|
|
8
8
|
hash text not null,
|
|
@@ -10,10 +10,10 @@ import{collectDeployMigrationFiles as e}from"./migrations.js";import{createProje
|
|
|
10
10
|
name text,
|
|
11
11
|
applied_at timestamp with time zone default now()
|
|
12
12
|
)
|
|
13
|
-
`);for(let{migration:t,hash:r,createdAt:i}of n)await e.execute(
|
|
13
|
+
`);for(let{migration:t,hash:r,createdAt:i}of n)await e.execute(d`
|
|
14
14
|
insert into drizzle.__drizzle_migrations ("hash", "created_at", "name")
|
|
15
15
|
values (${r}, ${i}, ${t.id})
|
|
16
|
-
`)})}async function _(e,t){let n=await h(t),r=new Date().toISOString();await e.transaction(async e=>{await e.run(
|
|
16
|
+
`)})}async function _(e,t){let n=await h(t),r=new Date().toISOString();await e.transaction(async e=>{await e.run(d`
|
|
17
17
|
create table if not exists __drizzle_migrations (
|
|
18
18
|
id integer primary key,
|
|
19
19
|
hash text not null,
|
|
@@ -21,7 +21,7 @@ import{collectDeployMigrationFiles as e}from"./migrations.js";import{createProje
|
|
|
21
21
|
name text,
|
|
22
22
|
applied_at text
|
|
23
23
|
)
|
|
24
|
-
`);for(let{migration:t,hash:i,createdAt:a}of n)await e.run(
|
|
24
|
+
`);for(let{migration:t,hash:i,createdAt:a}of n)await e.run(d`
|
|
25
25
|
insert into __drizzle_migrations ("hash", "created_at", "name", "applied_at")
|
|
26
26
|
values (${i}, ${a}, ${t.id}, ${r})
|
|
27
27
|
`)})}function v(e){let t=e.slice(0,14);return/^\d{14}$/u.test(t)?Date.UTC(Number(t.slice(0,4)),Number(t.slice(4,6))-1,Number(t.slice(6,8)),Number(t.slice(8,10)),Number(t.slice(10,12)),Number(t.slice(12,14))):null}export{p as resetLocalDb};
|
package/dist/schema-push.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{pathToFileURL as e}from"node:url";import{resolve as t}from"node:path";import{is as n}from"drizzle-orm";import{PgMaterializedView as r,PgTable as i,PgView as a}from"drizzle-orm/pg-core";import{SQLiteTable as o,SQLiteView as s}from"drizzle-orm/sqlite-core";import{resolve as c}from"import-meta-resolve";const l={postgres:`drizzle-kit/payload/postgres`,sqlite:`drizzle-kit/payload/sqlite`};async function u(e,t,n,r=m){if(e===`postgres`){let{pushSchema:e}=await r(`postgres`);return e(t,n)}let{pushSchema:i}=await r(`sqlite`);return i(t,d(n))}function d(e){if(f(e))return e;let t=e?.$client??e;if(!t||typeof t.execute!=`function`)throw Error(`SQLite schema push requires a Drizzle database with a libSQL client or a native SQLite schema-push client.`);return{query:async(e,n=[])=>(await t.execute({sql:e,args:n})).rows,run:async e=>{await t.execute(e)},batch:async e=>{if(t.migrate){await t.migrate(e);return}if(t.batch){await t.batch(e);return}for(let n of e)await t.execute(n)}}}function f(e){if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.query==`function`&&typeof t.run==`function`&&typeof t.batch==`function`}function p(e){let t=!1,n=!1;for(let r of Object.values(e))t||=h(r),n||=g(r);if(t&&n)throw Error(`Local database schema mixes PostgreSQL and SQLite entities.`);if(t)return`postgres`;if(n)return`sqlite`;throw Error(`Could not infer local database dialect from src/db/schema.ts.`)}async function m(n){return await import(c(l[n],e(t(`package.json`)).href))}function h(e){return n(e,i)||n(e,a)||n(e,r)}function g(e){return n(e,o)||n(e,s)}export{p as inferSchemaDialect,u as planLocalSchemaPush};
|
package/dist/seed.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{startProjectPostgres as e}from"./local-postgres.js";import{startProjectTurso as t}from"./local-turso.js";import{getSeedProfile as n}from"./config.js";import r from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{importProjectModule as i,readProjectConfig as a}from"@coreframe/config";async function o(i=n(),{config:o,cwd:l=process.cwd(),env:u=process.env}={}){try{var d=r();let n=o??await a({cwd:l}),p=c(n);if(n.database?.dialect===`sqlite`)try{var f=r();await s(i,{cwd:l,env:f.a(await t({config:n,cwd:l,env:u,useSupervisorLease:!0})).env,seedEntryPoint:p}),console.log(`Seeded local database with "${i}" profile`);return}catch(e){f.e=e}finally{await f.d()}await s(i,{cwd:l,env:d.a(await e({config:n,cwd:l,env:u,useSupervisorLease:!0})).env,seedEntryPoint:p}),console.log(`Seeded local database with "${i}" profile`)}catch(e){d.e=e}finally{await d.d()}}async function s(e=n(),{cwd:t=process.cwd(),env:r,seedEntryPoint:o}){o??=c(await a({cwd:t})),await u(r,async()=>{await l(await i(o,t),o)({env:r,profile:e})})}function c(e){let t=e.database?.seed;if(!t)throw Error(`coreframe.config.ts must configure database.seed before running db seed.`);return t}function l(e,t){let n=e.default??e.seed??e.seedLocalDb;if(!n)throw Error(`${t} must export a default seed function, seed function, or seedLocalDb function.`);return n}async function u(e,t){let n=new Map;for(let[t,r]of Object.entries(e))n.set(t,process.env[t]),r===void 0?delete process.env[t]:process.env[t]=r;try{await t()}finally{for(let[e,t]of n)t===void 0?delete process.env[e]:process.env[e]=t}}export{s as runProjectSeed,o as seedLocalDb};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coreframe/db",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Database setup, migration, seeding, and local PostgreSQL and SQLite/libSQL utilities for Coreframe apps",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,6 +15,10 @@
|
|
|
15
15
|
".": {
|
|
16
16
|
"types": "./dist/index.d.ts",
|
|
17
17
|
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./dev-supervisor": {
|
|
20
|
+
"types": "./dist/dev-supervisor.d.ts",
|
|
21
|
+
"default": "./dist/dev-supervisor.js"
|
|
18
22
|
}
|
|
19
23
|
},
|
|
20
24
|
"dependencies": {
|
|
@@ -22,8 +26,8 @@
|
|
|
22
26
|
"import-meta-resolve": "^4.2.0",
|
|
23
27
|
"local-postgres": "0.3.1",
|
|
24
28
|
"nano-spawn": "^2.1.0",
|
|
25
|
-
"@coreframe/config": "^0.1.
|
|
26
|
-
"@coreframe/dev-supervisor": "^0.1.
|
|
29
|
+
"@coreframe/config": "^0.1.7",
|
|
30
|
+
"@coreframe/dev-supervisor": "^0.1.7"
|
|
27
31
|
},
|
|
28
32
|
"devDependencies": {
|
|
29
33
|
"@libsql/client": "^0.17.4",
|