@dxos/echo-generator 0.7.1 → 0.7.2-main.f1adc9f

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
@@ -3,18 +3,19 @@
3
3
  //
4
4
 
5
5
  import { type Space, Filter } from '@dxos/client/echo';
6
- import { type EchoReactiveObject } from '@dxos/echo-db';
6
+ import { type ReactiveEchoObject } from '@dxos/echo-db';
7
7
  import {
8
8
  create,
9
- MutableSchema,
10
- type ReactiveObject,
11
9
  getObjectAnnotation,
12
10
  getSchema,
13
11
  isReactiveObject,
12
+ MutableSchema,
13
+ type ReactiveObject,
14
14
  type S,
15
15
  } from '@dxos/echo-schema';
16
16
  import { invariant } from '@dxos/invariant';
17
17
  import { faker } from '@dxos/random';
18
+ import { range } from '@dxos/util';
18
19
 
19
20
  import { type TestSchemaType } from './data';
20
21
  import {
@@ -24,7 +25,6 @@ import {
24
25
  type TestObjectProvider,
25
26
  type TestSchemaMap,
26
27
  } from './types';
27
- import { range } from './util';
28
28
 
29
29
  /**
30
30
  * Typed object generator.
@@ -60,12 +60,11 @@ export class TestObjectGenerator<T extends string = TestSchemaType> {
60
60
  return schema ? create(schema, data) : create(data);
61
61
  }
62
62
 
63
- // TODO(burdon): Create batch.
64
63
  // TODO(burdon): Based on dependencies (e.g., organization before contact).
65
64
  async createObjects(map: Partial<Record<T, number>>) {
66
65
  const tasks = Object.entries<number>(map as any)
67
66
  .map(([type, count]) => {
68
- return range(() => this.createObject({ types: [type as T] }), count);
67
+ return range(count, () => this.createObject({ types: [type as T] }));
69
68
  })
70
69
  .flatMap((t) => t);
71
70
 
@@ -105,7 +104,7 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
105
104
  return result;
106
105
  }
107
106
 
108
- override async createObject({ types }: { types?: T[] } = {}): Promise<EchoReactiveObject<any>> {
107
+ override async createObject({ types }: { types?: T[] } = {}): Promise<ReactiveEchoObject<any>> {
109
108
  return this._space.db.add(await super.createObject({ types }));
110
109
  }
111
110
 
@@ -127,7 +126,7 @@ export class SpaceObjectGenerator<T extends string> extends TestObjectGenerator<
127
126
  }
128
127
  }
129
128
 
130
- async mutateObject(object: EchoReactiveObject<any>, params: MutationsProviderParams) {
129
+ async mutateObject(object: ReactiveEchoObject<any>, params: MutationsProviderParams) {
131
130
  invariant(this._mutations, 'Mutations not defined.');
132
131
  const type = getObjectAnnotation(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![type](object, params);
136
135
  }
137
136
 
138
- async mutateObjects(objects: EchoReactiveObject<any>[], params: MutationsProviderParams) {
137
+ async mutateObjects(objects: ReactiveEchoObject<any>[], params: MutationsProviderParams) {
139
138
  for (const object of objects) {
140
139
  await this.mutateObject(object, params);
141
140
  }
package/src/types.ts CHANGED
@@ -2,18 +2,22 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type EchoReactiveObject } from '@dxos/echo-db';
5
+ import { type ReactiveEchoObject } from '@dxos/echo-db';
6
6
  import { type MutableSchema, type ReactiveObject, type S } from '@dxos/echo-schema';
7
7
 
8
+ // TODO(burdon): Use echo-schema types.
8
9
  export type TestObject = { id: string } & Record<string, any>;
9
10
 
10
- export type TestSchemaMap<T extends string> = Record<T, MutableSchema | S.Schema<any>>;
11
+ export type TestSchemaMap<T extends string = string> = Record<T, MutableSchema | S.Schema<any>>;
11
12
 
12
- export type TestGeneratorMap<T extends string> = Record<T, (provider: TestObjectProvider<T> | undefined) => any>;
13
+ export type TestObjectProvider<T extends string = string> = (type: T) => Promise<ReactiveObject<any>[]>;
13
14
 
14
- export type TestObjectProvider<T extends string> = (type: T) => Promise<ReactiveObject<any>[]>;
15
+ export type TestGeneratorMap<T extends string = string> = Record<
16
+ T,
17
+ (provider: TestObjectProvider<T> | undefined) => any
18
+ >;
15
19
 
16
- export type TestMutationsMap<T extends string> = Record<T, TestObjectMutators>;
20
+ export type TestMutationsMap<T extends string = string> = Record<T, TestObjectMutators>;
17
21
 
18
22
  export type MutationsProviderParams = {
19
23
  count: number;
@@ -21,4 +25,4 @@ export type MutationsProviderParams = {
21
25
  maxContentLength: number;
22
26
  };
23
27
 
24
- export type TestObjectMutators = (object: EchoReactiveObject<any>, params: MutationsProviderParams) => Promise<void>;
28
+ export type TestObjectMutators = (object: ReactiveEchoObject<any>, params: MutationsProviderParams) => Promise<void>;
package/src/util.ts CHANGED
@@ -2,12 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- // TODO(burdon): Util.
6
- export const range = <T>(fn: (i: number) => T | undefined, length: number): T[] =>
7
- Array.from({ length })
8
- .map((_, i) => fn(i))
9
- .filter(Boolean) as T[];
10
-
11
5
  export const randomText = (length: number) => {
12
6
  let result = '';
13
7
  const characters = 'abcdefghijklmnopqrstuvwxyz';
@@ -15,5 +9,6 @@ export const randomText = (length: number) => {
15
9
  for (let index = 0; index < length; index++) {
16
10
  result += characters.charAt(Math.floor(Math.random() * charactersLength));
17
11
  }
12
+
18
13
  return result;
19
14
  };