Merge pull request #17875 from bradleyayers/node-pg-migrate

Add types for node-pg-migrate.
This commit is contained in:
Ryan Cavanaugh
2017-07-13 14:40:16 -07:00
committed by GitHub
4 changed files with 159 additions and 0 deletions

76
types/node-pg-migrate/index.d.ts vendored Normal file
View File

@@ -0,0 +1,76 @@
// Type definitions for node-pg-migrate 2.3
// Project: https://github.com/theoephraim/node-pg-migrate#readme
// Definitions by: Bradley Ayers <https://github.com/bradleyayers>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
export interface ColumnDefinition {
type: string;
unique?: boolean;
primaryKey?: boolean;
notNull?: boolean;
check?: string;
references?: string;
onDelete?: string;
onUpdate?: string;
}
export interface ColumnOptions {
type?: string;
default?: string | PgLiteral | null;
notNull?: boolean;
allowNull?: boolean;
}
export interface CreateIndexOptions {
name?: string;
unique?: boolean;
where?: string;
concurrently?: boolean;
method?: 'btree' | 'hash' | 'gist' | 'spgist' | 'gin';
}
export interface ColumnDefinitions {
[name: string]: ColumnDefinition;
}
export type TableDescriptor = string | { schema: string, name: string };
export interface MigrationBuilder {
addExtension(extension: string | string[]): void;
createExtension(extension: string | string[]): void;
dropExtension(extension: string | string[]): void;
createTable(tableName: TableDescriptor, columns: ColumnDefinitions, options?: { inherits?: string }): void;
dropTable(tableName: TableDescriptor): void;
renameTable(tablename: TableDescriptor, new_tablename: TableDescriptor): void;
addColumn(tablename: TableDescriptor, new_columns: ColumnDefinitions): void;
addColumns(tablename: TableDescriptor, new_columns: ColumnDefinitions): void;
dropColumn(tablename: TableDescriptor, columns: string[] | { [name: string]: any }): void;
dropColumns(tablename: TableDescriptor, columns: string[] | { [name: string]: any }): void;
renameColumn(tablename: TableDescriptor, old_column_name: string, new_column_name: string): void;
alterColumn(tableName: TableDescriptor, columnName: string, options: ColumnOptions): void;
addConstraint(tablename: TableDescriptor, constraint_name: string, expression: string): void;
createConstraint(tablename: TableDescriptor, constraint_name: string, expression: string): void;
dropConstraint(tablename: TableDescriptor, constraint_name: string): void;
addType(type_name: string, values: string[] | { [name: string]: string }): void;
createType(type_name: string, values: string[] | { [name: string]: string }): void;
dropType(type_name: string): void;
createIndex(tableName: TableDescriptor, columns: string | string[], options?: CreateIndexOptions): void;
dropIndex(tableName: TableDescriptor, columns: string | string[], options?: CreateIndexOptions): void;
addIndex(tableName: TableDescriptor, columns: string | string[], options?: CreateIndexOptions): void;
sql(sql: string, args?: object): void;
func(sql: string): PgLiteral;
}
export default function(options: any): Promise<void>;
export class PgLiteral {
static create(str: string): PgLiteral;
constructor(str: string);
}

View File

@@ -0,0 +1,60 @@
import { MigrationBuilder } from 'node-pg-migrate';
const pgm = {} as MigrationBuilder;
// Minimal examples
pgm.addColumn('table', {
column1: {
type: 'int4',
},
});
pgm.addExtension('extension');
pgm.addExtension(['extension1', 'extension2']);
pgm.createExtension('extension');
pgm.createExtension(['extension1', 'extension2']);
pgm.dropExtension('extension');
pgm.dropExtension(['extension1', 'extension2']);
pgm.createTable('table', { column1: { type: 'int4' } });
pgm.dropTable('table');
pgm.renameTable('table', 'tablenew');
pgm.addColumn('table', { column1: { type: 'int4' } });
pgm.addColumns('table', { column1: { type: 'int4' } });
pgm.dropColumn('table', ['column1', 'column2']);
pgm.dropColumns('table', ['column1', 'column2']);
pgm.renameColumn('table', 'old_column_name', 'new_column_name');
pgm.alterColumn('table', 'column1', {});
pgm.addConstraint('table', 'table_constraint', 'column1 is not null');
pgm.createConstraint('table', 'table_constraint', 'column1 is not null');
pgm.dropConstraint('table', 'table_constraint');
pgm.addType('composite_type', ['enum1', 'enum2']);
pgm.createType('composite_type', { field1: 'int', field2: 'int' });
pgm.dropType('composite_type');
pgm.createIndex('table', 'column1');
pgm.createIndex('table', ['column1', 'column2']);
pgm.dropIndex('table', 'column1');
pgm.dropIndex('table', ['column1', 'column2']);
pgm.addIndex('table', 'column1');
pgm.addIndex('table', ['column1', 'column2']);
pgm.sql(`select 1;`);
pgm.sql(`select 1;`, { one: '1' });
pgm.func('now()');
// Elaborate example
pgm.addColumn({ schema: 'schema', name: 'table' }, {
column1: {
type: 'int4',
unique: true,
primaryKey: true,
notNull: true,
check: '',
references: '',
onDelete: '',
onUpdate: '',
}
});

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"node-pg-migrate-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }