@cravery/core 0.0.2 → 0.0.4
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/config/ai.d.ts +3 -0
- package/dist/config/ai.d.ts.map +1 -0
- package/dist/config/ai.js +36 -0
- package/dist/config/ai.js.map +1 -0
- package/dist/config/collections.d.ts +1 -0
- package/dist/config/collections.d.ts.map +1 -1
- package/dist/config/collections.js +1 -0
- package/dist/config/collections.js.map +1 -1
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +1 -0
- package/dist/config/index.js.map +1 -1
- package/dist/enums/cuisine.d.ts +1 -1
- package/dist/enums/difficulty.d.ts +1 -1
- package/dist/enums/moderation_status.d.ts +1 -1
- package/dist/enums/priority.d.ts +1 -1
- package/dist/enums/profile_status.d.ts +2 -2
- package/dist/enums/recipe_source.d.ts +2 -2
- package/dist/enums/recipe_status.d.ts +2 -2
- package/dist/enums/role.d.ts +1 -1
- package/dist/enums/severity.d.ts +1 -1
- package/dist/enums/spiciness.d.ts +1 -1
- package/dist/enums/status.d.ts +2 -2
- package/dist/enums/unit.d.ts +5 -5
- package/dist/enums/user_status.d.ts +2 -2
- package/dist/lib/ai/cost.d.ts +22 -0
- package/dist/lib/ai/cost.d.ts.map +1 -0
- package/dist/lib/ai/cost.js +45 -0
- package/dist/lib/ai/cost.js.map +1 -0
- package/dist/lib/ai/embedding.d.ts +4 -0
- package/dist/lib/ai/embedding.d.ts.map +1 -0
- package/dist/lib/ai/embedding.js +48 -0
- package/dist/lib/ai/embedding.js.map +1 -0
- package/dist/lib/ai/errors.d.ts +14 -0
- package/dist/lib/ai/errors.d.ts.map +1 -0
- package/dist/lib/ai/errors.js +40 -0
- package/dist/lib/ai/errors.js.map +1 -0
- package/dist/lib/ai/flow.d.ts +53 -0
- package/dist/lib/ai/flow.d.ts.map +1 -0
- package/dist/lib/ai/flow.js +60 -0
- package/dist/lib/ai/flow.js.map +1 -0
- package/dist/lib/ai/genkit.d.ts +4 -0
- package/dist/lib/ai/genkit.d.ts.map +1 -0
- package/dist/lib/ai/genkit.js +16 -0
- package/dist/lib/ai/genkit.js.map +1 -0
- package/dist/lib/ai/image.d.ts +7 -0
- package/dist/lib/ai/image.d.ts.map +1 -0
- package/dist/lib/ai/image.js +23 -0
- package/dist/lib/ai/image.js.map +1 -0
- package/dist/lib/ai/index.d.ts +7 -0
- package/dist/lib/ai/index.d.ts.map +1 -0
- package/dist/lib/ai/index.js +23 -0
- package/dist/lib/ai/index.js.map +1 -0
- package/dist/lib/api.d.ts +18 -0
- package/dist/lib/api.d.ts.map +1 -0
- package/dist/lib/api.js +69 -0
- package/dist/lib/api.js.map +1 -0
- package/dist/lib/cost.d.ts +22 -0
- package/dist/lib/cost.d.ts.map +1 -0
- package/dist/lib/cost.js +45 -0
- package/dist/lib/cost.js.map +1 -0
- package/dist/lib/embedding.d.ts +4 -0
- package/dist/lib/embedding.d.ts.map +1 -0
- package/dist/lib/embedding.js +48 -0
- package/dist/lib/embedding.js.map +1 -0
- package/dist/lib/firebase.d.ts +3 -0
- package/dist/lib/firebase.d.ts.map +1 -0
- package/dist/lib/firebase.js +8 -0
- package/dist/lib/firebase.js.map +1 -0
- package/dist/lib/flow.d.ts +53 -0
- package/dist/lib/flow.d.ts.map +1 -0
- package/dist/lib/flow.js +60 -0
- package/dist/lib/flow.js.map +1 -0
- package/dist/lib/genkit.d.ts +4 -0
- package/dist/lib/genkit.d.ts.map +1 -0
- package/dist/lib/genkit.js +16 -0
- package/dist/lib/genkit.js.map +1 -0
- package/dist/lib/iam.d.ts +3 -0
- package/dist/lib/iam.d.ts.map +1 -0
- package/dist/lib/iam.js +57 -0
- package/dist/lib/iam.js.map +1 -0
- package/dist/lib/image.d.ts +7 -0
- package/dist/lib/image.d.ts.map +1 -0
- package/dist/lib/image.js +24 -0
- package/dist/lib/image.js.map +1 -0
- package/dist/lib/index.d.ts +6 -1
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +20 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/repository/errors.d.ts +20 -0
- package/dist/lib/repository/errors.d.ts.map +1 -0
- package/dist/lib/repository/errors.js +33 -0
- package/dist/lib/repository/errors.js.map +1 -0
- package/dist/lib/repository/firestore.repository.d.ts +50 -0
- package/dist/lib/repository/firestore.repository.d.ts.map +1 -0
- package/dist/lib/repository/firestore.repository.js +407 -0
- package/dist/lib/repository/firestore.repository.js.map +1 -0
- package/dist/lib/repository/index.d.ts +7 -0
- package/dist/lib/repository/index.d.ts.map +1 -0
- package/dist/lib/repository/index.js +23 -0
- package/dist/lib/repository/index.js.map +1 -0
- package/dist/lib/repository/profile.repository.d.ts +12 -0
- package/dist/lib/repository/profile.repository.d.ts.map +1 -0
- package/dist/lib/repository/profile.repository.js +39 -0
- package/dist/lib/repository/profile.repository.js.map +1 -0
- package/dist/lib/repository/rtdb.repository.d.ts +21 -0
- package/dist/lib/repository/rtdb.repository.d.ts.map +1 -0
- package/dist/lib/repository/rtdb.repository.js +55 -0
- package/dist/lib/repository/rtdb.repository.js.map +1 -0
- package/dist/lib/repository/settings.repository.d.ts +10 -0
- package/dist/lib/repository/settings.repository.d.ts.map +1 -0
- package/dist/lib/repository/settings.repository.js +30 -0
- package/dist/lib/repository/settings.repository.js.map +1 -0
- package/dist/lib/repository/user.repository.d.ts +11 -0
- package/dist/lib/repository/user.repository.d.ts.map +1 -0
- package/dist/lib/repository/user.repository.js +36 -0
- package/dist/lib/repository/user.repository.js.map +1 -0
- package/dist/lib/storage.d.ts +6 -0
- package/dist/lib/storage.d.ts.map +1 -0
- package/dist/lib/storage.js +34 -0
- package/dist/lib/storage.js.map +1 -0
- package/dist/types/ai/config.d.ts +2 -2
- package/dist/types/ai/config.d.ts.map +1 -1
- package/dist/types/ai/filters.d.ts +3 -3
- package/dist/types/ai/recipe.d.ts +10 -10
- package/dist/types/equipment.d.ts +2 -2
- package/dist/types/equipment.js +1 -1
- package/dist/types/error.d.ts +14 -0
- package/dist/types/error.d.ts.map +1 -0
- package/dist/types/error.js +40 -0
- package/dist/types/error.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/ingredient.d.ts +24 -24
- package/dist/types/ingredient.js +1 -1
- package/dist/types/ingredient.js.map +1 -1
- package/dist/types/moderation.d.ts +5 -6
- package/dist/types/moderation.d.ts.map +1 -1
- package/dist/types/profile.d.ts +3 -3
- package/dist/types/profile.d.ts.map +1 -1
- package/dist/types/profile.js.map +1 -1
- package/dist/types/recipe.d.ts +28 -29
- package/dist/types/recipe.d.ts.map +1 -1
- package/dist/types/repository.d.ts +83 -0
- package/dist/types/repository.d.ts.map +1 -0
- package/dist/types/repository.js +6 -0
- package/dist/types/repository.js.map +1 -0
- package/dist/types/settings.d.ts +141 -0
- package/dist/types/settings.d.ts.map +1 -0
- package/dist/types/settings.js +70 -0
- package/dist/types/settings.js.map +1 -0
- package/dist/types/subscription.d.ts +0 -1
- package/dist/types/subscription.d.ts.map +1 -1
- package/dist/types/user.d.ts +4 -4
- package/dist/types/user.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/config/ai.ts +34 -0
- package/src/config/collections.ts +1 -0
- package/src/config/index.ts +1 -0
- package/src/lib/ai/cost.ts +72 -0
- package/src/lib/ai/embedding.ts +53 -0
- package/src/lib/ai/errors.ts +43 -0
- package/src/lib/ai/flow.ts +88 -0
- package/src/lib/ai/genkit.ts +18 -0
- package/src/lib/ai/image.ts +27 -0
- package/src/lib/ai/index.ts +6 -0
- package/src/lib/api.ts +90 -0
- package/src/lib/firebase.ts +5 -0
- package/src/lib/iam.ts +22 -0
- package/src/lib/index.ts +6 -0
- package/src/lib/repository/errors.ts +37 -0
- package/src/lib/repository/firestore.repository.ts +607 -0
- package/src/lib/repository/index.ts +6 -0
- package/src/lib/repository/profile.repository.ts +51 -0
- package/src/lib/repository/rtdb.repository.ts +68 -0
- package/src/lib/repository/settings.repository.ts +38 -0
- package/src/lib/repository/user.repository.ts +44 -0
- package/src/lib/storage.ts +42 -0
- package/src/types/ai/config.ts +3 -2
- package/src/types/equipment.ts +1 -1
- package/src/types/index.ts +2 -0
- package/src/types/ingredient.ts +1 -1
- package/src/types/profile.ts +2 -0
- package/src/types/repository.ts +115 -0
- package/src/types/settings.ts +85 -0
- package/src/types/user.ts +2 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Database, Reference } from "firebase-admin/database";
|
|
2
|
+
import type { IRTDBRepository } from "../../types/repository";
|
|
3
|
+
|
|
4
|
+
export abstract class RTDBRepository<T extends { id: string }>
|
|
5
|
+
implements IRTDBRepository<T>
|
|
6
|
+
{
|
|
7
|
+
protected readonly ref: Reference;
|
|
8
|
+
|
|
9
|
+
constructor(
|
|
10
|
+
protected readonly database: Database,
|
|
11
|
+
protected readonly path: string,
|
|
12
|
+
) {
|
|
13
|
+
this.ref = database.ref(path);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
protected toDocument(entity: Partial<T>): Record<string, unknown> {
|
|
17
|
+
return { ...entity };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
protected fromDocument(id: string, data: Record<string, unknown>): T {
|
|
21
|
+
return { id, ...data } as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async get(id: string): Promise<T | null> {
|
|
25
|
+
const snapshot = await this.ref.child(id).get();
|
|
26
|
+
if (!snapshot.exists()) return null;
|
|
27
|
+
return this.fromDocument(id, snapshot.val());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getAll(): Promise<T[]> {
|
|
31
|
+
const snapshot = await this.ref.get();
|
|
32
|
+
if (!snapshot.exists()) return [];
|
|
33
|
+
|
|
34
|
+
const results: T[] = [];
|
|
35
|
+
snapshot.forEach((child) => {
|
|
36
|
+
results.push(this.fromDocument(child.key!, child.val()));
|
|
37
|
+
});
|
|
38
|
+
return results;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async set(id: string, data: T): Promise<void> {
|
|
42
|
+
const docData = this.toDocument(data);
|
|
43
|
+
await this.ref.child(id).set(docData);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async update(id: string, data: Partial<T>): Promise<void> {
|
|
47
|
+
const docData = this.toDocument(data);
|
|
48
|
+
await this.ref.child(id).update(docData);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async remove(id: string): Promise<void> {
|
|
52
|
+
await this.ref.child(id).remove();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async increment(id: string, field: string, delta: number = 1): Promise<void> {
|
|
56
|
+
const fieldRef = this.ref.child(id).child(field);
|
|
57
|
+
await fieldRef.transaction((current) => (current || 0) + delta);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async decrement(id: string, field: string, delta: number = 1): Promise<void> {
|
|
61
|
+
await this.increment(id, field, -delta);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async multiUpdate(updates: Record<string, unknown>): Promise<void> {
|
|
65
|
+
await this.ref.update(updates);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Firestore } from "firebase-admin/firestore";
|
|
2
|
+
import { FirestoreRepository } from "./firestore.repository";
|
|
3
|
+
import type { Settings } from "../../types/settings";
|
|
4
|
+
import { SettingsSchema } from "../../types/settings";
|
|
5
|
+
import { Collections } from "../../config/collections";
|
|
6
|
+
import { RepositoryError, RepositoryErrorCode } from "./errors";
|
|
7
|
+
|
|
8
|
+
export class SettingsRepository extends FirestoreRepository<Settings> {
|
|
9
|
+
constructor(firestore: Firestore) {
|
|
10
|
+
super(firestore, Collections.Settings, {
|
|
11
|
+
enableTimestamps: true,
|
|
12
|
+
enableSoftDelete: false,
|
|
13
|
+
validateOnWrite: true,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected validate(data: Partial<Settings>): void {
|
|
18
|
+
const result = SettingsSchema.partial().safeParse(data);
|
|
19
|
+
if (!result.success) {
|
|
20
|
+
throw new RepositoryError(
|
|
21
|
+
RepositoryErrorCode.VALIDATION_ERROR,
|
|
22
|
+
"Settings validation failed",
|
|
23
|
+
{ errors: result.error.issues },
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async findByUserId(userId: string): Promise<Settings | null> {
|
|
29
|
+
return this.findById(userId);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async createForUser(
|
|
33
|
+
userId: string,
|
|
34
|
+
data: Omit<Settings, "id">,
|
|
35
|
+
): Promise<Settings> {
|
|
36
|
+
return this.create(data, userId);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Firestore } from "firebase-admin/firestore";
|
|
2
|
+
import { FirestoreRepository } from "./firestore.repository";
|
|
3
|
+
import type { User } from "../../types/user";
|
|
4
|
+
import { UserSchema } from "../../types/user";
|
|
5
|
+
import { Collections } from "../../config/collections";
|
|
6
|
+
import { RepositoryError, RepositoryErrorCode } from "./errors";
|
|
7
|
+
|
|
8
|
+
export class UserRepository extends FirestoreRepository<User> {
|
|
9
|
+
constructor(firestore: Firestore) {
|
|
10
|
+
super(firestore, Collections.Users, {
|
|
11
|
+
enableTimestamps: true,
|
|
12
|
+
enableSoftDelete: true,
|
|
13
|
+
validateOnWrite: true,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected validate(data: Partial<User>): void {
|
|
18
|
+
const result = UserSchema.partial().safeParse(data);
|
|
19
|
+
if (!result.success) {
|
|
20
|
+
throw new RepositoryError(
|
|
21
|
+
RepositoryErrorCode.VALIDATION_ERROR,
|
|
22
|
+
"User validation failed",
|
|
23
|
+
{ errors: result.error.issues },
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async findByEmail(email: string): Promise<User | null> {
|
|
29
|
+
const results = await this.findWhere(
|
|
30
|
+
[{ field: "email", op: "==", value: email }],
|
|
31
|
+
{ limit: 1 },
|
|
32
|
+
);
|
|
33
|
+
return results[0] ?? null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async createWithId(uid: string, data: Omit<User, "id">): Promise<User> {
|
|
37
|
+
return this.create(data, uid);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async emailExists(email: string): Promise<boolean> {
|
|
41
|
+
const user = await this.findByEmail(email);
|
|
42
|
+
return user !== null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getStorage } from "firebase-admin/storage";
|
|
2
|
+
|
|
3
|
+
export interface UploadImageResult {
|
|
4
|
+
url: string;
|
|
5
|
+
path: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const uploadImageToStorage = async (
|
|
9
|
+
base64Data: string,
|
|
10
|
+
path: string,
|
|
11
|
+
contentType: string = "image/png",
|
|
12
|
+
): Promise<UploadImageResult> => {
|
|
13
|
+
try {
|
|
14
|
+
const bucket = getStorage().bucket();
|
|
15
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
16
|
+
const file = bucket.file(path);
|
|
17
|
+
|
|
18
|
+
const fileExists = await file.exists();
|
|
19
|
+
if (fileExists[0]) {
|
|
20
|
+
await file.delete();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
await file.save(buffer, {
|
|
24
|
+
metadata: {
|
|
25
|
+
contentType,
|
|
26
|
+
cacheControl: "public",
|
|
27
|
+
},
|
|
28
|
+
resumable: false,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${path}`;
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
url: publicUrl,
|
|
35
|
+
path,
|
|
36
|
+
};
|
|
37
|
+
} catch {
|
|
38
|
+
const uploadError = new Error("Image upload failed");
|
|
39
|
+
uploadError.name = "UPLOAD_FAILED";
|
|
40
|
+
throw uploadError;
|
|
41
|
+
}
|
|
42
|
+
};
|
package/src/types/ai/config.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* AI model configuration, costs are in USD:
|
|
3
3
|
* - multimodal: cost per 1,000,000 input/output tokens
|
|
4
|
-
* - image: cost per image
|
|
4
|
+
* - image: cost per image (stored in cost.output)
|
|
5
5
|
* - embedding: cost per 1,000 input tokens
|
|
6
6
|
*/
|
|
7
7
|
export type AIModel =
|
|
8
8
|
| "Gemini25Flash"
|
|
9
9
|
| "Gemini25Pro"
|
|
10
|
+
| "Gemini3Flash"
|
|
10
11
|
| "Imagen4Fast"
|
|
11
|
-
| "
|
|
12
|
+
| "GeminiEmbedding001";
|
|
12
13
|
export type AIModelType = "multimodal" | "image" | "embedding";
|
|
13
14
|
|
|
14
15
|
export interface AIModelCost {
|
package/src/types/equipment.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { SlugRegex } from "../config";
|
|
3
3
|
|
|
4
4
|
export const EquipmentMetaSchema = z.object({
|
|
5
|
-
|
|
5
|
+
required: z.boolean().optional(),
|
|
6
6
|
slug: z.string().regex(SlugRegex),
|
|
7
7
|
});
|
|
8
8
|
export type EquipmentMeta = z.infer<typeof EquipmentMetaSchema>;
|
package/src/types/index.ts
CHANGED
|
@@ -6,6 +6,8 @@ export * from "./moderation";
|
|
|
6
6
|
export * from "./nutrition";
|
|
7
7
|
export * from "./profile";
|
|
8
8
|
export * from "./recipe";
|
|
9
|
+
export * from "./repository";
|
|
10
|
+
export * from "./settings";
|
|
9
11
|
export * from "./subscription";
|
|
10
12
|
export * from "./temperature";
|
|
11
13
|
export * from "./user";
|
package/src/types/ingredient.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { SlugRegex } from "../config";
|
|
|
3
3
|
import { UnitSchema } from "../enums";
|
|
4
4
|
|
|
5
5
|
export const IngredientMetaSchema = z.object({
|
|
6
|
-
optional: z.boolean().optional(),
|
|
7
6
|
quantity: z.number().positive().optional(),
|
|
7
|
+
required: z.boolean().optional(),
|
|
8
8
|
slug: z.string().regex(SlugRegex),
|
|
9
9
|
unit: UnitSchema.optional(),
|
|
10
10
|
});
|
package/src/types/profile.ts
CHANGED
|
@@ -16,6 +16,8 @@ export const ProfileSchema = z.object({
|
|
|
16
16
|
});
|
|
17
17
|
export type Profile = z.infer<typeof ProfileSchema>;
|
|
18
18
|
|
|
19
|
+
export type ProfileInput = Omit<Profile, "id" | "createdAt" | "updatedAt">;
|
|
20
|
+
|
|
19
21
|
export const ProfileStatsSchema = z.object({
|
|
20
22
|
followers: z.number().int().nonnegative(),
|
|
21
23
|
following: z.number().int().nonnegative(),
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type {
|
|
3
|
+
Firestore,
|
|
4
|
+
Transaction,
|
|
5
|
+
WriteBatch,
|
|
6
|
+
} from "firebase-admin/firestore";
|
|
7
|
+
|
|
8
|
+
export const SortDirectionSchema = z.enum(["asc", "desc"]);
|
|
9
|
+
export type SortDirection = z.infer<typeof SortDirectionSchema>;
|
|
10
|
+
|
|
11
|
+
export interface OrderBy {
|
|
12
|
+
field: string;
|
|
13
|
+
direction: SortDirection;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface QueryOptions {
|
|
17
|
+
limit?: number;
|
|
18
|
+
offset?: number;
|
|
19
|
+
orderBy?: OrderBy;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PaginatedResult<T> {
|
|
23
|
+
data: T[];
|
|
24
|
+
total: number;
|
|
25
|
+
hasMore: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface CursorPaginatedResult<T> {
|
|
29
|
+
data: T[];
|
|
30
|
+
nextCursor: string | null;
|
|
31
|
+
hasMore: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type WhereOp =
|
|
35
|
+
| "<"
|
|
36
|
+
| "<="
|
|
37
|
+
| "=="
|
|
38
|
+
| "!="
|
|
39
|
+
| ">="
|
|
40
|
+
| ">"
|
|
41
|
+
| "array-contains"
|
|
42
|
+
| "array-contains-any"
|
|
43
|
+
| "in"
|
|
44
|
+
| "not-in";
|
|
45
|
+
|
|
46
|
+
export interface WhereClause {
|
|
47
|
+
field: string;
|
|
48
|
+
op: WhereOp;
|
|
49
|
+
value: unknown;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type TransactionCallback<R> = (transaction: Transaction) => Promise<R>;
|
|
53
|
+
|
|
54
|
+
export interface RepositoryConfig {
|
|
55
|
+
enableTimestamps?: boolean;
|
|
56
|
+
enableSoftDelete?: boolean;
|
|
57
|
+
validateOnWrite?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface IRepository<T extends { id: string }> {
|
|
61
|
+
// Read operations
|
|
62
|
+
findById(id: string): Promise<T | null>;
|
|
63
|
+
findByIds(ids: string[]): Promise<T[]>;
|
|
64
|
+
findAll(options?: QueryOptions): Promise<T[]>;
|
|
65
|
+
findWhere(clauses: WhereClause[], options?: QueryOptions): Promise<T[]>;
|
|
66
|
+
findPaginated(
|
|
67
|
+
clauses?: WhereClause[],
|
|
68
|
+
options?: QueryOptions,
|
|
69
|
+
): Promise<PaginatedResult<T>>;
|
|
70
|
+
findPaginatedCursor(
|
|
71
|
+
clauses?: WhereClause[],
|
|
72
|
+
options?: QueryOptions & { cursor?: string },
|
|
73
|
+
): Promise<CursorPaginatedResult<T>>;
|
|
74
|
+
exists(id: string): Promise<boolean>;
|
|
75
|
+
count(clauses?: WhereClause[]): Promise<number>;
|
|
76
|
+
|
|
77
|
+
// Write operations (timestamps are auto-added by the repository)
|
|
78
|
+
create(data: Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">, id?: string): Promise<T>;
|
|
79
|
+
update(id: string, data: Partial<Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">>): Promise<T>;
|
|
80
|
+
delete(id: string): Promise<void>;
|
|
81
|
+
|
|
82
|
+
// Batch operations
|
|
83
|
+
createMany(items: Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">[]): Promise<T[]>;
|
|
84
|
+
updateMany(updates: Array<{ id: string; data: Partial<Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">> }>): Promise<void>;
|
|
85
|
+
deleteMany(ids: string[]): Promise<void>;
|
|
86
|
+
|
|
87
|
+
// Field operations
|
|
88
|
+
increment(id: string, field: keyof T, value?: number): Promise<void>;
|
|
89
|
+
decrement(id: string, field: keyof T, value?: number): Promise<void>;
|
|
90
|
+
|
|
91
|
+
// Transaction support
|
|
92
|
+
runTransaction<R>(callback: TransactionCallback<R>): Promise<R>;
|
|
93
|
+
getFirestore(): Firestore;
|
|
94
|
+
batch(): WriteBatch;
|
|
95
|
+
|
|
96
|
+
// Batch operations (for cross-repository atomic writes)
|
|
97
|
+
createInBatch(batch: WriteBatch, data: Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">, id?: string): T;
|
|
98
|
+
setInBatch(batch: WriteBatch, id: string, data: Partial<Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">>): void;
|
|
99
|
+
updateInBatch(batch: WriteBatch, id: string, data: Partial<Omit<T, "id" | "createdAt" | "updatedAt" | "deletedAt">>): void;
|
|
100
|
+
deleteInBatch(batch: WriteBatch, id: string): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface IRTDBRepository<T extends { id: string }> {
|
|
104
|
+
get(id: string): Promise<T | null>;
|
|
105
|
+
getAll(): Promise<T[]>;
|
|
106
|
+
|
|
107
|
+
set(id: string, data: T): Promise<void>;
|
|
108
|
+
update(id: string, data: Partial<T>): Promise<void>;
|
|
109
|
+
remove(id: string): Promise<void>;
|
|
110
|
+
|
|
111
|
+
increment(id: string, field: string, delta?: number): Promise<void>;
|
|
112
|
+
decrement(id: string, field: string, delta?: number): Promise<void>;
|
|
113
|
+
|
|
114
|
+
multiUpdate(updates: Record<string, unknown>): Promise<void>;
|
|
115
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Timestamp } from "firebase-admin/firestore";
|
|
3
|
+
import { AllergenSchema } from "../enums/allergen";
|
|
4
|
+
import { DietaryTagSchema } from "../enums/dietary_tag";
|
|
5
|
+
import { CuisineSchema } from "../enums/cuisine";
|
|
6
|
+
import { SpicinessSchema } from "../enums/spiciness";
|
|
7
|
+
import { LocaleSchema } from "../enums/locale";
|
|
8
|
+
import { TemperatureUnitSchema } from "../enums/temperature_unit";
|
|
9
|
+
|
|
10
|
+
export const MEASUREMENT_SYSTEM_VALUES = ["metric", "imperial"] as const;
|
|
11
|
+
export const MeasurementSystemSchema = z.enum(MEASUREMENT_SYSTEM_VALUES);
|
|
12
|
+
export type MeasurementSystem = (typeof MEASUREMENT_SYSTEM_VALUES)[number];
|
|
13
|
+
|
|
14
|
+
export const THEME_VALUES = ["light", "dark", "system"] as const;
|
|
15
|
+
export const ThemeSchema = z.enum(THEME_VALUES);
|
|
16
|
+
export type Theme = (typeof THEME_VALUES)[number];
|
|
17
|
+
|
|
18
|
+
export const NotificationSettingsSchema = z.object({
|
|
19
|
+
email: z.boolean(),
|
|
20
|
+
push: z.boolean(),
|
|
21
|
+
marketing: z.boolean(),
|
|
22
|
+
weeklyDigest: z.boolean(),
|
|
23
|
+
});
|
|
24
|
+
export type NotificationSettings = z.infer<typeof NotificationSettingsSchema>;
|
|
25
|
+
|
|
26
|
+
export const PrivacySettingsSchema = z.object({
|
|
27
|
+
showAllergensOnProfile: z.boolean(),
|
|
28
|
+
showDietaryTagsOnProfile: z.boolean(),
|
|
29
|
+
allowRecipeRecommendations: z.boolean(),
|
|
30
|
+
});
|
|
31
|
+
export type PrivacySettings = z.infer<typeof PrivacySettingsSchema>;
|
|
32
|
+
|
|
33
|
+
export const SettingsSchema = z.object({
|
|
34
|
+
id: z.string(),
|
|
35
|
+
|
|
36
|
+
// Dietary preferences
|
|
37
|
+
allergens: z.array(AllergenSchema),
|
|
38
|
+
dietaryTags: z.array(DietaryTagSchema),
|
|
39
|
+
cuisinePreferences: z.array(CuisineSchema),
|
|
40
|
+
spicinessPreference: SpicinessSchema.optional(),
|
|
41
|
+
excludedIngredients: z.array(z.string()),
|
|
42
|
+
|
|
43
|
+
// App preferences
|
|
44
|
+
locale: LocaleSchema,
|
|
45
|
+
temperatureUnit: TemperatureUnitSchema,
|
|
46
|
+
measurementSystem: MeasurementSystemSchema,
|
|
47
|
+
theme: ThemeSchema,
|
|
48
|
+
|
|
49
|
+
// Notifications
|
|
50
|
+
notifications: NotificationSettingsSchema,
|
|
51
|
+
|
|
52
|
+
// Privacy
|
|
53
|
+
privacy: PrivacySettingsSchema,
|
|
54
|
+
|
|
55
|
+
// Timestamps
|
|
56
|
+
createdAt: z.custom<Timestamp>(),
|
|
57
|
+
updatedAt: z.custom<Timestamp>(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export type Settings = z.infer<typeof SettingsSchema>;
|
|
61
|
+
|
|
62
|
+
export type SettingsInput = Omit<Settings, "id" | "createdAt" | "updatedAt">;
|
|
63
|
+
|
|
64
|
+
export const createDefaultSettings = (): SettingsInput => ({
|
|
65
|
+
allergens: [],
|
|
66
|
+
dietaryTags: [],
|
|
67
|
+
cuisinePreferences: [],
|
|
68
|
+
spicinessPreference: undefined,
|
|
69
|
+
excludedIngredients: [],
|
|
70
|
+
locale: "en",
|
|
71
|
+
temperatureUnit: "celsius",
|
|
72
|
+
measurementSystem: "metric",
|
|
73
|
+
theme: "system",
|
|
74
|
+
notifications: {
|
|
75
|
+
email: true,
|
|
76
|
+
push: true,
|
|
77
|
+
marketing: false,
|
|
78
|
+
weeklyDigest: true,
|
|
79
|
+
},
|
|
80
|
+
privacy: {
|
|
81
|
+
showAllergensOnProfile: false,
|
|
82
|
+
showDietaryTagsOnProfile: false,
|
|
83
|
+
allowRecipeRecommendations: true,
|
|
84
|
+
},
|
|
85
|
+
});
|