Skip to content

Commit

Permalink
Update CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Feb 19, 2024
1 parent 6dc5db1 commit 6534ad3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

## Version History

### v15.12.0

- :rocket: Expose key helper fns

### v15.11.1

- :rocket: Simpler type defs
Expand Down
16 changes: 8 additions & 8 deletions generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ export default class Drizzle<T extends GenericTable> {
this.generic = generic;
}

#requiredPrimaryKey(): PgColumn {
const primaryKey = this.#primaryKey();
requiredPrimaryKey(): PgColumn {
const primaryKey = this.primaryKey();
if (!primaryKey) throw new Err(500, null, `Cannot access ${this.generic.name} without primaryKey`);
return primaryKey;
}

#primaryKey(): PgColumn | null {
primaryKey(): PgColumn | null {
let primaryKey;
for (const key in this.generic) {
if (this.generic[key].primary) primaryKey = this.generic[key];
Expand All @@ -120,7 +120,7 @@ export default class Drizzle<T extends GenericTable> {
return primaryKey || null;
}

#key(key: string): PgColumn {
key(key: string): PgColumn {
if (this.generic[key]) return this.generic[key];
throw new Err(500, null, `Cannot access ${this.generic.name}.${key} as it does not exist`);
}
Expand All @@ -133,7 +133,7 @@ export default class Drizzle<T extends GenericTable> {

async list(query: GenericListInput = {}): Promise<GenericList<InferSelectModel<T>>> {
const order = query.order && query.order === 'desc' ? desc : asc;
const orderBy = order(query.sort ? this.#key(query.sort) : this.#requiredPrimaryKey());
const orderBy = order(query.sort ? this.key(query.sort) : this.requiredPrimaryKey());

const pgres = await this.pool.select({
count: sql<string>`count(*) OVER()`.as('count'),
Expand All @@ -159,7 +159,7 @@ export default class Drizzle<T extends GenericTable> {
async from(id: unknown | SQL<unknown>): Promise<InferSelectModel<T>> {
const pgres = await this.pool.select()
.from(this.generic)
.where(is(id, SQL)? id as SQL<unknown> : eq(this.#requiredPrimaryKey(), id))
.where(is(id, SQL)? id as SQL<unknown> : eq(this.requiredPrimaryKey(), id))
.limit(1)

if (pgres.length !== 1) throw new Err(404, null, `Item Not Found`);
Expand All @@ -170,7 +170,7 @@ export default class Drizzle<T extends GenericTable> {
async commit(id: unknown | SQL<unknown>, values: object): Promise<InferSelectModel<T>> {
const pgres = await this.pool.update(this.generic)
.set(values)
.where(is(id, SQL)? id as SQL<unknown> : eq(this.#requiredPrimaryKey(), id))
.where(is(id, SQL)? id as SQL<unknown> : eq(this.requiredPrimaryKey(), id))
.returning();

return pgres[0] as InferSelectModel<T>;
Expand All @@ -190,7 +190,7 @@ export default class Drizzle<T extends GenericTable> {

async delete(id: unknown | SQL<unknown>): Promise<void> {
await this.pool.delete(this.generic)
.where(is(id, SQL)? id as SQL<unknown> : eq(this.#requiredPrimaryKey(), id))
.where(is(id, SQL)? id as SQL<unknown> : eq(this.requiredPrimaryKey(), id))
}
}

Expand Down

0 comments on commit 6534ad3

Please sign in to comment.