@kiri-orm/core 0.1.4 → 0.1.5

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/package.json +1 -1
  2. package/src/schema/sync.js +21 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiri-orm/core",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./dist/types/index.d.ts",
@@ -98,7 +98,7 @@ export async function sync(db, modelRegistry, options = {}) {
98
98
  const existingColumns = await db(model.tableName).columnInfo();
99
99
  const missingFieldEntries = Object.entries(model.fields).filter(
100
100
  ([fieldName]) => !(model.columns[fieldName] in existingColumns)
101
- );
101
+ );
102
102
  const addedColumns = new Set(missingFieldEntries.map(([fieldName]) => model.columns[fieldName]));
103
103
  const wanted = foreignKeysFor(model);
104
104
 
@@ -128,6 +128,16 @@ export async function sync(db, modelRegistry, options = {}) {
128
128
  );
129
129
  }
130
130
  }
131
+
132
+ const removedFields = getRemovedFields(model.columns, existingColumns)
133
+ if(removedFields.length > 0) {
134
+ const preposition = removedFields.length === 1 ? 'has': 'have'
135
+ const field = removedFields.length === 1 ? 'Field': 'Fields'
136
+ result.warnings.push(
137
+ `${field} ${removedFields} ${preposition} been removed from code on model ${model.tableName}, this does not have any effect on database`+
138
+ `but it will cause unexpected issue especially for required fields!`
139
+ )
140
+ }
131
141
  }
132
142
 
133
143
  await ensurePivotTables(db, modelRegistry);
@@ -136,3 +146,13 @@ export async function sync(db, modelRegistry, options = {}) {
136
146
 
137
147
  return result;
138
148
  }
149
+
150
+
151
+ function getRemovedFields(currentModelColumns, existingModelColumns) {
152
+ const modelFields = new Set(Object.values(currentModelColumns));
153
+
154
+ const removedFields = Object.entries(existingModelColumns)
155
+ .filter(([fieldName]) => !modelFields.has(fieldName))
156
+ .map(([fieldName, fieldsDetails]) => (fieldName));
157
+ return removedFields;
158
+ }