Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose createMigrationBuilder to allow using this lib as sql builder #1285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { Migration } from './migration';
export { createMigrationBuilder } from './migrationBuilder';
export type {
CreateCast,
CreateCastFn,
Expand Down
26 changes: 26 additions & 0 deletions src/migrationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
17 changes: 17 additions & 0 deletions test/__snapshots__/createMigrationBuilder.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[`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");
"
`;
34 changes: 34 additions & 0 deletions test/createMigrationBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});