`createTable` function with dynamic column array

Hey folks, I'm trying to create a create table function; I feel like this should be straight forward and just work. But, I'm obviously missing something. Please let me know if more details are required. Thank you for your time.  🙏
//....
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);
}
};
It's throwing a generic sqlite error
[Error: SQLITE_ERROR: near ")": syntax error
Emitted 'error' event on Statement instance at:
] {
errno: 1,
code: 'SQLITE_ERROR'
}
[Error: SQLITE_ERROR: near ")": syntax error
Emitted 'error' event on Statement instance at:
] {
errno: 1,
code: 'SQLITE_ERROR'
}
5 Replies
koskimas
koskimas15mo ago
Everything's immutable in kysely. you need to reassign the results
export const createTable = async ({ tableName, columns }: CreateTableProps) => {
try {
let table = db.schema.createTable(tableName);

columns.forEach((column: ColumnProps) => {
table = 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);
}
};
export const createTable = async ({ tableName, columns }: CreateTableProps) => {
try {
let table = db.schema.createTable(tableName);

columns.forEach((column: ColumnProps) => {
table = 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);
}
};
aDevWhosTrying
aDevWhosTryingOP15mo ago
Thank you for the response, I’ll confirm in a bit. Any tips on being more aware in the future?
koskimas
koskimas15mo ago
Don't eat yellow snow
aDevWhosTrying
aDevWhosTryingOP15mo ago
Is that an npm package? New frontend framework? kek kek thank for the sage advice.
koskimas
koskimas15mo ago
It probably is an npm package 😄 But yeah, as you can see, no actual advice comes to mind
Want results from more Discord servers?
Add your server