Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
feat: initialize drizzle with tables
Browse files Browse the repository at this point in the history
  • Loading branch information
relby committed Jan 16, 2024
1 parent 9d04a8c commit cb42f6f
Show file tree
Hide file tree
Showing 16 changed files with 599 additions and 3 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@automapper/classes": "8.7.7",
"@automapper/core": "8.7.7",
"@automapper/nestjs": "8.7.7",
"@automapper/pojos": "^8.7.7",
"@knaadh/nestjs-drizzle-pg": "^1.0.0",
"@nestjs/common": "9.0.8",
"@nestjs/config": "3.1.1",
"@nestjs/core": "9.0.8",
Expand All @@ -34,6 +36,7 @@
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"dotenv": "16.3.1",
"drizzle-orm": "^0.29.3",
"nest-transact": "9.1.2",
"nestjs-seeder": "0.3.2",
"nestjs-typeorm-paginate": "^4.0.4",
Expand All @@ -55,6 +58,7 @@
"@types/node": "20.10.5",
"@types/passport-jwt": "3.0.13",
"@types/passport-local": "1.0.38",
"@types/pg": "^8.10.9",
"@typescript-eslint/eslint-plugin": "6.15.0",
"@typescript-eslint/parser": "6.15.0",
"eslint": "8.56.0",
Expand Down
158 changes: 156 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions src/core/services/base.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { eq, ExtractTablesWithRelations } from 'drizzle-orm';
import { PgInsertValue, PgTransaction, PgUpdateSetSource } from 'drizzle-orm/pg-core';
import { PostgresJsDatabase, PostgresJsQueryResultHKT } from 'drizzle-orm/postgres-js';
import { RemovePropertiesWithNever } from 'src/infra/postgres/other/types';
import * as tables from 'src/infra/postgres/tables';

export class BaseService<
TableName extends keyof RemovePropertiesWithNever<{
[K in keyof ExtractTablesWithRelations<typeof tables>]:
'id' extends keyof ExtractTablesWithRelations<typeof tables>[K]['columns']
? K
: never
}>
> {
public constructor(
private readonly tableName: TableName,
protected readonly drizzle: PostgresJsDatabase<typeof tables>,
) {}

public async findOne(
config?: Parameters<PostgresJsDatabase<typeof tables>['query'][TableName]['findFirst']>[0],
) {
return this.drizzle.query[this.tableName]
.findFirst(config)
.then((entity) => entity ?? null);
}

public async exists(
config?: Parameters<PostgresJsDatabase<typeof tables>['query'][TableName]['findFirst']>[0],
): Promise<boolean> {
return this.findOne(config)
.then((entity) => Boolean(entity));
}

public async findMany(
config?: Parameters<PostgresJsDatabase<typeof tables>['query'][TableName]['findMany']>[0],
) {
return this.drizzle.query[this.tableName]
.findMany(config);
}

public async findManyWithPagination(
paginationOptions: { page: number, limit: number },
config?: Parameters<PostgresJsDatabase<typeof tables>['query'][TableName]['findMany']>[0],
) {
// TODO: check for boundaries
const { limit, offset } = {
limit: paginationOptions.limit,
offset: (paginationOptions.page - 1) * paginationOptions.limit,
};

return this.drizzle.query[this.tableName]
.findMany({
...config,
limit,
offset,
});
}

public async createOne(
values: PgInsertValue<typeof tables[TableName]>,
tx?: PgTransaction<PostgresJsQueryResultHKT, typeof tables, ExtractTablesWithRelations<typeof tables>>,
): Promise<typeof tables[TableName]['$inferSelect']> {
return (tx ?? this.drizzle)
.insert(tables[this.tableName])
.values(values)
.returning()
.then(([entity]) => entity!);
}

public async updateOne(
entity: typeof tables[TableName]['$inferSelect'],
values: PgUpdateSetSource<typeof tables[TableName]>,
tx?: PgTransaction<PostgresJsQueryResultHKT, typeof tables, ExtractTablesWithRelations<typeof tables>>,
): Promise<typeof tables[TableName]['$inferSelect']> {
return (tx ?? this.drizzle)
.update(tables[this.tableName])
.set(values)
.where(eq(tables[this.tableName].id, entity.id))
.returning()
.then(([entity]) => entity!);
}

public async deleteOne(
entity: typeof tables[TableName]['$inferSelect'],
tx?: PgTransaction<PostgresJsQueryResultHKT, typeof tables, ExtractTablesWithRelations<typeof tables>>,
): Promise<typeof tables[TableName]['$inferSelect']> {
return (tx ?? this.drizzle)
.delete(tables[this.tableName])
.where(eq(tables[this.tableName].id, entity.id))
.returning()
.then(([entity]) => entity!);
}
}
Loading

0 comments on commit cb42f6f

Please sign in to comment.