mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +00:00
chore: Magic phantom types
This commit is contained in:
@@ -25,7 +25,7 @@ export function updateConfig<
|
|||||||
noRepeat?: NoRepeat<version, type>,
|
noRepeat?: NoRepeat<version, type>,
|
||||||
noFail = false,
|
noFail = false,
|
||||||
): M.MigrationFn<version, type> {
|
): M.MigrationFn<version, type> {
|
||||||
return async (effects: T.Effects) => {
|
return M.migrationFn(async (effects: T.Effects) => {
|
||||||
await noRepeatGuard(effects, noRepeat, async () => {
|
await noRepeatGuard(effects, noRepeat, async () => {
|
||||||
let config = util.unwrapResultType(await getConfig({})(effects)).config;
|
let config = util.unwrapResultType(await getConfig({})(effects)).config;
|
||||||
if (config) {
|
if (config) {
|
||||||
@@ -42,7 +42,7 @@ export function updateConfig<
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
return { configured };
|
return { configured };
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function noRepeatGuard<
|
export async function noRepeatGuard<
|
||||||
|
|||||||
@@ -4,7 +4,15 @@ import { matches } from "./dependencies.ts";
|
|||||||
|
|
||||||
export type MigrationFn<version extends string, type extends "up" | "down"> = (
|
export type MigrationFn<version extends string, type extends "up" | "down"> = (
|
||||||
effects: T.Effects,
|
effects: T.Effects,
|
||||||
) => Promise<T.MigrationRes>;
|
) => Promise<T.MigrationRes> & { _type: type; _version: version };
|
||||||
|
|
||||||
|
export function migrationFn<version extends string, type extends "up" | "down">(
|
||||||
|
fn: (
|
||||||
|
effects: T.Effects,
|
||||||
|
) => Promise<T.MigrationRes>,
|
||||||
|
): MigrationFn<version, type> {
|
||||||
|
return fn as MigrationFn<version, type>;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Migration<version extends string> {
|
export interface Migration<version extends string> {
|
||||||
up: MigrationFn<version, "up">;
|
up: MigrationFn<version, "up">;
|
||||||
@@ -34,17 +42,16 @@ export function fromMapping<versions extends string>(
|
|||||||
const current = EmVer.parse(currentVersion);
|
const current = EmVer.parse(currentVersion);
|
||||||
const other = EmVer.parse(version);
|
const other = EmVer.parse(version);
|
||||||
|
|
||||||
const filteredMigrations =
|
const filteredMigrations = (Object.entries(migrations) as [
|
||||||
(Object.entries(migrations) as [
|
keyof MigrationMapping<string>,
|
||||||
keyof MigrationMapping<string>,
|
Migration<string>,
|
||||||
Migration<string>,
|
][])
|
||||||
][])
|
.map(([version, migration]) => ({
|
||||||
.map(([version, migration]) => ({
|
version: EmVer.parse(version),
|
||||||
version: EmVer.parse(version),
|
migration,
|
||||||
migration,
|
})).filter(({ version }) =>
|
||||||
})).filter(({ version }) =>
|
version.greaterThan(other) && version.lessThanOrEqual(current)
|
||||||
version.greaterThan(other) && version.lessThanOrEqual(current)
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const migrationsToRun = matches.matches(direction)
|
const migrationsToRun = matches.matches(direction)
|
||||||
.when("from", () =>
|
.when("from", () =>
|
||||||
|
|||||||
Reference in New Issue
Block a user