@olasphe/mongo 1.0.0

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.
@@ -0,0 +1,17 @@
1
+ import { IUserRepository, ISessionRepository, User, Session } from '@olasubomimk/core';
2
+ export declare class MongoUserRepository implements IUserRepository {
3
+ create(data: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Promise<User>;
4
+ findByEmail(email: string): Promise<User | null>;
5
+ findById(id: string): Promise<User | null>;
6
+ update(id: string, updates: Partial<User>): Promise<User>;
7
+ delete(id: string): Promise<void>;
8
+ private map;
9
+ }
10
+ export declare class MongoSessionRepository implements ISessionRepository {
11
+ create(data: Session): Promise<Session>;
12
+ findByToken(token: string): Promise<Session | null>;
13
+ deleteByToken(token: string): Promise<void>;
14
+ revokeAllForUser(userId: string): Promise<void>;
15
+ revokeOtherSessions(userId: string, currentSessionId: string): Promise<void>;
16
+ private map;
17
+ }
package/dist/index.js ADDED
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.MongoSessionRepository = exports.MongoUserRepository = void 0;
37
+ const mongoose_1 = __importStar(require("mongoose"));
38
+ const UserSchema = new mongoose_1.Schema({
39
+ email: { type: String, required: true, unique: true },
40
+ passwordHash: { type: String, required: true },
41
+ isVerified: { type: Boolean, default: false },
42
+ verificationToken: { type: String },
43
+ verificationExpires: { type: Date },
44
+ createdAt: { type: Date, default: Date.now },
45
+ updatedAt: { type: Date, default: Date.now }
46
+ });
47
+ const UserModel = mongoose_1.default.model('User', UserSchema);
48
+ class MongoUserRepository {
49
+ async create(data) {
50
+ const doc = await UserModel.create(data);
51
+ return this.map(doc);
52
+ }
53
+ async findByEmail(email) {
54
+ const doc = await UserModel.findOne({ email });
55
+ return doc ? this.map(doc) : null;
56
+ }
57
+ async findById(id) {
58
+ const doc = await UserModel.findById(id);
59
+ return doc ? this.map(doc) : null;
60
+ }
61
+ async update(id, updates) {
62
+ const doc = await UserModel.findByIdAndUpdate(id, updates, { new: true });
63
+ if (!doc)
64
+ throw new Error("User not found");
65
+ return this.map(doc);
66
+ }
67
+ async delete(id) {
68
+ await UserModel.findByIdAndDelete(id);
69
+ }
70
+ map(doc) {
71
+ return {
72
+ id: doc._id.toString(),
73
+ email: doc.email,
74
+ passwordHash: doc.passwordHash,
75
+ isVerified: doc.isVerified,
76
+ verificationToken: doc.verificationToken,
77
+ verificationExpires: doc.verificationExpires,
78
+ createdAt: doc.createdAt,
79
+ updatedAt: doc.updatedAt
80
+ };
81
+ }
82
+ }
83
+ exports.MongoUserRepository = MongoUserRepository;
84
+ const SessionSchema = new mongoose_1.Schema({
85
+ userId: { type: String, required: true },
86
+ token: { type: String, required: true, unique: true },
87
+ userAgent: String,
88
+ ipAddress: String,
89
+ expiresAt: { type: Date, required: true },
90
+ createdAt: { type: Date, default: Date.now }
91
+ });
92
+ const SessionModel = mongoose_1.default.model('Session', SessionSchema);
93
+ class MongoSessionRepository {
94
+ async create(data) {
95
+ // Mongoose creates _id automatically, but our interface expects `id` to be passed or generated?
96
+ // In Core `MemorySessionRepository`, we manually generated `id`.
97
+ // Here we can let Mongo generate it, but we need to respect the interface.
98
+ // The interface says `create(session: Session): Promise<Session>`.
99
+ // Usually, the service generates the ID.
100
+ // If we pass an ID, Mongoose can use it if we set `_id`.
101
+ // Or we can ignore the passed ID and use Mongoose's.
102
+ // Let's standardise on using the passed ID as `_id` to be consistent across generic logic.
103
+ const doc = await SessionModel.create({
104
+ _id: data.id,
105
+ ...data
106
+ });
107
+ return this.map(doc);
108
+ }
109
+ async findByToken(token) {
110
+ const doc = await SessionModel.findOne({ token });
111
+ return doc ? this.map(doc) : null;
112
+ }
113
+ async deleteByToken(token) {
114
+ await SessionModel.deleteOne({ token });
115
+ }
116
+ async revokeAllForUser(userId) {
117
+ await SessionModel.deleteMany({ userId });
118
+ }
119
+ async revokeOtherSessions(userId, currentSessionId) {
120
+ await SessionModel.deleteMany({ userId, _id: { $ne: currentSessionId } });
121
+ }
122
+ map(doc) {
123
+ return {
124
+ id: doc._id.toString(),
125
+ userId: doc.userId,
126
+ token: doc.token,
127
+ userAgent: doc.userAgent,
128
+ ipAddress: doc.ipAddress,
129
+ expiresAt: doc.expiresAt,
130
+ createdAt: doc.createdAt
131
+ };
132
+ }
133
+ }
134
+ exports.MongoSessionRepository = MongoSessionRepository;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@olasphe/mongo",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "author": "Auth SDK User <user@example.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/example/auth-sdk-monorepo"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc"
20
+ },
21
+ "dependencies": {
22
+ "@olasphe/core": "*",
23
+ "mongoose": "^8.0.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^20.0.0"
27
+ }
28
+ }