Skip to content

Commit

Permalink
add postgres driver and connect test
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Jul 27, 2020
1 parent 2997d28 commit d3fd2ae
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 16 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,36 @@
name: ci

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-16.04
strategy:
matrix:
deno: ["v1.2.1"]
name: Deno ${{ matrix.deno }}

services:
postgres:
image: postgres:10.8
ports:
- 5432:5432
env:
POSTGRES_USER: "reverb_orm"
POSTGRES_PASSWORD: "reverb_orm"
POSTGRES_DB: "reverb_orm"
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: actions/checkout@v2
- name: Setup Deno
uses: denolib/setup-deno@v2
with:
deno-version: ${{ matrix.deno }}

- name: Check format
run: |
deno fmt --check
- name: Test
run: |
deno test -c tsconfig.json --allow-net --allow-env
4 changes: 3 additions & 1 deletion deps.ts
@@ -1 +1,3 @@
import "./src/reflect.ts";
import "./src/util/reflect.ts";
export { Client } from "https://deno.land/x/postgres/mod.ts";
export { ConnectionOptions } from "https://deno.land/x/postgres/connection_params.ts";
3 changes: 2 additions & 1 deletion mod.ts
@@ -1,3 +1,4 @@
import "./deps.ts";
export { Entity } from "./src/decorators/entity.ts";
export { Schema } from "./src/decorators/schema.ts";
export { Schema } from "./src/decorators/schema.ts";
export { Deployment } from "./src/core/deployment.ts";
2 changes: 1 addition & 1 deletion src/common/constants.ts
Expand Up @@ -12,4 +12,4 @@ export type SCHEMA_METADATA_KEYS_TYPE =
export const DEFINITION_TYPE = {
ENTITY: "__entity__",
SCHEMA: "__schema__",
}
};
7 changes: 7 additions & 0 deletions src/core/db_driver.ts
@@ -0,0 +1,7 @@
import { QueryResult } from "./query_result.ts";

export interface DBDriver {
query(query: string, ...args: any[]): Promise<QueryResult>;
connect(): Promise<void>;
disconnect(): Promise<void>;
}
29 changes: 29 additions & 0 deletions src/core/deployment.ts
@@ -0,0 +1,29 @@
import { Type } from "../decorators/schema.ts";
import { Client, ConnectionOptions } from "../../deps.ts";
import { DBDriver } from "./db_driver.ts";
import { PostgresDriver } from "./drivers/postgres.ts";

export class Deployment {
private readonly driver: DBDriver;

constructor(
private schema: Type<any>,
connection: Client | ConnectionOptions,
) {
let client: Client;
if (connection instanceof Client) {
client = connection;
} else {
client = new Client(connection);
}
this.driver = new PostgresDriver(client);
}

async connect(): Promise<void> {
await this.driver.connect();
}

async disconnect(): Promise<void> {
await this.driver.disconnect();
}
}
19 changes: 19 additions & 0 deletions src/core/drivers/postgres.ts
@@ -0,0 +1,19 @@
import { DBDriver } from "../db_driver.ts";
import { QueryResult } from "../query_result.ts";
import { Client } from "../../../deps.ts";

export class PostgresDriver implements DBDriver {
constructor(private readonly client: Client) {}

async query(query: string, ...args: any[]): Promise<QueryResult> {
return this.client.query(query, args);
}

async connect(): Promise<void> {
await this.client.connect();
}

async disconnect(): Promise<void> {
await this.client.end();
}
}
3 changes: 3 additions & 0 deletions src/core/query_result.ts
@@ -0,0 +1,3 @@
export interface QueryResult {
rows: any[];
}
3 changes: 1 addition & 2 deletions src/decorators/entity.ts
Expand Up @@ -5,9 +5,8 @@ export interface EntityConfig {
}

export function Entity(config?: EntityConfig): ClassDecorator {

return (target: Function) => {
Reflect.defineMetadata(ENTITY_METADATA, config, target);
Reflect.defineMetadata(DEFINITION_TYPE.ENTITY, true, target);
}
};
}
17 changes: 12 additions & 5 deletions src/decorators/schema.ts
@@ -1,15 +1,22 @@
import { SCHEMA_METADATA, SCHEMA_METADATA_KEYS, SCHEMA_METADATA_KEYS_TYPE, DEFINITION_TYPE } from "../common/constants.ts";
import {
SCHEMA_METADATA,
SCHEMA_METADATA_KEYS,
SCHEMA_METADATA_KEYS_TYPE,
DEFINITION_TYPE,
} from "../common/constants.ts";

export interface Type<T> extends Function {
new (...args: any[]): T;
}

export interface SchemaDefinition {
[SCHEMA_METADATA.ENTITIES]?: Type<any>[],
[SCHEMA_METADATA.ENTITIES]?: Type<any>[];
}

export function Schema(definition: SchemaDefinition): ClassDecorator {
const propsKeys = (Object.keys(definition) as unknown) as Array<keyof SchemaDefinition>;
const propsKeys = (Object.keys(definition) as unknown) as Array<
keyof SchemaDefinition
>;
validateSchemaKeys(propsKeys);

return (target: Function) => {
Expand All @@ -19,7 +26,7 @@ export function Schema(definition: SchemaDefinition): ClassDecorator {
Reflect.defineMetadata(property, (definition as any)[property], target);
}
}
}
};
}

export const INVALID_SCHEMA_DEFINITION_MESSAGE = (
Expand All @@ -35,4 +42,4 @@ export function validateSchemaKeys(keys: SCHEMA_METADATA_KEYS_TYPE[]) {
throw new Error(INVALID_SCHEMA_DEFINITION_MESSAGE`${key}`);
};
keys.forEach(validateKey);
}
}
2 changes: 1 addition & 1 deletion src/util/reflect.ts
Expand Up @@ -2159,4 +2159,4 @@ namespace Reflect {
return obj;
}
});
}
}
19 changes: 14 additions & 5 deletions test.ts
@@ -1,6 +1,6 @@
import { Entity, Schema } from "./mod.ts";
import { Entity, Schema, Deployment } from "./mod.ts";

Deno.test("defineBasicSchema", function (): void {
Deno.test("defineBasicSchema", async function (): Promise<void> {
@Entity()
class User {
id!: number;
Expand All @@ -11,9 +11,18 @@ Deno.test("defineBasicSchema", function (): void {
}

@Schema({
entities: [User],
entities: [User],
})
class AppSchema {}

// TODO initialize schema
});
const deployment = new Deployment(AppSchema, {
hostname: Deno.env.get("DB_HOST") || "127.0.0.1",
user: "reverb_orm",
database: "reverb_orm",
port: 5432,
});

await deployment.connect();

await deployment.disconnect();
});

0 comments on commit d3fd2ae

Please sign in to comment.