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 committed Nov 5, 2024
1 parent 2b14645 commit b9a601d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
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();
});
});

0 comments on commit b9a601d

Please sign in to comment.