Open miketromba opened 1 year ago
Why copy paste that when you can just make a wrapper that throws an error if the result of a function is null or undefined? Boom carpal tunnel syndrome prevented.
const somethingOrThrow = async (fn) => {
const result = await fn();
if (!result) throw new NotFoundError()
return result
}
I believe you can also mess with the prototype and declaration files to add this to the object itself
Yeah This helps
const [user] = await db.select().from(users).limit(1);
if (user == null) throw new Error('not found')
drizzle is kind of a wrapper of SQL, so I think they shouldn't implement that kind of sugar syntax
Yeah This helps
const [user] = await db.select().from(users).limit(1); if (user == null) throw new Error('not found')
Considering already included by OP, how this adds any value to issue?
drizzle is kind of a wrapper of SQL, so I think they shouldn't implement that kind of sugar syntax
Drizzle claims to be an ORM-ish whatever. It would be an inconsistent statement if Drizzle supports relations and avoids such basic functionality. Even a true query builder like kysely supports such feature.
What about something like this guys:
import { ColumnsSelection } from 'drizzle-orm';
import {
BuildSubquerySelection,
JoinNullability,
SelectMode,
SelectResult,
} from 'drizzle-orm/query-builders/select.types';
import { PgSelectBase } from 'drizzle-orm/pg-core';
export {};
declare module 'drizzle-orm/pg-core' {
interface PgSelectBase<
TTableName extends string | undefined,
TSelection extends ColumnsSelection,
TSelectMode extends SelectMode,
TNullabilityMap extends Record<
string,
JoinNullability
> = TTableName extends string ? Record<TTableName, 'not-null'> : {},
TDynamic extends boolean = false,
TExcludedMethods extends string = never,
TResult extends any[] = SelectResult<
TSelection,
TSelectMode,
TNullabilityMap
>[],
TSelectedFields extends ColumnsSelection = BuildSubquerySelection<
TSelection,
TNullabilityMap
>,
> {
firstOrDefault(defaultValue?: TResult[0]): Promise<TResult[0] | null>;
}
}
PgSelectBase.prototype.firstOrDefault = async function <T>(
this: any,
defaultValue?: T | null,
): Promise<T | null> {
const result = await this;
return result.length > 0 ? result[0] : defaultValue || null;
};
and you can use it like:
await db
.select()
.from(table)
.where().firstOrDefault()
that is sort of same thing in c#
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
IList<TSource> list = source as IList<TSource>;
if (list != null)
{
if (list.Count > 0)
{
return list[0];
}
}
return default(TSource);
}
you just do not have enumerator in nodeJs as you have in c#.
that looks nice, would be great to have it integrated into the main library somehow, or in a separate (but community maintained) library. A DSL on top of the already existing DSL, for more ergonomic operations. That'd be great.
Describe what you want
I've been integrating drizzle into my codebase (replacing Prisma) and so far I've had to write hundreds of snippets like this:
To save users from (mild) carpal tunnel I suggest simply adding a
findFirstOrThrow
method (like Prisma has)The above becomes this:
Bikeshedding, but it would be a nice little DX improvement.