//....
type ColumnProps = {
name: string;
type: 'serial' | 'varchar' | 'integer';
isUnique?: boolean;
};
type CreateTableProps = {
tableName: string;
columns: ColumnProps[];
};
// create table
export const createTable = async ({ tableName, columns }: CreateTableProps) => {
try {
const table = db.schema.createTable(tableName);
columns.forEach((column: ColumnProps) => {
table.addColumn(column.name, column.type, (col) => {
if (column.isUnique) {
return col.notNull().unique();
}
return col;
});
});
const res = await table.execute();
console.log(res);
} catch (err) {
console.log(err);
}
};
//....
type ColumnProps = {
name: string;
type: 'serial' | 'varchar' | 'integer';
isUnique?: boolean;
};
type CreateTableProps = {
tableName: string;
columns: ColumnProps[];
};
// create table
export const createTable = async ({ tableName, columns }: CreateTableProps) => {
try {
const table = db.schema.createTable(tableName);
columns.forEach((column: ColumnProps) => {
table.addColumn(column.name, column.type, (col) => {
if (column.isUnique) {
return col.notNull().unique();
}
return col;
});
});
const res = await table.execute();
console.log(res);
} catch (err) {
console.log(err);
}
};