Skip to content

fix(database): adds support for connection via URL #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Deco Server

[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)
![npm](https://img.shields.io/npm/dw/%40deco-team/deco-server?logo=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40deco-team%2Fdeco-server)

Deco Server is an easy way to set up a web server that is compatible with the Deco protocol and ecosystem of plugins. The mission of Deco is [to create a network of artificially intelligent, **bidirectional** personal assistants](https://decojs.com).

Expand All @@ -10,18 +11,38 @@ Why? I believe the future is networked artificial intelligence profiles. These p

## Installation

1. Install

```bash
npm i deco
```

2. Connect a root database via .env file

```bash
echo "\nDBURL=postgresql://dbuser:secretpassword@database.server.com:3211/mydb\n" >> .env
```

Replace the variables with your values. Don't have a database? We recommend using [Railway](https://railway.app).

3. Create index.js file

```js
// index.js
import { decoServer } from "deco";
import { deco } from "deco";

decoServer();
deco();
```

That's all that is required. Your can begin configuring your profile at `http://localhost:3456`.
4. Run

```
node index.js
```

That's all that is required. Your can begin configuring your profile at `http://localhost:3000`.

Additionally, you can view your OpenAPI Spec at `/_meta/open-api` to get started making requests.

## Stability

Expand All @@ -33,6 +54,8 @@ Below is an overview of some of the concepts of a Deco server.

### Plugins

You can view [a live plugin here](https://registry.decojs.com/plugins/deco-permissions/latest/manifest.json).

With a Deco server, plugins allow you to extend the capabilities of your server. Plugins can create new tables on your database, create new endpoints, and interact with other plugins. A plugin consists of two files: `manifest.json` and `app.js`. The manifest outlines the name, version, dependencies, and other meta information including the URI of the `app.js` file. The app will export specially named functions:

- `tables`
Expand Down Expand Up @@ -107,7 +130,7 @@ Contributions are welcome. Here is what is still needed:
- [x] Testing infrastructure
- [x] Release infrastructure
- [x] Packaging plugins
- [ ] Packaging server
- [x] Packaging server
- [ ] Multi-user database handling
- [ ] Reset password flow
- [ ] [Isolated VM](https://www.npmjs.com/package/isolated-vm) for safe plugin execution
Expand Down
25 changes: 18 additions & 7 deletions db/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* We allow two options for connecting to your database:
* 1. One .env variable `DBURL`
* 2. All .env variables `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER`, `PGPASSWORD`
*/
import pg from "pg";
import { config } from "dotenv";

Expand All @@ -7,21 +12,27 @@ config();
// Ensure necessary variables are present in a .env file
const REQUIRED_DB_VARS = ["PGHOST", "PGPORT", "PGDATABASE", "PGUSER", "PGPASSWORD"];
REQUIRED_DB_VARS.forEach((variable) => {
if (!!process.env.DBURL) return;
if (!process.env[variable]) {
console.log(`Missing required .env variable ${variable}.`);
process.exit(1);
}
});

const hasConnectionString = !!process.env.DBURL;
const connection = hasConnectionString
? { connectionString: process.env.DBURL }
: {
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.PGDATABASE,
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
};

// Create a pool of connections
const { Pool } = pg;
const client = new Pool({
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.PGDATABASE,
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
});
const client = new Pool(connection);

// Initiate root database connection
try {
Expand Down