@dxos/echo-generator 0.4.9 → 0.4.10-main.068c3d8

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/src/generator.ts CHANGED
@@ -2,7 +2,9 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Expando, type TypedObject, Schema, type Space } from '@dxos/client/echo';
5
+ import { type Space } from '@dxos/client/echo';
6
+ import { type DynamicEchoSchema, Filter, type ReactiveObject } from '@dxos/echo-schema';
7
+ import { create } from '@dxos/echo-schema';
6
8
  import { faker } from '@dxos/random';
7
9
 
8
10
  import { type TestSchemaType } from './data';
@@ -20,30 +22,30 @@ export class TestObjectGenerator<T extends string = TestSchemaType> {
20
22
  private readonly _provider?: TestObjectProvider<T>
21
23
  ) {}
22
24
 
23
- get schemas(): Schema[] {
25
+ get schemas(): DynamicEchoSchema[] {
24
26
  return Object.values(this._schemas);
25
27
  }
26
28
 
27
- getSchema(type: T) {
29
+ getSchema(type: T): DynamicEchoSchema | undefined {
28
30
  return this.schemas.find((schema) => schema.typename === type);
29
31
  }
30
32
 
31
- protected setSchema(type: T, schema: Schema) {
33
+ protected setSchema(type: T, schema: DynamicEchoSchema) {
32
34
  this._schemas[type] = schema;
33
35
  }
34
36
 
35
37
  // TODO(burdon): Runtime type check via: https://github.com/Effect-TS/schema (or zod).
36
- createObject({ types }: { types?: T[] } = {}): TypedObject {
38
+ createObject({ types }: { types?: T[] } = {}): ReactiveObject<any> {
37
39
  const type = faker.helpers.arrayElement(types ?? (Object.keys(this._schemas) as T[]));
38
40
  const data = this._generators[type](this._provider);
39
41
  const schema = this.getSchema(type);
40
- return new Expando(data, { schema });
42
+ return schema ? create(schema, data) : create(data);
41
43
  }
42
44
 
43
45
  // TODO(burdon): Create batch.
44
46
  // TODO(burdon): Based on dependencies (e.g., organization before contact).
45
- createObjects(map: Partial<Record<T, number>>): Expando[] {
46
- const objects: Expando[] = [];
47
+ createObjects(map: Partial<Record<T, number>>): ReactiveObject<any>[] {
48
+ const objects: ReactiveObject<any>[] = [];
47
49
  Object.entries<number>(map as any).forEach(([type, count]) => {
48
50
  range(() => objects.push(this.createObject({ types: [type as T] })), count);
49
51
  });
@@ -62,36 +64,38 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
62
64
  generators: TestGeneratorMap<T>,
63
65
  ) {
64
66
  super(schemaMap, generators, (type: T) => {
65
- const { objects } = this._space.db.query((object: TypedObject) => {
66
- return object.__schema?.id === this.getSchema(type)?.id;
67
- });
68
-
69
- return objects;
67
+ const schema = this.getSchema(type);
68
+ return (schema && this._space.db.query(Filter.schema(schema)).objects) ?? [];
70
69
  });
71
70
 
72
71
  // TODO(burdon): Map initially are objects that have not been added to the space.
73
72
  // Merge existing schema in space with defaults.
74
- const { objects } = this._space.db.query(Schema.filter());
75
- Object.keys(schemaMap).forEach((type) => {
76
- const schema = objects.find((object) => object.typename === type);
77
- if (schema) {
78
- this.setSchema(type as T, schema);
73
+ const schemas = this._space.db.schemaRegistry.getAll();
74
+ Object.entries<DynamicEchoSchema>(schemaMap).forEach(([type, dynamicSchema]) => {
75
+ let schema = schemas.find((object) => object.typename === type);
76
+ if (schema == null) {
77
+ schema = this._space.db.schemaRegistry.add(dynamicSchema.schema);
79
78
  }
79
+ this.setSchema(type as T, schema);
80
80
  });
81
81
  }
82
82
 
83
83
  addSchemas() {
84
- const { objects } = this._space.db.query(Schema.filter());
84
+ const result: DynamicEchoSchema[] = [];
85
+ const dbSchemas = this._space.db.schemaRegistry.getAll();
85
86
  this.schemas.forEach((schema) => {
86
- if (!objects.find((object) => object.typename === schema.typename)) {
87
- this._space.db.add(schema);
87
+ const existing = dbSchemas.find((object) => object.typename === schema.typename);
88
+ if (existing == null) {
89
+ result.push(this._space.db.schemaRegistry.add(schema.schema));
90
+ } else {
91
+ result.push(existing);
88
92
  }
89
93
  });
90
94
 
91
- return this.schemas;
95
+ return result;
92
96
  }
93
97
 
94
- override createObject({ types }: { types?: T[] } = {}): Expando {
98
+ override createObject({ types }: { types?: T[] } = {}): ReactiveObject<any> {
95
99
  return this._space.db.add(super.createObject({ types }));
96
100
  }
97
101
  }
package/src/types.ts CHANGED
@@ -2,12 +2,12 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Schema, type Expando } from '@dxos/client/echo';
5
+ import { type DynamicEchoSchema, type ReactiveObject } from '@dxos/echo-schema';
6
6
 
7
7
  export type TestObject = { id: string } & Record<string, any>;
8
8
 
9
- export type TestSchemaMap<T extends string> = Record<T, Schema>;
9
+ export type TestSchemaMap<T extends string> = Record<T, DynamicEchoSchema>;
10
10
 
11
11
  export type TestGeneratorMap<T extends string> = Record<T, (provider: TestObjectProvider<T> | undefined) => any>;
12
12
 
13
- export type TestObjectProvider<T extends string> = (type: T) => Expando[];
13
+ export type TestObjectProvider<T extends string> = (type: T) => ReactiveObject<any>[];