mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
chore: Update to fix the types
This commit is contained in:
@@ -1,28 +1,48 @@
|
||||
import { ManifestVersion } from "../../manifest/ManifestTypes"
|
||||
import { ManifestVersion, SDKManifest } from "../../manifest/ManifestTypes"
|
||||
import { Effects } from "../../types"
|
||||
import { Utils } from "../../util/utils"
|
||||
|
||||
export class Migration<Stor, Version extends ManifestVersion> {
|
||||
export class Migration<
|
||||
Manifest extends SDKManifest,
|
||||
Store,
|
||||
Version extends ManifestVersion,
|
||||
> {
|
||||
constructor(
|
||||
readonly options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
up: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Manifest, Store>
|
||||
}) => Promise<void>
|
||||
down: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Manifest, Store>
|
||||
}) => Promise<void>
|
||||
},
|
||||
) {}
|
||||
static of<Stor, Version extends ManifestVersion>(options: {
|
||||
static of<
|
||||
Manifest extends SDKManifest,
|
||||
Store,
|
||||
Version extends ManifestVersion,
|
||||
>(options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
up: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Manifest, Store>
|
||||
}) => Promise<void>
|
||||
down: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Manifest, Store>
|
||||
}) => Promise<void>
|
||||
}) {
|
||||
return new Migration<Stor, Version>(options)
|
||||
return new Migration<Manifest, Store, Version>(options)
|
||||
}
|
||||
|
||||
async up(opts: { effects: Effects; utils: Utils<Stor> }) {
|
||||
async up(opts: { effects: Effects; utils: Utils<Manifest, Store> }) {
|
||||
this.up(opts)
|
||||
}
|
||||
|
||||
async down(opts: { effects: Effects; utils: Utils<Stor> }) {
|
||||
async down(opts: { effects: Effects; utils: Utils<Manifest, Store> }) {
|
||||
this.down(opts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,30 +5,34 @@ import { createUtils } from "../../util"
|
||||
import { once } from "../../util/once"
|
||||
import { Migration } from "./Migration"
|
||||
|
||||
export class Migrations<Store> {
|
||||
export class Migrations<Manifest extends SDKManifest, Store> {
|
||||
private constructor(
|
||||
readonly manifest: SDKManifest,
|
||||
readonly migrations: Array<Migration<Store, any>>,
|
||||
readonly migrations: Array<Migration<Manifest, Store, any>>,
|
||||
) {}
|
||||
private sortedMigrations = once(() => {
|
||||
const migrationsAsVersions = (
|
||||
this.migrations as Array<Migration<Store, any>>
|
||||
this.migrations as Array<Migration<Manifest, Store, 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<Store, Migrations extends Array<Migration<Store, any>>>(
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
return new Migrations(manifest, migrations as Array<Migration<Store, any>>)
|
||||
static of<
|
||||
Manifest extends SDKManifest,
|
||||
Store,
|
||||
Migrations extends Array<Migration<Manifest, Store, any>>,
|
||||
>(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations>) {
|
||||
return new Migrations(
|
||||
manifest,
|
||||
migrations as Array<Migration<Manifest, Store, any>>,
|
||||
)
|
||||
}
|
||||
async init({
|
||||
effects,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
const utils = createUtils<Store>(effects)
|
||||
const utils = createUtils<Manifest, Store>(effects)
|
||||
if (!!previousVersion) {
|
||||
const previousVersionEmVer = EmVer.parse(previousVersion)
|
||||
for (const [_, migration] of this.sortedMigrations()
|
||||
@@ -42,7 +46,7 @@ export class Migrations<Store> {
|
||||
effects,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
const utils = createUtils<Store>(effects)
|
||||
const utils = createUtils<Manifest, Store>(effects)
|
||||
if (!!nextVersion) {
|
||||
const nextVersionEmVer = EmVer.parse(nextVersion)
|
||||
const reversed = [...this.sortedMigrations()].reverse()
|
||||
@@ -56,16 +60,17 @@ export class Migrations<Store> {
|
||||
}
|
||||
|
||||
export function setupMigrations<
|
||||
Manifest extends SDKManifest,
|
||||
Store,
|
||||
Migrations extends Array<Migration<Store, any>>,
|
||||
Migrations extends Array<Migration<Manifest, Store, any>>,
|
||||
>(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations>) {
|
||||
return Migrations.of<Store, Migrations>(manifest, ...migrations)
|
||||
return Migrations.of<Manifest, Store, Migrations>(manifest, ...migrations)
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
export type EnsureUniqueId<A, B = A, ids = never> =
|
||||
B extends [] ? A :
|
||||
B extends [Migration<any, infer id>, ...infer Rest] ? (
|
||||
B extends [Migration<any, 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"[]
|
||||
|
||||
Reference in New Issue
Block a user