Skip to content

Commit

Permalink
feat: expose createMigrationBuilder to allow using this lib as sql bu…
Browse files Browse the repository at this point in the history
…ilder
  • Loading branch information
wenerme authored and Shinigami92 committed Mar 1, 2025
1 parent 78dee90 commit 33a0f48
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { Migration } from './migration';
export type { MigrationBuilder } from './migrationBuilder';
export { MigrationBuilder } from './migrationBuilder';
export type {
CreateCast,
CreateCastFn,
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/migrationBuilder.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`migrationBuilder > should expose MigrationBuilder to allow using as sql builder 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");
"
`;
44 changes: 44 additions & 0 deletions test/migrationBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it, vi } from 'vitest';
import { MigrationBuilder } from '../src';

describe('migrationBuilder', () => {
it('should expose MigrationBuilder to allow using as sql builder', () => {
const pgm = new MigrationBuilder(
{
query: vi.fn(),
select: vi.fn(),
},
undefined,
true,
console
);

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();
});
});

0 comments on commit 33a0f48

Please sign in to comment.