Skip to content

Commit

Permalink
Remove global realm (#12)
Browse files Browse the repository at this point in the history
This PR removes the global realm.

All realms must now be explicitly constructed.
  • Loading branch information
maxdeviant committed Sep 9, 2023
1 parent 2e6cb90 commit c0cb6df
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 56 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Removed

- **@thaumaturgy/io-ts**: Removed global realm functions
- `define`
- `manifest`
- `persist`
- `persistLeaves`
- **@thaumaturgy/zod**: Removed global realm functions
- `define`
- `manifest`
- `persist`
- `persistLeaves`

## [0.14.0] - 2023-05-15

### Changed
Expand Down
18 changes: 11 additions & 7 deletions packages/io-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npm install -D @thaumaturgy/io-ts

```ts
import * as t from 'io-ts';
import { define, manifest } from '@thaumaturgy/io-ts';
import { Realm } from '@thaumaturgy/io-ts';

const Movie = t.strict(
{
Expand All @@ -30,7 +30,9 @@ const Movie = t.strict(
'Movie'
);

define(Movie, {
const realm = new Realm();

realm.define(Movie, {
sequences: {
titles: new Sequence(n => `Movie ${n}` as const),
years: new Sequence(n => 2022 - n),
Expand All @@ -41,7 +43,7 @@ define(Movie, {
}),
});

const movie = manifest(Movie);
const movie = realm.manifest(Movie);

console.log(movie);
// > { title: 'Movie 1', year: 2021 }
Expand All @@ -51,7 +53,7 @@ console.log(movie);

```ts
import * as t from 'io-ts';
import { define, manifest, Ref } from '@thaumaturgy/io-ts';
import { Realm, Ref } from '@thaumaturgy/io-ts';

const Author = t.type(
{
Expand All @@ -70,22 +72,24 @@ const Book = t.type(
'Book'
);

define(Author, {
const realm = new Realm();

realm.define(Author, {
manifest: ({ uuid }) => ({
id: uuid(),
name: 'J. R. R. Tolkien',
}),
});

define(Book, {
realm.define(Book, {
manifest: ({ uuid }) => ({
id: uuid(),
authorId: Ref.to(Author).through(author => author.id),
title: 'The Lord of the Rings',
}),
});

const book = manifest(Book);
const book = realm.manifest(Book);

console.log(book);
// > {
Expand Down
22 changes: 14 additions & 8 deletions packages/io-ts/src/__tests__/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import * as t from 'io-ts';
import { describe, expect, it } from 'vitest';
// Importing from the root barrel file intentionally to simulate what library
// consumers will see.
import { define, manifest, Ref } from '..';
import { Realm, Ref } from '..';

describe('Public API', () => {
it('works with a basic example', () => {
const realm = new Realm();

const Movie = t.strict({
title: t.string,
year: t.number,
});

define(Movie, {
realm.define(Movie, {
sequences: {
movies: new Sequence(n => `Movie ${n}` as const),
years: new Sequence(n => 2022 - n),
Expand All @@ -23,7 +25,7 @@ describe('Public API', () => {
}),
});

const movie = manifest(Movie);
const movie = realm.manifest(Movie);

expect(movie).toEqual({
title: expect.any(String),
Expand All @@ -32,12 +34,14 @@ describe('Public API', () => {
});

it('works with an entity containing an array field', () => {
const realm = new Realm();

const Post = t.strict({
title: t.string,
tags: t.array(t.string),
});

define(Post, {
realm.define(Post, {
manifest: ({ sequences }) => ({
title: sequences.titles.next(),
tags: sequences.tags.take(3),
Expand All @@ -48,7 +52,7 @@ describe('Public API', () => {
},
});

const post = manifest(Post);
const post = realm.manifest(Post);

expect(post).toEqual({
title: expect.any(String),
Expand All @@ -57,6 +61,8 @@ describe('Public API', () => {
});

it('works with an entity hierarchy', () => {
const realm = new Realm();

const Author = t.type({
id: t.string,
name: t.string,
Expand All @@ -68,22 +74,22 @@ describe('Public API', () => {
title: t.string,
});

define(Author, {
realm.define(Author, {
manifest: ({ uuid }) => ({
id: uuid(),
name: 'J. R. R. Tolkien',
}),
});

define(Book, {
realm.define(Book, {
manifest: ({ uuid }) => ({
id: uuid(),
authorId: Ref.to(Author).through(author => author.id),
title: 'The Lord of the Rings',
}),
});

const book = manifest(Book);
const book = realm.manifest(Book);

expect(book).toEqual({
id: expect.any(String),
Expand Down
12 changes: 0 additions & 12 deletions packages/io-ts/src/global-realm.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/io-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './global-realm';
export * from './re-exports';
export * from './realm';
export * from './ref';
Expand Down
18 changes: 11 additions & 7 deletions packages/zod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npm install -D @thaumaturgy/zod

```ts
import { z } from 'zod';
import { define, manifest } from '@thaumaturgy/zod';
import { Realm } from '@thaumaturgy/zod';

const Movie = z
.object({
Expand All @@ -30,7 +30,9 @@ const Movie = z
.strict()
.describe('Movie');

define(Movie, {
const realm = new Realm();

realm.define(Movie, {
sequences: {
titles: new Sequence(n => `Movie ${n}` as const),
years: new Sequence(n => 2022 - n),
Expand All @@ -41,7 +43,7 @@ define(Movie, {
}),
});

const movie = manifest(Movie);
const movie = realm.manifest(Movie);

console.log(movie);
// > { title: 'Movie 1', year: 2021 }
Expand All @@ -51,7 +53,7 @@ console.log(movie);

```ts
import { z } from 'zod';
import { define, manifest, Ref } from '@thaumaturgy/zod';
import { Realm, Ref } from '@thaumaturgy/zod';

const Author = z
.object({
Expand All @@ -68,22 +70,24 @@ const Book = z
})
.describe('Book');

define(Author, {
const realm = new Realm();

realm.define(Author, {
manifest: ({ uuid }) => ({
id: uuid(),
name: 'J. R. R. Tolkien',
}),
});

define(Book, {
realm.define(Book, {
manifest: ({ uuid }) => ({
id: uuid(),
authorId: Ref.to(Author).through(author => author.id),
title: 'The Lord of the Rings',
}),
});

const book = manifest(Book);
const book = realm.manifest(Book);

console.log(book);
// > {
Expand Down
22 changes: 14 additions & 8 deletions packages/zod/src/__tests__/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { describe, expect, it } from 'vitest';
import { z } from 'zod';
// Importing from the root barrel file intentionally to simulate what library
// consumers will see.
import { define, manifest, Ref } from '..';
import { Realm, Ref } from '..';

describe('Public API', () => {
it('works with a basic example', () => {
const realm = new Realm();

const Movie = z
.object({
title: z.string(),
Expand All @@ -15,7 +17,7 @@ describe('Public API', () => {
.strict()
.describe('Movie');

define(Movie, {
realm.define(Movie, {
sequences: {
movies: new Sequence(n => `Movie ${n}` as const),
years: new Sequence(n => 2022 - n),
Expand All @@ -26,7 +28,7 @@ describe('Public API', () => {
}),
});

const movie = manifest(Movie);
const movie = realm.manifest(Movie);

expect(movie).toEqual({
title: expect.any(String),
Expand All @@ -35,6 +37,8 @@ describe('Public API', () => {
});

it('works with an entity containing an array field', () => {
const realm = new Realm();

const Post = z
.object({
title: z.string(),
Expand All @@ -43,7 +47,7 @@ describe('Public API', () => {
.strict()
.describe('Post');

define(Post, {
realm.define(Post, {
manifest: ({ sequences }) => ({
title: sequences.titles.next(),
tags: sequences.tags.take(3),
Expand All @@ -54,7 +58,7 @@ describe('Public API', () => {
},
});

const post = manifest(Post);
const post = realm.manifest(Post);

expect(post).toEqual({
title: expect.any(String),
Expand All @@ -63,6 +67,8 @@ describe('Public API', () => {
});

it('works with an entity hierarchy', () => {
const realm = new Realm();

const Author = z
.object({
id: z.string(),
Expand All @@ -78,22 +84,22 @@ describe('Public API', () => {
})
.describe('Book');

define(Author, {
realm.define(Author, {
manifest: ({ uuid }) => ({
id: uuid(),
name: 'J. R. R. Tolkien',
}),
});

define(Book, {
realm.define(Book, {
manifest: ({ uuid }) => ({
id: uuid(),
authorId: Ref.to(Author).through(author => author.id),
title: 'The Lord of the Rings',
}),
});

const book = manifest(Book);
const book = realm.manifest(Book);

expect(book).toEqual({
id: expect.any(String),
Expand Down
12 changes: 0 additions & 12 deletions packages/zod/src/global-realm.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/zod/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './global-realm';
export * from './re-exports';
export * from './realm';
export * from './ref';
Expand Down

0 comments on commit c0cb6df

Please sign in to comment.