Create Database Engine

  • It is highly recommended that you use TypeScript.

  • Add @synor/core package as both devDependencies and peerDependencies in package.json.

  • If you use a database driver (e.g. mysql, pg etc.), add them as dependencies in package.json.

  • Implement a function with the signature of type DatabaseEngineFactory.

  • The return value should be of type DatabaseEngine.

  • The uri string should contain everything you need to configure the engine. So you will need to parse it. You can use the connection-string package for doing that.

import { SynorError } from "@synor/core";
import { performance } from "perf_hooks";
type DatabaseEngine = import("@synor/core").DatabaseEngine;
type DatabaseEngineFactory = import("@synor/core").DatabaseEngineFactory;
type MigrationSource = import("@synor/core").MigrationSource;
export const ExampleDatabaseEngine: DatabaseEngineFactory = (
uri,
{ baseVersion, getAdvisoryLockId, getUserInfo }
): DatabaseEngine => {
const { databaseConfig, engineConfig } = getConfig(uri);
if (typeof getAdvisoryLockId !== "function") {
throw new SynorError(`Missing: getAdvisoryLockId`);
}
if (typeof getUserInfo !== "function") {
throw new SynorError(`Missing: getUserInfo`);
}
const advisoryLockId = getAdvisoryLockId(
databaseConfig.database,
engineConfig.migrationRecordTable
).join(":");
let appliedBy = "";
const open: DatabaseEngine["open"] = async () => {
appliedBy = await getUserInfo();
// Do stuffs
};
const close: DatabaseEngine["close"] = async () => {
// Do stuffs
};
const lock: DatabaseEngine["lock"] = async () => {
// Do stuffs
};
const unlock: DatabaseEngine["unlock"] = async () => {
// Do stuffs
};
const drop: DatabaseEngine["drop"] = async () => {
// Do stuffs
};
const run: DatabaseEngine["run"] = async ({
version,
type,
title,
hash,
body
}: MigrationSource) => {
let dirty = false;
const startTime = performance.now();
try {
// Do stuffs
} catch (err) {
dirty = true;
throw err;
} finally {
const endTime = performance.now();
// Add migration record
}
};
const repair: DatabaseEngine["repair"] = async records => {
// Do stuffs
};
const records: DatabaseEngine["records"] = async startId => {
// Do stuffs
};
return {
open,
close,
lock,
unlock,
drop,
run,
repair,
records
};
};
export default ExampleDatabaseEngine;
  • Your module should contain both named and default exports. The named export should have the prefix DatabaseEngine.

  • You should use the SynorError class for throwing errors.

  • For testing, it is recommended to use Jest.

  • For recommended repository setup, check the GitHub repositories with synor-database-engine topic.