Skip to content

Commit

Permalink
Update CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Feb 13, 2024
1 parent f7948ed commit af9f61d
Show file tree
Hide file tree
Showing 3 changed files with 17 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.10.0

- :rocket: Add TS `strict` mode

### v15.9.0

- :bug: Set no-verify option in pgStructure as it uses node-pg
Expand Down
20 changes: 12 additions & 8 deletions generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class GenericEmitter<T extends Table<TableConfig<Column<ColumnBaseConfig<
this.emit('count', count[0].count);

let it = 0;
let pgres = [];
let pgres: any = [];
do {
pgres = await this.pool.select()
.from(this.generic)
Expand Down Expand Up @@ -103,14 +103,18 @@ export default class Drizzle<T extends Table<TableConfig<Column<ColumnBaseConfig
this.generic = generic;
}

#primaryKey(required = false): PgColumn | null {
#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 {
let primaryKey;
for (const key in this.generic) {
if (this.generic[key].primary) primaryKey = this.generic[key];
}

if (required && !primaryKey) throw new Err(500, null, `Cannot access ${this.generic.name} without primaryKey`);

return primaryKey || null;
}

Expand All @@ -127,7 +131,7 @@ export default class Drizzle<T extends Table<TableConfig<Column<ColumnBaseConfig

async list(query: GenericListInput = {}): Promise<GenericList<InferSelectModel<T>>> {
const order = query.sort && query.sort === 'desc' ? desc : asc;
const orderBy = order(query.sort ? this.#key(query.sort) : this.#primaryKey());
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 @@ -153,7 +157,7 @@ export default class Drizzle<T extends Table<TableConfig<Column<ColumnBaseConfig
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.#primaryKey(true), 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 @@ -164,7 +168,7 @@ export default class Drizzle<T extends Table<TableConfig<Column<ColumnBaseConfig
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.#primaryKey(true), id))
.where(is(id, SQL)? id as SQL<unknown> : eq(this.#requiredPrimaryKey(), id))
.returning();

return pgres[0] as InferSelectModel<T>;
Expand All @@ -184,7 +188,7 @@ export default class Drizzle<T extends Table<TableConfig<Column<ColumnBaseConfig

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

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"esm": true
},
"compilerOptions": {
"strict": true,
"skipLibCheck": true,
"module": "es2022",
"esModuleInterop": true,
Expand Down

0 comments on commit af9f61d

Please sign in to comment.