mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 04:11:57 +00:00
chore: Wrapper Data Contract
This commit is contained in:
@@ -1,27 +1,33 @@
|
||||
import { ManifestVersion } from "../../manifest/ManifestTypes"
|
||||
import { Effects } from "../../types"
|
||||
import { Utils } from "../../util"
|
||||
import { WrapperDataContract } from "../../wrapperData/wrapperDataContract"
|
||||
|
||||
export class Migration<Version extends ManifestVersion> {
|
||||
export class Migration<WD, Version extends ManifestVersion> {
|
||||
constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
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: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects }) => Promise<void>
|
||||
down: (opts: { effects: Effects }) => Promise<void>
|
||||
}) {
|
||||
return new Migration(options)
|
||||
static of<WD, Version extends ManifestVersion>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
},
|
||||
) {
|
||||
return new Migration(wrapperDataContract, 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,38 +2,47 @@ import { setupActions } from "../../actions/setupActions"
|
||||
import { EmVer } from "../../emverLite/mod"
|
||||
import { SDKManifest } from "../../manifest/ManifestTypes"
|
||||
import { ExpectedExports } from "../../types"
|
||||
import { createUtils } from "../../util"
|
||||
import { once } from "../../util/once"
|
||||
import { WrapperDataContract } from "../../wrapperData/wrapperDataContract"
|
||||
import { Migration } from "./Migration"
|
||||
|
||||
export class Migrations {
|
||||
export class Migrations<WD> {
|
||||
private constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly manifest: SDKManifest,
|
||||
readonly migrations: Array<Migration<any>>,
|
||||
readonly migrations: Array<Migration<WD, any>>,
|
||||
) {}
|
||||
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<WD, any>>
|
||||
).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<WD, Migrations extends Array<Migration<WD, any>>>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
return new Migrations(manifest, migrations as Array<Migration<any>>)
|
||||
return new Migrations(
|
||||
wrapperDataContract,
|
||||
manifest,
|
||||
migrations as Array<Migration<WD, any>>,
|
||||
)
|
||||
}
|
||||
async init({
|
||||
effects,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
const utils = createUtils(this.wrapperDataContract, effects)
|
||||
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 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,29 +50,34 @@ export class Migrations {
|
||||
effects,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
const utils = createUtils(this.wrapperDataContract, effects)
|
||||
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>>>(
|
||||
export function setupMigrations<
|
||||
WD,
|
||||
Migrations extends Array<Migration<WD, any>>,
|
||||
>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
return Migrations.of(manifest, ...migrations)
|
||||
return Migrations.of(wrapperDataContract, manifest, ...migrations)
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
export type EnsureUniqueId<A, B = A, ids = never> =
|
||||
B extends [] ? A :
|
||||
B extends [Migration<infer id>, ...infer Rest] ? (
|
||||
B extends [Migration<any, infer id>, ...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"[]
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Install } from "./setupInstall"
|
||||
import { Uninstall } from "./setupUninstall"
|
||||
|
||||
export function setupInit<WrapperData>(
|
||||
migrations: Migrations,
|
||||
migrations: Migrations<WrapperData>,
|
||||
install: Install<WrapperData>,
|
||||
uninstall: Uninstall<WrapperData>,
|
||||
): {
|
||||
|
||||
@@ -1,24 +1,38 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util"
|
||||
import { WrapperDataContract } from "../wrapperData/wrapperDataContract"
|
||||
|
||||
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>) {
|
||||
return new Install(fn)
|
||||
export class Install<WD> {
|
||||
private constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly fn: InstallFn<WD>,
|
||||
) {}
|
||||
static of<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: InstallFn<WD>,
|
||||
) {
|
||||
return new Install(wrapperDataContract, fn)
|
||||
}
|
||||
|
||||
async init({
|
||||
effects,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
if (!previousVersion) await this.fn({ effects, utils: utils(effects) })
|
||||
if (!previousVersion)
|
||||
await this.fn({
|
||||
effects,
|
||||
utils: utils(this.wrapperDataContract, effects),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function setupInstall<WrapperData>(fn: InstallFn<WrapperData>) {
|
||||
return Install.of(fn)
|
||||
export function setupInstall<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: InstallFn<WD>,
|
||||
) {
|
||||
return Install.of(wrapperDataContract, fn)
|
||||
}
|
||||
|
||||
@@ -1,24 +1,38 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util"
|
||||
import { WrapperDataContract } from "../wrapperData/wrapperDataContract"
|
||||
|
||||
export type UninstallFn<WrapperData> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<WrapperData>
|
||||
}) => Promise<void>
|
||||
export class Uninstall<WrapperData> {
|
||||
private constructor(readonly fn: UninstallFn<WrapperData>) {}
|
||||
static of<WrapperData>(fn: UninstallFn<WrapperData>) {
|
||||
return new Uninstall(fn)
|
||||
export class Uninstall<WD> {
|
||||
private constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly fn: UninstallFn<WD>,
|
||||
) {}
|
||||
static of<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: UninstallFn<WD>,
|
||||
) {
|
||||
return new Uninstall(wrapperDataContract, fn)
|
||||
}
|
||||
|
||||
async uninit({
|
||||
effects,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
if (!nextVersion) await this.fn({ effects, utils: utils(effects) })
|
||||
if (!nextVersion)
|
||||
await this.fn({
|
||||
effects,
|
||||
utils: utils(this.wrapperDataContract, effects),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function setupUninstall<WrapperData>(fn: UninstallFn<WrapperData>) {
|
||||
return Uninstall.of(fn)
|
||||
export function setupUninstall<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: UninstallFn<WD>,
|
||||
) {
|
||||
return Uninstall.of(wrapperDataContract, fn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user