@pelatform/starter.db 0.1.0 → 0.1.1

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/dist/index.d.ts CHANGED
@@ -30,7 +30,18 @@ declare function createEmailLog(db: any, email: string, metadata: any): Promise<
30
30
  * @param customSelect - Optional Prisma select object to customize returned fields.
31
31
  * @returns The user object with role and active workspace, or an error object.
32
32
  */
33
- declare function getUser(db: any, session: any, customSelect?: any): Promise<any>;
33
+ declare function getUser(db: any, session: any, customSelect?: any): Promise<{
34
+ ok: boolean;
35
+ error: {
36
+ code: string;
37
+ message: string;
38
+ };
39
+ data?: undefined;
40
+ } | {
41
+ ok: boolean;
42
+ data: any;
43
+ error?: undefined;
44
+ }>;
34
45
  /**
35
46
  * Updates a user's information in the database.
36
47
  *
@@ -43,7 +54,18 @@ declare function getUser(db: any, session: any, customSelect?: any): Promise<any
43
54
  * @param customSelect - Optional Prisma select object to customize returned fields.
44
55
  * @returns The updated user object with role and active workspace, or an error object.
45
56
  */
46
- declare function updateUser(db: any, session: any, data: Record<string, any>, customSelect?: any): Promise<any>;
57
+ declare function updateUser(db: any, session: any, data: Record<string, any>, customSelect?: any): Promise<{
58
+ ok: boolean;
59
+ error: {
60
+ code: string;
61
+ message: string;
62
+ };
63
+ data?: undefined;
64
+ } | {
65
+ ok: boolean;
66
+ data: any;
67
+ error?: undefined;
68
+ }>;
47
69
  /**
48
70
  * Updates a specific user's data by their ID.
49
71
  *
@@ -82,7 +104,18 @@ declare function getInitialUserData(db: any, session: any): Promise<{
82
104
  * @param customSelect - Optional Prisma select object.
83
105
  * @returns The workspace object with currentUserRole, or an error object.
84
106
  */
85
- declare function getWorkspacebySlug(db: any, session: any, slug: string, customSelect?: any): Promise<any>;
107
+ declare function getWorkspacebySlug(db: any, session: any, slug: string, customSelect?: any): Promise<{
108
+ ok: boolean;
109
+ error: {
110
+ code: string;
111
+ message: string;
112
+ };
113
+ data?: undefined;
114
+ } | {
115
+ ok: boolean;
116
+ data: any;
117
+ error?: undefined;
118
+ }>;
86
119
  /**
87
120
  * Retrieves a list of workspaces the user is a member of.
88
121
  *
@@ -91,7 +124,18 @@ declare function getWorkspacebySlug(db: any, session: any, slug: string, customS
91
124
  * @param customSelect - Optional Prisma select object.
92
125
  * @returns Array of workspace objects with currentUserRole, or an error object.
93
126
  */
94
- declare function getWorkspaceList(db: any, session: any, customSelect?: any): Promise<any>;
127
+ declare function getWorkspaceList(db: any, session: any, customSelect?: any): Promise<{
128
+ ok: boolean;
129
+ error: {
130
+ code: string;
131
+ message: string;
132
+ };
133
+ data?: undefined;
134
+ } | {
135
+ ok: boolean;
136
+ data: any;
137
+ error?: undefined;
138
+ }>;
95
139
  /**
96
140
  * Fetches the initial workspace data for the active user.
97
141
  *
package/dist/index.js CHANGED
@@ -65,7 +65,7 @@ var userSelect = (workspaceId) => ({
65
65
  // src/queries/user.ts
66
66
  async function getUser(db, session, customSelect) {
67
67
  if (!session || !session.user) {
68
- return { error: true, message: "User not authenticated" };
68
+ return { ok: false, error: { code: "UNAUTHORIZED", message: "User not authenticated" } };
69
69
  }
70
70
  try {
71
71
  const result = await db.user.findUnique({
@@ -73,22 +73,25 @@ async function getUser(db, session, customSelect) {
73
73
  select: customSelect || userSelect(session?.session?.activeOrganizationId || "")
74
74
  });
75
75
  if (!result) {
76
- return { error: true, message: "User not found" };
76
+ return { ok: false, error: { code: "NOT_FOUND", message: "User not found" } };
77
77
  }
78
78
  const user = {
79
79
  ...result,
80
80
  workspaceRole: result?.members[0]?.role || null,
81
81
  activeWorkspace: result?.members[0]?.organization || null
82
82
  };
83
- return user;
83
+ return { ok: true, data: user };
84
84
  } catch (error) {
85
85
  console.error("Error fetching user data:", error);
86
- return { error: true, message: "Error fetching user data." };
86
+ return {
87
+ ok: false,
88
+ error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching user data." }
89
+ };
87
90
  }
88
91
  }
89
92
  async function updateUser(db, session, data, customSelect) {
90
93
  if (!session || !session.user) {
91
- return { error: true, message: "User not authenticated" };
94
+ return { ok: false, error: { code: "UNAUTHORIZED", message: "User not authenticated" } };
92
95
  }
93
96
  try {
94
97
  const result = await db.user.update({
@@ -97,17 +100,20 @@ async function updateUser(db, session, data, customSelect) {
97
100
  select: customSelect || userSelect(session?.session?.activeOrganizationId || "")
98
101
  });
99
102
  if (!result) {
100
- return { error: true, message: "User not found" };
103
+ return { ok: false, error: { code: "NOT_FOUND", message: "User not found" } };
101
104
  }
102
105
  const user = {
103
106
  ...result,
104
107
  workspaceRole: result?.members[0]?.role || null,
105
108
  activeWorkspace: result?.members[0]?.organization || null
106
109
  };
107
- return user;
110
+ return { ok: true, data: user };
108
111
  } catch (error) {
109
112
  console.error("Error updating user data:", error);
110
- return { error: true, message: "Error updating user data." };
113
+ return {
114
+ ok: false,
115
+ error: { code: "INTERNAL_SERVER_ERROR", message: "Error updating user data." }
116
+ };
111
117
  }
112
118
  }
113
119
  async function updateUserData(db, userId, data) {
@@ -220,7 +226,7 @@ var workspaceSelect = {
220
226
  // src/queries/workspace.ts
221
227
  async function getWorkspacebySlug(db, session, slug, customSelect) {
222
228
  if (!session || !session.user) {
223
- return { error: true, message: "User not authenticated" };
229
+ return { ok: false, error: { code: "UNAUTHORIZED", message: "User not authenticated" } };
224
230
  }
225
231
  try {
226
232
  const result = await db.organization.findUnique({
@@ -228,11 +234,14 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
228
234
  select: customSelect || workspaceSelect
229
235
  });
230
236
  if (!result) {
231
- return { error: true, message: "Workspace not found" };
237
+ return { ok: false, error: { code: "NOT_FOUND", message: "Workspace not found" } };
232
238
  }
233
239
  const isUserMember = result.members.some((member) => member.userId === session.user.id);
234
240
  if (!isUserMember) {
235
- return { error: true, message: "User is not a member of the workspace" };
241
+ return {
242
+ ok: false,
243
+ error: { code: "FORBIDDEN", message: "User is not a member of the workspace" }
244
+ };
236
245
  }
237
246
  const currentUserMember = result.members.find(
238
247
  (member) => member.userId === session.user.id
@@ -242,15 +251,18 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
242
251
  ...result,
243
252
  currentUserRole
244
253
  };
245
- return workspace;
254
+ return { ok: true, data: workspace };
246
255
  } catch (error) {
247
256
  console.error("Error fetching workspace data:", error);
248
- return { error: true, message: "Error fetching workspace data." };
257
+ return {
258
+ ok: false,
259
+ error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching workspace data." }
260
+ };
249
261
  }
250
262
  }
251
263
  async function getWorkspaceList(db, session, customSelect) {
252
264
  if (!session || !session.user) {
253
- return { error: true, message: "User not authenticated" };
265
+ return { ok: false, error: { code: "UNAUTHORIZED", message: "User not authenticated" } };
254
266
  }
255
267
  try {
256
268
  const result = await db.organization.findMany({
@@ -273,10 +285,13 @@ async function getWorkspaceList(db, session, customSelect) {
273
285
  currentUserRole: currentUserMember?.role || null
274
286
  };
275
287
  });
276
- return workspaces;
288
+ return { ok: true, data: workspaces };
277
289
  } catch (error) {
278
290
  console.error("Error fetching workspace list:", error);
279
- return { error: true, message: "Error fetching workspace list." };
291
+ return {
292
+ ok: false,
293
+ error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching workspace list." }
294
+ };
280
295
  }
281
296
  }
282
297
  async function getInitialWorkspaceData(db, session, customSelect) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pelatform/starter.db",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A part of SaaS starter kit for Pelatform applications.",
5
5
  "author": "Pelatform",
6
6
  "license": "MIT",