@murumets-ee/db 0.8.0 → 0.10.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.d.mts +171 -132
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +6 -6
- package/dist/index.mjs.map +1 -1
- package/dist/test-utils.mjs +1 -1
- package/dist/test-utils.mjs.map +1 -1
- package/package.json +4 -1
package/dist/test-utils.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{drizzle as e}from"drizzle-orm/postgres-js";import t from"postgres";import{sql as n}from"drizzle-orm";async function r(){let r=process.env.DATABASE_TEST_URL||process.env.DATABASE_URL;if(!r)throw Error(`DATABASE_TEST_URL (or DATABASE_URL) env var is required for integration tests`);let
|
|
1
|
+
import{drizzle as e}from"drizzle-orm/postgres-js";import t from"postgres";import{sql as n}from"drizzle-orm";async function r(){if(process.env.NODE_ENV===`production`)throw Error(`createTestDb() is not allowed when NODE_ENV=production — it issues CREATE/DROP SCHEMA and is intended for tests only`);let r=process.env.DATABASE_TEST_URL||process.env.DATABASE_URL;if(!r)throw Error(`DATABASE_TEST_URL (or DATABASE_URL) env var is required for integration tests`);let a=`test_${crypto.randomUUID().replaceAll(`-`,``).slice(0,12)}`,o=t(r,{max:3,idle_timeout:10}),s=e(o);return await s.execute(n.raw(`CREATE SCHEMA "${a}"`)),await s.execute(n.raw(`SET search_path TO "${a}", public`)),{db:s,schema:a,async push(e){let{createRequire:t}=await import(`node:module`),{pushSchema:n}=t(import.meta.url)(`drizzle-kit/api`);await(await n(e,i(s))).apply()},async cleanup(){await s.execute(n.raw(`DROP SCHEMA IF EXISTS "${a}" CASCADE`)),await o.end()}}}function i(e){return new Proxy(e,{get(e,t,n){return t===`execute`?async(...t)=>{let n=await e.execute(...t);return Array.isArray(n)&&!(`rows`in n)&&Object.defineProperty(n,`rows`,{value:[...n],enumerable:!1}),n}:Reflect.get(e,t,n)}})}export{r as createTestDb};
|
|
2
2
|
//# sourceMappingURL=test-utils.mjs.map
|
package/dist/test-utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-utils.mjs","names":[],"sources":["../src/test-utils.ts"],"sourcesContent":["import { sql } from 'drizzle-orm'\nimport { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport postgres from 'postgres'\n\nexport interface TestDb {\n db: PostgresJsDatabase\n schema: string\n /** Push Drizzle table definitions to the test schema (uses drizzle-kit/api) */\n push: (schemas: Record<string, unknown>) => Promise<void>\n cleanup: () => Promise<void>\n}\n\n/**\n * Create an isolated test database using a unique PostgreSQL schema.\n * Each test suite gets its own schema so tests can run in parallel safely.\n *\n * Requires `DATABASE_TEST_URL` env var pointing to a running PostgreSQL instance.\n * Falls back to `DATABASE_URL` if test URL is not set.\n *\n * Usage:\n * ```ts\n * const testDb = await createTestDb()\n * await testDb.push({ articles: articlesTable, entity_refs: entityRefsTable })\n * // ... run tests with testDb.db ...\n * await testDb.cleanup()\n * ```\n */\nexport async function createTestDb(): Promise<TestDb> {\n const url = process.env.DATABASE_TEST_URL || process.env.DATABASE_URL\n if (!url) {\n throw new Error('DATABASE_TEST_URL (or DATABASE_URL) env var is required for integration tests')\n }\n\n const schemaName = `test_${crypto.randomUUID().replaceAll('-', '').slice(0, 12)}`\n\n const connection = postgres(url, {\n max: 3,\n idle_timeout: 10,\n })\n\n const db = drizzle(connection)\n\n // Create isolated schema\n await db.execute(sql.raw(`CREATE SCHEMA \"${schemaName}\"`))\n await db.execute(sql.raw(`SET search_path TO \"${schemaName}\", public`))\n\n return {\n db,\n schema: schemaName,\n\n async push(schemas: Record<string, unknown>) {\n // Use createRequire to load drizzle-kit/api via Node's CJS resolver.\n // drizzle-kit/api.mjs bundles CJS code with require('fs') — Vite's ESM\n // transform replaces require() with a polyfill that throws. Native CJS bypasses this.\n const { createRequire } = await import('node:module')\n const require = createRequire(import.meta.url)\n const { pushSchema } = require('drizzle-kit/api') as typeof import('drizzle-kit/api')\n\n //
|
|
1
|
+
{"version":3,"file":"test-utils.mjs","names":[],"sources":["../src/test-utils.ts"],"sourcesContent":["import { sql } from 'drizzle-orm'\nimport { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport postgres from 'postgres'\n\nexport interface TestDb {\n db: PostgresJsDatabase\n schema: string\n /** Push Drizzle table definitions to the test schema (uses drizzle-kit/api) */\n push: (schemas: Record<string, unknown>) => Promise<void>\n cleanup: () => Promise<void>\n}\n\n/**\n * Create an isolated test database using a unique PostgreSQL schema.\n * Each test suite gets its own schema so tests can run in parallel safely.\n *\n * Requires `DATABASE_TEST_URL` env var pointing to a running PostgreSQL instance.\n * Falls back to `DATABASE_URL` if test URL is not set.\n *\n * Usage:\n * ```ts\n * const testDb = await createTestDb()\n * await testDb.push({ articles: articlesTable, entity_refs: entityRefsTable })\n * // ... run tests with testDb.db ...\n * await testDb.cleanup()\n * ```\n */\nexport async function createTestDb(): Promise<TestDb> {\n // Refuse to ever provision a scratch schema in production. `createTestDb`\n // issues `CREATE SCHEMA` and (via `cleanup()`) `DROP SCHEMA … CASCADE`,\n // which would be catastrophic if pointed at a real database. The whole\n // function is test-only, but the npm subpath export means an app could\n // import it by accident — fail loud instead of running.\n if (process.env.NODE_ENV === 'production') {\n throw new Error(\n 'createTestDb() is not allowed when NODE_ENV=production — it issues CREATE/DROP SCHEMA and is intended for tests only',\n )\n }\n\n const url = process.env.DATABASE_TEST_URL || process.env.DATABASE_URL\n if (!url) {\n throw new Error('DATABASE_TEST_URL (or DATABASE_URL) env var is required for integration tests')\n }\n\n const schemaName = `test_${crypto.randomUUID().replaceAll('-', '').slice(0, 12)}`\n\n const connection = postgres(url, {\n max: 3,\n idle_timeout: 10,\n })\n\n const db = drizzle(connection)\n\n // Create isolated schema\n await db.execute(sql.raw(`CREATE SCHEMA \"${schemaName}\"`))\n await db.execute(sql.raw(`SET search_path TO \"${schemaName}\", public`))\n\n return {\n db,\n schema: schemaName,\n\n async push(schemas: Record<string, unknown>) {\n // Use createRequire to load drizzle-kit/api via Node's CJS resolver.\n // drizzle-kit/api.mjs bundles CJS code with require('fs') — Vite's ESM\n // transform replaces require() with a polyfill that throws. Native CJS bypasses this.\n const { createRequire } = await import('node:module')\n const require = createRequire(import.meta.url)\n const { pushSchema } = require('drizzle-kit/api') as typeof import('drizzle-kit/api')\n\n // Use default schemaFilters ([\"public\"]). Our tables are unqualified (= public),\n // and SET search_path ensures DDL actually creates them in the test schema.\n const result = await pushSchema(schemas, withRowsShim(db))\n await result.apply()\n },\n\n async cleanup() {\n await db.execute(sql.raw(`DROP SCHEMA IF EXISTS \"${schemaName}\" CASCADE`))\n await connection.end()\n },\n }\n}\n\n/**\n * Wrap a Drizzle handle so `.execute()` returns an array that *also* exposes\n * a `.rows` property. `drizzle-kit/api`'s `pushSchema` reads `res.rows`, but\n * postgres-js (the driver Drizzle is built on here) returns plain arrays\n * with no `.rows`. The shim is local to test-utils — production code never\n * needs it.\n */\nfunction withRowsShim(db: PostgresJsDatabase): PostgresJsDatabase {\n return new Proxy(db, {\n get(target, prop, receiver) {\n if (prop === 'execute') {\n return async (...args: unknown[]) => {\n const result = await (target.execute as (...a: unknown[]) => Promise<unknown[]>)(...args)\n if (Array.isArray(result) && !('rows' in result)) {\n Object.defineProperty(result, 'rows', { value: [...result], enumerable: false })\n }\n return result\n }\n }\n return Reflect.get(target, prop, receiver)\n },\n })\n}\n"],"mappings":"4GA2BA,eAAsB,GAAgC,CAMpD,GAAI,QAAQ,IAAI,WAAa,aAC3B,MAAU,MACR,uHACD,CAGH,IAAM,EAAM,QAAQ,IAAI,mBAAqB,QAAQ,IAAI,aACzD,GAAI,CAAC,EACH,MAAU,MAAM,gFAAgF,CAGlG,IAAM,EAAa,QAAQ,OAAO,YAAY,CAAC,WAAW,IAAK,GAAG,CAAC,MAAM,EAAG,GAAG,GAEzE,EAAa,EAAS,EAAK,CAC/B,IAAK,EACL,aAAc,GACf,CAAC,CAEI,EAAK,EAAQ,EAAW,CAM9B,OAHA,MAAM,EAAG,QAAQ,EAAI,IAAI,kBAAkB,EAAW,GAAG,CAAC,CAC1D,MAAM,EAAG,QAAQ,EAAI,IAAI,uBAAuB,EAAW,WAAW,CAAC,CAEhE,CACL,KACA,OAAQ,EAER,MAAM,KAAK,EAAkC,CAI3C,GAAM,CAAE,iBAAkB,MAAM,OAAO,eAEjC,CAAE,cADQ,EAAc,OAAO,KAAK,IAAI,CACf,kBAAkB,CAKjD,MADe,MAAM,EAAW,EAAS,EAAa,EAAG,CAAC,EAC7C,OAAO,EAGtB,MAAM,SAAU,CACd,MAAM,EAAG,QAAQ,EAAI,IAAI,0BAA0B,EAAW,WAAW,CAAC,CAC1E,MAAM,EAAW,KAAK,EAEzB,CAUH,SAAS,EAAa,EAA4C,CAChE,OAAO,IAAI,MAAM,EAAI,CACnB,IAAI,EAAQ,EAAM,EAAU,CAU1B,OATI,IAAS,UACJ,MAAO,GAAG,IAAoB,CACnC,IAAM,EAAS,MAAO,EAAO,QAAoD,GAAG,EAAK,CAIzF,OAHI,MAAM,QAAQ,EAAO,EAAI,EAAE,SAAU,IACvC,OAAO,eAAe,EAAQ,OAAQ,CAAE,MAAO,CAAC,GAAG,EAAO,CAAE,WAAY,GAAO,CAAC,CAE3E,GAGJ,QAAQ,IAAI,EAAQ,EAAM,EAAS,EAE7C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murumets-ee/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -26,6 +26,9 @@
|
|
|
26
26
|
"typescript": "^5.7.2",
|
|
27
27
|
"vitest": "^2.1.8"
|
|
28
28
|
},
|
|
29
|
+
"typeCoverage": {
|
|
30
|
+
"atLeast": 99.98
|
|
31
|
+
},
|
|
29
32
|
"scripts": {
|
|
30
33
|
"build": "tsdown",
|
|
31
34
|
"dev": "tsdown --watch",
|