@tldraw/tlstore
Advanced tools
Comparing version
@@ -91,8 +91,12 @@ import { Atom } from 'signia'; | ||
/** @public */ | ||
export declare function defineMigrations<FirstVersion extends number, CurrentVersion extends number>({ firstVersion, currentVersion, migrators, subTypeKey, subTypeMigrations, }: { | ||
firstVersion: FirstVersion; | ||
currentVersion: CurrentVersion; | ||
migrators: { | ||
export declare function defineMigrations<FirstVersion extends EMPTY_SYMBOL | number = EMPTY_SYMBOL, CurrentVersion extends EMPTY_SYMBOL | Exclude<number, 0> = EMPTY_SYMBOL>(opts: { | ||
firstVersion?: CurrentVersion extends number ? FirstVersion : never; | ||
currentVersion?: CurrentVersion; | ||
migrators?: CurrentVersion extends number ? FirstVersion extends number ? CurrentVersion extends FirstVersion ? { | ||
[version in Exclude<Range_2<1, CurrentVersion>, 0>]: Migration; | ||
} : { | ||
[version in Exclude<Range_2<FirstVersion, CurrentVersion>, FirstVersion>]: Migration; | ||
}; | ||
} : { | ||
[version in Exclude<Range_2<1, CurrentVersion>, 0>]: Migration; | ||
} : never; | ||
subTypeKey?: string; | ||
@@ -118,2 +122,4 @@ subTypeMigrations?: Record<string, BaseMigrationsInfo>; | ||
declare type EMPTY_SYMBOL = symbol; | ||
declare type ExtractR<T extends RecordType<any, any>> = T extends RecordType<infer S, any> ? S : never; | ||
@@ -160,5 +166,5 @@ | ||
/** @public */ | ||
export declare type Migration<T = any> = { | ||
up: (oldState: T) => T; | ||
down: (newState: T) => T; | ||
export declare type Migration<Before = any, After = any> = { | ||
up: (oldState: Before) => After; | ||
down: (newState: After) => Before; | ||
}; | ||
@@ -502,2 +508,7 @@ | ||
/** | ||
* The same as `serialize`, but only serializes records with a scope of `document`. | ||
* @returns The record store snapshot as a JSON payload. | ||
*/ | ||
serializeDocumentState: () => StoreSnapshot<R>; | ||
/** | ||
* Opposite of `serialize`. Replace the store's current records with records as defined by a | ||
@@ -504,0 +515,0 @@ * simple JSON structure into the stores. |
@@ -30,10 +30,20 @@ "use strict"; | ||
var import_BaseRecord = require("./BaseRecord"); | ||
function defineMigrations({ | ||
firstVersion, | ||
currentVersion, | ||
migrators, | ||
subTypeKey, | ||
subTypeMigrations | ||
}) { | ||
return { currentVersion, firstVersion, migrators, subTypeKey, subTypeMigrations }; | ||
function defineMigrations(opts) { | ||
const { currentVersion, firstVersion, migrators = {}, subTypeKey, subTypeMigrations } = opts; | ||
if (typeof currentVersion === "number" && typeof firstVersion === "number") { | ||
if (currentVersion === firstVersion) { | ||
throw Error(`Current version is equal to initial version.`); | ||
} else if (currentVersion < firstVersion) { | ||
throw Error(`Current version is lower than initial version.`); | ||
} | ||
} | ||
return { | ||
firstVersion: firstVersion ?? 0, | ||
// defaults | ||
currentVersion: currentVersion ?? 0, | ||
// defaults | ||
migrators, | ||
subTypeKey, | ||
subTypeMigrations | ||
}; | ||
} | ||
@@ -40,0 +50,0 @@ var MigrationFailureReason = /* @__PURE__ */ ((MigrationFailureReason2) => { |
@@ -298,2 +298,12 @@ "use strict"; | ||
/** | ||
* The same as `serialize`, but only serializes records with a scope of `document`. | ||
* @returns The record store snapshot as a JSON payload. | ||
*/ | ||
serializeDocumentState = () => { | ||
return this.serialize((r) => { | ||
const type = this.schema.types[r.typeName]; | ||
return type.scope === "document"; | ||
}); | ||
}; | ||
/** | ||
* Opposite of `serialize`. Replace the store's current records with records as defined by a | ||
@@ -300,0 +310,0 @@ * simple JSON structure into the stores. |
{ | ||
"name": "@tldraw/tlstore", | ||
"description": "A tiny little drawing app (store).", | ||
"version": "2.0.0-canary.529574f83a06", | ||
"version": "2.0.0-canary.53be9239218d", | ||
"packageManager": "yarn@3.5.0", | ||
@@ -37,3 +37,3 @@ "author": { | ||
"test-coverage": "lazy inherit", | ||
"build-package": "yarn run -T tsx ../../scripts/build-package.ts", | ||
"build": "yarn run -T tsx ../../scripts/build-package.ts", | ||
"build-api": "yarn run -T tsx ../../scripts/build-api.ts", | ||
@@ -46,3 +46,3 @@ "prepack": "yarn run -T tsx ../../scripts/prepack.ts", | ||
"dependencies": { | ||
"@tldraw/utils": "2.0.0-canary.529574f83a06", | ||
"@tldraw/utils": "2.0.0-canary.53be9239218d", | ||
"lodash.isequal": "^4.5.0", | ||
@@ -49,0 +49,0 @@ "nanoid": "4.0.2" |
import { BaseRecord, isRecord } from './BaseRecord' | ||
import { SerializedSchema } from './StoreSchema' | ||
type EMPTY_SYMBOL = symbol | ||
/** @public */ | ||
export function defineMigrations<FirstVersion extends number, CurrentVersion extends number>({ | ||
firstVersion, | ||
currentVersion, | ||
migrators, | ||
subTypeKey, | ||
subTypeMigrations, | ||
}: { | ||
firstVersion: FirstVersion | ||
currentVersion: CurrentVersion | ||
migrators: { | ||
[version in Exclude<Range<FirstVersion, CurrentVersion>, FirstVersion>]: Migration | ||
} | ||
export function defineMigrations< | ||
FirstVersion extends number | EMPTY_SYMBOL = EMPTY_SYMBOL, | ||
CurrentVersion extends Exclude<number, 0> | EMPTY_SYMBOL = EMPTY_SYMBOL | ||
>(opts: { | ||
firstVersion?: CurrentVersion extends number ? FirstVersion : never | ||
currentVersion?: CurrentVersion | ||
migrators?: CurrentVersion extends number | ||
? FirstVersion extends number | ||
? CurrentVersion extends FirstVersion | ||
? { [version in Exclude<Range<1, CurrentVersion>, 0>]: Migration } | ||
: { [version in Exclude<Range<FirstVersion, CurrentVersion>, FirstVersion>]: Migration } | ||
: { [version in Exclude<Range<1, CurrentVersion>, 0>]: Migration } | ||
: never | ||
subTypeKey?: string | ||
subTypeMigrations?: Record<string, BaseMigrationsInfo> | ||
}): Migrations { | ||
return { currentVersion, firstVersion, migrators, subTypeKey, subTypeMigrations } | ||
const { currentVersion, firstVersion, migrators = {}, subTypeKey, subTypeMigrations } = opts | ||
// Some basic guards against impossible version combinations, some of which will be caught by TypeScript | ||
if (typeof currentVersion === 'number' && typeof firstVersion === 'number') { | ||
if ((currentVersion as number) === (firstVersion as number)) { | ||
throw Error(`Current version is equal to initial version.`) | ||
} else if (currentVersion < firstVersion) { | ||
throw Error(`Current version is lower than initial version.`) | ||
} | ||
} | ||
return { | ||
firstVersion: (firstVersion as number) ?? 0, // defaults | ||
currentVersion: (currentVersion as number) ?? 0, // defaults | ||
migrators, | ||
subTypeKey, | ||
subTypeMigrations, | ||
} | ||
} | ||
/** @public */ | ||
export type Migration<T = any> = { | ||
up: (oldState: T) => T | ||
down: (newState: T) => T | ||
export type Migration<Before = any, After = any> = { | ||
up: (oldState: Before) => After | ||
down: (newState: After) => Before | ||
} | ||
@@ -28,0 +48,0 @@ |
@@ -6,2 +6,3 @@ import { throttledRaf } from '@tldraw/utils' | ||
import { devFreeze } from './devFreeze' | ||
import { RecordType } from './RecordType' | ||
import { StoreQueries } from './StoreQueries' | ||
@@ -434,2 +435,13 @@ import { StoreSchema } from './StoreSchema' | ||
/** | ||
* The same as `serialize`, but only serializes records with a scope of `document`. | ||
* @returns The record store snapshot as a JSON payload. | ||
*/ | ||
serializeDocumentState = (): StoreSnapshot<R> => { | ||
return this.serialize((r) => { | ||
const type = this.schema.types[r.typeName as R['typeName']] as RecordType<any, any> | ||
return type.scope === 'document' | ||
}) | ||
} | ||
/** | ||
* Opposite of `serialize`. Replace the store's current records with records as defined by a | ||
@@ -436,0 +448,0 @@ * simple JSON structure into the stores. |
@@ -7,6 +7,2 @@ import { assert } from '@tldraw/utils' | ||
const UserVersion = { | ||
Initial: 0, | ||
} as const | ||
/** A user of tldraw */ | ||
@@ -17,7 +13,3 @@ interface User extends BaseRecord<'user'> { | ||
const userMigrations = defineMigrations({ | ||
currentVersion: UserVersion.Initial, | ||
firstVersion: UserVersion.Initial, | ||
migrators: {}, | ||
}) | ||
const userMigrations = defineMigrations({}) | ||
@@ -37,10 +29,2 @@ const User = createRecordType<User>('user', { | ||
const ShapeVersion = { | ||
Initial: 0, | ||
} as const | ||
const RectangleVersion = { | ||
Initial: 0, | ||
} as const | ||
interface Shape<Props> extends BaseRecord<'shape'> { | ||
@@ -64,13 +48,6 @@ type: string | ||
const shapeMigrations = defineMigrations({ | ||
currentVersion: ShapeVersion.Initial, | ||
firstVersion: ShapeVersion.Initial, | ||
migrators: {}, | ||
const shapeTypeMigrations = defineMigrations({ | ||
subTypeKey: 'type', | ||
subTypeMigrations: { | ||
rectangle: defineMigrations({ | ||
currentVersion: RectangleVersion.Initial, | ||
firstVersion: RectangleVersion.Initial, | ||
migrators: {}, | ||
}), | ||
rectangle: defineMigrations({}), | ||
}, | ||
@@ -80,3 +57,3 @@ }) | ||
const Shape = createRecordType<Shape<RectangleProps | OvalProps>>('shape', { | ||
migrations: shapeMigrations, | ||
migrations: shapeTypeMigrations, | ||
validator: { | ||
@@ -108,3 +85,3 @@ validate: (record) => { | ||
const Org = createRecordType<Org>('org', { | ||
migrations: defineMigrations({ currentVersion: 0, firstVersion: 0, migrators: {} }), | ||
migrations: defineMigrations({}), | ||
validator: { | ||
@@ -128,4 +105,4 @@ validate: (record) => { | ||
{ | ||
snapshotMigrations: defineMigrations({ currentVersion: 0, firstVersion: 0, migrators: {} }), | ||
snapshotMigrations: defineMigrations({}), | ||
} | ||
) |
@@ -9,3 +9,2 @@ import { assert } from '@tldraw/utils' | ||
const UserVersion = { | ||
Initial: 0, | ||
AddLocale: 1, | ||
@@ -24,3 +23,2 @@ AddPhoneNumber: 2, | ||
currentVersion: UserVersion.AddPhoneNumber, | ||
firstVersion: UserVersion.Initial, | ||
migrators: { | ||
@@ -74,3 +72,2 @@ [UserVersion.AddLocale]: { | ||
const ShapeVersion = { | ||
Initial: 0, | ||
AddRotation: 1, | ||
@@ -81,3 +78,2 @@ AddParent: 2, | ||
const RectangleVersion = { | ||
Initial: 0, | ||
AddOpacity: 1, | ||
@@ -87,3 +83,2 @@ } as const | ||
const OvalVersion = { | ||
Initial: 0, | ||
AddBorderStyle: 1, | ||
@@ -112,5 +107,4 @@ } as const | ||
const shapeMigrations = defineMigrations({ | ||
const shapeTypeMigrations = defineMigrations({ | ||
currentVersion: ShapeVersion.AddParent, | ||
firstVersion: ShapeVersion.Initial, | ||
migrators: { | ||
@@ -144,3 +138,2 @@ [ShapeVersion.AddRotation]: { | ||
currentVersion: RectangleVersion.AddOpacity, | ||
firstVersion: RectangleVersion.Initial, | ||
migrators: { | ||
@@ -167,3 +160,2 @@ [RectangleVersion.AddOpacity]: { | ||
currentVersion: OvalVersion.AddBorderStyle, | ||
firstVersion: OvalVersion.Initial, | ||
migrators: { | ||
@@ -192,3 +184,3 @@ [OvalVersion.AddBorderStyle]: { | ||
const Shape = createRecordType<Shape<RectangleProps | OvalProps>>('shape', { | ||
migrations: shapeMigrations, | ||
migrations: shapeTypeMigrations, | ||
validator: { | ||
@@ -214,3 +206,2 @@ validate: (record) => { | ||
const StoreVersions = { | ||
Initial: 0, | ||
RemoveOrg: 1, | ||
@@ -221,3 +212,2 @@ } | ||
currentVersion: StoreVersions.RemoveOrg, | ||
firstVersion: StoreVersions.Initial, | ||
migrators: { | ||
@@ -224,0 +214,0 @@ [StoreVersions.RemoveOrg]: { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
545506
2.24%85
1.19%9426
2.83%+ Added
- Removed