@aeriajs/cli 0.0.170 → 0.0.171

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.
Files changed (2) hide show
  1. package/dist/migrate.js +39 -11
  2. package/package.json +6 -6
package/dist/migrate.js CHANGED
@@ -50,8 +50,14 @@ export const migrate = async () => {
50
50
  ? candidate()
51
51
  : candidate;
52
52
  await createCollection(collectionName);
53
+ const model = getDatabaseCollection(collectionName);
54
+ const collIndexes = await model.indexes();
53
55
  const { description } = collection;
54
56
  const refMap = await getReferences(description.properties);
57
+ const newIndexes = {
58
+ unique: new Map(),
59
+ temporary: new Map(),
60
+ };
55
61
  indexMap = recurseReferences(refMap, indexMap);
56
62
  if (description.search) {
57
63
  const set = indexMap[collectionName] ??= new Set();
@@ -61,21 +67,43 @@ export const migrate = async () => {
61
67
  const set = indexMap[collectionName] ??= new Set();
62
68
  description.indexes.forEach((index) => set.add(String(index)));
63
69
  }
70
+ if (description.unique) {
71
+ for (const propertyName of description.unique) {
72
+ newIndexes.unique.set(propertyName, true);
73
+ }
74
+ }
64
75
  if (description.temporary) {
65
- const model = getDatabaseCollection(collectionName);
66
- const collIndexes = await model.indexes();
67
76
  const { index: temporaryIndex, expireAfterSeconds } = description.temporary;
68
- const hasIndex = collIndexes.some((index) => {
69
- return temporaryIndex in index.key && 'expireAfterSeconds' in index;
77
+ newIndexes.temporary.set(temporaryIndex, expireAfterSeconds);
78
+ }
79
+ for (const propertyName of [
80
+ ...newIndexes.temporary.keys(),
81
+ ...newIndexes.unique.keys(),
82
+ ]) {
83
+ console.log({
84
+ propertyName,
85
+ collectionName,
70
86
  });
71
- if (!hasIndex) {
72
- await model.createIndex({
73
- [temporaryIndex]: 1,
74
- }, {
75
- expireAfterSeconds,
76
- });
77
- log('info', `temporary index created for ${collectionName}`);
87
+ const uniqueIndex = newIndexes.unique.get(propertyName);
88
+ const temporaryIndex = newIndexes.temporary.get(propertyName);
89
+ const existingIndex = collIndexes.find((index) => propertyName in index.key);
90
+ if (existingIndex?.name) {
91
+ if ((uniqueIndex === undefined || existingIndex.unique) && (temporaryIndex === undefined || existingIndex.expireAfterSeconds === temporaryIndex)) {
92
+ continue;
93
+ }
94
+ await model.dropIndex(existingIndex.name);
95
+ }
96
+ const indexOptions = {};
97
+ if (uniqueIndex) {
98
+ indexOptions.unique = true;
99
+ }
100
+ if (typeof temporaryIndex === 'number') {
101
+ indexOptions.expireAfterSeconds = temporaryIndex;
78
102
  }
103
+ await model.createIndex({
104
+ [propertyName]: 1,
105
+ }, indexOptions);
106
+ log('info', `new index created for ${collectionName}.${propertyName}`);
79
107
  }
80
108
  }
81
109
  for (const [collectionName, indexesSet] of Object.entries(indexMap)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aeriajs/cli",
3
3
  "type": "module",
4
- "version": "0.0.170",
4
+ "version": "0.0.171",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -39,11 +39,11 @@
39
39
  "@aeriajs/types": "link:../types"
40
40
  },
41
41
  "peerDependencies": {
42
- "@aeriajs/builtins": "^0.0.222",
43
- "@aeriajs/common": "^0.0.128",
44
- "@aeriajs/core": "^0.0.222",
45
- "@aeriajs/entrypoint": "^0.0.131",
46
- "@aeriajs/types": "^0.0.110"
42
+ "@aeriajs/builtins": "^0.0.223",
43
+ "@aeriajs/common": "^0.0.129",
44
+ "@aeriajs/core": "^0.0.223",
45
+ "@aeriajs/entrypoint": "^0.0.132",
46
+ "@aeriajs/types": "^0.0.111"
47
47
  },
48
48
  "optionalDependencies": {
49
49
  "aeria-lang": "0.0.11"