Skip to content

Commit 04dd5af

Browse files
authored
feat: Add functional client creator and documentation (#9)
1 parent 2a2ab88 commit 04dd5af

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
# Postgres support for Kapeta
1+
# NodeJS Postgres support for Kapeta
2+
3+
Creates a client using Prisma and connects to a Postgres database.
4+
5+
Is meant to be used with Kapeta and resources defined in a NodeJS Kapeta block.
26

37
Uses Prisma to make it simple to work with Postgres from Kapeta - and
48
add support for DB migrations.
59

610
Also exposes a CLI tool called ```kap-postgres-url``` that can be used
711
to generate a PG database URL from within a Kapeta block - for a given environment.
812

13+
To learn more about Kapeta, visit [kapeta.com](https://kapeta.com).
14+
15+
## Usage
16+
17+
This library exposes a class and function that can be used to create a client.
18+
19+
Normal usage is generated using Kapeta - but it can also be used directly.
20+
21+
### `createPostgresDBClient`
22+
Async function that creates a client using Prisma and connects to a Postgres database
923

24+
### `PostgresDB`
25+
Class that creates a client using Prisma and connects to a Postgres database.
26+
It will auto-initialize once the configuration provider is ready.

src/PostgresDB.ts

+31-14
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,41 @@ interface PrismaClient {
1111
$disconnect(): Promise<void>;
1212
}
1313

14+
/**
15+
* Create a postgres client and connect it to the database
16+
*
17+
* @param config the kapeta config provider
18+
* @param resourceName the resource name within the block
19+
* @param createClient a function that creates the prisma client
20+
*/
21+
export const createPostgresDBClient = async <T extends PrismaClient>(config:ConfigProvider, resourceName: string, createClient: (opts: any) => T) => {
22+
const url = await createDBURI(config, resourceName);
23+
console.log('Connecting to postgres database: %s', resourceName);
24+
25+
const prisma = createClient({
26+
datasources: {
27+
db: {
28+
url
29+
},
30+
},
31+
});
32+
33+
await prisma.$connect();
34+
console.log('Connected successfully to postgres database: %s', resourceName);
35+
return prisma;
36+
}
1437

38+
/**
39+
* A base class for postgres databases.
40+
*
41+
* See also {@link createPostgresDBClient} which is the recommended way to create a postgres client.
42+
*/
1543
export abstract class PostgresDB<T extends PrismaClient> {
1644
private readonly _resourceName: string;
1745
private _ready: boolean = false;
1846
private _prisma?: T;
19-
constructor(resourceName:string) {
47+
48+
protected constructor(resourceName:string) {
2049
this._resourceName = resourceName;
2150
Config.onReady(async (provider) => {
2251
await this.init(provider);
@@ -26,19 +55,7 @@ export abstract class PostgresDB<T extends PrismaClient> {
2655
abstract createClient(opts: any): T;
2756

2857
async init(provider: ConfigProvider) {
29-
const url = await createDBURI(provider, this._resourceName);
30-
console.log('Connecting to postgres database: %s', url);
31-
32-
this._prisma = this.createClient({
33-
datasources: {
34-
db: {
35-
url
36-
},
37-
},
38-
});
39-
40-
await this._prisma.$connect();
41-
console.log('Connected successfully to postgres database: %s', url);
58+
this._prisma = await createPostgresDBClient(provider, this._resourceName, this.createClient);
4259
this._ready = true;
4360
}
4461

0 commit comments

Comments
 (0)