jazz-tools 0.14.19 → 0.14.21

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.
Files changed (60) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/CHANGELOG.md +19 -0
  3. package/dist/{chunk-AA3SCYKI.js → chunk-DAOCWHIV.js} +248 -125
  4. package/dist/chunk-DAOCWHIV.js.map +1 -0
  5. package/dist/coValues/CoValueBase.d.ts +14 -3
  6. package/dist/coValues/CoValueBase.d.ts.map +1 -1
  7. package/dist/coValues/coMap.d.ts +27 -1
  8. package/dist/coValues/coMap.d.ts.map +1 -1
  9. package/dist/coValues/group.d.ts +8 -0
  10. package/dist/coValues/group.d.ts.map +1 -1
  11. package/dist/coValues/interfaces.d.ts +5 -0
  12. package/dist/coValues/interfaces.d.ts.map +1 -1
  13. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  14. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  15. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  16. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +1 -0
  17. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  18. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +1 -0
  19. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  20. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +2 -0
  21. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  22. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -1
  23. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  24. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +1 -0
  25. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  26. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +1 -0
  27. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  28. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +1 -0
  29. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  30. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -1
  31. package/dist/implementation/zodSchema/zodSchema.d.ts +1 -1
  32. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  33. package/dist/index.js +1 -1
  34. package/dist/lib/migration.d.ts +3 -0
  35. package/dist/lib/migration.d.ts.map +1 -0
  36. package/dist/subscribe/SubscriptionScope.d.ts +3 -1
  37. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
  38. package/dist/testing.js +1 -1
  39. package/package.json +2 -2
  40. package/src/coValues/CoValueBase.ts +31 -7
  41. package/src/coValues/coMap.ts +14 -1
  42. package/src/coValues/group.ts +12 -0
  43. package/src/coValues/interfaces.ts +5 -0
  44. package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +12 -15
  45. package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +3 -1
  46. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -0
  47. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +2 -0
  48. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +11 -0
  49. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +2 -1
  50. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +1 -0
  51. package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +1 -0
  52. package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +1 -0
  53. package/src/implementation/zodSchema/zodCo.ts +214 -246
  54. package/src/implementation/zodSchema/zodSchema.ts +1 -1
  55. package/src/lib/migration.ts +23 -0
  56. package/src/subscribe/SubscriptionScope.ts +48 -3
  57. package/src/tests/coMap.test.ts +280 -1
  58. package/src/tests/groupsAndAccounts.test.ts +12 -0
  59. package/src/tests/schemaUnion.test.ts +5 -5
  60. package/dist/chunk-AA3SCYKI.js.map +0 -1
@@ -1,4 +1,5 @@
1
1
  import { ControlledAccount, RawAccount, type RawCoValue } from "cojson";
2
+ import { z } from "../implementation/zodSchema/zodReExport.js";
2
3
  import {
3
4
  AnonymousJazzAgent,
4
5
  CoValue,
@@ -11,7 +12,12 @@ import {
11
12
  coValuesCache,
12
13
  inspect,
13
14
  } from "../internal.js";
14
- import type { Account, Group } from "../internal.js";
15
+ import type {
16
+ Account,
17
+ CoValueClassFromZodSchema,
18
+ Group,
19
+ InstanceOfSchemaCoValuesNullable,
20
+ } from "../internal.js";
15
21
 
16
22
  /** @internal */
17
23
 
@@ -61,7 +67,6 @@ export class CoValueBase implements CoValue {
61
67
  static fromRaw<V extends CoValue>(this: CoValueClass<V>, raw: RawCoValue): V {
62
68
  return new this({ fromRaw: raw });
63
69
  }
64
-
65
70
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
71
  toJSON(): object | any[] | string {
67
72
  return {
@@ -76,13 +81,32 @@ export class CoValueBase implements CoValue {
76
81
  }
77
82
 
78
83
  /** @category Type Helpers */
79
- castAs<Cl extends CoValueClass & CoValueFromRaw<CoValue>>(
80
- cl: Cl,
81
- ): InstanceType<Cl> {
84
+ castAs<
85
+ S extends
86
+ | CoValueClass
87
+ | z.core.$ZodType
88
+ | (z.core.$ZodObject<any, any> & {
89
+ builtin: "Account";
90
+ migration?: (account: any, creationProps?: { name: string }) => void;
91
+ })
92
+ | (z.core.$ZodCustom<any, any> & { builtin: "FileStream" })
93
+ | (z.core.$ZodCustom<any, any> & {
94
+ builtin: "CoFeed";
95
+ element: z.core.$ZodType;
96
+ }),
97
+ >(
98
+ schema: S,
99
+ ): S extends CoValueClass
100
+ ? InstanceType<S>
101
+ : S extends z.core.$ZodType
102
+ ? NonNullable<InstanceOfSchemaCoValuesNullable<S>>
103
+ : never {
104
+ const cl = "getCoSchema" in schema ? (schema as any).getCoSchema() : schema;
105
+
82
106
  if (this.constructor === cl) {
83
- return this as InstanceType<Cl>;
107
+ return this as any;
84
108
  }
85
109
 
86
- return cl.fromRaw(this._raw) as InstanceType<Cl>;
110
+ return (cl as unknown as CoValueFromRaw<CoValue>).fromRaw(this._raw) as any;
87
111
  }
88
112
  }
@@ -35,7 +35,6 @@ import {
35
35
  accessChildById,
36
36
  accessChildByKey,
37
37
  activeAccountContext,
38
- anySchemaToCoSchema,
39
38
  ensureCoValueLoaded,
40
39
  inspect,
41
40
  isRefEncoded,
@@ -119,6 +118,20 @@ export class CoMap extends CoValueBase implements CoValue {
119
118
  } & { [ItemsSym]?: Schema };
120
119
  }
121
120
 
121
+ /**
122
+ * The timestamp of the creation time of the CoMap
123
+ */
124
+ get _createdAt() {
125
+ return this._raw.earliestTxMadeAt ?? Number.MAX_SAFE_INTEGER;
126
+ }
127
+
128
+ /**
129
+ * The timestamp of the last updated time of the CoMap
130
+ */
131
+ get _lastUpdatedAt() {
132
+ return this._raw.latestTxMadeAt;
133
+ }
134
+
122
135
  /**
123
136
  * If property `prop` is a `coField.ref(...)`, you can use `coMaps._refs.prop` to access
124
137
  * the `Ref` instead of the potentially loaded/null value.
@@ -215,6 +215,18 @@ export class Group extends CoValueBase implements CoValue {
215
215
  );
216
216
  }
217
217
 
218
+ /**
219
+ * Make the group public, so that everyone can read it.
220
+ * Alias for `addMember("everyone", role)`.
221
+ *
222
+ * @param role - Optional: the role to grant to everyone. Defaults to "reader".
223
+ * @returns The group itself.
224
+ */
225
+ makePublic(role: "reader" | "writer" = "reader") {
226
+ this.addMember("everyone", role);
227
+ return this;
228
+ }
229
+
218
230
  getParentGroups(): Array<Group> {
219
231
  return this._raw.getParentGroups().map((group) => Group.fromRaw(group));
220
232
  }
@@ -294,6 +294,11 @@ export function subscribeToCoValue<
294
294
  return unsubscribe;
295
295
  }
296
296
 
297
+ /**
298
+ * @deprecated Used for the React integration in the past, but we moved to use SubscriptionScope directly.
299
+ *
300
+ * Going to be removed in the next minor version.
301
+ */
297
302
  export function createCoValueObservable<
298
303
  S extends CoValueOrZodSchema,
299
304
  const R extends ResolveQuery<S>,
@@ -61,19 +61,6 @@ export function tryZodSchemaToCoSchema<S extends z.core.$ZodType>(
61
61
  }
62
62
  };
63
63
 
64
- if ("migration" in schema) {
65
- const migration = schema.migration;
66
- if (typeof migration !== "function") {
67
- throw new Error("migration must be a function");
68
- }
69
- (coSchema.prototype as Account).migrate = async function (
70
- this,
71
- creationProps,
72
- ) {
73
- await migration(this, creationProps);
74
- };
75
- }
76
-
77
64
  coSchemasForZodSchemas.set(schema, coSchema as unknown as CoValueClass);
78
65
  return coSchema as unknown as CoValueClassFromZodSchema<S>;
79
66
  } else if (isZodArray(schema)) {
@@ -175,9 +162,19 @@ export function anySchemaToCoSchema<
175
162
  : never {
176
163
  if (isCoValueClass(schema)) {
177
164
  return schema as any;
178
- } else {
179
- return zodSchemaToCoSchema(schema as any) as any;
165
+ } else if ("getCoSchema" in schema) {
166
+ return (schema as any).getCoSchema() as any;
167
+ } else if ("def" in schema) {
168
+ const coSchema = tryZodSchemaToCoSchema(schema as z.core.$ZodType);
169
+ if (!coSchema) {
170
+ throw new Error(
171
+ `Unsupported zod type: ${(schema.def as any)?.type || JSON.stringify(schema)}`,
172
+ );
173
+ }
174
+ return coSchema as any;
180
175
  }
176
+
177
+ throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`);
181
178
  }
182
179
 
183
180
  export function zodSchemaToCoSchemaOrKeepPrimitive<S extends z.core.$ZodType>(
@@ -33,7 +33,7 @@ export type AccountSchema<
33
33
  }>;
34
34
  root: CoMapSchema<{}>;
35
35
  },
36
- > = Omit<CoMapSchema<Shape>, "create" | "load"> & {
36
+ > = Omit<CoMapSchema<Shape>, "create" | "load" | "withMigration"> & {
37
37
  builtin: "Account";
38
38
 
39
39
  create: (options: {
@@ -64,6 +64,8 @@ export type AccountSchema<
64
64
  creationProps?: { name: string },
65
65
  ) => void,
66
66
  ): AccountSchema<Shape>;
67
+
68
+ getCoSchema: () => typeof Account;
67
69
  };
68
70
 
69
71
  export type DefaultProfileShape = {
@@ -57,6 +57,8 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
57
57
  unsubscribe: () => void,
58
58
  ) => void,
59
59
  ): () => void;
60
+
61
+ getCoSchema: () => typeof CoFeed;
60
62
  };
61
63
 
62
64
  // less precise verion to avoid circularity issues and allow matching against
@@ -51,6 +51,8 @@ export type CoListSchema<T extends z.core.$ZodType> = z.core.$ZodArray<T> & {
51
51
  this: S,
52
52
  helpers: (Self: S) => T,
53
53
  ): WithHelpers<S, T>;
54
+
55
+ getCoSchema: () => typeof CoList;
54
56
  };
55
57
 
56
58
  // less precise verion to avoid circularity issues and allow matching against
@@ -97,6 +97,17 @@ export type CoMapSchema<
97
97
  this: S,
98
98
  helpers: (Self: S) => T,
99
99
  ): WithHelpers<S, T>;
100
+
101
+ withMigration(
102
+ migration: (
103
+ value: Resolved<
104
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
105
+ true
106
+ >,
107
+ ) => undefined,
108
+ ): CoMapSchema<Shape, Config, Owner>;
109
+
110
+ getCoSchema: () => typeof CoMap;
100
111
  };
101
112
 
102
113
  export type optionalKeys<Shape extends z.core.$ZodLooseShape> = {
@@ -1,7 +1,7 @@
1
1
  import { CoValueUniqueness } from "cojson";
2
2
  import {
3
3
  Account,
4
- CoMap,
4
+ type CoMap,
5
5
  Group,
6
6
  ID,
7
7
  RefsToResolve,
@@ -83,6 +83,7 @@ export type CoRecordSchema<
83
83
  this: S,
84
84
  helpers: (Self: S) => T,
85
85
  ): WithHelpers<S, T>;
86
+ getCoSchema: () => typeof CoMap;
86
87
  };
87
88
 
88
89
  // less precise verion to avoid circularity issues and allow matching against
@@ -40,4 +40,5 @@ export type FileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
40
40
  id: string,
41
41
  listener: (value: FileStream, unsubscribe: () => void) => void,
42
42
  ): () => void;
43
+ getCoSchema: () => typeof FileStream;
43
44
  };
@@ -24,4 +24,5 @@ export type PlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
24
24
  listener: (value: CoPlainText, unsubscribe: () => void) => void,
25
25
  ): () => void;
26
26
  fromRaw(raw: RawCoPlainText): CoPlainText;
27
+ getCoSchema: () => typeof CoPlainText;
27
28
  };
@@ -22,4 +22,5 @@ export type RichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
22
22
  id: string,
23
23
  listener: (value: CoRichText, unsubscribe: () => void) => void,
24
24
  ): () => void;
25
+ getCoSchema: () => typeof CoRichText;
25
26
  };