Skip to content

Commit

Permalink
Isolate MongoDB references to its migration (#673)
Browse files Browse the repository at this point in the history
* Move mongoose models into SQLite migration

* Remove references to mongo, except in migration
  • Loading branch information
goto-bus-stop authored Nov 26, 2024
1 parent 77fa696 commit 5099496
Show file tree
Hide file tree
Showing 18 changed files with 412 additions and 487 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The backend server for üWave, the collaborative listening platform.
The server on its own only provides an HTTP API, so you must also run the web
client to actually use it.

üWave requires MongoDB and Redis databases.
üWave requires a Redis database.

## Usage

Expand All @@ -35,10 +35,9 @@ Environment Variables:
- `SECRET` - A secret key used for encrypting passwords. Must be a 64-character
hexadecimal string (= 256 bits).
- `PORT` - Port to listen on. Defaults to 6042.
- `SQLITE_PATH` - Path to the database file to use. Defaults to `uwave.sqlite`.
- `REDIS_URL` - URL of the Redis instance to connect to. Defaults to
`redis://localhost:6379/`.
- `MONGODB_URL` - URL of the MongoDB database to use. Defaults to
`mongodb://localhost:27017/uwave`.
- `YOUTUBE_API_KEY` (optional) - Your YouTube Data API key.

## Development
Expand All @@ -58,7 +57,7 @@ of the repository.
```bash
# Database connection URLs.
REDIS_URL=redis://localhost:6379/
MONGODB_URL=mongodb://localhost:27017/uwave_dev
SQLITE_PATH=uwave_dev.sqlite

# Enables the YouTube media source if given.
YOUTUBE_API_KEY=your key
Expand All @@ -80,7 +79,7 @@ Create and start a üWave server.

**Parameters**

- `mongo` - A MongoDB connection URL.
- `sqlite` - Path to the SQLite database file.
- `redis` - A Redis connection URL.

### uw.source(sourcePlugin, options={})
Expand All @@ -103,7 +102,6 @@ Stops the üWave server.

[MIT][]

[Mongoose]: http://mongoosejs.com/
[IORedis]: https://github.com/luin/ioredis
[u-wave-source keyword]: https://www.npmjs.com/browse/keyword/u-wave-source

Expand Down
15 changes: 5 additions & 10 deletions bin/u-wave-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ const envSchema = {
format: 'uri',
default: 'redis://localhost:6379',
},
MONGODB_URL: {
SQLITE_PATH: {
type: 'string',
format: 'uri',
default: 'mongodb://localhost:27017/uwave',
default: 'uwave.sqlite',
},
SECRET: {
type: 'string',
Expand Down Expand Up @@ -74,10 +73,10 @@ if (argv.h || argv.help || !validConfig) {
console.log(' A secret key used for encrypting passwords. Must be a 64-character hexadecimal string (= 256 bits).');
console.log(' PORT');
console.log(' Port to listen on. Defaults to 6042.');
console.log(' SQLITE_PATH');
console.log(' Path to the database file. Defaults to uwave.sqlite.');
console.log(' REDIS_URL');
console.log(' URL of the Redis instance to connect to. Defaults to redis://localhost:6379/.');
console.log(' MONGODB_URL');
console.log(' URL of the MongoDB database to use. Defaults to mongodb://localhost:27017/uwave.');
console.log(' YOUTUBE_API_KEY [optional]');
console.log(' Your YouTube Data API key.');
console.log();
Expand Down Expand Up @@ -107,14 +106,10 @@ const secret = Buffer.from(config.SECRET, 'hex');
const uw = uwave({
port,
redis: config.REDIS_URL,
mongo: config.MONGODB_URL,
sqlite: config.SQLITE_PATH,
secret,
});

uw.on('mongoError', (err) => {
throw explain(err, 'Could not connect to MongoDB. Is it installed and running?');
});

uw.on('redisError', (err) => {
throw explain(err, 'Could not connect to the Redis server. Is it installed and running?');
});
Expand Down
6 changes: 1 addition & 5 deletions dev/u-wave-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function start() {
const uw = uwave({
port,
redis: process.env.REDIS_URL,
mongo: process.env.MONGODB_URL ?? 'mongodb://localhost/uwave',
sqlite: process.env.SQLITE_PATH ?? 'uwave.sqlite',
logger: { level: 'trace' },
secret,
mailTransport: testTransport,
Expand All @@ -54,10 +54,6 @@ async function start() {
instance.express.set('json spaces', 2);
});

uw.on('mongoError', (err) => {
throw explain(err, 'Could not connect to MongoDB. Is it installed and running?');
});

uw.on('redisError', (err) => {
throw explain(err, 'Could not connect to the Redis server. Is it installed and running?');
});
Expand Down
3 changes: 0 additions & 3 deletions example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
version: "3.1"
services:
mongo:
image: mongo
redis:
image: redis
u-wave:
Expand All @@ -10,7 +8,6 @@ services:
ports:
- "${PORT}:${PORT}"
links:
- mongo
- redis
env_file:
- .env
3 changes: 1 addition & 2 deletions example/example.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
PORT=6042
MONGO_CONNECTION_URL=mongodb://mongo/uwave
REDIS_CONNECTION_URL=redis://redis

SECRET=USE_YOUR_OWN
YOUTUBE_API_KEY=USE_YOUR_OWN
SOUNDCLOUD_API_KEY=USE_YOUR_OWN
SOUNDCLOUD_API_KEY=USE_YOUR_OWN
4 changes: 2 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ dotenv.config();
const port = process.env.PORT ?? 80;
const secret = Buffer.from(process.env.SECRET, 'hex');

const DEFAULT_MONGO_URL = 'mongodb://localhost:27017/uwave';
const DEFAULT_SQLITE_PATH = 'uwave.sqlite';
const DEFAULT_REDIS_URL = 'redis://localhost:6379';

const uw = uwave({
mongo: process.env.MONGO_CONNECTION_URL ?? DEFAULT_MONGO_URL,
sqlite: process.env.SQLITE_PATH ?? DEFAULT_SQLITE_PATH,
redis: process.env.REDIS_CONNECTION_URL ?? DEFAULT_REDIS_URL,
port,
secret,
Expand Down
7 changes: 3 additions & 4 deletions src/Uwave.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import passport from './plugins/passport.js';
import migrations from './plugins/migrations.js';
import { SqliteDateColumnsPlugin, connect as connectSqlite } from './utils/sqlite.js';

const DEFAULT_MONGO_URL = 'mongodb://localhost:27017/uwave';
const DEFAULT_SQLITE_PATH = './uwave.sqlite';
const DEFAULT_REDIS_URL = 'redis://localhost:6379';

class UwCamelCasePlugin extends CamelCasePlugin {
Expand All @@ -49,7 +49,6 @@ class UwCamelCasePlugin extends CamelCasePlugin {
* @typedef {{
* port?: number,
* sqlite?: string,
* mongo?: string,
* redis?: string | RedisOptions,
* logger?: import('pino').LoggerOptions,
* } & import('./HttpApi.js').HttpApiOptions} Options
Expand Down Expand Up @@ -146,15 +145,15 @@ class UwaveServer extends EventEmitter {
this.locale = i18n.cloneInstance();

this.options = {
mongo: DEFAULT_MONGO_URL,
sqlite: DEFAULT_SQLITE_PATH,
redis: DEFAULT_REDIS_URL,
...options,
};

/** @type {Kysely<import('./schema.js').Database>} */
this.db = new Kysely({
dialect: new SqliteDialect({
database: () => connectSqlite(options.sqlite ?? 'uwave_local.sqlite'),
database: () => connectSqlite(options.sqlite ?? DEFAULT_SQLITE_PATH),
}),
// dialect: new PostgresDialect({
// pool: new pg.Pool({
Expand Down
Loading

0 comments on commit 5099496

Please sign in to comment.