From b9a601df974b1d89a5a069a374de30a69c28c6eb Mon Sep 17 00:00:00 2001 From: wener Date: Thu, 10 Oct 2024 10:39:46 +0800 Subject: [PATCH] feat: expose createMigrationBuilder to allow using this lib as sql builder --- src/index.ts | 1 + src/migrationBuilder.ts | 26 ++++++++++++++ .../createMigrationBuilder.spec.ts.snap | 17 ++++++++++ test/createMigrationBuilder.spec.ts | 34 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 test/__snapshots__/createMigrationBuilder.spec.ts.snap create mode 100644 test/createMigrationBuilder.spec.ts diff --git a/src/index.ts b/src/index.ts index 3003a19d..f47efd40 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export { Migration } from './migration'; +export { createMigrationBuilder } from './migrationBuilder'; export type { CreateCast, CreateCastFn, diff --git a/src/migrationBuilder.ts b/src/migrationBuilder.ts index a23fd4f9..d92c9f11 100644 --- a/src/migrationBuilder.ts +++ b/src/migrationBuilder.ts @@ -531,3 +531,29 @@ export default class MigrationBuilderImpl implements MigrationBuilder { return this._REVERSE_MODE ? [...this._steps].reverse() : this._steps; } } + +export function createMigrationBuilder({ + db, + typeShorthands, + shouldDecamelize = true, + logger = console, +}: { + db?: DB; + typeShorthands?: ColumnDefinitions; + shouldDecamelize?: boolean; + logger?: Logger; +} = {}): MigrationBuilderImpl { + return new MigrationBuilderImpl( + db || { + select: () => { + throw new Error('Not implemented'); + }, + query: () => { + throw new Error('Not implemented'); + }, + }, + typeShorthands, + shouldDecamelize, + logger + ); +} diff --git a/test/__snapshots__/createMigrationBuilder.spec.ts.snap b/test/__snapshots__/createMigrationBuilder.spec.ts.snap new file mode 100644 index 00000000..2aaf2e73 --- /dev/null +++ b/test/__snapshots__/createMigrationBuilder.spec.ts.snap @@ -0,0 +1,17 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`createMigrationBuilder > should generate the proper sql 1`] = ` +"CREATE TABLE "users" ( + "id" serial PRIMARY KEY, + "name" varchar(1000) NOT NULL, + "created_at" timestamp DEFAULT current_timestamp NOT NULL +); +CREATE TABLE "posts" ( + "id" serial PRIMARY KEY, + "user_id" integer NOT NULL REFERENCES "users" ON DELETE CASCADE, + "body" text NOT NULL, + "created_at" timestamp DEFAULT current_timestamp NOT NULL +); +CREATE INDEX "posts_user_id_index" ON "posts" ("user_id"); +" +`; diff --git a/test/createMigrationBuilder.spec.ts b/test/createMigrationBuilder.spec.ts new file mode 100644 index 00000000..853e9d2f --- /dev/null +++ b/test/createMigrationBuilder.spec.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest'; +import { createMigrationBuilder } from '../src'; + +describe('createMigrationBuilder', () => { + it('should generate the proper sql', () => { + const pgm = createMigrationBuilder(); + pgm.createTable('users', { + id: 'id', + name: { type: 'varchar(1000)', notNull: true }, + createdAt: { + type: 'timestamp', + notNull: true, + default: pgm.func('current_timestamp'), + }, + }); + pgm.createTable('posts', { + id: 'id', + userId: { + type: 'integer', + notNull: true, + references: '"users"', + onDelete: 'CASCADE', + }, + body: { type: 'text', notNull: true }, + createdAt: { + type: 'timestamp', + notNull: true, + default: pgm.func('current_timestamp'), + }, + }); + pgm.createIndex('posts', 'userId'); + expect(pgm.getSql()).toMatchSnapshot(); + }); +});