@dxos/echo-generator 0.8.2-main.fbd8ed0 → 0.8.2-staging.7ac8446
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 +27 -28
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +24 -25
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +27 -28
- 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 +12 -13
- package/dist/types/src/generator.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +6 -7
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -14
- package/src/data.ts +20 -22
- package/src/generator.test.ts +7 -8
- package/src/generator.ts +17 -19
- package/src/types.ts +6 -8
package/src/generator.ts
CHANGED
|
@@ -2,13 +2,11 @@
|
|
|
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 { getTypeAnnotation, EchoSchema,
|
|
6
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
7
|
+
import { getTypeAnnotation, EchoSchema, type S } from '@dxos/echo-schema';
|
|
10
8
|
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import {
|
|
9
|
+
import { create, getSchema, isReactiveObject, type ReactiveObject } from '@dxos/live-object';
|
|
12
10
|
import { faker } from '@dxos/random';
|
|
13
11
|
import { range } from '@dxos/util';
|
|
14
12
|
|
|
@@ -32,27 +30,27 @@ export class TestObjectGenerator<T extends string = TestSchemaType> {
|
|
|
32
30
|
private readonly _provider?: TestObjectProvider<T>,
|
|
33
31
|
) {}
|
|
34
32
|
|
|
35
|
-
get schemas(): (EchoSchema |
|
|
33
|
+
get schemas(): (EchoSchema | S.Schema.AnyNoContext)[] {
|
|
36
34
|
return Object.values(this._schemas);
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
getSchema(type: T): EchoSchema |
|
|
37
|
+
getSchema(type: T): EchoSchema | S.Schema.AnyNoContext | undefined {
|
|
40
38
|
return this.schemas.find((schema) => getTypeAnnotation(schema)!.typename === type);
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
protected setSchema(type: T, schema: EchoSchema |
|
|
41
|
+
protected setSchema(type: T, schema: EchoSchema | S.Schema.AnyNoContext) {
|
|
44
42
|
this._schemas[type] = schema;
|
|
45
43
|
}
|
|
46
44
|
|
|
47
|
-
async createObject({ types }: { types?: T[] } = {}): Promise<
|
|
45
|
+
async createObject({ types }: { types?: T[] } = {}): Promise<ReactiveObject<any>> {
|
|
48
46
|
const type = faker.helpers.arrayElement(types ?? (Object.keys(this._schemas) as T[]));
|
|
49
47
|
const data = await this._generators[type](this._provider);
|
|
50
|
-
if (
|
|
48
|
+
if (isReactiveObject(data)) {
|
|
51
49
|
return data;
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
const schema = this.getSchema(type);
|
|
55
|
-
return schema ?
|
|
53
|
+
return schema ? create(schema, data) : create(data);
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
// TODO(burdon): Based on dependencies (e.g., organization before contact).
|
|
@@ -79,14 +77,14 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
79
77
|
) {
|
|
80
78
|
super(schemaMap, generators, async (type: T) => {
|
|
81
79
|
const schema = this.getSchema(type);
|
|
82
|
-
return (schema && (await this._space.db.query(Filter.
|
|
80
|
+
return (schema && (await this._space.db.query(Filter.schema(schema)).run()).objects) ?? [];
|
|
83
81
|
});
|
|
84
82
|
}
|
|
85
83
|
|
|
86
84
|
async addSchemas() {
|
|
87
|
-
const result: (EchoSchema |
|
|
85
|
+
const result: (EchoSchema | S.Schema.AnyNoContext)[] = [];
|
|
88
86
|
for (const [typename, schema] of Object.entries(this._schemas)) {
|
|
89
|
-
const echoSchema = await this._maybeRegisterSchema(typename, schema as EchoSchema |
|
|
87
|
+
const echoSchema = await this._maybeRegisterSchema(typename, schema as EchoSchema | S.Schema.AnyNoContext);
|
|
90
88
|
this.setSchema(typename as T, echoSchema);
|
|
91
89
|
result.push(echoSchema);
|
|
92
90
|
}
|
|
@@ -94,14 +92,14 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
94
92
|
return result;
|
|
95
93
|
}
|
|
96
94
|
|
|
97
|
-
override async createObject({ types }: { types?: T[] } = {}): Promise<
|
|
95
|
+
override async createObject({ types }: { types?: T[] } = {}): Promise<ReactiveEchoObject<any>> {
|
|
98
96
|
return this._space.db.add(await super.createObject({ types }));
|
|
99
97
|
}
|
|
100
98
|
|
|
101
99
|
private async _maybeRegisterSchema(
|
|
102
100
|
typename: string,
|
|
103
|
-
schema: EchoSchema |
|
|
104
|
-
): Promise<EchoSchema |
|
|
101
|
+
schema: EchoSchema | S.Schema.AnyNoContext,
|
|
102
|
+
): Promise<EchoSchema | S.Schema.AnyNoContext> {
|
|
105
103
|
if (schema instanceof EchoSchema) {
|
|
106
104
|
const existingSchema = this._space.db.schemaRegistry.getSchema(typename);
|
|
107
105
|
if (existingSchema != null) {
|
|
@@ -119,7 +117,7 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
119
117
|
}
|
|
120
118
|
}
|
|
121
119
|
|
|
122
|
-
async mutateObject(object:
|
|
120
|
+
async mutateObject(object: ReactiveEchoObject<any>, params: MutationsProviderParams) {
|
|
123
121
|
invariant(this._mutations, 'Mutations not defined.');
|
|
124
122
|
const type = getTypeAnnotation(getSchema(object)!)!.typename as T;
|
|
125
123
|
invariant(type && this._mutations?.[type], 'Invalid object type.');
|
|
@@ -127,7 +125,7 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
|
|
|
127
125
|
await this._mutations;
|
|
128
126
|
}
|
|
129
127
|
|
|
130
|
-
async mutateObjects(objects:
|
|
128
|
+
async mutateObjects(objects: ReactiveEchoObject<any>[], params: MutationsProviderParams) {
|
|
131
129
|
for (const object of objects) {
|
|
132
130
|
await this.mutateObject(object, params);
|
|
133
131
|
}
|
package/src/types.ts
CHANGED
|
@@ -2,18 +2,16 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type
|
|
6
|
-
|
|
7
|
-
import { type
|
|
8
|
-
import { type EchoSchema } from '@dxos/echo-schema';
|
|
9
|
-
import { type Live } from '@dxos/live-object';
|
|
5
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
6
|
+
import { type EchoSchema, type S } from '@dxos/echo-schema';
|
|
7
|
+
import { type ReactiveObject } from '@dxos/live-object';
|
|
10
8
|
|
|
11
9
|
// TODO(burdon): Use echo-schema types.
|
|
12
10
|
export type TestObject = { id: string } & Record<string, any>;
|
|
13
11
|
|
|
14
|
-
export type TestSchemaMap<T extends string = string> = Record<T, EchoSchema |
|
|
12
|
+
export type TestSchemaMap<T extends string = string> = Record<T, EchoSchema | S.Schema.AnyNoContext>;
|
|
15
13
|
|
|
16
|
-
export type TestObjectProvider<T extends string = string> = (type: T) => Promise<
|
|
14
|
+
export type TestObjectProvider<T extends string = string> = (type: T) => Promise<ReactiveObject<any>[]>;
|
|
17
15
|
|
|
18
16
|
export type TestGeneratorMap<T extends string = string> = Record<
|
|
19
17
|
T,
|
|
@@ -28,4 +26,4 @@ export type MutationsProviderParams = {
|
|
|
28
26
|
maxContentLength: number;
|
|
29
27
|
};
|
|
30
28
|
|
|
31
|
-
export type TestObjectMutators = (object:
|
|
29
|
+
export type TestObjectMutators = (object: ReactiveEchoObject<any>, params: MutationsProviderParams) => Promise<void>;
|