@dxos/echo-generator 0.4.10-main.a1b90a1 → 0.4.10-main.b39148d

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/data.ts CHANGED
@@ -5,11 +5,7 @@
5
5
  // TODO(burdon): Reconcile with @dxos/plugin-debug, @dxos/aurora/testing.
6
6
  // TODO(burdon): Bug when adding stale objects to space (e.g., static objects already added in previous story invocation).
7
7
 
8
- import * as S from '@effect/schema/Schema';
9
-
10
- import { type Space } from '@dxos/client/echo';
11
- import * as E from '@dxos/echo-schema';
12
- import { DynamicEchoSchema, effectToJsonSchema, StoredEchoSchema } from '@dxos/echo-schema';
8
+ import { Schema, type Space, TextObject } from '@dxos/client/echo';
13
9
  import { faker } from '@dxos/random';
14
10
 
15
11
  import { SpaceObjectGenerator, TestObjectGenerator } from './generator';
@@ -26,46 +22,111 @@ export enum TestSchemaType {
26
22
  project = 'example.com/schema/project',
27
23
  }
28
24
 
29
- const createDynamicSchema = (typename: string, fields: S.Struct.Fields): DynamicEchoSchema => {
30
- const typeSchema = S.partial(S.struct(fields)).pipe(E.echoObject(typename, '1.0.0'));
31
- return new DynamicEchoSchema(
32
- E.object(StoredEchoSchema, {
33
- typename,
34
- version: '1.0.0',
35
- jsonSchema: effectToJsonSchema(typeSchema),
36
- }),
37
- );
38
- };
39
-
40
- const testSchemas = (): TestSchemaMap<TestSchemaType> => {
41
- const document = createDynamicSchema(TestSchemaType.document, {
42
- title: S.string.pipe(S.description('title of the document')),
43
- content: S.string,
25
+ export const testSchemas = (): TestSchemaMap<TestSchemaType> => {
26
+ const document = new Schema({
27
+ typename: TestSchemaType.document,
28
+ props: [
29
+ {
30
+ id: 'title',
31
+ type: Schema.PropType.STRING,
32
+ description: 'title of the document',
33
+ },
34
+ {
35
+ id: 'content',
36
+ type: Schema.PropType.STRING, // TODO(burdon): Rich text.
37
+ },
38
+ ],
44
39
  });
45
40
 
46
- const organization = createDynamicSchema(TestSchemaType.organization, {
47
- name: S.string.pipe(S.description('name of the company or organization')),
48
- website: S.string.pipe(S.description('public website URL')),
49
- description: S.string.pipe(S.description('short summary of the company')),
41
+ const organization = new Schema({
42
+ typename: TestSchemaType.organization,
43
+ props: [
44
+ {
45
+ id: 'name',
46
+ type: Schema.PropType.STRING,
47
+ description: 'name of the company or organization',
48
+ },
49
+ {
50
+ id: 'website',
51
+ type: Schema.PropType.STRING,
52
+ description: 'public website URL',
53
+ },
54
+ {
55
+ id: 'description',
56
+ type: Schema.PropType.STRING,
57
+ description: 'short summary of the company',
58
+ },
59
+ ],
50
60
  });
51
61
 
52
- const contact = createDynamicSchema(TestSchemaType.contact, {
53
- name: S.string.pipe(S.description('name of the person')),
54
- email: S.string,
55
- org: E.ref(organization),
56
- lat: S.number,
57
- lng: S.number,
62
+ const contact = new Schema({
63
+ typename: TestSchemaType.contact,
64
+ props: [
65
+ {
66
+ id: 'name',
67
+ type: Schema.PropType.STRING,
68
+ description: 'name of the person',
69
+ },
70
+ // TODO(burdon): Support multiple tagged properties.
71
+ {
72
+ id: 'email',
73
+ type: Schema.PropType.STRING,
74
+ },
75
+ {
76
+ id: 'org',
77
+ type: Schema.PropType.REF,
78
+ ref: organization,
79
+ },
80
+ // TODO(burdon): Convert to object with nested props.
81
+ {
82
+ id: 'lat',
83
+ type: Schema.PropType.NUMBER,
84
+ },
85
+ {
86
+ id: 'lng',
87
+ type: Schema.PropType.NUMBER,
88
+ },
89
+ ],
58
90
  });
59
91
 
60
- const project = createDynamicSchema(TestSchemaType.project, {
61
- name: S.string.pipe(S.description('name of the project')),
62
- description: S.string,
63
- website: S.string,
64
- repo: S.string,
65
- status: S.string,
66
- priority: S.number,
67
- active: S.boolean,
68
- org: E.ref(organization),
92
+ const project = new Schema({
93
+ typename: TestSchemaType.project,
94
+ props: [
95
+ {
96
+ id: 'name',
97
+ type: Schema.PropType.STRING,
98
+ description: 'name of the project',
99
+ },
100
+ {
101
+ id: 'description',
102
+ type: Schema.PropType.STRING, // TODO(burdon): Text.
103
+ },
104
+ {
105
+ id: 'website',
106
+ type: Schema.PropType.STRING,
107
+ },
108
+ {
109
+ id: 'repo',
110
+ type: Schema.PropType.STRING,
111
+ },
112
+ {
113
+ id: 'status',
114
+ type: Schema.PropType.STRING,
115
+ },
116
+ {
117
+ id: 'priority',
118
+ type: Schema.PropType.NUMBER,
119
+ },
120
+ {
121
+ id: 'active',
122
+ type: Schema.PropType.BOOLEAN,
123
+ },
124
+ {
125
+ id: 'org',
126
+ type: Schema.PropType.REF,
127
+ ref: organization,
128
+ },
129
+ ],
69
130
  });
70
131
 
71
132
  return {
@@ -76,7 +137,7 @@ const testSchemas = (): TestSchemaMap<TestSchemaType> => {
76
137
  };
77
138
  };
78
139
 
79
- const testObjectGenerators: TestGeneratorMap<TestSchemaType> = {
140
+ export const testObjectGenerators: TestGeneratorMap<TestSchemaType> = {
80
141
  [TestSchemaType.document]: () => ({
81
142
  title: faker.lorem.sentence(3),
82
143
  content: faker.lorem.sentences({ min: 1, max: faker.number.int({ min: 1, max: 3 }) }),
@@ -85,7 +146,7 @@ const testObjectGenerators: TestGeneratorMap<TestSchemaType> = {
85
146
  [TestSchemaType.organization]: () => ({
86
147
  name: faker.company.name(),
87
148
  website: faker.datatype.boolean({ probability: 0.3 }) ? faker.internet.url() : undefined,
88
- description: faker.lorem.sentences(),
149
+ description: new TextObject(faker.lorem.sentences()),
89
150
  }),
90
151
 
91
152
  [TestSchemaType.contact]: (provider) => {
@@ -4,11 +4,10 @@
4
4
 
5
5
  import { expect } from 'chai';
6
6
 
7
- import { Client, Config } from '@dxos/client';
7
+ import { Client } from '@dxos/client';
8
8
  import { debug } from '@dxos/client/echo';
9
- import * as E from '@dxos/echo-schema';
10
9
  import { faker } from '@dxos/random';
11
- import { afterTest, describe, test } from '@dxos/test';
10
+ import { describe, test } from '@dxos/test';
12
11
 
13
12
  import { createSpaceObjectGenerator, createTestObjectGenerator, TestSchemaType } from './data';
14
13
 
@@ -24,22 +23,30 @@ describe('TestObjectGenerator', () => {
24
23
  });
25
24
 
26
25
  test('with space', async () => {
27
- const { space } = await setupTest();
26
+ const client = new Client();
27
+ await client.initialize();
28
+ await client.halo.createIdentity();
29
+ const space = await client.spaces.create();
28
30
 
29
31
  const generator = createSpaceObjectGenerator(space);
30
32
  generator.addSchemas();
31
33
 
32
34
  // Create org object.
33
35
  const organization = generator.createObject({ types: [TestSchemaType.organization] });
34
- expect(E.typeOf(organization)).to.exist;
36
+ expect(organization.__schema).to.exist;
35
37
 
36
38
  // Expect at least one person object with a linked org reference.
37
39
  const objects = generator.createObjects({ [TestSchemaType.contact]: 10 });
38
40
  expect(objects.some((object) => object.org === organization)).to.be.true;
41
+
42
+ await client.destroy();
39
43
  });
40
44
 
41
- test('idempotence', async () => {
42
- const { space } = await setupTest();
45
+ test.only('idempotence', async () => {
46
+ const client = new Client();
47
+ await client.initialize();
48
+ await client.halo.createIdentity();
49
+ const space = await client.spaces.create();
43
50
 
44
51
  const schemaId: string[] = [];
45
52
 
@@ -47,30 +54,22 @@ describe('TestObjectGenerator', () => {
47
54
  const generator = createSpaceObjectGenerator(space);
48
55
  generator.addSchemas();
49
56
  const organization = generator.createObject({ types: [TestSchemaType.organization] });
50
- schemaId.push(E.typeOf(organization)!.itemId);
57
+ schemaId.push(organization.__schema!.id);
51
58
  }
52
59
 
53
60
  {
54
61
  const generator = createSpaceObjectGenerator(space);
55
62
  generator.addSchemas();
56
63
  const organization = generator.createObject({ types: [TestSchemaType.organization] });
57
- schemaId.push(E.typeOf(organization)!.itemId);
64
+ schemaId.push(organization.__schema!.id);
58
65
  }
59
66
 
60
- expect(schemaId[0]).not.to.be.undefined;
61
67
  expect(schemaId[0]).to.eq(schemaId[1]);
62
68
 
63
69
  {
64
70
  console.log(space.db.objects.map((object) => object[debug]));
65
71
  }
66
- });
67
72
 
68
- const setupTest = async () => {
69
- const client = new Client({ config: new Config({ runtime: { client: { useReactiveObjectApi: true } } }) });
70
- await client.initialize();
71
- afterTest(async () => await client.destroy());
72
- await client.halo.createIdentity();
73
- const space = await client.spaces.create();
74
- return { client, space };
75
- };
73
+ await client.destroy();
74
+ });
76
75
  });
package/src/generator.ts CHANGED
@@ -2,9 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Space } from '@dxos/client/echo';
6
- import { type DynamicEchoSchema, Filter, type ReactiveObject } from '@dxos/echo-schema';
7
- import * as E from '@dxos/echo-schema';
5
+ import { Expando, type TypedObject, Schema, type Space } from '@dxos/client/echo';
8
6
  import { faker } from '@dxos/random';
9
7
 
10
8
  import { type TestSchemaType } from './data';
@@ -22,30 +20,30 @@ export class TestObjectGenerator<T extends string = TestSchemaType> {
22
20
  private readonly _provider?: TestObjectProvider<T>
23
21
  ) {}
24
22
 
25
- get schemas(): DynamicEchoSchema[] {
23
+ get schemas(): Schema[] {
26
24
  return Object.values(this._schemas);
27
25
  }
28
26
 
29
- getSchema(type: T): DynamicEchoSchema | undefined {
27
+ getSchema(type: T) {
30
28
  return this.schemas.find((schema) => schema.typename === type);
31
29
  }
32
30
 
33
- protected setSchema(type: T, schema: DynamicEchoSchema) {
31
+ protected setSchema(type: T, schema: Schema) {
34
32
  this._schemas[type] = schema;
35
33
  }
36
34
 
37
35
  // TODO(burdon): Runtime type check via: https://github.com/Effect-TS/schema (or zod).
38
- createObject({ types }: { types?: T[] } = {}): ReactiveObject<any> {
36
+ createObject({ types }: { types?: T[] } = {}): TypedObject {
39
37
  const type = faker.helpers.arrayElement(types ?? (Object.keys(this._schemas) as T[]));
40
38
  const data = this._generators[type](this._provider);
41
39
  const schema = this.getSchema(type);
42
- return schema ? E.object(schema, data) : E.object(data);
40
+ return new Expando(data, { schema });
43
41
  }
44
42
 
45
43
  // TODO(burdon): Create batch.
46
44
  // TODO(burdon): Based on dependencies (e.g., organization before contact).
47
- createObjects(map: Partial<Record<T, number>>): ReactiveObject<any>[] {
48
- const objects: ReactiveObject<any>[] = [];
45
+ createObjects(map: Partial<Record<T, number>>): Expando[] {
46
+ const objects: Expando[] = [];
49
47
  Object.entries<number>(map as any).forEach(([type, count]) => {
50
48
  range(() => objects.push(this.createObject({ types: [type as T] })), count);
51
49
  });
@@ -64,38 +62,36 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
64
62
  generators: TestGeneratorMap<T>,
65
63
  ) {
66
64
  super(schemaMap, generators, (type: T) => {
67
- const schema = this.getSchema(type);
68
- return (schema && this._space.db.query(Filter.schema(schema)).objects) ?? [];
65
+ const { objects } = this._space.db.query((object: TypedObject) => {
66
+ return object.__schema?.id === this.getSchema(type)?.id;
67
+ });
68
+
69
+ return objects;
69
70
  });
70
71
 
71
72
  // TODO(burdon): Map initially are objects that have not been added to the space.
72
73
  // Merge existing schema in space with defaults.
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);
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);
78
79
  }
79
- this.setSchema(type as T, schema);
80
80
  });
81
81
  }
82
82
 
83
83
  addSchemas() {
84
- const result: DynamicEchoSchema[] = [];
85
- const dbSchemas = this._space.db.schemaRegistry.getAll();
84
+ const { objects } = this._space.db.query(Schema.filter());
86
85
  this.schemas.forEach((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);
86
+ if (!objects.find((object) => object.typename === schema.typename)) {
87
+ this._space.db.add(schema);
92
88
  }
93
89
  });
94
90
 
95
- return result;
91
+ return this.schemas;
96
92
  }
97
93
 
98
- override createObject({ types }: { types?: T[] } = {}): ReactiveObject<any> {
94
+ override createObject({ types }: { types?: T[] } = {}): Expando {
99
95
  return this._space.db.add(super.createObject({ types }));
100
96
  }
101
97
  }
package/src/types.ts CHANGED
@@ -2,12 +2,12 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type DynamicEchoSchema, type ReactiveObject } from '@dxos/echo-schema';
5
+ import { type Schema, type Expando } from '@dxos/client/echo';
6
6
 
7
7
  export type TestObject = { id: string } & Record<string, any>;
8
8
 
9
- export type TestSchemaMap<T extends string> = Record<T, DynamicEchoSchema>;
9
+ export type TestSchemaMap<T extends string> = Record<T, Schema>;
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) => ReactiveObject<any>[];
13
+ export type TestObjectProvider<T extends string> = (type: T) => Expando[];