-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
572 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
import pg from "pg"; | ||
import mongoose from "mongoose"; | ||
|
||
import logger from "./utils/logger.js"; | ||
|
||
/** @type {pg.Pool} */ | ||
let pool; | ||
mongoose.connection.on("error", (err) => logger.error("%O", err)); | ||
mongoose.set("debug", (collection, method, query, doc) => { | ||
logger.debug("%O", { collection, method, query, doc }); | ||
}); | ||
|
||
/** | ||
* @param {import("pg").ClientConfig} config | ||
* @param {string} uri | ||
* @param {import("mongoose").ConnectOptions} [options] | ||
*/ | ||
export const connectDb = async (config) => { | ||
pool = new pg.Pool(config); | ||
pool.on("error", (err) => logger.error("%O", err)); | ||
const client = await pool.connect(); | ||
logger.info("connected to %s", client.database); | ||
client.release(); | ||
export const connectDb = async (uri, options) => { | ||
const client = await mongoose.connect(uri, { | ||
bufferCommands: false, | ||
...options, | ||
}); | ||
logger.info("connected to %s", client.connection.name); | ||
}; | ||
|
||
export const disconnectDb = async () => { | ||
if (pool) { | ||
await pool.end(); | ||
} | ||
await mongoose.disconnect(); | ||
}; | ||
|
||
export const testConnection = async () => { | ||
await query("SELECT 1;"); | ||
const state = mongoose.connection.readyState; | ||
if (state !== mongoose.ConnectionStates.connected) { | ||
throw new Error(`database connection state: ${mongoose.STATES[state]}`); | ||
} | ||
}; | ||
|
||
function query(...args) { | ||
logger.debug("Postgres query: %O", args); | ||
return pool.query.apply(pool, args); | ||
} | ||
|
||
export default { query }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import db from "../db.js"; | ||
import mongoose from "mongoose"; | ||
|
||
const MessageSchema = new mongoose.Schema({ content: String }); | ||
|
||
const Message = mongoose.model("messages", MessageSchema); | ||
|
||
export async function getAll() { | ||
const { rows } = await db.query("SELECT * FROM message;"); | ||
const rows = await Message.find(); | ||
return rows; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { dirname, join } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
import config from "./utils/config.js"; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
/** @type {import("migrate-mongo").config.Config} */ | ||
export default { | ||
changelogCollectionName: "changelog", | ||
migrationFileExtension: ".js", | ||
migrationsDir: join(__dirname, "migrations"), | ||
moduleSystem: "esm", | ||
mongodb: { url: config.dbUrl }, | ||
useFileHash: false, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Template for | ||
* {@link https://github.com/seppevs/migrate-mongo?tab=readme-ov-file#creating-a-new-migration-script defining migrations}. | ||
*/ | ||
|
||
/** | ||
* @param {import("mongodb").Db} db | ||
* @param {import("mongodb").MongoClient} client | ||
*/ | ||
export const up = async (db) => { | ||
await db.collection("messages").insertOne({ content: "Hello, world!" }); | ||
}; | ||
|
||
/** | ||
* | ||
* @param {import("mongodb").Db} db | ||
* @param {import("mongodb").MongoClient} client | ||
*/ | ||
export const down = async (db) => { | ||
await db.collection("messages").drop(); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Template for | ||
* {@link https://github.com/seppevs/migrate-mongo?tab=readme-ov-file#creating-a-new-migration-script defining migrations}. | ||
*/ | ||
|
||
/** | ||
* @param {import("mongodb").Db} db | ||
* @param {import("mongodb").MongoClient} client | ||
*/ | ||
export const up = async (db, client) => { | ||
// TODO write your migration here. | ||
}; | ||
|
||
/** | ||
* @param {import("mongodb").Db} db | ||
* @param {import("mongodb").MongoClient} client | ||
*/ | ||
export const down = async (db, client) => { | ||
// TODO reverse your migration here. | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import app from "./app.js"; | ||
import { connectDb } from "./db.js"; | ||
import config from "./utils/config.cjs"; | ||
import config from "./utils/config.js"; | ||
import logger from "./utils/logger.js"; | ||
|
||
const { port } = config; | ||
|
||
await connectDb(config.dbConfig); | ||
await connectDb(config.dbUrl); | ||
|
||
app.listen(port, () => logger.info(`listening on ${port}`)); |
Oops, something went wrong.