mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 10:21:55 +00:00
add utils to migrations up and down
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { Effects, ExpectedExports, ExportedAction } from "../types"
|
||||
import { ActionMetaData } from "../types"
|
||||
import { once } from "../util/once"
|
||||
import { CreatedAction } from "./createAction"
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { ManifestVersion } from "../../manifest/ManifestTypes"
|
||||
import { Effects } from "../../types"
|
||||
import { Utils } from "../../util"
|
||||
|
||||
export class Migration<Version extends ManifestVersion> {
|
||||
export class Migration<Version extends ManifestVersion, WD> {
|
||||
constructor(
|
||||
readonly options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects }) => Promise<void>
|
||||
down: (opts: { effects: Effects }) => Promise<void>
|
||||
up: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
},
|
||||
) {}
|
||||
static of<Version extends ManifestVersion>(options: {
|
||||
@@ -17,11 +18,11 @@ export class Migration<Version extends ManifestVersion> {
|
||||
return new Migration(options)
|
||||
}
|
||||
|
||||
async up(opts: { effects: Effects }) {
|
||||
async up(opts: { effects: Effects; utils: Utils<WD> }) {
|
||||
this.up(opts)
|
||||
}
|
||||
|
||||
async down(opts: { effects: Effects }) {
|
||||
async down(opts: { effects: Effects; utils: Utils<WD> }) {
|
||||
this.down(opts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +1,70 @@
|
||||
import { setupActions } from "../../actions/setupActions"
|
||||
import { EmVer } from "../../emverLite/mod"
|
||||
import { SDKManifest } from "../../manifest/ManifestTypes"
|
||||
import { ExpectedExports } from "../../types"
|
||||
import { once } from "../../util/once"
|
||||
import { Migration } from "./Migration"
|
||||
|
||||
export class Migrations {
|
||||
export class Migrations<WD> {
|
||||
private constructor(
|
||||
readonly manifest: SDKManifest,
|
||||
readonly migrations: Array<Migration<any>>,
|
||||
readonly migrations: Array<Migration<any, WD>>,
|
||||
) {}
|
||||
private sortedMigrations = once(() => {
|
||||
const migrationsAsVersions = (this.migrations as Array<Migration<any>>).map(
|
||||
(x) => [EmVer.parse(x.options.version), x] as const,
|
||||
)
|
||||
const migrationsAsVersions = (
|
||||
this.migrations as Array<Migration<any, WD>>
|
||||
).map((x) => [EmVer.parse(x.options.version), x] as const)
|
||||
migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0]))
|
||||
return migrationsAsVersions
|
||||
})
|
||||
private currentVersion = once(() => EmVer.parse(this.manifest.version))
|
||||
static of<Migrations extends Array<Migration<any>>>(
|
||||
static of<Migrations extends Array<Migration<any, WD>>, WD>(
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
...migrations: EnsureUniqueId<Migrations, WD>
|
||||
) {
|
||||
return new Migrations(manifest, migrations as Array<Migration<any>>)
|
||||
return new Migrations(manifest, migrations as Array<Migration<any, WD>>)
|
||||
}
|
||||
async init({
|
||||
effects,
|
||||
utils,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
}: Parameters<ExpectedExports.init<WD>>[0]) {
|
||||
if (!!previousVersion) {
|
||||
const previousVersionEmVer = EmVer.parse(previousVersion)
|
||||
for (const [_, migration] of this.sortedMigrations()
|
||||
.filter((x) => x[0].greaterThan(previousVersionEmVer))
|
||||
.filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) {
|
||||
await migration.up({ effects })
|
||||
await migration.up({ effects, utils })
|
||||
}
|
||||
}
|
||||
}
|
||||
async uninit({
|
||||
effects,
|
||||
utils,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
}: Parameters<ExpectedExports.uninit<WD>>[0]) {
|
||||
if (!!nextVersion) {
|
||||
const nextVersionEmVer = EmVer.parse(nextVersion)
|
||||
const reversed = [...this.sortedMigrations()].reverse()
|
||||
for (const [_, migration] of reversed
|
||||
.filter((x) => x[0].greaterThan(nextVersionEmVer))
|
||||
.filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) {
|
||||
await migration.down({ effects })
|
||||
await migration.down({ effects, utils })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function setupMigrations<Migrations extends Array<Migration<any>>>(
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
export function setupMigrations<
|
||||
Migrations extends Array<Migration<any, WD>>,
|
||||
WD,
|
||||
>(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations, WD>) {
|
||||
return Migrations.of(manifest, ...migrations)
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
export type EnsureUniqueId<A, B = A, ids = never> =
|
||||
export type EnsureUniqueId<A, WD, B = A, ids = never> =
|
||||
B extends [] ? A :
|
||||
B extends [Migration<infer id>, ...infer Rest] ? (
|
||||
B extends [Migration<infer id, WD>, ...infer Rest] ? (
|
||||
id extends ids ? "One of the ids are not unique"[] :
|
||||
EnsureUniqueId<A, Rest, id | ids>
|
||||
) : "There exists a migration that is not a Migration"[]
|
||||
|
||||
@@ -3,13 +3,13 @@ import { Migrations } from "./migrations/setupMigrations"
|
||||
import { Install } from "./setupInstall"
|
||||
import { Uninstall } from "./setupUninstall"
|
||||
|
||||
export function setupInit<WrapperData>(
|
||||
migrations: Migrations,
|
||||
install: Install<WrapperData>,
|
||||
uninstall: Uninstall<WrapperData>,
|
||||
export function setupInit<WD>(
|
||||
migrations: Migrations<WD>,
|
||||
install: Install<WD>,
|
||||
uninstall: Uninstall<WD>,
|
||||
): {
|
||||
init: ExpectedExports.init
|
||||
uninit: ExpectedExports.uninit
|
||||
init: ExpectedExports.init<WD>
|
||||
uninit: ExpectedExports.uninit<WD>
|
||||
} {
|
||||
return {
|
||||
init: async (opts) => {
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util"
|
||||
|
||||
export type InstallFn<WrapperData> = (opts: {
|
||||
export type InstallFn<WD> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<WrapperData>
|
||||
utils: Utils<WD>
|
||||
}) => Promise<void>
|
||||
export class Install<WrapperData> {
|
||||
private constructor(readonly fn: InstallFn<WrapperData>) {}
|
||||
static of<WrapperData>(fn: InstallFn<WrapperData>) {
|
||||
export class Install<WD> {
|
||||
private constructor(readonly fn: InstallFn<WD>) {}
|
||||
static of<WD>(fn: InstallFn<WD>) {
|
||||
return new Install(fn)
|
||||
}
|
||||
|
||||
async init({
|
||||
effects,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
}: Parameters<ExpectedExports.init<WD>>[0]) {
|
||||
if (!previousVersion) await this.fn({ effects, utils: utils(effects) })
|
||||
}
|
||||
}
|
||||
|
||||
export function setupInstall<WrapperData>(fn: InstallFn<WrapperData>) {
|
||||
export function setupInstall<WD>(fn: InstallFn<WD>) {
|
||||
return Install.of(fn)
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util"
|
||||
|
||||
export type UninstallFn<WrapperData> = (opts: {
|
||||
export type UninstallFn<WD> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<WrapperData>
|
||||
utils: Utils<WD>
|
||||
}) => Promise<void>
|
||||
export class Uninstall<WrapperData> {
|
||||
private constructor(readonly fn: UninstallFn<WrapperData>) {}
|
||||
static of<WrapperData>(fn: UninstallFn<WrapperData>) {
|
||||
export class Uninstall<WD> {
|
||||
private constructor(readonly fn: UninstallFn<WD>) {}
|
||||
static of<WD>(fn: UninstallFn<WD>) {
|
||||
return new Uninstall(fn)
|
||||
}
|
||||
|
||||
async uninit({
|
||||
effects,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
}: Parameters<ExpectedExports.uninit<WD>>[0]) {
|
||||
if (!nextVersion) await this.fn({ effects, utils: utils(effects) })
|
||||
}
|
||||
}
|
||||
|
||||
export function setupUninstall<WrapperData>(fn: UninstallFn<WrapperData>) {
|
||||
export function setupUninstall<WD>(fn: UninstallFn<WD>) {
|
||||
return Uninstall.of(fn)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export * as configTypes from "./config/configTypes"
|
||||
import { InputSpec } from "./config/configTypes"
|
||||
import { DependenciesReceipt } from "./config/setupConfig"
|
||||
import { Utils } from "./util"
|
||||
|
||||
export type ExportedAction = (options: {
|
||||
effects: Effects
|
||||
@@ -66,15 +67,17 @@ export namespace ExpectedExports {
|
||||
* Every time a package completes an install, this function is called before the main.
|
||||
* Can be used to do migration like things.
|
||||
*/
|
||||
export type init = (options: {
|
||||
export type init<WD> = (options: {
|
||||
effects: Effects
|
||||
utils: Utils<WD>
|
||||
previousVersion: null | string
|
||||
}) => Promise<unknown>
|
||||
/** This will be ran during any time a package is uninstalled, for example during a update
|
||||
* this will be called.
|
||||
*/
|
||||
export type uninit = (options: {
|
||||
export type uninit<WD> = (options: {
|
||||
effects: Effects
|
||||
utils: Utils<WD>
|
||||
nextVersion: null | string
|
||||
}) => Promise<unknown>
|
||||
|
||||
|
||||
@@ -101,14 +101,14 @@ export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
|
||||
generator: DefaultString
|
||||
}) => {
|
||||
if (value) {
|
||||
await effects.vaultSet({ key, value })
|
||||
await effects.vault.set({ key, value })
|
||||
return value
|
||||
}
|
||||
if (await effects.vaultList().then((x) => x.includes(key))) {
|
||||
if (await effects.vault.list().then((x) => x.includes(key))) {
|
||||
return null
|
||||
}
|
||||
const newValue = getDefaultString(generator)
|
||||
await effects.vaultSet({ key, value: newValue })
|
||||
await effects.vault.set({ key, value: newValue })
|
||||
return newValue
|
||||
},
|
||||
getSystemSmtp: () =>
|
||||
|
||||
Reference in New Issue
Block a user