@branchly/migrator-drizzle 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.
@@ -0,0 +1,15 @@
1
+ import { MigratorAdapter } from 'branchly';
2
+
3
+ type CommandRunner = (command: string, env: NodeJS.ProcessEnv, cwd: string) => Promise<void>;
4
+ interface DrizzleMigratorOptions {
5
+ readonly migrationsDir?: string;
6
+ readonly seed?: string;
7
+ readonly applyCommand?: string;
8
+ readonly urlEnv?: string;
9
+ readonly cwd?: string;
10
+ readonly run?: CommandRunner;
11
+ }
12
+ declare const fingerprintFiles: (files: readonly string[]) => string;
13
+ declare const createDrizzleMigrator: (options?: DrizzleMigratorOptions) => MigratorAdapter;
14
+
15
+ export { type CommandRunner, type DrizzleMigratorOptions, createDrizzleMigrator, createDrizzleMigrator as default, fingerprintFiles };
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ // src/index.ts
2
+ import { exec } from "child_process";
3
+ import { createHash } from "crypto";
4
+ import { readdir } from "fs/promises";
5
+ import { join } from "path";
6
+ import process from "process";
7
+ import { promisify } from "util";
8
+ var execAsync = promisify(exec);
9
+ var FINGERPRINT_LENGTH = 16;
10
+ var DEFAULT_MIGRATIONS_DIR = "drizzle";
11
+ var DEFAULT_APPLY_COMMAND = "npx drizzle-kit migrate";
12
+ var DEFAULT_URL_ENV = "DATABASE_URL";
13
+ var fingerprintFiles = (files) => createHash("sha256").update([...files].sort().join("\n")).digest("hex").slice(0, FINGERPRINT_LENGTH);
14
+ var listMigrationFiles = async (dir) => {
15
+ const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
16
+ return entries.filter((entry) => entry.isFile() && entry.name.endsWith(".sql")).map((entry) => entry.name);
17
+ };
18
+ var defaultRunner = async (command, env, cwd) => {
19
+ await execAsync(command, { env, cwd });
20
+ };
21
+ var buildEnv = (urlEnv, connection) => ({ ...process.env, [urlEnv]: connection });
22
+ var createDrizzleMigrator = (options = {}) => {
23
+ const migrationsDir = options.migrationsDir ?? DEFAULT_MIGRATIONS_DIR;
24
+ const applyCommand = options.applyCommand ?? DEFAULT_APPLY_COMMAND;
25
+ const urlEnv = options.urlEnv ?? DEFAULT_URL_ENV;
26
+ const cwd = options.cwd ?? process.cwd();
27
+ const run = options.run ?? defaultRunner;
28
+ return {
29
+ id: "drizzle",
30
+ apiVersion: 1,
31
+ fingerprint: async () => fingerprintFiles(await listMigrationFiles(join(cwd, migrationsDir))),
32
+ apply: (connection) => run(applyCommand, buildEnv(urlEnv, connection), cwd),
33
+ seed: async (connection) => {
34
+ if (options.seed === void 0) {
35
+ return;
36
+ }
37
+ await run(options.seed, buildEnv(urlEnv, connection), cwd);
38
+ }
39
+ };
40
+ };
41
+ var index_default = createDrizzleMigrator;
42
+ export {
43
+ createDrizzleMigrator,
44
+ index_default as default,
45
+ fingerprintFiles
46
+ };
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { exec } from 'node:child_process';\nimport { createHash } from 'node:crypto';\nimport { readdir } from 'node:fs/promises';\nimport { join } from 'node:path';\nimport process from 'node:process';\nimport { promisify } from 'node:util';\n\nimport type { MigratorAdapter } from 'branchly';\n\nconst execAsync = promisify(exec);\n\nconst FINGERPRINT_LENGTH = 16;\nconst DEFAULT_MIGRATIONS_DIR = 'drizzle';\nconst DEFAULT_APPLY_COMMAND = 'npx drizzle-kit migrate';\nconst DEFAULT_URL_ENV = 'DATABASE_URL';\n\nexport type CommandRunner = (command: string, env: NodeJS.ProcessEnv, cwd: string) => Promise<void>;\n\nexport interface DrizzleMigratorOptions {\n readonly migrationsDir?: string;\n readonly seed?: string;\n readonly applyCommand?: string;\n readonly urlEnv?: string;\n readonly cwd?: string;\n readonly run?: CommandRunner;\n}\n\nexport const fingerprintFiles = (files: readonly string[]): string =>\n createHash('sha256')\n .update([...files].sort().join('\\n'))\n .digest('hex')\n .slice(0, FINGERPRINT_LENGTH);\n\nconst listMigrationFiles = async (dir: string): Promise<string[]> => {\n const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);\n return entries.filter((entry) => entry.isFile() && entry.name.endsWith('.sql')).map((entry) => entry.name);\n};\n\nconst defaultRunner: CommandRunner = async (command, env, cwd) => {\n await execAsync(command, { env, cwd });\n};\n\nconst buildEnv = (urlEnv: string, connection: string): NodeJS.ProcessEnv => ({ ...process.env, [urlEnv]: connection });\n\nexport const createDrizzleMigrator = (options: DrizzleMigratorOptions = {}): MigratorAdapter => {\n const migrationsDir = options.migrationsDir ?? DEFAULT_MIGRATIONS_DIR;\n const applyCommand = options.applyCommand ?? DEFAULT_APPLY_COMMAND;\n const urlEnv = options.urlEnv ?? DEFAULT_URL_ENV;\n const cwd = options.cwd ?? process.cwd();\n const run = options.run ?? defaultRunner;\n return {\n id: 'drizzle',\n apiVersion: 1,\n fingerprint: async () => fingerprintFiles(await listMigrationFiles(join(cwd, migrationsDir))),\n apply: (connection) => run(applyCommand, buildEnv(urlEnv, connection), cwd),\n seed: async (connection) => {\n if (options.seed === undefined) {\n return;\n }\n await run(options.seed, buildEnv(urlEnv, connection), cwd);\n },\n };\n};\n\nexport default createDrizzleMigrator;\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,SAAS,kBAAkB;AAC3B,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,OAAO,aAAa;AACpB,SAAS,iBAAiB;AAI1B,IAAM,YAAY,UAAU,IAAI;AAEhC,IAAM,qBAAqB;AAC3B,IAAM,yBAAyB;AAC/B,IAAM,wBAAwB;AAC9B,IAAM,kBAAkB;AAajB,IAAM,mBAAmB,CAAC,UAC/B,WAAW,QAAQ,EAChB,OAAO,CAAC,GAAG,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI,CAAC,EACnC,OAAO,KAAK,EACZ,MAAM,GAAG,kBAAkB;AAEhC,IAAM,qBAAqB,OAAO,QAAmC;AACnE,QAAM,UAAU,MAAM,QAAQ,KAAK,EAAE,eAAe,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC,CAAC;AAC1E,SAAO,QAAQ,OAAO,CAAC,UAAU,MAAM,OAAO,KAAK,MAAM,KAAK,SAAS,MAAM,CAAC,EAAE,IAAI,CAAC,UAAU,MAAM,IAAI;AAC3G;AAEA,IAAM,gBAA+B,OAAO,SAAS,KAAK,QAAQ;AAChE,QAAM,UAAU,SAAS,EAAE,KAAK,IAAI,CAAC;AACvC;AAEA,IAAM,WAAW,CAAC,QAAgB,gBAA2C,EAAE,GAAG,QAAQ,KAAK,CAAC,MAAM,GAAG,WAAW;AAE7G,IAAM,wBAAwB,CAAC,UAAkC,CAAC,MAAuB;AAC9F,QAAM,gBAAgB,QAAQ,iBAAiB;AAC/C,QAAM,eAAe,QAAQ,gBAAgB;AAC7C,QAAM,SAAS,QAAQ,UAAU;AACjC,QAAM,MAAM,QAAQ,OAAO,QAAQ,IAAI;AACvC,QAAM,MAAM,QAAQ,OAAO;AAC3B,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,YAAY;AAAA,IACZ,aAAa,YAAY,iBAAiB,MAAM,mBAAmB,KAAK,KAAK,aAAa,CAAC,CAAC;AAAA,IAC5F,OAAO,CAAC,eAAe,IAAI,cAAc,SAAS,QAAQ,UAAU,GAAG,GAAG;AAAA,IAC1E,MAAM,OAAO,eAAe;AAC1B,UAAI,QAAQ,SAAS,QAAW;AAC9B;AAAA,MACF;AACA,YAAM,IAAI,QAAQ,MAAM,SAAS,QAAQ,UAAU,GAAG,GAAG;AAAA,IAC3D;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@branchly/migrator-drizzle",
3
+ "version": "0.1.0",
4
+ "description": "Reference Drizzle migrator adapter for branchly.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "devDependencies": {
19
+ "branchly": "0.1.0",
20
+ "@branchly/adapter-test-kit": "0.1.0"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "tsup",
27
+ "dev": "tsup --watch",
28
+ "typecheck": "tsc --noEmit"
29
+ }
30
+ }