-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(database): [nan-1063] migrate to database package (#2236)
## Describe your changes As part of another ticket related to webhooks, I want to make a specific webhook package. I don't want the webhook package to depend on shared but shared contained the database as of now. This PR moves the database connection logic to its own package freeing it from shared and making it easier to use freely in other packages now. Note that the seeders have to stay due to the usage of services. ## Issue ticket number and link Contributes to NAN-1063 ## Checklist before requesting a review (skip if just adding/editing APIs & templates) - [ ] I added tests, otherwise the reason is: - [ ] I added observability, otherwise the reason is: - [ ] I added analytics, otherwise the reason is:
- Loading branch information
1 parent
376c5b1
commit 1d72c45
Showing
204 changed files
with
323 additions
and
149 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
tsconfig.tsbuildinfo | ||
dist/* | ||
node_modules |
2 changes: 1 addition & 1 deletion
2
packages/shared/lib/db/config.ts → packages/database/lib/config.ts
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
2 changes: 1 addition & 1 deletion
2
...ages/shared/lib/db/db.integration.test.ts → packages/database/lib/db.integration.test.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Knex } from 'knex'; | ||
|
||
export const defaultSchema = process.env['NANGO_DB_SCHEMA'] || 'nango'; | ||
const additionalSchemas = process.env['NANGO_DB_ADDITIONAL_SCHEMAS'] | ||
? process.env['NANGO_DB_ADDITIONAL_SCHEMAS'].split(',').map((schema: string) => schema.trim()) | ||
: []; | ||
|
||
export function getDbConfig({ timeoutMs }: { timeoutMs: number }): Knex.Config { | ||
return { | ||
client: process.env['NANGO_DB_CLIENT'] || 'pg', | ||
connection: process.env['NANGO_DATABASE_URL'] || { | ||
host: process.env['NANGO_DB_HOST'] || (process.env['SERVER_RUN_MODE'] === 'DOCKERIZED' ? 'nango-db' : 'localhost'), | ||
port: +(process.env['NANGO_DB_PORT'] || 5432), | ||
user: process.env['NANGO_DB_USER'] || 'nango', | ||
database: process.env['NANGO_DB_NAME'] || 'nango', | ||
password: process.env['NANGO_DB_PASSWORD'] || 'nango', | ||
ssl: process.env['NANGO_DB_SSL'] != null && process.env['NANGO_DB_SSL'].toLowerCase() === 'true' ? { rejectUnauthorized: false } : undefined, | ||
statement_timeout: timeoutMs | ||
}, | ||
pool: { | ||
min: parseInt(process.env['NANGO_DB_POOL_MIN'] || '2'), | ||
max: parseInt(process.env['NANGO_DB_POOL_MAX'] || '50') | ||
}, | ||
// SearchPath needs the current db and public because extension can only be installed once per DB | ||
searchPath: [defaultSchema, 'public', ...additionalSchemas] | ||
}; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/shared/lib/db/knexfile.cjs → packages/database/lib/knexfile.cjs
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,6 @@ | ||
// Knex CLI for migration needs this Knexfile but doesn't play well with ESM modules. | ||
// That's why the content of the Knex config is moved to ./config.ts to be imported by the app in ESM-fashion, and the Knexfile is only used for the Knex CLI in CommonJS-fashion. | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { config } = require('../../dist/db/config.js'); | ||
const { config } = require('../dist/config.js'); | ||
|
||
module.exports = config; |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
packages/database/lib/migrations/20240527094039_add_notifications_table.cjs
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,22 @@ | ||
const DB_TABLE = '_nango_active_logs'; | ||
|
||
exports.up = async function (knex, _) { | ||
return knex.schema.createTable(DB_TABLE, function (table) { | ||
table.increments('id').primary(); | ||
table.string('type', 'varchar(255)').notNullable(); | ||
table.string('action', 'varchar(255)').notNullable(); | ||
table.integer('connection_id').unsigned().notNullable(); | ||
table.foreign('connection_id').references('id').inTable('_nango_connections').onDelete('CASCADE'); | ||
table.integer('activity_log_id').unsigned().notNullable(); | ||
table.foreign('activity_log_id').references('id').inTable('_nango_activity_logs').onDelete('CASCADE'); | ||
table.string('log_id'); | ||
table.boolean('active').defaultTo(true); | ||
table.uuid('sync_id').defaultTo(null); | ||
table.foreign('sync_id').references('id').inTable('_nango_syncs').onDelete('CASCADE'); | ||
table.timestamps(true, true); | ||
}); | ||
}; | ||
|
||
exports.down = async function (knex, _) { | ||
return knex.schema.dropTable(DB_TABLE); | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/database/lib/migrations/20240529180658_add_notifications_table_index.cjs
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,9 @@ | ||
exports.config = { transaction: false }; | ||
|
||
exports.up = async function (knex, _) { | ||
await knex.schema.raw('CREATE INDEX CONCURRENTLY "idx_sync_id_active_true" ON "_nango_active_logs" USING BTREE ("sync_id") WHERE active = true'); | ||
}; | ||
|
||
exports.down = async function (knex, _) { | ||
await knex.schema.raw('DROP INDEX CONCURRENTLY idx_sync_id_active_true'); | ||
}; |
20 changes: 20 additions & 0 deletions
20
packages/database/lib/migrations/20240531122550_add_webhooks_table.cjs
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 @@ | ||
const DB_TABLE = '_nango_external_webhooks'; | ||
|
||
exports.up = async function (knex, _) { | ||
return knex.schema.createTable(DB_TABLE, function (table) { | ||
table.increments('id').primary(); | ||
table.integer('environment_id').unsigned().notNullable(); | ||
table.foreign('environment_id').references('id').inTable('_nango_environments').onDelete('CASCADE'); | ||
table.string('primary_url').notNullable(); | ||
table.string('secondary_url').notNullable(); | ||
table.boolean('on_sync_completion_always').defaultTo(false); | ||
table.boolean('on_auth_creation').defaultTo(false); | ||
table.boolean('on_auth_refesh_error').defaultTo(false); | ||
table.boolean('on_sync_error').defaultTo(false); | ||
table.timestamps(true, true); | ||
}); | ||
}; | ||
|
||
exports.down = async function (knex, _) { | ||
return knex.schema.dropTable(DB_TABLE); | ||
}; |
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,32 @@ | ||
{ | ||
"name": "@nangohq/database", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.js", | ||
"private": true, | ||
"bundleDependencies": [ | ||
"@nangohq/utils" | ||
], | ||
"scripts": { | ||
"build": "rimraf ./dist && tsc" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/NangoHQ/nango.git", | ||
"directory": "packages/database" | ||
}, | ||
"keywords": [], | ||
"dependencies": { | ||
"@nangohq/utils": "file:../utils", | ||
"knex": "3.1.0", | ||
"tarn": "3.0.2" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.3.3", | ||
"vitest": "0.33.0" | ||
}, | ||
"files": [ | ||
"dist/**/*" | ||
] | ||
} |
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,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "lib", | ||
"outDir": "dist" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../utils" | ||
} | ||
], | ||
"include": ["lib/**/*", "../utils/lib/vitest.d.ts"] | ||
} |
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
Oops, something went wrong.