@astratra/store-mongo 1.0.0 → 1.0.2

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/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@astratra/store-mongo",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "MongoDB/Mongoose persistence adapters for Astratra SaaS stores.",
5
+ "engines": {
6
+ "node": ">=20"
7
+ },
5
8
  "main": "src/index.js",
6
9
  "types": "src/index.d.ts",
7
10
  "type": "commonjs",
package/src/index.d.ts CHANGED
@@ -42,6 +42,8 @@ export interface MongoUsersStore<TUser extends MongoUserBase = MongoUser> {
42
42
  findById(id: string): Promise<TUser | null>;
43
43
  create(userData: Partial<TUser> & Record<string, unknown>): Promise<TUser>;
44
44
  list(options?: UsersListOptions): Promise<TUser[]>;
45
+ count(options?: Pick<UsersListOptions, 'role'>): Promise<number>;
46
+ countByRole(): Promise<Record<string, number>>;
45
47
  update(id: string, patch: Partial<TUser> & Record<string, unknown>): Promise<TUser | null>;
46
48
  disconnect(): Promise<void>;
47
49
  }
@@ -49,8 +51,8 @@ export interface MongoUsersStore<TUser extends MongoUserBase = MongoUser> {
49
51
  export interface MongoSettingsStore<TSettings extends object = Record<string, unknown>> {
50
52
  get<TKey extends keyof TSettings & string>(key: TKey): Promise<TSettings[TKey] | null>;
51
53
  get(key: string): Promise<unknown | null>;
52
- set<TKey extends keyof TSettings & string>(key: TKey, value: TSettings[TKey]): Promise<void>;
53
- set(key: string, value: unknown): Promise<void>;
54
+ set<TKey extends keyof TSettings & string>(key: TKey, value: TSettings[TKey]): Promise<TSettings[TKey]>;
55
+ set(key: string, value: unknown): Promise<unknown>;
54
56
  getAll(): Promise<Partial<TSettings>>;
55
57
  disconnect(): Promise<void>;
56
58
  }
package/src/index.js CHANGED
@@ -72,6 +72,10 @@ function safeNumber(value, fallback) {
72
72
  return Number.isFinite(number) ? Math.max(number, 0) : fallback;
73
73
  }
74
74
 
75
+ function normalizeEmail(email) {
76
+ return String(email || '').trim().toLowerCase();
77
+ }
78
+
75
79
  function initModel(Model) {
76
80
  if (!Model.astratraInitPromise) {
77
81
  Model.astratraInitPromise = Model.init();
@@ -101,7 +105,8 @@ function createMongoUsersStore(options = {}) {
101
105
  return {
102
106
  async findByEmail(email) {
103
107
  await ready();
104
- const user = await User.findOne({ email }).lean();
108
+ const normalized = normalizeEmail(email);
109
+ const user = await User.findOne({ email: normalized }).lean();
105
110
  return stripMongoFields(user);
106
111
  },
107
112
 
@@ -118,6 +123,9 @@ function createMongoUsersStore(options = {}) {
118
123
  async create(userData) {
119
124
  await ready();
120
125
  const data = { ...(userData || {}) };
126
+ if (data.email !== undefined) {
127
+ data.email = normalizeEmail(data.email);
128
+ }
121
129
  if (data.id !== undefined && data._id === undefined && context.mongoose.isValidObjectId(data.id)) {
122
130
  data._id = data.id;
123
131
  delete data.id;
@@ -145,11 +153,29 @@ function createMongoUsersStore(options = {}) {
145
153
  return users.map(stripMongoFields);
146
154
  },
147
155
 
156
+ async count({ role } = {}) {
157
+ await ready();
158
+ const query = role ? { role } : {};
159
+ return User.countDocuments(query);
160
+ },
161
+
162
+ async countByRole() {
163
+ await ready();
164
+ const rows = await User.aggregate([
165
+ { $group: { _id: { $ifNull: ['$role', 'unknown'] }, count: { $sum: 1 } } },
166
+ { $sort: { _id: 1 } }
167
+ ]);
168
+ return Object.fromEntries(rows.map((row) => [row._id, row.count]));
169
+ },
170
+
148
171
  async update(id, patch) {
149
172
  await ready();
150
173
  const data = { ...(patch || {}) };
151
174
  delete data.id;
152
175
  delete data._id;
176
+ if (data.email !== undefined) {
177
+ data.email = normalizeEmail(data.email);
178
+ }
153
179
 
154
180
  try {
155
181
  const user = await User.findByIdAndUpdate(id, { $set: data }, {
@@ -195,11 +221,12 @@ function createMongoSettingsStore(options = {}) {
195
221
 
196
222
  async set(key, value) {
197
223
  await ready();
198
- await Setting.findOneAndUpdate(
224
+ const setting = await Setting.findOneAndUpdate(
199
225
  { key },
200
226
  { $set: { key, value } },
201
227
  { upsert: true, new: true, setDefaultsOnInsert: true }
202
228
  ).lean();
229
+ return setting.value;
203
230
  },
204
231
 
205
232
  async getAll() {