@murumets-ee/db 0.1.1 → 0.1.2
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/package.json +4 -6
- package/dist/index.cjs +0 -15
- package/dist/index.d.cts +0 -74
- package/dist/test-utils.cjs +0 -1
- package/dist/test-utils.d.cts +0 -27
package/package.json
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murumets-ee/db",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
-
"import": "./dist/index.js"
|
|
10
|
-
"require": "./dist/index.cjs"
|
|
9
|
+
"import": "./dist/index.js"
|
|
11
10
|
},
|
|
12
11
|
"./test-utils": {
|
|
13
12
|
"types": "./dist/test-utils.d.ts",
|
|
14
|
-
"import": "./dist/test-utils.js"
|
|
15
|
-
"require": "./dist/test-utils.cjs"
|
|
13
|
+
"import": "./dist/test-utils.js"
|
|
16
14
|
}
|
|
17
15
|
},
|
|
18
16
|
"files": [
|
|
@@ -23,7 +21,7 @@
|
|
|
23
21
|
"postgres": "^3.4.5"
|
|
24
22
|
},
|
|
25
23
|
"devDependencies": {
|
|
26
|
-
"drizzle-kit": "^0.
|
|
24
|
+
"drizzle-kit": "^0.31.10",
|
|
27
25
|
"tsup": "^8.3.5",
|
|
28
26
|
"typescript": "^5.7.2",
|
|
29
27
|
"vitest": "^2.1.8"
|
package/dist/index.cjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';var postgresJs=require('drizzle-orm/postgres-js'),g=require('postgres'),fs=require('fs'),promises=require('fs/promises'),path=require('path'),drizzleOrm=require('drizzle-orm');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var g__default=/*#__PURE__*/_interopDefault(g);function S(a){let t=g__default.default(a.url,{max:a.poolMax||10,idle_timeout:20,max_lifetime:1800});return postgresJs.drizzle(t)}function b(a){let t=a.readOnlyUrl||a.url,n=g__default.default(t,{max:a.poolMax||10,idle_timeout:20,max_lifetime:1800,connection:{application_name:"toolkit_readonly"},onnotice:()=>{}});return n.unsafe("SET default_transaction_read_only = on").catch(()=>{}),postgresJs.drizzle(n)}async function d(a){await a.execute(drizzleOrm.sql`
|
|
2
|
-
CREATE TABLE IF NOT EXISTS _toolkit_migrations (
|
|
3
|
-
id SERIAL PRIMARY KEY,
|
|
4
|
-
namespace VARCHAR(50) NOT NULL,
|
|
5
|
-
name VARCHAR(255) NOT NULL,
|
|
6
|
-
applied_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
7
|
-
UNIQUE(namespace, name)
|
|
8
|
-
)
|
|
9
|
-
`);}async function h(a){let t=[],n=path.join(a,"migrations",".toolkit");if(fs.existsSync(n)){let o=await promises.readdir(n);for(let e of o)(e.endsWith(".sql")||e.endsWith(".ts"))&&t.push({path:path.join(n,e),namespace:"toolkit",name:e});}let i=path.join(a,"migrations");if(fs.existsSync(i)){let o=await promises.readdir(i);for(let e of o)e!==".toolkit"&&(e.endsWith(".sql")||e.endsWith(".ts"))&&t.push({path:path.join(i,e),namespace:"project",name:e});}let r=t.filter(o=>o.namespace==="toolkit").sort((o,e)=>o.name.localeCompare(e.name)),s=t.filter(o=>o.namespace==="project").sort((o,e)=>o.name.localeCompare(e.name));return [...r,...s]}async function y(a,t){await d(a);let n=await h(t),i=await a.execute(drizzleOrm.sql`
|
|
10
|
-
SELECT namespace, name FROM _toolkit_migrations
|
|
11
|
-
ORDER BY applied_at ASC
|
|
12
|
-
`),r=new Set(i.map(e=>`${e.namespace}:${e.name}`)),s=[],o=[];for(let e of n){let M=`${e.namespace}:${e.name}`;r.has(M)?s.push(e):o.push(e);}return {applied:s,pending:o}}async function x(a,t){await d(a);let{pending:n}=await y(a,t);if(n.length===0){console.log("No pending migrations");return}console.log(`Running ${n.length} pending migrations...`);for(let i of n){console.log(` Applying ${i.namespace}/${i.name}...`);try{let r=await promises.readFile(i.path,"utf-8");await a.transaction(async s=>{await s.execute(drizzleOrm.sql.raw(r)),await s.execute(drizzleOrm.sql`
|
|
13
|
-
INSERT INTO _toolkit_migrations (namespace, name)
|
|
14
|
-
VALUES (${i.namespace}, ${i.name})
|
|
15
|
-
`);}),console.log(` \u2713 Applied ${i.namespace}/${i.name}`);}catch(r){throw console.error(` \u2717 Failed to apply ${i.namespace}/${i.name}:`,r),r}}console.log(`Successfully applied ${n.length} migrations`);}var m=class{schemas=new Map;register(t,n){this.schemas.set(t,n);}get(t){return this.schemas.get(t)}all(){let t={};for(let[n,i]of this.schemas.entries())t[n]=i;return t}has(t){return this.schemas.has(t)}},R=new m;function A(){return new m}exports.createDbClient=S;exports.createReadOnlyClient=b;exports.createSchemaRegistry=A;exports.discoverMigrations=h;exports.getMigrationStatus=y;exports.runMigrations=x;exports.schemaRegistry=R;
|
package/dist/index.d.cts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
|
-
import { PgTableWithColumns } from 'drizzle-orm/pg-core';
|
|
3
|
-
|
|
4
|
-
interface DbConfig {
|
|
5
|
-
url: string;
|
|
6
|
-
readOnlyUrl?: string;
|
|
7
|
-
poolMin?: number;
|
|
8
|
-
poolMax?: number;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Create a full read-write database client
|
|
12
|
-
*/
|
|
13
|
-
declare function createDbClient(config: DbConfig): PostgresJsDatabase;
|
|
14
|
-
/**
|
|
15
|
-
* Create a read-only database client
|
|
16
|
-
* Sets default_transaction_read_only=on to prevent writes even if SQL injection occurs
|
|
17
|
-
*/
|
|
18
|
-
declare function createReadOnlyClient(config: DbConfig): PostgresJsDatabase;
|
|
19
|
-
|
|
20
|
-
interface MigrationSource {
|
|
21
|
-
path: string;
|
|
22
|
-
namespace: 'toolkit' | 'project';
|
|
23
|
-
name: string;
|
|
24
|
-
}
|
|
25
|
-
interface MigrationStatus {
|
|
26
|
-
applied: MigrationSource[];
|
|
27
|
-
pending: MigrationSource[];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Discover migrations from both toolkit (.toolkit/) and project (migrations/) directories
|
|
31
|
-
*/
|
|
32
|
-
declare function discoverMigrations(projectRoot: string): Promise<MigrationSource[]>;
|
|
33
|
-
/**
|
|
34
|
-
* Get the status of migrations (applied vs pending)
|
|
35
|
-
*/
|
|
36
|
-
declare function getMigrationStatus(db: PostgresJsDatabase, projectRoot: string): Promise<MigrationStatus>;
|
|
37
|
-
/**
|
|
38
|
-
* Run pending migrations
|
|
39
|
-
*/
|
|
40
|
-
declare function runMigrations(db: PostgresJsDatabase, projectRoot: string): Promise<void>;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Schema registry for storing Drizzle schemas
|
|
44
|
-
* This will be populated by @org/entity when schemas are generated from entity definitions
|
|
45
|
-
*
|
|
46
|
-
* We use PgTableWithColumns<any> because entity table schemas are dynamic (columns
|
|
47
|
-
* not known at compile time) and we need runtime property access (.id, .status, etc.).
|
|
48
|
-
* AnyPgTable won't work here — it doesn't expose column accessors.
|
|
49
|
-
*/
|
|
50
|
-
type AnyTable = PgTableWithColumns<any>;
|
|
51
|
-
interface SchemaRegistry {
|
|
52
|
-
register(name: string, schema: AnyTable): void;
|
|
53
|
-
get(name: string): AnyTable | undefined;
|
|
54
|
-
all(): Record<string, AnyTable>;
|
|
55
|
-
has(name: string): boolean;
|
|
56
|
-
}
|
|
57
|
-
declare class SchemaRegistryImpl implements SchemaRegistry {
|
|
58
|
-
private schemas;
|
|
59
|
-
register(name: string, schema: AnyTable): void;
|
|
60
|
-
get(name: string): AnyTable | undefined;
|
|
61
|
-
all(): Record<string, AnyTable>;
|
|
62
|
-
has(name: string): boolean;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Global schema registry instance
|
|
66
|
-
* Entities will register their schemas here when they are defined
|
|
67
|
-
*/
|
|
68
|
-
declare const schemaRegistry: SchemaRegistryImpl;
|
|
69
|
-
/**
|
|
70
|
-
* Create a new isolated schema registry (useful for testing)
|
|
71
|
-
*/
|
|
72
|
-
declare function createSchemaRegistry(): SchemaRegistry;
|
|
73
|
-
|
|
74
|
-
export { type DbConfig, type MigrationSource, type MigrationStatus, type SchemaRegistry, createDbClient, createReadOnlyClient, createSchemaRegistry, discoverMigrations, getMigrationStatus, runMigrations, schemaRegistry };
|
package/dist/test-utils.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
'use strict';var drizzleOrm=require('drizzle-orm'),postgresJs=require('drizzle-orm/postgres-js'),T=require('postgres');var _documentCurrentScript=typeof document!=='undefined'?document.currentScript:null;function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var T__default=/*#__PURE__*/_interopDefault(T);async function S(){let a=process.env.DATABASE_TEST_URL||process.env.DATABASE_URL;if(!a)throw new Error("DATABASE_TEST_URL (or DATABASE_URL) env var is required for integration tests");let r=`test_${crypto.randomUUID().replaceAll("-","").slice(0,12)}`,n=T__default.default(a,{max:3,idle_timeout:10}),e=postgresJs.drizzle(n);return await e.execute(drizzleOrm.sql.raw(`CREATE SCHEMA "${r}"`)),await e.execute(drizzleOrm.sql.raw(`SET search_path TO "${r}", public`)),{db:e,schema:r,async push(c){let{createRequire:u}=await import('module'),p=u((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('test-utils.cjs', document.baseURI).href))),{pushSchema:m}=p("drizzle-kit/api"),l=new Proxy(e,{get(o,i,w){return i==="execute"?async(...A)=>{let t=await o.execute(...A);return Array.isArray(t)&&!("rows"in t)&&Object.defineProperty(t,"rows",{value:[...t],enumerable:false}),t}:Reflect.get(o,i,w)}});await(await m(c,l)).apply();},async cleanup(){await e.execute(drizzleOrm.sql.raw(`DROP SCHEMA IF EXISTS "${r}" CASCADE`)),await n.end();}}}exports.createTestDb=S;
|
package/dist/test-utils.d.cts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
|
-
|
|
3
|
-
interface TestDb {
|
|
4
|
-
db: PostgresJsDatabase;
|
|
5
|
-
schema: string;
|
|
6
|
-
/** Push Drizzle table definitions to the test schema (uses drizzle-kit/api) */
|
|
7
|
-
push: (schemas: Record<string, unknown>) => Promise<void>;
|
|
8
|
-
cleanup: () => Promise<void>;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Create an isolated test database using a unique PostgreSQL schema.
|
|
12
|
-
* Each test suite gets its own schema so tests can run in parallel safely.
|
|
13
|
-
*
|
|
14
|
-
* Requires `DATABASE_TEST_URL` env var pointing to a running PostgreSQL instance.
|
|
15
|
-
* Falls back to `DATABASE_URL` if test URL is not set.
|
|
16
|
-
*
|
|
17
|
-
* Usage:
|
|
18
|
-
* ```ts
|
|
19
|
-
* const testDb = await createTestDb()
|
|
20
|
-
* await testDb.push({ articles: articlesTable, entity_refs: entityRefsTable })
|
|
21
|
-
* // ... run tests with testDb.db ...
|
|
22
|
-
* await testDb.cleanup()
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
declare function createTestDb(): Promise<TestDb>;
|
|
26
|
-
|
|
27
|
-
export { type TestDb, createTestDb };
|