0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-24 22:46:02 -05:00

feat: pretty error inserting into table

This commit is contained in:
bholmesdev 2024-02-29 16:56:10 -05:00
parent 868beb535c
commit 63f042f631
3 changed files with 26 additions and 18 deletions

View file

@ -21,14 +21,8 @@ export const MISSING_EXECUTE_PATH_ERROR = `${red(
export const FILE_NOT_FOUND_ERROR = (path: string) =>
`${red('▶ File not found:')} ${bold(path)}\n`;
export const SEED_ERROR = (tableName: string, error: string) => {
return `${red(`Error seeding table ${bold(tableName)}:`)}\n\n${error}`;
};
export const SEED_EMPTY_ARRAY_ERROR = (tableName: string) => {
// Drizzle error says "values() must be called with at least one value."
// This is specific to db.insert(). Prettify for seed().
return SEED_ERROR(tableName, `Empty array was passed. seed() must receive at least one value.`);
export const LIBSQL_ERROR = (action: string, tableName: string, error: string) => {
return `${red(`Error ${action} table ${bold(tableName)}:`)}\n\n${error}`;
};
export const REFERENCE_DNE_ERROR = (columnName: string) => {

View file

@ -1,10 +1,13 @@
import type { InStatement } from '@libsql/client';
import { createClient } from '@libsql/client';
import { LibsqlError, createClient } from '@libsql/client';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
import type { SQLiteTable } from 'drizzle-orm/sqlite-core';
import { drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
import { AsyncLocalStorage } from 'node:async_hooks';
import { z } from 'zod';
import { LIBSQL_ERROR } from '../core/errors.js';
import { getTableName } from 'drizzle-orm';
const isWebContainer = !!process.versions?.webcontainer;
@ -19,7 +22,26 @@ export function createLocalDatabaseClient({ dbUrl }: { dbUrl: string }): LocalDa
},
});
return db;
// Format Libsql errors on insert().values() for readable seed errors
const { insert: drizzleInsert } = db;
return Object.assign(db, {
insert(Table: SQLiteTable) {
const insert = drizzleInsert.call(this, Table);
const { values: drizzleValues } = insert;
return Object.assign(insert, {
async values(values: { [x: string]: any }[]) {
try {
return await drizzleValues.call(this, values);
} catch (e) {
if (e instanceof LibsqlError) {
throw new Error(LIBSQL_ERROR('inserting into', getTableName(Table), e.message));
}
throw e;
}
},
});
},
});
}
export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string) {

View file

@ -19,7 +19,6 @@ import {
FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
REFERENCE_DNE_ERROR,
SEED_EMPTY_ARRAY_ERROR,
FOREIGN_KEY_DNE_ERROR,
} from '../core/errors.js';
@ -79,13 +78,6 @@ export async function recreateTables({ db, tables }: { db: SqliteDB; tables: DBT
]);
}
// TODO: add error checks to seed file by intercepting db.insert()
function seedErrorChecks(mode: 'dev' | 'build', tableName: string, values: MaybeArray<unknown>) {
if (Array.isArray(values) && values.length === 0) {
throw new Error(SEED_EMPTY_ARRAY_ERROR(tableName));
}
}
export function getCreateTableQuery(tableName: string, table: DBTable) {
let query = `CREATE TABLE ${sqlite.escapeName(tableName)} (`;