@fragno-dev/create 0.1.3 → 0.1.5
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/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +13 -0
- package/dist/index.js +12 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/integration.test.ts +19 -7
- package/src/package-json.ts +8 -12
- package/templates/fragment/src/client/react.ts +1 -1
- package/templates/fragment/src/client/solid.ts +1 -1
- package/templates/fragment/src/client/svelte.ts +1 -1
- package/templates/fragment/src/client/vanilla.ts +1 -1
- package/templates/fragment/src/client/vue.ts +1 -1
- package/templates/fragment/src/index.ts +23 -28
- package/templates/optional/builder/rollup.config.js +1 -0
- package/templates/optional/builder/vite.config.ts +3 -0
- package/templates/optional/database/index.ts +220 -74
- package/templates/optional/database/schema.ts +25 -12
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -1,14 +1,10 @@
|
|
|
1
|
+
import { defineFragment, defineRoutes, instantiate } from "@fragno-dev/core";
|
|
2
|
+
import { createClientBuilder, type FragnoPublicClientConfig } from "@fragno-dev/core/client";
|
|
1
3
|
import {
|
|
2
|
-
|
|
3
|
-
defineRoutes,
|
|
4
|
-
createFragment,
|
|
5
|
-
type FragnoPublicClientConfig,
|
|
6
|
-
} from "@fragno-dev/core";
|
|
7
|
-
import { createClientBuilder } from "@fragno-dev/core/client";
|
|
8
|
-
import {
|
|
9
|
-
defineFragmentWithDatabase,
|
|
4
|
+
withDatabase,
|
|
10
5
|
type FragnoPublicConfigWithDatabase,
|
|
11
|
-
|
|
6
|
+
ExponentialBackoffRetryPolicy,
|
|
7
|
+
} from "@fragno-dev/db";
|
|
12
8
|
import type { TableToInsertValues } from "@fragno-dev/db/query";
|
|
13
9
|
import { noteSchema } from "./schema";
|
|
14
10
|
|
|
@@ -19,35 +15,125 @@ import { z } from "zod";
|
|
|
19
15
|
|
|
20
16
|
export interface ExampleConfig {
|
|
21
17
|
// Add any server-side configuration here if needed
|
|
18
|
+
onNoteCreated?: (nonce: string, payload: { noteId: string; userId: string }) => Promise<void>;
|
|
22
19
|
}
|
|
23
20
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
21
|
+
const exampleFragmentDefinition = defineFragment<ExampleConfig>("example-fragment")
|
|
22
|
+
.extend(withDatabase(noteSchema))
|
|
23
|
+
.provideHooks(({ defineHook, config }) => ({
|
|
24
|
+
onNoteCreated: defineHook(async function (payload: { noteId: string; userId: string }) {
|
|
25
|
+
// Hook runs after transaction commits, with retries on failure
|
|
26
|
+
// Use this.nonce for idempotency (available via this context)
|
|
27
|
+
await config.onNoteCreated?.(this.nonce, payload);
|
|
28
|
+
}),
|
|
29
|
+
}))
|
|
30
|
+
.providesBaseService(({ defineService }) => {
|
|
31
|
+
return defineService({
|
|
32
|
+
createNote: async function (
|
|
33
|
+
note: Omit<TableToInsertValues<typeof noteSchema.tables.note>, "userId"> & {
|
|
34
|
+
userId: string;
|
|
35
|
+
},
|
|
36
|
+
) {
|
|
37
|
+
const uow = this.uow(noteSchema);
|
|
38
|
+
|
|
39
|
+
// Find user first to get FragnoId for reference
|
|
40
|
+
const userUow = uow.findFirst("user", (b) =>
|
|
41
|
+
b.whereIndex("idx_user_email", (eb) => eb("email", "=", note.userId)),
|
|
42
|
+
);
|
|
43
|
+
const [user] = await userUow.retrievalPhase;
|
|
44
|
+
|
|
45
|
+
if (!user) {
|
|
46
|
+
throw new Error("User not found");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Create note with reference to user
|
|
50
|
+
const noteId = uow.create("note", {
|
|
51
|
+
content: note.content,
|
|
52
|
+
userId: user.id,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Trigger durable hook (recorded in transaction, executed after commit)
|
|
56
|
+
uow.triggerHook("onNoteCreated", {
|
|
57
|
+
noteId: noteId.valueOf(),
|
|
58
|
+
userId: user.id.valueOf(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Wait for handler to execute mutation phase
|
|
62
|
+
await uow.mutationPhase;
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
id: noteId.valueOf(),
|
|
66
|
+
content: note.content,
|
|
67
|
+
userId: user.id.valueOf(),
|
|
68
|
+
createdAt: new Date(),
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
getNotes: async function () {
|
|
72
|
+
const uow = this.uow(noteSchema).find("note", (b) =>
|
|
73
|
+
b.whereIndex("primary").join((j) => j.author()),
|
|
74
|
+
);
|
|
75
|
+
const [notes] = await uow.retrievalPhase;
|
|
76
|
+
return notes;
|
|
77
|
+
},
|
|
78
|
+
getNotesByUser: async function (userEmail: string) {
|
|
79
|
+
const uow = this.uow(noteSchema);
|
|
80
|
+
|
|
81
|
+
// First find the user by email
|
|
82
|
+
const userUow = uow.findFirst("user", (b) =>
|
|
83
|
+
b.whereIndex("idx_user_email", (eb) => eb("email", "=", userEmail)),
|
|
84
|
+
);
|
|
85
|
+
const [user] = await userUow.retrievalPhase;
|
|
86
|
+
|
|
87
|
+
if (!user) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Then find notes by the user's FragnoId
|
|
92
|
+
// Note: userId is a reference column, FragnoId works directly in whereIndex
|
|
93
|
+
const notesUow = uow.find("note", (b) =>
|
|
94
|
+
b.whereIndex("idx_note_user", (eb) => eb("userId", "=", user.id)).join((j) => j.author()),
|
|
95
|
+
);
|
|
96
|
+
const [notes] = await notesUow.retrievalPhase;
|
|
97
|
+
return notes;
|
|
98
|
+
},
|
|
99
|
+
updateNote: async function (noteId: string, content: string) {
|
|
100
|
+
const uow = this.uow(noteSchema);
|
|
101
|
+
|
|
102
|
+
// Find note first to get FragnoId for optimistic concurrency control
|
|
103
|
+
// Join with author to get user information
|
|
104
|
+
const noteUow = uow.findFirst("note", (b) =>
|
|
105
|
+
b.whereIndex("primary", (eb) => eb("id", "=", noteId)).join((j) => j.author()),
|
|
106
|
+
);
|
|
107
|
+
const [note] = await noteUow.retrievalPhase;
|
|
108
|
+
|
|
109
|
+
if (!note) {
|
|
110
|
+
throw new Error("Note not found");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Update with optimistic concurrency control (.check())
|
|
114
|
+
uow.update("note", note.id, (b) => b.set({ content }).check());
|
|
115
|
+
|
|
116
|
+
// Wait for handler to execute mutation phase
|
|
117
|
+
// On optimistic conflict, handler will retry the whole transaction
|
|
118
|
+
await uow.mutationPhase;
|
|
119
|
+
|
|
120
|
+
if (!note.author) {
|
|
121
|
+
throw new Error("Note author not found");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
id: note.id.valueOf(),
|
|
126
|
+
content,
|
|
127
|
+
userId: note.author.id.valueOf(),
|
|
128
|
+
createdAt: note.createdAt,
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
})
|
|
133
|
+
.build();
|
|
134
|
+
|
|
135
|
+
const exampleRoutesFactory = defineRoutes(exampleFragmentDefinition).create(
|
|
136
|
+
({ services, defineRoute }) => {
|
|
51
137
|
return [
|
|
52
138
|
defineRoute({
|
|
53
139
|
method: "GET",
|
|
@@ -58,26 +144,84 @@ const exampleRoutesFactory = defineRoutes<ExampleConfig, {}, ExampleServices>().
|
|
|
58
144
|
id: z.string(),
|
|
59
145
|
content: z.string(),
|
|
60
146
|
userId: z.string(),
|
|
147
|
+
userName: z.string(),
|
|
61
148
|
createdAt: z.date(),
|
|
62
149
|
}),
|
|
63
150
|
),
|
|
64
|
-
handler: async ({ query }, { json })
|
|
65
|
-
const
|
|
151
|
+
handler: async function ({ query }, { json }) {
|
|
152
|
+
const userEmail = query.get("userId"); // Using userId param name for backward compatibility
|
|
153
|
+
|
|
154
|
+
const result = await this.uow(async ({ executeRetrieve }) => {
|
|
155
|
+
const notesPromise = userEmail
|
|
156
|
+
? services.getNotesByUser(userEmail)
|
|
157
|
+
: services.getNotes();
|
|
158
|
+
|
|
159
|
+
// Execute all reads scheduled by services
|
|
160
|
+
await executeRetrieve();
|
|
161
|
+
|
|
162
|
+
return notesPromise;
|
|
163
|
+
});
|
|
66
164
|
|
|
67
|
-
|
|
68
|
-
const notes = await services.getNotesByUser(userId);
|
|
69
|
-
return json(notes);
|
|
70
|
-
}
|
|
165
|
+
const notes = await result;
|
|
71
166
|
|
|
72
|
-
|
|
73
|
-
|
|
167
|
+
return json(
|
|
168
|
+
notes
|
|
169
|
+
.filter((note) => note.author !== null)
|
|
170
|
+
.map((note) => ({
|
|
171
|
+
id: note.id.valueOf(),
|
|
172
|
+
content: note.content,
|
|
173
|
+
userId: note.author!.id.valueOf(),
|
|
174
|
+
userName: note.author!.name,
|
|
175
|
+
createdAt: note.createdAt,
|
|
176
|
+
})),
|
|
177
|
+
);
|
|
74
178
|
},
|
|
75
179
|
}),
|
|
76
180
|
|
|
77
181
|
defineRoute({
|
|
78
182
|
method: "POST",
|
|
79
183
|
path: "/notes",
|
|
80
|
-
inputSchema: z.object({ content: z.string(),
|
|
184
|
+
inputSchema: z.object({ content: z.string(), userEmail: z.string() }),
|
|
185
|
+
outputSchema: z.object({
|
|
186
|
+
id: z.string(),
|
|
187
|
+
content: z.string(),
|
|
188
|
+
userId: z.string(),
|
|
189
|
+
createdAt: z.date(),
|
|
190
|
+
}),
|
|
191
|
+
errorCodes: [],
|
|
192
|
+
handler: async function ({ input }, { json }) {
|
|
193
|
+
const { content, userEmail } = await input.valid();
|
|
194
|
+
|
|
195
|
+
// Handler controls transaction execution with retry policy
|
|
196
|
+
const result = await this.uow(
|
|
197
|
+
async ({ executeMutate }) => {
|
|
198
|
+
const notePromise = services.createNote({ content, userId: userEmail });
|
|
199
|
+
|
|
200
|
+
// Execute retrieval (if needed) and mutation atomically
|
|
201
|
+
// On optimistic conflict, this whole callback retries
|
|
202
|
+
await executeMutate();
|
|
203
|
+
|
|
204
|
+
return notePromise;
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
// Retry policy for optimistic concurrency conflicts
|
|
208
|
+
retryPolicy: new ExponentialBackoffRetryPolicy({
|
|
209
|
+
maxRetries: 5,
|
|
210
|
+
initialDelayMs: 10,
|
|
211
|
+
maxDelayMs: 250,
|
|
212
|
+
}),
|
|
213
|
+
},
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
const note = await result;
|
|
217
|
+
return json(note);
|
|
218
|
+
},
|
|
219
|
+
}),
|
|
220
|
+
|
|
221
|
+
defineRoute({
|
|
222
|
+
method: "PATCH",
|
|
223
|
+
path: "/notes/:noteId",
|
|
224
|
+
inputSchema: z.object({ content: z.string() }),
|
|
81
225
|
outputSchema: z.object({
|
|
82
226
|
id: z.string(),
|
|
83
227
|
content: z.string(),
|
|
@@ -85,10 +229,31 @@ const exampleRoutesFactory = defineRoutes<ExampleConfig, {}, ExampleServices>().
|
|
|
85
229
|
createdAt: z.date(),
|
|
86
230
|
}),
|
|
87
231
|
errorCodes: [],
|
|
88
|
-
handler: async ({ input }, { json })
|
|
89
|
-
const { content
|
|
232
|
+
handler: async function ({ input, pathParams }, { json }) {
|
|
233
|
+
const { content } = await input.valid();
|
|
234
|
+
const noteId = pathParams.noteId;
|
|
235
|
+
|
|
236
|
+
// Handler controls transaction with optimistic concurrency control
|
|
237
|
+
const result = await this.uow(
|
|
238
|
+
async ({ executeMutate }) => {
|
|
239
|
+
const notePromise = services.updateNote(noteId, content);
|
|
240
|
+
|
|
241
|
+
// Execute with optimistic concurrency control
|
|
242
|
+
// If note was modified, whole transaction retries
|
|
243
|
+
await executeMutate();
|
|
244
|
+
|
|
245
|
+
return notePromise;
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
retryPolicy: new ExponentialBackoffRetryPolicy({
|
|
249
|
+
maxRetries: 5,
|
|
250
|
+
initialDelayMs: 10,
|
|
251
|
+
maxDelayMs: 250,
|
|
252
|
+
}),
|
|
253
|
+
},
|
|
254
|
+
);
|
|
90
255
|
|
|
91
|
-
const note = await
|
|
256
|
+
const note = await result;
|
|
92
257
|
return json(note);
|
|
93
258
|
},
|
|
94
259
|
}),
|
|
@@ -96,34 +261,15 @@ const exampleRoutesFactory = defineRoutes<ExampleConfig, {}, ExampleServices>().
|
|
|
96
261
|
},
|
|
97
262
|
);
|
|
98
263
|
|
|
99
|
-
const exampleFragmentDefinition = defineFragmentWithDatabase<ExampleConfig>("example-fragment")
|
|
100
|
-
.withDatabase(noteSchema)
|
|
101
|
-
.providesService(({ db }) => {
|
|
102
|
-
return {
|
|
103
|
-
createNote: async (note: TableToInsertValues<typeof noteSchema.tables.note>) => {
|
|
104
|
-
const id = await db.create("note", note);
|
|
105
|
-
return {
|
|
106
|
-
...note,
|
|
107
|
-
id: id.toJSON(),
|
|
108
|
-
createdAt: note.createdAt ?? new Date(),
|
|
109
|
-
};
|
|
110
|
-
},
|
|
111
|
-
getNotes: () => {
|
|
112
|
-
return db.find("note", (b) => b);
|
|
113
|
-
},
|
|
114
|
-
getNotesByUser: (userId: string) => {
|
|
115
|
-
return db.find("note", (b) =>
|
|
116
|
-
b.whereIndex("idx_note_user", (eb) => eb("userId", "=", userId)),
|
|
117
|
-
);
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
});
|
|
121
|
-
|
|
122
264
|
export function createExampleFragment(
|
|
123
265
|
config: ExampleConfig = {},
|
|
124
|
-
|
|
266
|
+
options: FragnoPublicConfigWithDatabase,
|
|
125
267
|
) {
|
|
126
|
-
return
|
|
268
|
+
return instantiate(exampleFragmentDefinition)
|
|
269
|
+
.withConfig(config)
|
|
270
|
+
.withRoutes([exampleRoutesFactory])
|
|
271
|
+
.withOptions(options)
|
|
272
|
+
.build();
|
|
127
273
|
}
|
|
128
274
|
|
|
129
275
|
export function createExampleFragmentClients(fragnoConfig: FragnoPublicClientConfig) {
|
|
@@ -134,4 +280,4 @@ export function createExampleFragmentClients(fragnoConfig: FragnoPublicClientCon
|
|
|
134
280
|
useCreateNote: b.createMutator("POST", "/notes"),
|
|
135
281
|
};
|
|
136
282
|
}
|
|
137
|
-
export type { FragnoRouteConfig } from "@fragno-dev/core
|
|
283
|
+
export type { FragnoRouteConfig } from "@fragno-dev/core";
|
|
@@ -1,15 +1,28 @@
|
|
|
1
|
-
import { column, idColumn, schema } from "@fragno-dev/db/schema";
|
|
1
|
+
import { column, idColumn, referenceColumn, schema } from "@fragno-dev/db/schema";
|
|
2
2
|
|
|
3
3
|
export const noteSchema = schema((s) => {
|
|
4
|
-
return s
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
return s
|
|
5
|
+
.addTable("user", (t) => {
|
|
6
|
+
return t
|
|
7
|
+
.addColumn("id", idColumn())
|
|
8
|
+
.addColumn("name", column("string"))
|
|
9
|
+
.addColumn("email", column("string"))
|
|
10
|
+
.createIndex("idx_user_email", ["email"], { unique: true });
|
|
11
|
+
})
|
|
12
|
+
.addTable("note", (t) => {
|
|
13
|
+
return t
|
|
14
|
+
.addColumn("id", idColumn())
|
|
15
|
+
.addColumn("content", column("string"))
|
|
16
|
+
.addColumn("userId", referenceColumn())
|
|
17
|
+
.addColumn(
|
|
18
|
+
"createdAt",
|
|
19
|
+
column("timestamp").defaultTo((b) => b.now()),
|
|
20
|
+
)
|
|
21
|
+
.createIndex("idx_note_user", ["userId"]);
|
|
22
|
+
})
|
|
23
|
+
.addReference("author", {
|
|
24
|
+
type: "one",
|
|
25
|
+
from: { table: "note", column: "userId" },
|
|
26
|
+
to: { table: "user", column: "id" },
|
|
27
|
+
});
|
|
15
28
|
});
|