I’m running code in n8n in the script function that basically converts a json into a sqlite file. The code runs and works in my ide (vsCode), but in n8n it returns an empty sqlite file. Follow the code below. I’ve already tried working with promises and the file keeps returning empty I’m trying to do the conversion to send the data to the n8n sqlite agent and generate an analysis with this data
const js = ["json"]
// Dados JSON (simulação)
const jsonData = js
// Caminho para o banco de dados
const dbPath = '/tmp/banco_de_dados.db';
// Criar o banco de dados SQLite
const db = new sqlite3.Database(dbPath);
// Criar a tabela e inserir dados
const columns = Object.keys(jsonData[0]).map(key => `${key} TEXT`).join(', ');
const createTableQuery = `CREATE TABLE IF NOT EXISTS dados (${columns})`;
db.run(createTableQuery, (err) => {
if (err) {
console.error('Erro ao criar tabela:', err);
return;
}
// Inserir os dados JSON na tabela
const placeholders = Object.keys(jsonData[0]).map(() => '?').join(', ');
const insertQuery = `INSERT INTO dados (${Object.keys(jsonData[0]).join(', ')}) VALUES (${placeholders})`;
jsonData.forEach(item => {
db.run(insertQuery, Object.values(item), (err) => {
if (err) {
console.error('Erro ao inserir dados:', err);
}
});
});
// Fechar o banco de dados
db.close((err) => {
if (err) {
console.error('Erro ao fechar o banco de dados:', err);
} else {
console.log('Banco de dados SQLite criado com sucesso!');
}
});
});
function retornarArquivo(){
return [
{
json: {},
binary: {
data: {
data: fs.readFileSync(dbPath).toString('base64'),
mimeType: 'application/x-sqlite3',
fileName: 'banco_de_dados.db'
}
}
}
];
}
retornarArquivo();
// Retornar o arquivo como binário para o n8n
return [
{
json: {},
binary: {
data: {
data: fs.readFileSync(dbPath).toString('base64'),
mimeType: 'application/x-sqlite3',
fileName: 'banco_de_dados.db'
}
}
}
];