@dxos/echo-generator 0.8.4-main.7ace549 → 0.8.4-main.937b3ca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +23 -15
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +23 -15
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/data.d.ts.map +1 -1
- package/dist/types/src/generator.d.ts +14 -14
- package/dist/types/src/generator.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +5 -8
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -15
- package/src/data.ts +4 -5
- package/src/generator.test.ts +8 -7
- package/src/generator.ts +30 -31
- package/src/types.ts +5 -9
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/echo-generator",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.937b3ca",
|
|
4
4
|
"description": "ECHO data generator for testing.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -25,23 +29,22 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@automerge/automerge": "3.
|
|
29
|
-
"effect": "3.
|
|
30
|
-
"@dxos/client": "0.8.4-main.
|
|
31
|
-
"@dxos/echo": "0.8.4-main.
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/node-std": "0.8.4-main.7ace549"
|
|
32
|
+
"@automerge/automerge": "3.2.3",
|
|
33
|
+
"effect": "3.19.11",
|
|
34
|
+
"@dxos/client": "0.8.4-main.937b3ca",
|
|
35
|
+
"@dxos/echo": "0.8.4-main.937b3ca",
|
|
36
|
+
"@dxos/echo-db": "0.8.4-main.937b3ca",
|
|
37
|
+
"@dxos/log": "0.8.4-main.937b3ca",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.937b3ca",
|
|
39
|
+
"@dxos/util": "0.8.4-main.937b3ca",
|
|
40
|
+
"@dxos/node-std": "0.8.4-main.937b3ca"
|
|
38
41
|
},
|
|
39
42
|
"devDependencies": {
|
|
40
|
-
"@dxos/random": "0.8.4-main.
|
|
43
|
+
"@dxos/random": "0.8.4-main.937b3ca"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
|
-
"effect": "
|
|
44
|
-
"@dxos/random": "0.8.4-main.
|
|
46
|
+
"effect": "3.19.11",
|
|
47
|
+
"@dxos/random": "0.8.4-main.937b3ca"
|
|
45
48
|
},
|
|
46
49
|
"publishConfig": {
|
|
47
50
|
"access": "public"
|
package/src/data.ts
CHANGED
|
@@ -7,7 +7,6 @@ import * as Schema from 'effect/Schema';
|
|
|
7
7
|
|
|
8
8
|
import { type Space } from '@dxos/client/echo';
|
|
9
9
|
import { Ref, Type } from '@dxos/echo';
|
|
10
|
-
import { EchoObjectSchema } from '@dxos/echo/internal';
|
|
11
10
|
import { createDocAccessor } from '@dxos/echo-db';
|
|
12
11
|
import { faker } from '@dxos/random';
|
|
13
12
|
|
|
@@ -39,13 +38,13 @@ const testSchemas = (): TestSchemaMap<TestSchemaType> => {
|
|
|
39
38
|
const document = Schema.Struct({
|
|
40
39
|
title: Schema.String.annotations({ description: 'title of the document' }),
|
|
41
40
|
content: Schema.String,
|
|
42
|
-
}).pipe(
|
|
41
|
+
}).pipe(Type.object({ typename: TestSchemaType.document, version: '0.1.0' }));
|
|
43
42
|
|
|
44
43
|
const organization = Schema.Struct({
|
|
45
44
|
name: Schema.String.annotations({ description: 'name of the company or organization' }),
|
|
46
45
|
website: Schema.optional(Schema.String.annotations({ description: 'public website URL' })),
|
|
47
46
|
description: Schema.String.annotations({ description: 'short summary of the company' }),
|
|
48
|
-
}).pipe(
|
|
47
|
+
}).pipe(Type.object({ typename: TestSchemaType.organization, version: '0.1.0' }));
|
|
49
48
|
|
|
50
49
|
const contact = Schema.Struct({
|
|
51
50
|
name: Schema.String.annotations({ description: 'name of the person' }),
|
|
@@ -53,7 +52,7 @@ const testSchemas = (): TestSchemaMap<TestSchemaType> => {
|
|
|
53
52
|
org: Schema.optional(Type.Ref(organization)),
|
|
54
53
|
lat: Schema.optional(Schema.Number),
|
|
55
54
|
lng: Schema.optional(Schema.Number),
|
|
56
|
-
}).pipe(
|
|
55
|
+
}).pipe(Type.object({ typename: TestSchemaType.contact, version: '0.1.0' }));
|
|
57
56
|
|
|
58
57
|
const project = Schema.Struct({
|
|
59
58
|
name: Schema.String.annotations({ description: 'name of the project' }),
|
|
@@ -64,7 +63,7 @@ const testSchemas = (): TestSchemaMap<TestSchemaType> => {
|
|
|
64
63
|
priority: Schema.Number,
|
|
65
64
|
active: Schema.Boolean,
|
|
66
65
|
org: Schema.optional(Type.Ref(organization)),
|
|
67
|
-
}).pipe(
|
|
66
|
+
}).pipe(Type.object({ typename: TestSchemaType.project, version: '0.1.0' }));
|
|
68
67
|
|
|
69
68
|
return {
|
|
70
69
|
[TestSchemaType.document]: document,
|
package/src/generator.test.ts
CHANGED
|
@@ -7,8 +7,7 @@ import * as Schema from 'effect/Schema';
|
|
|
7
7
|
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import { Client } from '@dxos/client';
|
|
10
|
-
import { Obj } from '@dxos/echo';
|
|
11
|
-
import { TypedObject } from '@dxos/echo/internal';
|
|
10
|
+
import { Obj, Type } from '@dxos/echo';
|
|
12
11
|
import { getObjectCore } from '@dxos/echo-db';
|
|
13
12
|
import { faker } from '@dxos/random';
|
|
14
13
|
|
|
@@ -93,12 +92,14 @@ describe('TestObjectGenerator', () => {
|
|
|
93
92
|
});
|
|
94
93
|
|
|
95
94
|
test('create object with in memory schema', async () => {
|
|
96
|
-
|
|
97
|
-
typename: 'example.org/type/Task',
|
|
98
|
-
version: '0.1.0',
|
|
99
|
-
})({
|
|
95
|
+
const Task = Schema.Struct({
|
|
100
96
|
name: Schema.optional(Schema.String),
|
|
101
|
-
})
|
|
97
|
+
}).pipe(
|
|
98
|
+
Type.object({
|
|
99
|
+
typename: 'example.org/type/Task',
|
|
100
|
+
version: '0.1.0',
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
102
103
|
|
|
103
104
|
enum Types {
|
|
104
105
|
task = 'example.org/type/Task',
|
package/src/generator.ts
CHANGED
|
@@ -2,20 +2,17 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type * as Schema from 'effect/Schema';
|
|
6
|
-
|
|
7
5
|
import { Filter, type Space } from '@dxos/client/echo';
|
|
8
|
-
import { Obj } from '@dxos/echo';
|
|
6
|
+
import { Obj, type Type } from '@dxos/echo';
|
|
9
7
|
import { EchoSchema, getTypeAnnotation } from '@dxos/echo/internal';
|
|
10
|
-
import {
|
|
8
|
+
import { isProxy } from '@dxos/echo/internal';
|
|
11
9
|
import { invariant } from '@dxos/invariant';
|
|
12
|
-
import { type Live, isLiveObject, live } from '@dxos/live-object';
|
|
13
10
|
import { faker } from '@dxos/random';
|
|
14
11
|
import { entries, range } from '@dxos/util';
|
|
15
12
|
|
|
16
13
|
import { type TestSchemaType } from './data';
|
|
17
14
|
import {
|
|
18
|
-
type
|
|
15
|
+
type MutationsProviderProps,
|
|
19
16
|
type TestGeneratorMap,
|
|
20
17
|
type TestMutationsMap,
|
|
21
18
|
type TestObjectProvider,
|
|
@@ -29,37 +26,38 @@ import {
|
|
|
29
26
|
export class TestObjectGenerator<T extends string = TestSchemaType> {
|
|
30
27
|
// prettier-ignore
|
|
31
28
|
constructor(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
protected readonly _schemas: TestSchemaMap<T>,
|
|
30
|
+
private readonly _generators: TestGeneratorMap<T>,
|
|
31
|
+
private readonly _provider?: TestObjectProvider<T>,
|
|
32
|
+
) {}
|
|
36
33
|
|
|
37
|
-
get schemas():
|
|
34
|
+
get schemas(): Type.Obj.Any[] {
|
|
38
35
|
return Object.values(this._schemas);
|
|
39
36
|
}
|
|
40
37
|
|
|
41
|
-
getSchema(type: T):
|
|
38
|
+
getSchema(type: T): Type.Obj.Any | undefined {
|
|
42
39
|
return this.schemas.find((schema) => getTypeAnnotation(schema)!.typename === type);
|
|
43
40
|
}
|
|
44
41
|
|
|
45
|
-
protected setSchema(type: T, schema:
|
|
42
|
+
protected setSchema(type: T, schema: Type.Obj.Any): void {
|
|
46
43
|
this._schemas[type] = schema;
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
async createObject({ types }: { types?: T[] } = {}): Promise<
|
|
46
|
+
async createObject({ types }: { types?: T[] } = {}): Promise<any> {
|
|
50
47
|
const type = faker.helpers.arrayElement(types ?? (Object.keys(this._schemas) as T[]));
|
|
51
48
|
const data = await this._generators[type](this._provider);
|
|
52
|
-
if (
|
|
49
|
+
if (isProxy(data)) {
|
|
53
50
|
return data;
|
|
54
51
|
}
|
|
55
52
|
|
|
56
53
|
const schema = this.getSchema(type);
|
|
57
|
-
|
|
54
|
+
invariant(schema, `Schema is required for type: ${type}. Register a schema for this type.`);
|
|
55
|
+
return Obj.make(schema, data);
|
|
58
56
|
}
|
|
59
57
|
|
|
60
58
|
// TODO(burdon): Based on dependencies (e.g., organization before contact).
|
|
61
59
|
async createObjects(map: Partial<Record<T, number>>) {
|
|
62
|
-
const results:
|
|
60
|
+
const results: any[] = [];
|
|
63
61
|
for (const [type, count] of entries(map)) {
|
|
64
62
|
results.push(...(await Promise.all(range(count ?? 0, () => this.createObject({ types: [type as T] })))));
|
|
65
63
|
}
|
|
@@ -86,15 +84,15 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
86
84
|
) {
|
|
87
85
|
super(schemaMap, generators, async (type: T) => {
|
|
88
86
|
const schema = this.getSchema(type);
|
|
89
|
-
const
|
|
87
|
+
const objects = await this._space.db.query(schema ? Filter.type(schema) : Filter.nothing()).run();
|
|
90
88
|
return objects;
|
|
91
89
|
});
|
|
92
90
|
}
|
|
93
91
|
|
|
94
92
|
async addSchemas() {
|
|
95
|
-
const result:
|
|
93
|
+
const result: Type.Obj.Any[] = [];
|
|
96
94
|
for (const [typename, schema] of Object.entries(this._schemas)) {
|
|
97
|
-
const echoSchema = await this._maybeRegisterSchema(typename, schema as
|
|
95
|
+
const echoSchema = await this._maybeRegisterSchema(typename, schema as Type.Obj.Any);
|
|
98
96
|
this.setSchema(typename as T, echoSchema);
|
|
99
97
|
result.push(echoSchema);
|
|
100
98
|
}
|
|
@@ -102,32 +100,33 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
102
100
|
return result;
|
|
103
101
|
}
|
|
104
102
|
|
|
105
|
-
override async createObject({
|
|
103
|
+
override async createObject({
|
|
104
|
+
types,
|
|
105
|
+
}: {
|
|
106
|
+
types?: T[];
|
|
107
|
+
} = {}): Promise<Obj.Any> {
|
|
106
108
|
return this._space.db.add(await super.createObject({ types }));
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
private async _maybeRegisterSchema(
|
|
110
|
-
typename: string,
|
|
111
|
-
schema: EchoSchema | Schema.Schema.AnyNoContext,
|
|
112
|
-
): Promise<EchoSchema | Schema.Schema.AnyNoContext> {
|
|
111
|
+
private async _maybeRegisterSchema(typename: string, schema: Type.Obj.Any): Promise<Type.Obj.Any> {
|
|
113
112
|
if (schema instanceof EchoSchema) {
|
|
114
|
-
const existingSchema = this._space.db.schemaRegistry.getSchema(typename);
|
|
113
|
+
const existingSchema = this._space.internal.db.schemaRegistry.getSchema(typename);
|
|
115
114
|
if (existingSchema != null) {
|
|
116
115
|
return existingSchema;
|
|
117
116
|
}
|
|
118
|
-
const [registeredSchema] = await this._space.db.schemaRegistry.register([schema]);
|
|
117
|
+
const [registeredSchema] = await this._space.internal.db.schemaRegistry.register([schema]);
|
|
119
118
|
return registeredSchema;
|
|
120
119
|
} else {
|
|
121
|
-
const existingSchema = this._space.db.graph.schemaRegistry.getSchema(typename);
|
|
120
|
+
const existingSchema = this._space.internal.db.graph.schemaRegistry.getSchema(typename);
|
|
122
121
|
if (existingSchema != null) {
|
|
123
122
|
return existingSchema;
|
|
124
123
|
}
|
|
125
|
-
this._space.db.graph.schemaRegistry.
|
|
124
|
+
await this._space.internal.db.graph.schemaRegistry.register([schema]);
|
|
126
125
|
return schema;
|
|
127
126
|
}
|
|
128
127
|
}
|
|
129
128
|
|
|
130
|
-
async mutateObject(object:
|
|
129
|
+
async mutateObject(object: Obj.Any, params: MutationsProviderProps): Promise<void> {
|
|
131
130
|
invariant(this._mutations, 'Mutations not defined.');
|
|
132
131
|
const type = getTypeAnnotation(Obj.getSchema(object)!)!.typename as T;
|
|
133
132
|
invariant(type && this._mutations?.[type], 'Invalid object type.');
|
|
@@ -135,7 +134,7 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
135
134
|
await this._mutations;
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
async mutateObjects(objects:
|
|
137
|
+
async mutateObjects(objects: Obj.Any[], params: MutationsProviderProps): Promise<void> {
|
|
139
138
|
for (const object of objects) {
|
|
140
139
|
await this.mutateObject(object, params);
|
|
141
140
|
}
|
package/src/types.ts
CHANGED
|
@@ -2,18 +2,14 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type
|
|
6
|
-
|
|
7
|
-
import { type EchoSchema } from '@dxos/echo/internal';
|
|
8
|
-
import { type AnyLiveObject } from '@dxos/echo-db';
|
|
9
|
-
import { type Live } from '@dxos/live-object';
|
|
5
|
+
import { type Obj, type Type } from '@dxos/echo';
|
|
10
6
|
|
|
11
7
|
// TODO(burdon): Use echo-schema types.
|
|
12
8
|
export type TestObject = { id: string } & Record<string, any>;
|
|
13
9
|
|
|
14
|
-
export type TestSchemaMap<T extends string = string> = Record<T,
|
|
10
|
+
export type TestSchemaMap<T extends string = string> = Record<T, Type.Obj.Any>;
|
|
15
11
|
|
|
16
|
-
export type TestObjectProvider<T extends string = string> = (type: T) => Promise<
|
|
12
|
+
export type TestObjectProvider<T extends string = string> = (type: T) => Promise<any[]>;
|
|
17
13
|
|
|
18
14
|
export type TestGeneratorMap<T extends string = string> = Record<
|
|
19
15
|
T,
|
|
@@ -22,10 +18,10 @@ export type TestGeneratorMap<T extends string = string> = Record<
|
|
|
22
18
|
|
|
23
19
|
export type TestMutationsMap<T extends string = string> = Record<T, TestObjectMutators>;
|
|
24
20
|
|
|
25
|
-
export type
|
|
21
|
+
export type MutationsProviderProps = {
|
|
26
22
|
count: number;
|
|
27
23
|
mutationSize: number;
|
|
28
24
|
maxContentLength: number;
|
|
29
25
|
};
|
|
30
26
|
|
|
31
|
-
export type TestObjectMutators = (object:
|
|
27
|
+
export type TestObjectMutators = (object: Obj.Any, params: MutationsProviderProps) => Promise<void>;
|