@dxos/echo-generator 0.8.4-main.b97322e → 0.8.4-main.bc674ce
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 +41 -26
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +41 -26
- 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 +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 +19 -16
- package/src/data.ts +11 -10
- package/src/generator.test.ts +19 -15
- package/src/generator.ts +32 -32
- package/src/types.ts +5 -9
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/echo-generator",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bc674ce",
|
|
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
|
".": {
|
|
17
|
+
"source": "./src/index.ts",
|
|
13
18
|
"types": "./dist/types/src/index.d.ts",
|
|
14
19
|
"browser": "./dist/lib/browser/index.mjs",
|
|
15
20
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
@@ -24,24 +29,22 @@
|
|
|
24
29
|
"src"
|
|
25
30
|
],
|
|
26
31
|
"dependencies": {
|
|
27
|
-
"@automerge/automerge": "3.
|
|
28
|
-
"effect": "3.
|
|
29
|
-
"@dxos/client": "0.8.4-main.
|
|
30
|
-
"@dxos/echo
|
|
31
|
-
"@dxos/echo-
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/schema": "0.8.4-main.b97322e",
|
|
37
|
-
"@dxos/util": "0.8.4-main.b97322e"
|
|
32
|
+
"@automerge/automerge": "3.2.3",
|
|
33
|
+
"effect": "3.19.11",
|
|
34
|
+
"@dxos/client": "0.8.4-main.bc674ce",
|
|
35
|
+
"@dxos/echo": "0.8.4-main.bc674ce",
|
|
36
|
+
"@dxos/echo-db": "0.8.4-main.bc674ce",
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.bc674ce",
|
|
38
|
+
"@dxos/log": "0.8.4-main.bc674ce",
|
|
39
|
+
"@dxos/node-std": "0.8.4-main.bc674ce",
|
|
40
|
+
"@dxos/util": "0.8.4-main.bc674ce"
|
|
38
41
|
},
|
|
39
42
|
"devDependencies": {
|
|
40
|
-
"@dxos/random": "0.8.4-main.
|
|
43
|
+
"@dxos/random": "0.8.4-main.bc674ce"
|
|
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.bc674ce"
|
|
45
48
|
},
|
|
46
49
|
"publishConfig": {
|
|
47
50
|
"access": "public"
|
package/src/data.ts
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { next as A } from '@automerge/automerge';
|
|
6
|
-
import
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { type Space } from '@dxos/client/echo';
|
|
9
|
+
import { Ref, Type } from '@dxos/echo';
|
|
10
|
+
import { createDocAccessor } from '@dxos/echo-db';
|
|
10
11
|
import { faker } from '@dxos/random';
|
|
11
12
|
|
|
12
13
|
import { SpaceObjectGenerator, TestObjectGenerator } from './generator';
|
|
@@ -26,7 +27,7 @@ export const Priority = [1, 2, 3, 4, 5];
|
|
|
26
27
|
export enum TestSchemaType {
|
|
27
28
|
document = 'example.com/type/Document',
|
|
28
29
|
organization = 'example.com/type/Organization',
|
|
29
|
-
contact = 'example.com/type/
|
|
30
|
+
contact = 'example.com/type/Person',
|
|
30
31
|
project = 'example.com/type/Project',
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -37,21 +38,21 @@ const testSchemas = (): TestSchemaMap<TestSchemaType> => {
|
|
|
37
38
|
const document = Schema.Struct({
|
|
38
39
|
title: Schema.String.annotations({ description: 'title of the document' }),
|
|
39
40
|
content: Schema.String,
|
|
40
|
-
}).pipe(
|
|
41
|
+
}).pipe(Type.object({ typename: TestSchemaType.document, version: '0.1.0' }));
|
|
41
42
|
|
|
42
43
|
const organization = Schema.Struct({
|
|
43
44
|
name: Schema.String.annotations({ description: 'name of the company or organization' }),
|
|
44
45
|
website: Schema.optional(Schema.String.annotations({ description: 'public website URL' })),
|
|
45
46
|
description: Schema.String.annotations({ description: 'short summary of the company' }),
|
|
46
|
-
}).pipe(
|
|
47
|
+
}).pipe(Type.object({ typename: TestSchemaType.organization, version: '0.1.0' }));
|
|
47
48
|
|
|
48
49
|
const contact = Schema.Struct({
|
|
49
50
|
name: Schema.String.annotations({ description: 'name of the person' }),
|
|
50
51
|
email: Schema.optional(Schema.String),
|
|
51
|
-
org: Schema.optional(Ref(organization)),
|
|
52
|
+
org: Schema.optional(Type.Ref(organization)),
|
|
52
53
|
lat: Schema.optional(Schema.Number),
|
|
53
54
|
lng: Schema.optional(Schema.Number),
|
|
54
|
-
}).pipe(
|
|
55
|
+
}).pipe(Type.object({ typename: TestSchemaType.contact, version: '0.1.0' }));
|
|
55
56
|
|
|
56
57
|
const project = Schema.Struct({
|
|
57
58
|
name: Schema.String.annotations({ description: 'name of the project' }),
|
|
@@ -61,8 +62,8 @@ const testSchemas = (): TestSchemaMap<TestSchemaType> => {
|
|
|
61
62
|
status: Schema.String,
|
|
62
63
|
priority: Schema.Number,
|
|
63
64
|
active: Schema.Boolean,
|
|
64
|
-
org: Schema.optional(Ref(organization)),
|
|
65
|
-
}).pipe(
|
|
65
|
+
org: Schema.optional(Type.Ref(organization)),
|
|
66
|
+
}).pipe(Type.object({ typename: TestSchemaType.project, version: '0.1.0' }));
|
|
66
67
|
|
|
67
68
|
return {
|
|
68
69
|
[TestSchemaType.document]: document,
|
package/src/generator.test.ts
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { next as A } from '@automerge/automerge';
|
|
6
|
-
import
|
|
6
|
+
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, Type } from '@dxos/echo';
|
|
10
11
|
import { getObjectCore } from '@dxos/echo-db';
|
|
11
|
-
import { TypedObject, getType } from '@dxos/echo-schema';
|
|
12
12
|
import { faker } from '@dxos/random';
|
|
13
13
|
|
|
14
|
-
import { createSpaceObjectGenerator, createTestObjectGenerator
|
|
14
|
+
import { TestSchemaType, createSpaceObjectGenerator, createTestObjectGenerator } from './data';
|
|
15
15
|
import { SpaceObjectGenerator } from './generator';
|
|
16
16
|
|
|
17
17
|
faker.seed(3);
|
|
@@ -43,7 +43,7 @@ describe('TestObjectGenerator', () => {
|
|
|
43
43
|
|
|
44
44
|
// Create org object.
|
|
45
45
|
const organization = await generator.createObject({ types: [TestSchemaType.organization] });
|
|
46
|
-
expect(
|
|
46
|
+
expect(Obj.getTypeDXN(organization)).to.exist;
|
|
47
47
|
|
|
48
48
|
// Expect at least one person object with a linked org reference.
|
|
49
49
|
const objects = await generator.createObjects({ [TestSchemaType.contact]: 10 });
|
|
@@ -59,14 +59,14 @@ describe('TestObjectGenerator', () => {
|
|
|
59
59
|
const generator = createSpaceObjectGenerator(space);
|
|
60
60
|
await generator.addSchemas();
|
|
61
61
|
const organization = await generator.createObject({ types: [TestSchemaType.organization] });
|
|
62
|
-
schemaId.push(
|
|
62
|
+
schemaId.push(Obj.getTypeDXN(organization)!.toString());
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
{
|
|
66
66
|
const generator = createSpaceObjectGenerator(space);
|
|
67
67
|
await generator.addSchemas();
|
|
68
68
|
const organization = await generator.createObject({ types: [TestSchemaType.organization] });
|
|
69
|
-
schemaId.push(
|
|
69
|
+
schemaId.push(Obj.getTypeDXN(organization)!.toString());
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
expect(schemaId[0]).not.to.be.undefined;
|
|
@@ -78,7 +78,7 @@ describe('TestObjectGenerator', () => {
|
|
|
78
78
|
const generator = createSpaceObjectGenerator(space);
|
|
79
79
|
await generator.addSchemas();
|
|
80
80
|
const document = await generator.createObject({ types: [TestSchemaType.document] });
|
|
81
|
-
expect(
|
|
81
|
+
expect(Obj.getTypeDXN(document)).to.exist;
|
|
82
82
|
|
|
83
83
|
const beforeChangesCount = A.getAllChanges(getObjectCore(document).docHandle!.doc()).length;
|
|
84
84
|
|
|
@@ -92,12 +92,14 @@ describe('TestObjectGenerator', () => {
|
|
|
92
92
|
});
|
|
93
93
|
|
|
94
94
|
test('create object with in memory schema', async () => {
|
|
95
|
-
|
|
96
|
-
typename: 'example.org/type/Task',
|
|
97
|
-
version: '0.1.0',
|
|
98
|
-
})({
|
|
95
|
+
const Task = Schema.Struct({
|
|
99
96
|
name: Schema.optional(Schema.String),
|
|
100
|
-
})
|
|
97
|
+
}).pipe(
|
|
98
|
+
Type.object({
|
|
99
|
+
typename: 'example.org/type/Task',
|
|
100
|
+
version: '0.1.0',
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
101
103
|
|
|
102
104
|
enum Types {
|
|
103
105
|
task = 'example.org/type/Task',
|
|
@@ -106,7 +108,9 @@ describe('TestObjectGenerator', () => {
|
|
|
106
108
|
const { space } = await setupTest();
|
|
107
109
|
const generator = new SpaceObjectGenerator<Types>(
|
|
108
110
|
space,
|
|
109
|
-
{
|
|
111
|
+
{
|
|
112
|
+
[Types.task]: Task,
|
|
113
|
+
},
|
|
110
114
|
{
|
|
111
115
|
[Types.task]: () => ({ name: 'Default' }),
|
|
112
116
|
},
|
|
@@ -121,7 +125,7 @@ describe('TestObjectGenerator', () => {
|
|
|
121
125
|
await generator.addSchemas();
|
|
122
126
|
|
|
123
127
|
const todo = await generator.createObject({ types: [Types.task] });
|
|
124
|
-
expect(
|
|
128
|
+
expect(Obj.getTypeDXN(todo)).to.exist;
|
|
125
129
|
});
|
|
126
130
|
|
|
127
131
|
test('references', async () => {
|
|
@@ -141,7 +145,7 @@ describe('TestObjectGenerator', () => {
|
|
|
141
145
|
test('create project', async () => {
|
|
142
146
|
const generator = createTestObjectGenerator();
|
|
143
147
|
const project = await generator.createObject({ types: [TestSchemaType.project] });
|
|
144
|
-
expect(
|
|
148
|
+
expect(Obj.getTypeDXN(project)).to.exist;
|
|
145
149
|
});
|
|
146
150
|
|
|
147
151
|
test('create object with not type', async () => {
|
package/src/generator.ts
CHANGED
|
@@ -2,19 +2,17 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Schema } from 'effect';
|
|
6
|
-
|
|
7
5
|
import { Filter, type Space } from '@dxos/client/echo';
|
|
8
|
-
import { type
|
|
9
|
-
import {
|
|
6
|
+
import { Obj, type Type } from '@dxos/echo';
|
|
7
|
+
import { EchoSchema, getTypeAnnotation } from '@dxos/echo/internal';
|
|
8
|
+
import { isProxy } from '@dxos/echo/internal';
|
|
10
9
|
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { live, isLiveObject, type Live } from '@dxos/live-object';
|
|
12
10
|
import { faker } from '@dxos/random';
|
|
13
11
|
import { entries, range } from '@dxos/util';
|
|
14
12
|
|
|
15
13
|
import { type TestSchemaType } from './data';
|
|
16
14
|
import {
|
|
17
|
-
type
|
|
15
|
+
type MutationsProviderProps,
|
|
18
16
|
type TestGeneratorMap,
|
|
19
17
|
type TestMutationsMap,
|
|
20
18
|
type TestObjectProvider,
|
|
@@ -28,37 +26,38 @@ import {
|
|
|
28
26
|
export class TestObjectGenerator<T extends string = TestSchemaType> {
|
|
29
27
|
// prettier-ignore
|
|
30
28
|
constructor(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
protected readonly _schemas: TestSchemaMap<T>,
|
|
30
|
+
private readonly _generators: TestGeneratorMap<T>,
|
|
31
|
+
private readonly _provider?: TestObjectProvider<T>,
|
|
32
|
+
) {}
|
|
35
33
|
|
|
36
|
-
get schemas():
|
|
34
|
+
get schemas(): Type.Obj.Any[] {
|
|
37
35
|
return Object.values(this._schemas);
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
getSchema(type: T):
|
|
38
|
+
getSchema(type: T): Type.Obj.Any | undefined {
|
|
41
39
|
return this.schemas.find((schema) => getTypeAnnotation(schema)!.typename === type);
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
protected setSchema(type: T, schema:
|
|
42
|
+
protected setSchema(type: T, schema: Type.Obj.Any): void {
|
|
45
43
|
this._schemas[type] = schema;
|
|
46
44
|
}
|
|
47
45
|
|
|
48
|
-
async createObject({ types }: { types?: T[] } = {}): Promise<
|
|
46
|
+
async createObject({ types }: { types?: T[] } = {}): Promise<any> {
|
|
49
47
|
const type = faker.helpers.arrayElement(types ?? (Object.keys(this._schemas) as T[]));
|
|
50
48
|
const data = await this._generators[type](this._provider);
|
|
51
|
-
if (
|
|
49
|
+
if (isProxy(data)) {
|
|
52
50
|
return data;
|
|
53
51
|
}
|
|
54
52
|
|
|
55
53
|
const schema = this.getSchema(type);
|
|
56
|
-
|
|
54
|
+
invariant(schema, `Schema is required for type: ${type}. Register a schema for this type.`);
|
|
55
|
+
return Obj.make(schema, data);
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
// TODO(burdon): Based on dependencies (e.g., organization before contact).
|
|
60
59
|
async createObjects(map: Partial<Record<T, number>>) {
|
|
61
|
-
const results:
|
|
60
|
+
const results: any[] = [];
|
|
62
61
|
for (const [type, count] of entries(map)) {
|
|
63
62
|
results.push(...(await Promise.all(range(count ?? 0, () => this.createObject({ types: [type as T] })))));
|
|
64
63
|
}
|
|
@@ -85,15 +84,15 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
85
84
|
) {
|
|
86
85
|
super(schemaMap, generators, async (type: T) => {
|
|
87
86
|
const schema = this.getSchema(type);
|
|
88
|
-
const
|
|
87
|
+
const objects = await this._space.db.query(schema ? Filter.type(schema) : Filter.nothing()).run();
|
|
89
88
|
return objects;
|
|
90
89
|
});
|
|
91
90
|
}
|
|
92
91
|
|
|
93
92
|
async addSchemas() {
|
|
94
|
-
const result:
|
|
93
|
+
const result: Type.Obj.Any[] = [];
|
|
95
94
|
for (const [typename, schema] of Object.entries(this._schemas)) {
|
|
96
|
-
const echoSchema = await this._maybeRegisterSchema(typename, schema as
|
|
95
|
+
const echoSchema = await this._maybeRegisterSchema(typename, schema as Type.Obj.Any);
|
|
97
96
|
this.setSchema(typename as T, echoSchema);
|
|
98
97
|
result.push(echoSchema);
|
|
99
98
|
}
|
|
@@ -101,40 +100,41 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
101
100
|
return result;
|
|
102
101
|
}
|
|
103
102
|
|
|
104
|
-
override async createObject({
|
|
103
|
+
override async createObject({
|
|
104
|
+
types,
|
|
105
|
+
}: {
|
|
106
|
+
types?: T[];
|
|
107
|
+
} = {}): Promise<Obj.Any> {
|
|
105
108
|
return this._space.db.add(await super.createObject({ types }));
|
|
106
109
|
}
|
|
107
110
|
|
|
108
|
-
private async _maybeRegisterSchema(
|
|
109
|
-
typename: string,
|
|
110
|
-
schema: EchoSchema | Schema.Schema.AnyNoContext,
|
|
111
|
-
): Promise<EchoSchema | Schema.Schema.AnyNoContext> {
|
|
111
|
+
private async _maybeRegisterSchema(typename: string, schema: Type.Obj.Any): Promise<Type.Obj.Any> {
|
|
112
112
|
if (schema instanceof EchoSchema) {
|
|
113
|
-
const existingSchema = this._space.db.schemaRegistry.getSchema(typename);
|
|
113
|
+
const existingSchema = this._space.internal.db.schemaRegistry.getSchema(typename);
|
|
114
114
|
if (existingSchema != null) {
|
|
115
115
|
return existingSchema;
|
|
116
116
|
}
|
|
117
|
-
const [registeredSchema] = await this._space.db.schemaRegistry.register([schema]);
|
|
117
|
+
const [registeredSchema] = await this._space.internal.db.schemaRegistry.register([schema]);
|
|
118
118
|
return registeredSchema;
|
|
119
119
|
} else {
|
|
120
|
-
const existingSchema = this._space.db.graph.schemaRegistry.getSchema(typename);
|
|
120
|
+
const existingSchema = this._space.internal.db.graph.schemaRegistry.getSchema(typename);
|
|
121
121
|
if (existingSchema != null) {
|
|
122
122
|
return existingSchema;
|
|
123
123
|
}
|
|
124
|
-
this._space.db.graph.schemaRegistry.
|
|
124
|
+
await this._space.internal.db.graph.schemaRegistry.register([schema]);
|
|
125
125
|
return schema;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
async mutateObject(object:
|
|
129
|
+
async mutateObject(object: Obj.Any, params: MutationsProviderProps): Promise<void> {
|
|
130
130
|
invariant(this._mutations, 'Mutations not defined.');
|
|
131
|
-
const type = getTypeAnnotation(getSchema(object)!)!.typename as T;
|
|
131
|
+
const type = getTypeAnnotation(Obj.getSchema(object)!)!.typename as T;
|
|
132
132
|
invariant(type && this._mutations?.[type], 'Invalid object type.');
|
|
133
133
|
|
|
134
134
|
await this._mutations;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
async mutateObjects(objects:
|
|
137
|
+
async mutateObjects(objects: Obj.Any[], params: MutationsProviderProps): Promise<void> {
|
|
138
138
|
for (const object of objects) {
|
|
139
139
|
await this.mutateObject(object, params);
|
|
140
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 AnyLiveObject } from '@dxos/echo-db';
|
|
8
|
-
import { type EchoSchema } from '@dxos/echo-schema';
|
|
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>;
|