@stoker-platform/cli 0.5.139 → 0.5.141

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/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stoker-platform/cli",
3
- "version": "0.5.138",
3
+ "version": "0.5.140",
4
4
  "type": "module",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "main": "./lib/src/main.js",
@@ -24,9 +24,9 @@
24
24
  "@google-cloud/secret-manager": "^6.1.2",
25
25
  "@google-cloud/storage": "^7.19.0",
26
26
  "@inquirer/prompts": "^8.5.2",
27
- "@stoker-platform/node-client": "0.5.87",
27
+ "@stoker-platform/node-client": "0.5.88",
28
28
  "@stoker-platform/types": "0.5.65",
29
- "@stoker-platform/utils": "0.5.78",
29
+ "@stoker-platform/utils": "0.5.79",
30
30
  "algoliasearch": "^5.53.0",
31
31
  "commander": "^15.0.0",
32
32
  "cross-spawn": "^7.0.6",
@@ -1,8 +1,10 @@
1
1
  import { deleteField } from "./operations/deleteField.js";
2
+ import { replayProjections } from "./operations/replayProjections.js";
2
3
  export const migrateFirestore = async (currentSchema, lastSchema) => {
3
4
  console.log("Migrating Firestore...");
4
5
  if (lastSchema) {
5
6
  await deleteField(currentSchema, lastSchema);
7
+ await replayProjections(currentSchema, lastSchema);
6
8
  }
7
9
  return;
8
10
  };
@@ -46,7 +46,7 @@ export const deleteField = async (currentSchema, lastSchema) => {
46
46
  .doc(tenantId)
47
47
  .collection("system_migration")
48
48
  .doc(currentSchema.version.toString())
49
- .collection(collection)
49
+ .collection(`Migration-${collection}`)
50
50
  .doc(doc.id), {
51
51
  [fieldName]: doc.get(fieldName),
52
52
  }, { merge: true });
@@ -0,0 +1,160 @@
1
+ import { getFirestorePathRef, getStokerFirestore } from "@stoker-platform/node-client";
2
+ import { addDenormalized, getAllRoleGroups, getDependencyIndexFields, getFieldAccessGroupFields, getFieldAccessGroupIndexFields, getFieldAccessGroupKey, getRoleGroups, isDependencyField, } from "@stoker-platform/utils";
3
+ import { FieldValue } from "firebase-admin/firestore";
4
+ import isEqual from "lodash/isEqual.js";
5
+ const getUniqueFieldNames = (collectionSchema) => collectionSchema.fields
6
+ .filter((field) => "unique" in field && field.unique)
7
+ .map((field) => field.name)
8
+ .sort();
9
+ const getDependencyIndexShape = (collectionSchema, schema) => {
10
+ const shape = {};
11
+ for (const field of collectionSchema.fields) {
12
+ if (isDependencyField(field, collectionSchema, schema)) {
13
+ shape[field.name] = getDependencyIndexFields(field, collectionSchema, schema)
14
+ .map((indexField) => indexField.name)
15
+ .sort();
16
+ }
17
+ }
18
+ return shape;
19
+ };
20
+ const getRoleGroupShape = (collectionSchema, schema) => Array.from(getRoleGroups(collectionSchema, schema))
21
+ .map((group) => ({
22
+ key: group.key,
23
+ roles: [...group.roles].sort(),
24
+ fields: group.fields.map((field) => field.name).sort(),
25
+ }))
26
+ .sort((a, b) => a.key.localeCompare(b.key));
27
+ const getFieldAccessGroupFieldShape = (collectionSchema) => {
28
+ const shape = {};
29
+ for (const [groupKey, groupFields] of Object.entries(getFieldAccessGroupFields(collectionSchema))) {
30
+ // eslint-disable-next-line security/detect-object-injection
31
+ shape[groupKey] = groupFields.map((field) => field.name).sort();
32
+ }
33
+ return shape;
34
+ };
35
+ const getProjectionKeys = (collectionSchema, schema) => {
36
+ const keys = new Set();
37
+ const roleGroups = getRoleGroups(collectionSchema, schema);
38
+ for (const group of roleGroups) {
39
+ keys.add(group.key);
40
+ }
41
+ const fieldAccessGroups = getFieldAccessGroupFields(collectionSchema);
42
+ for (const [groupKey, groupFields] of Object.entries(fieldAccessGroups)) {
43
+ if (groupFields.length === 0)
44
+ continue;
45
+ for (const group of roleGroups) {
46
+ const indexFields = getFieldAccessGroupIndexFields(groupKey, collectionSchema, group);
47
+ if (indexFields.length === 0)
48
+ continue;
49
+ const overlayKey = getFieldAccessGroupKey(groupKey, group.key);
50
+ keys.add(overlayKey);
51
+ }
52
+ }
53
+ for (const field of collectionSchema.fields) {
54
+ if (isDependencyField(field, collectionSchema, schema)) {
55
+ keys.add(field.name);
56
+ }
57
+ }
58
+ return keys;
59
+ };
60
+ const replayProjectionsForCollection = async (collection, currentSchema, lastSchema) => {
61
+ console.log(`Projections for collection ${collection} have changed. Replaying...`);
62
+ const db = getStokerFirestore();
63
+ const bulkWriter = db.bulkWriter();
64
+ bulkWriter.onWriteError((error) => {
65
+ console.log(error);
66
+ return true;
67
+ });
68
+ // eslint-disable-next-line security/detect-object-injection
69
+ const lastCollectionSchema = lastSchema.collections[collection];
70
+ // eslint-disable-next-line security/detect-object-injection
71
+ const currentCollectionSchema = currentSchema.collections[collection];
72
+ const currentRoleGroups = getAllRoleGroups(currentSchema);
73
+ const projectionKeys = new Set([
74
+ ...getProjectionKeys(lastCollectionSchema, lastSchema),
75
+ ...getProjectionKeys(currentCollectionSchema, currentSchema),
76
+ ]);
77
+ const uniqueFieldNames = new Set([
78
+ ...getUniqueFieldNames(lastCollectionSchema),
79
+ ...getUniqueFieldNames(currentCollectionSchema),
80
+ ]);
81
+ const tenants = await db.collection("tenants").listDocuments();
82
+ for (const tenant of tenants) {
83
+ for (const key of projectionKeys) {
84
+ await db.recursiveDelete(tenant.collection("system_fields").doc(collection).collection(`${collection}-${key}`));
85
+ }
86
+ for (const fieldName of uniqueFieldNames) {
87
+ await db.recursiveDelete(tenant.collection("system_unique").doc(collection).collection(`Unique-${collection}-${fieldName}`));
88
+ }
89
+ }
90
+ const querySnapshot = await db.collectionGroup(collection).get();
91
+ for (const doc of querySnapshot.docs) {
92
+ const tenantId = doc.ref.path.split("/")[1];
93
+ const record = { id: doc.id, ...doc.data() };
94
+ const path = record.Collection_Path;
95
+ if (!path) {
96
+ continue;
97
+ }
98
+ const dependencyRef = (field) => db
99
+ .collection("tenants")
100
+ .doc(tenantId)
101
+ .collection("system_fields")
102
+ .doc(collection)
103
+ .collection(`${collection}-${field.name}`)
104
+ .doc(doc.id);
105
+ const uniqueRef = (field, uniqueValue) => db
106
+ .collection("tenants")
107
+ .doc(tenantId)
108
+ .collection("system_unique")
109
+ .doc(collection)
110
+ .collection(`Unique-${collection}-${field.name}`)
111
+ .doc(uniqueValue);
112
+ const privateRef = (role) => db
113
+ .collection("tenants")
114
+ .doc(tenantId)
115
+ .collection("system_fields")
116
+ .doc(collection)
117
+ .collection(`${collection}-${role}`)
118
+ .doc(doc.id);
119
+ const twoWayIncludeRef = (relationPath, id) => {
120
+ const ref = getFirestorePathRef(db, relationPath, tenantId);
121
+ return ref.doc(id);
122
+ };
123
+ const twoWayDependencyRef = (field, dependencyField, id) => db
124
+ .collection("tenants")
125
+ .doc(tenantId)
126
+ .collection("system_fields")
127
+ .doc(field.collection)
128
+ .collection(`${field.collection}-${dependencyField}`)
129
+ .doc(id);
130
+ const twoWayPrivateRef = (field, role, id) => db
131
+ .collection("tenants")
132
+ .doc(tenantId)
133
+ .collection("system_fields")
134
+ .doc(field.collection)
135
+ .collection(`${field.collection}-${role.replaceAll(" ", "-")}`)
136
+ .doc(id);
137
+ addDenormalized("create", bulkWriter, path, doc.id, record, currentSchema, currentCollectionSchema, { noTwoWay: true }, currentRoleGroups, FieldValue.arrayUnion, FieldValue.arrayRemove, FieldValue.delete, dependencyRef, uniqueRef, privateRef, twoWayIncludeRef, twoWayDependencyRef, twoWayPrivateRef);
138
+ }
139
+ await bulkWriter.close();
140
+ };
141
+ export const replayProjections = async (currentSchema, lastSchema) => {
142
+ const currentSchemaKeys = Object.keys(currentSchema.collections);
143
+ for (const collection of currentSchemaKeys) {
144
+ // eslint-disable-next-line security/detect-object-injection
145
+ if (!lastSchema.collections[collection])
146
+ continue;
147
+ // eslint-disable-next-line security/detect-object-injection
148
+ const lastCollectionSchema = lastSchema.collections[collection];
149
+ // eslint-disable-next-line security/detect-object-injection
150
+ const currentCollectionSchema = currentSchema.collections[collection];
151
+ const roleGroupsChanged = !isEqual(getRoleGroupShape(lastCollectionSchema, lastSchema), getRoleGroupShape(currentCollectionSchema, currentSchema));
152
+ const fieldAccessGroupsChanged = !isEqual(lastCollectionSchema.fieldAccessGroups, currentCollectionSchema.fieldAccessGroups) ||
153
+ !isEqual(getFieldAccessGroupFieldShape(lastCollectionSchema), getFieldAccessGroupFieldShape(currentCollectionSchema));
154
+ const uniqueFieldsChanged = !isEqual(getUniqueFieldNames(lastCollectionSchema), getUniqueFieldNames(currentCollectionSchema));
155
+ const dependenciesChanged = !isEqual(getDependencyIndexShape(lastCollectionSchema, lastSchema), getDependencyIndexShape(currentCollectionSchema, currentSchema));
156
+ if (roleGroupsChanged || fieldAccessGroupsChanged || uniqueFieldsChanged || dependenciesChanged) {
157
+ await replayProjectionsForCollection(collection, currentSchema, lastSchema);
158
+ }
159
+ }
160
+ };
@@ -6,7 +6,7 @@ import { migrateFirestore } from "./firestore/migrateFirestore.js";
6
6
  import { generateSchema } from "../deploy/schema/generateSchema.js";
7
7
  export const migrateAll = async () => {
8
8
  await initializeFirebase();
9
- const currentSchema = await generateSchema();
9
+ const currentSchema = await generateSchema(true);
10
10
  const lastSchema = await fetchLastSchema();
11
11
  console.log("Migration started...");
12
12
  if (isNaN(currentSchema.version)) {
@@ -1 +1 @@
1
- {"root":["../src/main.ts","../src/data/exporttobigquery.ts","../src/data/seeddata.ts","../src/deploy/deployproject.ts","../src/deploy/cloud-functions/getfunctionsdata.ts","../src/deploy/firestore-export/exportfirestoredata.ts","../src/deploy/firestore-ttl/deployttls.ts","../src/deploy/live-update/liveupdate.ts","../src/deploy/maintenance/activatemaintenancemode.ts","../src/deploy/maintenance/disablemaintenancemode.ts","../src/deploy/maintenance/setdeploymentstatus.ts","../src/deploy/rules-indexes/generatefirestoreindexes.ts","../src/deploy/rules-indexes/generatefirestorerules.ts","../src/deploy/rules-indexes/generatestoragerules.ts","../src/deploy/schema/applyschema.ts","../src/deploy/schema/generateschema.ts","../src/deploy/schema/persistschema.ts","../src/deploy/schema/updateliveschema.ts","../src/lint/lintschema.ts","../src/lint/securityreport.ts","../src/migration/migrateall.ts","../src/migration/firestore/migratefirestore.ts","../src/migration/firestore/operations/deletefield.ts","../src/ops/auditdenormalized.ts","../src/ops/auditpermissions.ts","../src/ops/auditrelations.ts","../src/ops/explainpreloadqueries.ts","../src/ops/getuser.ts","../src/ops/getuserpermissions.ts","../src/ops/getuserrecord.ts","../src/ops/listprojects.ts","../src/ops/mfastatus.ts","../src/ops/setusercollection.ts","../src/ops/setuserdocument.ts","../src/ops/setuserrole.ts","../src/project/addproject.ts","../src/project/addrecord.ts","../src/project/addrecordprompt.ts","../src/project/addtenant.ts","../src/project/buildwebapp.ts","../src/project/customdomain.ts","../src/project/deleteproject.ts","../src/project/deleterecord.ts","../src/project/deletetenant.ts","../src/project/getone.ts","../src/project/getsome.ts","../src/project/initproject.ts","../src/project/prepareemulatordata.ts","../src/project/setproject.ts","../src/project/startemulators.ts","../src/project/updaterecord.ts","../src/types/generatetypes.ts"],"version":"6.0.3"}
1
+ {"root":["../src/main.ts","../src/data/exporttobigquery.ts","../src/data/seeddata.ts","../src/deploy/deployproject.ts","../src/deploy/cloud-functions/getfunctionsdata.ts","../src/deploy/firestore-export/exportfirestoredata.ts","../src/deploy/firestore-ttl/deployttls.ts","../src/deploy/live-update/liveupdate.ts","../src/deploy/maintenance/activatemaintenancemode.ts","../src/deploy/maintenance/disablemaintenancemode.ts","../src/deploy/maintenance/setdeploymentstatus.ts","../src/deploy/rules-indexes/generatefirestoreindexes.ts","../src/deploy/rules-indexes/generatefirestorerules.ts","../src/deploy/rules-indexes/generatestoragerules.ts","../src/deploy/schema/applyschema.ts","../src/deploy/schema/generateschema.ts","../src/deploy/schema/persistschema.ts","../src/deploy/schema/updateliveschema.ts","../src/lint/lintschema.ts","../src/lint/securityreport.ts","../src/migration/migrateall.ts","../src/migration/firestore/migratefirestore.ts","../src/migration/firestore/operations/deletefield.ts","../src/migration/firestore/operations/replayprojections.ts","../src/ops/auditdenormalized.ts","../src/ops/auditpermissions.ts","../src/ops/auditrelations.ts","../src/ops/explainpreloadqueries.ts","../src/ops/getuser.ts","../src/ops/getuserpermissions.ts","../src/ops/getuserrecord.ts","../src/ops/listprojects.ts","../src/ops/mfastatus.ts","../src/ops/setusercollection.ts","../src/ops/setuserdocument.ts","../src/ops/setuserrole.ts","../src/project/addproject.ts","../src/project/addrecord.ts","../src/project/addrecordprompt.ts","../src/project/addtenant.ts","../src/project/buildwebapp.ts","../src/project/customdomain.ts","../src/project/deleteproject.ts","../src/project/deleterecord.ts","../src/project/deletetenant.ts","../src/project/getone.ts","../src/project/getsome.ts","../src/project/initproject.ts","../src/project/prepareemulatordata.ts","../src/project/setproject.ts","../src/project/startemulators.ts","../src/project/updaterecord.ts","../src/types/generatetypes.ts"],"version":"6.0.3"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stoker-platform/cli",
3
- "version": "0.5.139",
3
+ "version": "0.5.141",
4
4
  "type": "module",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "main": "./lib/src/main.js",