Skip to content

Commit

Permalink
feat: Add functional client creator and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hofmeister committed Jan 7, 2024
1 parent 093394e commit 7d47693
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# NodeJS Mongo support for the Kapeta project

Creates a client using Prisma and connects to a MongoDB database.

Is meant to be used with Kapeta and resources defined in a NodeJS Kapeta block.

Uses Prisma to make it simple to work with MongoDB from Kapeta - and
adds support for DB migrations.

Also exposes a CLI tool called ```kap-mongodb-url``` that can be used
to generate a Mongo database URL from within a Kapeta block - for a given environment.

To learn more about Kapeta, visit [kapeta.com](https://kapeta.com).

## Usage

This library exposes a class and function that can be used to create a client.

Normal usage is generated using Kapeta - but it can also be used directly.

### `createMongoDBClient`
Async function that creates a client using Prisma and connects to a MongoDB database

### `MongoDB`
Class that creates a client using Prisma and connects to a MongoDB database.
It will auto-initialize once the configuration provider is ready.
33 changes: 19 additions & 14 deletions src/MongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@ interface PrismaClient {
$disconnect(): Promise<void>;
}

export const createMongoDBClient = async <T extends PrismaClient>(config:ConfigProvider, resourceName: string, createClient: (opts: any) => T) => {
const url = await createDBURI(config, resourceName);
console.log('Connecting to mongodb database: %s', resourceName);

const prisma = createClient({
datasources: {
db: {
url
},
},
});

await prisma.$connect();
console.log('Connected successfully to mongodb database: %s', resourceName);
return prisma;
}

export abstract class MongoDB<T extends PrismaClient> {
private readonly _resourceName: string;
private _ready: boolean = false;
private _prisma?: T;
constructor(resourceName:string) {

protected constructor(resourceName:string) {
this._resourceName = resourceName;
Config.onReady(async (provider) => {
await this.init(provider);
Expand All @@ -26,19 +43,7 @@ export abstract class MongoDB<T extends PrismaClient> {
abstract createClient(opts: any): T;

async init(provider: ConfigProvider) {
const url = await createDBURI(provider, this._resourceName);
console.log('Connecting to mongodb database: %s', url);

this._prisma = this.createClient({
datasources: {
db: {
url
},
},
});

await this._prisma.$connect();
console.log('Connected successfully to mongodb database: %s', url);
this._prisma = await createMongoDBClient(provider, this._resourceName, this.createClient);
this._ready = true;
}

Expand Down

0 comments on commit 7d47693

Please sign in to comment.