|
1 | 1 | import { |
2 | 2 | type Account, |
3 | | - AccountCreationProps, |
4 | | - AccountSchema, |
5 | | - AnyCoMapSchema, |
| 3 | + type AccountCreationProps, |
| 4 | + type AccountSchema, |
| 5 | + type AnyCoMapSchema, |
| 6 | + BaseAccountShape, |
6 | 7 | CoFeed, |
7 | | - CoFeedSchema, |
8 | | - CoListSchema, |
9 | | - CoMapSchema, |
| 8 | + type CoFeedSchema, |
| 9 | + type CoListSchema, |
| 10 | + type CoMapSchema, |
10 | 11 | CoPlainText, |
11 | | - CoProfileSchema, |
12 | | - CoRecordSchema, |
| 12 | + type CoProfileSchema, |
| 13 | + type CoRecordSchema, |
13 | 14 | CoRichText, |
14 | | - DefaultProfileShape, |
| 15 | + type DefaultProfileShape, |
15 | 16 | FileStream, |
16 | | - FileStreamSchema, |
| 17 | + type FileStreamSchema, |
17 | 18 | ImageDefinition, |
18 | | - PlainTextSchema, |
19 | | - Simplify, |
| 19 | + type PlainTextSchema, |
| 20 | + type Simplify, |
20 | 21 | zodSchemaToCoSchema, |
21 | 22 | } from "../../internal.js"; |
22 | 23 | import { RichTextSchema } from "./schemaTypes/RichTextSchema.js"; |
@@ -82,16 +83,9 @@ export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>( |
82 | 83 | return enrichCoMapSchema(objectSchema); |
83 | 84 | }; |
84 | 85 |
|
85 | | -function enrichAccountSchema< |
86 | | - Shape extends { |
87 | | - profile: AnyCoMapSchema<{ |
88 | | - name: z.core.$ZodString<string>; |
89 | | - inbox?: z.core.$ZodOptional<z.core.$ZodString>; |
90 | | - inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>; |
91 | | - }>; |
92 | | - root: AnyCoMapSchema; |
93 | | - }, |
94 | | ->(schema: z.ZodObject<Shape, z.core.$strip>) { |
| 86 | +function enrichAccountSchema<Shape extends BaseAccountShape>( |
| 87 | + schema: z.ZodObject<Shape, z.core.$strip>, |
| 88 | +) { |
95 | 89 | const enrichedSchema = Object.assign(schema, { |
96 | 90 | collaborative: true, |
97 | 91 | builtin: "Account", |
@@ -142,16 +136,44 @@ function enrichAccountSchema< |
142 | 136 | return enrichedSchema; |
143 | 137 | } |
144 | 138 |
|
145 | | -export const coAccountDefiner = < |
146 | | - Shape extends { |
147 | | - profile: AnyCoMapSchema<{ |
148 | | - name: z.core.$ZodString<string>; |
149 | | - inbox?: z.core.$ZodOptional<z.core.$ZodString>; |
150 | | - inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>; |
151 | | - }>; |
152 | | - root: AnyCoMapSchema; |
153 | | - }, |
154 | | ->( |
| 139 | +/** |
| 140 | + * Defines a collaborative account schema for Jazz applications. |
| 141 | + * |
| 142 | + * Creates an account schema that represents a user account with profile and root data. |
| 143 | + * Accounts are the primary way to identify and manage users in Jazz applications. |
| 144 | + * |
| 145 | + * @template Shape - The shape of the account schema extending BaseAccountShape |
| 146 | + * @param shape - The account schema shape. Defaults to a basic profile with name, inbox, and inboxInvite fields, plus an empty root object. |
| 147 | + * |
| 148 | + * @example |
| 149 | + * ```typescript |
| 150 | + * // Basic account with default profile |
| 151 | + * const BasicAccount = co.account(); |
| 152 | + * |
| 153 | + * // Custom account with specific profile and root structure |
| 154 | + * const JazzAccount = co.account({ |
| 155 | + * profile: co.profile({ |
| 156 | + * name: z.string(), |
| 157 | + * avatar: z.optional(z.string()), |
| 158 | + * }), |
| 159 | + * root: co.map({ |
| 160 | + * organizations: co.list(Organization), |
| 161 | + * draftOrganization: DraftOrganization, |
| 162 | + * }), |
| 163 | + * }).withMigration(async (account) => { |
| 164 | + * // Migration logic for existing accounts |
| 165 | + * if (account.profile === undefined) { |
| 166 | + * const group = Group.create(); |
| 167 | + * account.profile = co.profile().create( |
| 168 | + * { name: getRandomUsername() }, |
| 169 | + * group |
| 170 | + * ); |
| 171 | + * group.addMember("everyone", "reader"); |
| 172 | + * } |
| 173 | + * }); |
| 174 | + * ``` |
| 175 | + */ |
| 176 | +export const coAccountDefiner = <Shape extends BaseAccountShape>( |
155 | 177 | shape: Shape = { |
156 | 178 | profile: coMapDefiner({ |
157 | 179 | name: z.string(), |
|
0 commit comments