@anteros/core 0.0.21 → 0.0.23

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.
@@ -64,13 +64,25 @@ async function initializeOnDatabase(collections: Collection[]) {
64
64
  await db.createCollection(collection.slug)
65
65
  }
66
66
 
67
- // create default index on collection
67
+ // Fetch existing indexes once — used to skip already-created
68
+ // indexes: never force re-creation of an index that already
69
+ // exists (avoids the "same name but different options" error)
70
+ const col = db.collection(collection.slug);
71
+ const existingIndexes = await col.listIndexes().toArray();
72
+ const existingNames = new Set(existingIndexes.map((idx) => idx.name));
73
+ const existingKeys = new Set(
74
+ existingIndexes.map((idx) => JSON.stringify(Object.keys(idx.key ?? {}).sort()))
75
+ );
76
+
77
+ // create default index on collection (skip if it already exists)
68
78
  let specsTimeStamps = {
69
79
  createdAt: -1,
70
80
  updatedAt: -1
71
81
  }
72
- await db.collection(collection.slug).createIndex(specsTimeStamps)
73
- // console.log('index created on collection', collection.slug, index)
82
+ const defaultIndexName = 'createdAt_-1_updatedAt_-1';
83
+ if (!existingNames.has(defaultIndexName)) {
84
+ await col.createIndex(specsTimeStamps)
85
+ }
74
86
 
75
87
 
76
88
 
@@ -120,8 +132,15 @@ async function initializeOnDatabase(collections: Collection[]) {
120
132
 
121
133
 
122
134
  }
123
- if (specsFieldsIndexes.length) {
124
- await db.collection(collection.slug).createIndexes(specsFieldsIndexes)
135
+
136
+ // Only create indexes that don't already exist on the database
137
+ // (match by name OR by key pattern — never force re-creation)
138
+ const missingIndexes = specsFieldsIndexes.filter((spec) => {
139
+ const keySignature = JSON.stringify(Object.keys(spec.key ?? {}).sort());
140
+ return !existingNames.has(spec.name!) && !existingKeys.has(keySignature);
141
+ });
142
+ if (missingIndexes.length) {
143
+ await col.createIndexes(missingIndexes)
125
144
  }
126
145
 
127
146
  // Purge orphan indexes (fields removed from schema)
@@ -129,12 +148,11 @@ async function initializeOnDatabase(collections: Collection[]) {
129
148
  const expectedKeys = new Set(specsFieldsIndexes.map((idx) =>
130
149
  JSON.stringify(Object.keys(idx.key!).sort())
131
150
  ));
132
- const existingIndexes = await db.collection(collection.slug).listIndexes().toArray();
133
151
  for (const idx of existingIndexes) {
134
152
  if (idx.name === '_id_') continue; // built-in
135
153
  const idxKeys = JSON.stringify(Object.keys(idx.key!).sort());
136
154
  if (!expectedKeys.has(idxKeys)) {
137
- await db.collection(collection.slug).dropIndex(idx.name!);
155
+ await col.dropIndex(idx.name!);
138
156
  console.log(`🧹 Dropped orphan index: ${collection.slug}.${idx.name}`.gray);
139
157
  }
140
158
  }
@@ -142,7 +160,7 @@ async function initializeOnDatabase(collections: Collection[]) {
142
160
  }
143
161
 
144
162
  } catch (err: any) {
145
- console.error(err?.message)
163
+ console.error(`col: ${collection?.slug||''}`,err?.message)
146
164
 
147
165
  }
148
166
  }
package/database/file.ts CHANGED
@@ -4,6 +4,7 @@ import path from "path"
4
4
  import fs from "fs/promises"
5
5
  import type { FileCollection } from "../types/file"
6
6
  import { getTenant } from "./tenant"
7
+ import type { IndexDescription } from "mongodb"
7
8
 
8
9
  async function syncFileCollections() {
9
10
  try {
@@ -80,13 +81,24 @@ async function initializeFileCollectionsOnDatabase(files: FileCollection[]) {
80
81
  await db.createCollection(fileCollection.slug)
81
82
  }
82
83
 
83
- // Créer les indexes par défaut
84
- await db.collection(fileCollection.slug).createIndexes([
84
+ // Créer les indexes par défaut (uniquement ceux qui manquent —
85
+ // ne jamais forcer la recréation d'un index déjà existant)
86
+ const col = db.collection(fileCollection.slug)
87
+ const existingIndexes = await col.listIndexes().toArray()
88
+ const existingNames = new Set(existingIndexes.map((idx) => idx.name))
89
+ const defaultIndexes: IndexDescription[] = [
85
90
  { key: { filename: 1 } },
86
91
  { key: { name: 1 } },
87
92
  { key: { mimetype: 1 } },
88
93
  { key: { createdAt: -1, updatedAt: -1 } },
89
- ])
94
+ ]
95
+ const missingIndexes = defaultIndexes.filter((spec) => {
96
+ const autoName = Object.entries(spec.key).map(([k, v]) => `${k}_${v}`).join('_')
97
+ return !existingNames.has(autoName)
98
+ })
99
+ if (missingIndexes.length) {
100
+ await col.createIndexes(missingIndexes)
101
+ }
90
102
 
91
103
  } catch (err: any) {
92
104
  console.error(`Initialize file collection '${fileCollection.slug}' failed`, err?.message)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@anteros/core",
3
3
  "module": "index.ts",
4
- "version": "0.0.21",
4
+ "version": "0.0.23",
5
5
  "type": "module",
6
6
  "description": "Anteros Core",
7
7
  "private": false,