1
Fork 0

Compare commits

...

10 Commits
0.1.3 ... main

15 changed files with 2230 additions and 1662 deletions

3
.envrc Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
use flake

109
.gitignore vendored
View File

@ -1,107 +1,4 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Build output directory
.direnv/
build/
coverage/
node_modules/

35
Makefile.toml Normal file
View File

@ -0,0 +1,35 @@
# Build the package.
[tasks.build]
dependencies = ["clean", "lint"]
clear = true
command = "pnpm"
args = ["tsc"]
# Clean the build directory.
[tasks.clean]
clear = true
command = "pnpm"
args = ["trash", "build/"]
# Run the full set of linting and testing.
[tasks.lint]
clear = true
dependencies = ["lint-js", "typecheck", "test"]
# Lint the TypeScript using XO.
[tasks.lint-js]
clear = true
command = "pnpm"
args = ["xo"]
# Run the tests.
[tasks.test]
clear = true
command = "pnpm"
args = ["tsx", "tests/migrate.test.ts"]
# Do a typecheck with tsc.
[tasks.typecheck]
clear = true
command = "pnpm"
args = ["tsc", "--noEmit"]

View File

@ -8,14 +8,6 @@
* 100% code coverage.
* TypeScript definitions included.
## Registry Setup
To use this package, add a registry for the `@holllo` scope to your npm configuration.
```
npm config set @holllo:registry https://git.bauke.xyz/api/packages/Holllo/npm/
```
## Usage
```ts

59
flake.lock Normal file
View File

@ -0,0 +1,59 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1705975988,
"narHash": "sha256-HJS/ilLkwLaFe+kPciiVuEwa90p7d7Y2FUZXuq7ztVk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4d3d04cfd372a0511f12b885c9822e406b00fe8e",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

13
flake.nix Normal file
View File

@ -0,0 +1,13 @@
{
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.default = import ./shell.nix { inherit pkgs; };
}
);
}

View File

@ -2,23 +2,21 @@
"name": "@holllo/migration-helper",
"description": "Tiny helper library for migrating data.",
"license": "AGPL-3.0-or-later",
"version": "0.1.3",
"version": "0.1.4",
"author": "Holllo <helllo@holllo.org>",
"repository": {
"type": "git",
"url": "https://git.bauke.xyz/Holllo/migration-helper"
},
"scripts": {
"build": "tsc",
"prepublishOnly": "pnpm test && pnpm build",
"test": "xo && c8 ava && tsc --noEmit"
"prepublishOnly": "makers build"
},
"devDependencies": {
"ava": "^4.3.3",
"c8": "^7.12.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
"xo": "^0.52.3"
"@holllo/test": "^0.2.1",
"trash-cli": "^5.0.0",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
"xo": "^0.56.0"
},
"type": "module",
"main": "build/source/migration-helper.js",
@ -29,31 +27,6 @@
"LICENSE",
"README.md"
],
"ava": {
"extensions": {
"ts": "module"
},
"files": [
"tests/**/*.ts"
],
"nodeArguments": [
"--loader=ts-node/esm",
"--no-warnings"
]
},
"c8": {
"100": true,
"all": true,
"include": [
"source",
"tests"
],
"reportDir": "coverage",
"reporter": [
"text",
"html"
]
},
"xo": {
"prettier": true,
"rules": {

File diff suppressed because it is too large Load Diff

7
shell.nix Normal file
View File

@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell rec {
packages = [ cargo-make nodejs nodePackages.pnpm ];
}

View File

@ -7,9 +7,9 @@
* The migrate function should take the data you pass through {@link migrate}
* and modify it to your needs.
*/
export type Migration<V> = {
export type Migration<V, A = any, B = any> = {
version: V;
migrate: (data: any) => Promise<unknown>;
migrate: (data: A) => Promise<B>;
};
/**

View File

@ -1,22 +0,0 @@
import test from 'ava';
import {migrate, Migration} from '../source/migration-helper.js';
test('migrate<Date>', async (t) => {
const migrations: Array<Migration<Date>> = [
{
version: new Date('2022-01-01'),
migrate: async (data: string) => `${data} migrated`,
},
];
function offsetDate(offset: number): Date {
const d = new Date(migrations[0].version);
d.setDate(d.getDate() + offset);
return d;
}
const data = 'data';
t.is(await migrate(data, offsetDate(-1), migrations), 'data migrated');
t.is(await migrate(data, offsetDate(1), migrations), data);
});

77
tests/migrate.test.ts Normal file
View File

@ -0,0 +1,77 @@
import {setup} from '@holllo/test';
import {
migrate,
Migration,
SkipMigrationFn,
} from '../source/migration-helper.js';
await setup('migrate<T>', async (group) => {
group.test('T = Date', async (test) => {
const migrations: Array<Migration<Date, string, string>> = [
{
version: new Date('2022-01-01'),
migrate: async (data: string) => `${data} migrated`,
},
];
function offsetDate(offset: number): Date {
const d = new Date(migrations[0].version);
d.setDate(d.getDate() + offset);
return d;
}
const data = 'data';
test.equals(
await migrate(data, offsetDate(-1), migrations),
'data migrated',
);
test.equals(await migrate(data, offsetDate(1), migrations), data);
});
group.test('T = number', async (test) => {
const migrations: Array<Migration<number, string, string>> = [
{
version: 5,
migrate: async (data: string) => `${data} migrated`,
},
];
const data = 'data';
test.equals(await migrate(data, 4, migrations), 'data migrated');
test.equals(await migrate(data, 6, migrations), data);
});
group.test('T = {custom: number}', async (test) => {
type Custom = {
custom: number;
};
const skip: SkipMigrationFn<Custom> = (a, b) => a.custom > b.custom;
const migrations: Array<Migration<Custom, string, string>> = [
{
version: {custom: 5},
migrate: async (data: string) => `${data} migrated`,
},
];
const data = 'data';
test.equals(await migrate(data, {custom: 4}, migrations, skip), data);
test.equals(
await migrate(data, {custom: 6}, migrations, skip),
'data migrated',
);
});
group.test('T = string', async (test) => {
const migrations: Array<Migration<string, string, string>> = [
{
version: '0.2.4',
migrate: async (data: string) => `${data} migrated`,
},
];
const data = 'data';
test.equals(await migrate(data, '0.2.3', migrations), 'data migrated');
test.equals(await migrate(data, '0.2.5', migrations), data);
});
});

View File

@ -1,16 +0,0 @@
import test from 'ava';
import {migrate, Migration} from '../source/migration-helper.js';
test('migrate<number>', async (t) => {
const migrations: Array<Migration<number>> = [
{
version: 5,
migrate: async (data: string) => `${data} migrated`,
},
];
const data = 'data';
t.is(await migrate(data, 4, migrations), 'data migrated');
t.is(await migrate(data, 6, migrations), data);
});

View File

@ -1,25 +0,0 @@
import test from 'ava';
import {
migrate,
Migration,
SkipMigrationFn,
} from '../source/migration-helper.js';
test('migrate<{custom: number}>', async (t) => {
type Custom = {
custom: number;
};
const skip: SkipMigrationFn<Custom> = (a, b) => a.custom > b.custom;
const migrations: Array<Migration<Custom>> = [
{
version: {custom: 5},
migrate: async (data: string) => `${data} migrated`,
},
];
const data = 'data';
t.is(await migrate(data, {custom: 4}, migrations, skip), data);
t.is(await migrate(data, {custom: 6}, migrations, skip), 'data migrated');
});

View File

@ -1,16 +0,0 @@
import test from 'ava';
import {migrate, Migration} from '../source/migration-helper.js';
test('migrate<string>', async (t) => {
const migrations: Array<Migration<string>> = [
{
version: '0.2.4',
migrate: async (data: string) => `${data} migrated`,
},
];
const data = 'data';
t.is(await migrate(data, '0.2.3', migrations), 'data migrated');
t.is(await migrate(data, '0.2.5', migrations), data);
});