aDevWhosTrying
aDevWhosTrying
Explore posts from servers
KKysely
Created by aDevWhosTrying on 9/9/2023 in #help
Stumped - sqlite kysely no data from select query
I have an electron app with node-sqlite3 and kysely. I'm able to post data, but for some reason select comes up empty. I feel like I'm missing something low-level here. Thank you in advance. I've been banging my head on this off and on for 2 days. db.ts
import sqlite3 from 'sqlite3';
import path from 'path';
import { Kysely, SqliteDialect, SqliteIntrospector } from 'kysely';
import cliLogger from '../shared/colorLogger';
import { DatabaseType } from '../types';

sqlite3.verbose();
const dbPath = path.resolve(__dirname, '../data/base.db');
const sqliteDb = new sqlite3.Database(dbPath);
const dialect = new SqliteDialect({
// @ts-ignore
database: async () => sqliteDb,
});

const db = new Kysely<DatabaseType>({
dialect,
log: ['query', 'error'],
});

export default db;
import sqlite3 from 'sqlite3';
import path from 'path';
import { Kysely, SqliteDialect, SqliteIntrospector } from 'kysely';
import cliLogger from '../shared/colorLogger';
import { DatabaseType } from '../types';

sqlite3.verbose();
const dbPath = path.resolve(__dirname, '../data/base.db');
const sqliteDb = new sqlite3.Database(dbPath);
const dialect = new SqliteDialect({
// @ts-ignore
database: async () => sqliteDb,
});

const db = new Kysely<DatabaseType>({
dialect,
log: ['query', 'error'],
});

export default db;
Post Service
/**
Post logs to db
**/
export const postPomoLog = async (data: PostPomoProps) => {
if (!data) return -1;
cliLogger.info('--- post pomo log data');
console.log(data);
try {
const results = await db
.insertInto('logs')
.values({
user_id: 1,
type: data.type,
action: data.action,
timestamp: data.timestamp,
timezone: data.timezone,
})
.executeTakeFirstOrThrow();

console.log(results.insertId);
return results;
} catch (err) {
cliLogger.error('ERROR: post pomo log');
console.log(err);
return err;
}
};
/**
Get logs from Db
**/
export const getTodayPomoLog = async () => {
cliLogger.info('--- get pomo today log data');

try {
const results = db.selectFrom('logs').selectAll().execute();
console.log('logs');
console.log(results);
return results;
} catch (err) {
cliLogger.error('ERROR: get today pomo log');
console.log(err);
return err;
}
};
/**
Post logs to db
**/
export const postPomoLog = async (data: PostPomoProps) => {
if (!data) return -1;
cliLogger.info('--- post pomo log data');
console.log(data);
try {
const results = await db
.insertInto('logs')
.values({
user_id: 1,
type: data.type,
action: data.action,
timestamp: data.timestamp,
timezone: data.timezone,
})
.executeTakeFirstOrThrow();

console.log(results.insertId);
return results;
} catch (err) {
cliLogger.error('ERROR: post pomo log');
console.log(err);
return err;
}
};
/**
Get logs from Db
**/
export const getTodayPomoLog = async () => {
cliLogger.info('--- get pomo today log data');

try {
const results = db.selectFrom('logs').selectAll().execute();
console.log('logs');
console.log(results);
return results;
} catch (err) {
cliLogger.error('ERROR: get today pomo log');
console.log(err);
return err;
}
};
8 replies
KKysely
Created by aDevWhosTrying on 8/31/2023 in #help
How to access error events -- Is there a best/good practice for error handling
I'm writing my methods and associated tests for db access and when trying to validate error events. I get a SQL event log but nothing is caught in my try-catch block. When I log the response it is either empty or undefined. *Example
export const postLog = async ({
tableName,
data,
}: {
tableName: string;
data: {
day: string;
log: string;
};
}) => {
try {
const results = await db.insertInto(tableName).values(data).execute();
console.log(results)
return results;
} catch (err) {
console.error('ERROR: in postlog');
console.error(err);
return err;
}
};
export const postLog = async ({
tableName,
data,
}: {
tableName: string;
data: {
day: string;
log: string;
};
}) => {
try {
const results = await db.insertInto(tableName).values(data).execute();
console.log(results)
return results;
} catch (err) {
console.error('ERROR: in postlog');
console.error(err);
return err;
}
};
I don't see in the docs where expected errors are defined. Thank you for your time. Cheers,
3 replies
KKysely
Created by aDevWhosTrying on 8/30/2023 in #help
`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'
}
7 replies