@cravery/core 0.0.4 → 0.0.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.
@@ -1,6 +1,6 @@
1
- export * from "./errors";
2
- export * from "./firestore.repository";
3
- export * from "./rtdb.repository";
4
- export * from "./user.repository";
5
- export * from "./profile.repository";
6
- export * from "./settings.repository";
1
+ export * from "./errors";
2
+ export * from "./firestore.repository";
3
+ export * from "./rtdb.repository";
4
+ export * from "./user.repository";
5
+ export * from "./profile.repository";
6
+ export * from "./settings.repository";
@@ -1,51 +1,51 @@
1
- import { Firestore } from "firebase-admin/firestore";
2
- import { FirestoreRepository } from "./firestore.repository";
3
- import type { Profile } from "../../types/profile";
4
- import { ProfileSchema } from "../../types/profile";
5
- import { Collections } from "../../config/collections";
6
- import { RepositoryError, RepositoryErrorCode } from "./errors";
7
-
8
- export class ProfileRepository extends FirestoreRepository<Profile> {
9
- constructor(firestore: Firestore) {
10
- super(firestore, Collections.Profiles, {
11
- enableTimestamps: true,
12
- enableSoftDelete: true,
13
- validateOnWrite: true,
14
- });
15
- }
16
-
17
- protected validate(data: Partial<Profile>): void {
18
- const result = ProfileSchema.partial().safeParse(data);
19
- if (!result.success) {
20
- throw new RepositoryError(
21
- RepositoryErrorCode.VALIDATION_ERROR,
22
- "Profile validation failed",
23
- { errors: result.error.issues },
24
- );
25
- }
26
- }
27
-
28
- async findByHandle(handle: string): Promise<Profile | null> {
29
- const results = await this.findWhere(
30
- [{ field: "handle", op: "==", value: handle }],
31
- { limit: 1 },
32
- );
33
- return results[0] ?? null;
34
- }
35
-
36
- async findByUserId(userId: string): Promise<Profile | null> {
37
- return this.findById(userId);
38
- }
39
-
40
- async createWithUserId(
41
- userId: string,
42
- data: Omit<Profile, "id">,
43
- ): Promise<Profile> {
44
- return this.create(data, userId);
45
- }
46
-
47
- async handleExists(handle: string): Promise<boolean> {
48
- const profile = await this.findByHandle(handle);
49
- return profile !== null;
50
- }
51
- }
1
+ import { Firestore } from "firebase-admin/firestore";
2
+ import { FirestoreRepository } from "./firestore.repository";
3
+ import type { Profile } from "../../types/profile";
4
+ import { ProfileSchema } from "../../types/profile";
5
+ import { Collections } from "../../config/collections";
6
+ import { RepositoryError, RepositoryErrorCode } from "./errors";
7
+
8
+ export class ProfileRepository extends FirestoreRepository<Profile> {
9
+ constructor(firestore: Firestore) {
10
+ super(firestore, Collections.Profiles, {
11
+ enableTimestamps: true,
12
+ enableSoftDelete: true,
13
+ validateOnWrite: true,
14
+ });
15
+ }
16
+
17
+ protected validate(data: Partial<Profile>): void {
18
+ const result = ProfileSchema.partial().safeParse(data);
19
+ if (!result.success) {
20
+ throw new RepositoryError(
21
+ RepositoryErrorCode.VALIDATION_ERROR,
22
+ "Profile validation failed",
23
+ { errors: result.error.issues },
24
+ );
25
+ }
26
+ }
27
+
28
+ async findByHandle(handle: string): Promise<Profile | null> {
29
+ const results = await this.findWhere(
30
+ [{ field: "handle", op: "==", value: handle }],
31
+ { limit: 1 },
32
+ );
33
+ return results[0] ?? null;
34
+ }
35
+
36
+ async findByUserId(userId: string): Promise<Profile | null> {
37
+ return this.findById(userId);
38
+ }
39
+
40
+ async createWithUserId(
41
+ userId: string,
42
+ data: Omit<Profile, "id">,
43
+ ): Promise<Profile> {
44
+ return this.create(data, userId);
45
+ }
46
+
47
+ async handleExists(handle: string): Promise<boolean> {
48
+ const profile = await this.findByHandle(handle);
49
+ return profile !== null;
50
+ }
51
+ }
@@ -1,68 +1,67 @@
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
- }
1
+ import { Database, Reference } from "firebase-admin/database";
2
+ import type { IRTDBRepository } from "../../types/repository";
3
+
4
+ export abstract class RTDBRepository<
5
+ T extends { id: string },
6
+ > implements IRTDBRepository<T> {
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
+ }
@@ -1,38 +1,57 @@
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
- }
1
+ import { Firestore } from "firebase-admin/firestore";
2
+ import { FirestoreRepository } from "./firestore.repository";
3
+ import type { Settings, SettingsInput } 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
+
39
+ static createDefaultSettings(): SettingsInput {
40
+ return {
41
+ allergens: [],
42
+ dietaryTags: [],
43
+ cuisines: [],
44
+ excludedIngredients: [],
45
+ locale: "en",
46
+ temperatureUnit: "celsius",
47
+ measurementSystem: "metric",
48
+ theme: "system",
49
+ notifications: {
50
+ email: true,
51
+ push: true,
52
+ marketing: false,
53
+ weeklyDigest: true,
54
+ },
55
+ };
56
+ }
57
+ }
@@ -1,44 +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
- }
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
+ }