forms-angular 0.12.0-beta.210 → 0.12.0-beta.211

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.
@@ -473,6 +473,7 @@ declare module fng {
473
473
  onSchemaFetch?: (description: string, source: IFieldViewInfo[]) => void;
474
474
  onSchemaProcessed?: (description: string, formSchema: IFormInstruction[]) => void;
475
475
  updateQueryForTab?: (tab: string) => void;
476
+ showLoading? : boolean;
476
477
  tabDeselect?: ($event: any, $selectedIndex: number) => void;
477
478
  setUpCustomLookupOptions?: (
478
479
  schemaElement: IFormInstruction,
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FormsAngular = void 0;
4
+ const mongoose_1 = require("mongoose");
4
5
  // This part of forms-angular borrows _very_ heavily from https://github.com/Alexandre-Strzelewicz/angular-bridge
5
6
  // (now https://github.com/Unitech/angular-bridge
6
7
  const _ = require('lodash');
@@ -851,7 +852,7 @@ class FormsAngular {
851
852
  else {
852
853
  const objectIdTest = /^([0-9a-fA-F]{24})$/.exec(obj[prop]);
853
854
  if (objectIdTest) {
854
- obj[prop] = new this.mongoose.Types.ObjectId(objectIdTest[1]);
855
+ obj[prop] = new mongoose_1.Types.ObjectId(objectIdTest[1]);
855
856
  }
856
857
  }
857
858
  }
@@ -1534,76 +1535,86 @@ class FormsAngular {
1534
1535
  }, this);
1535
1536
  }
1536
1537
  ;
1537
- entityDelete() {
1538
- let that = this;
1539
- return _.bind(async function (req, res, next) {
1540
- function generateDependencyList(resource) {
1541
- if (resource.options.dependents === undefined) {
1542
- resource.options.dependents = that.resources.reduce(function (acc, r) {
1543
- function searchPaths(schema, prefix) {
1544
- let fldList = [];
1545
- for (let fld in schema.paths) {
1546
- if (schema.paths.hasOwnProperty(fld)) {
1547
- const parts = fld.split('.');
1548
- let schemaType = schema.tree;
1549
- while (parts.length > 0) {
1550
- schemaType = schemaType[parts.shift()];
1551
- }
1552
- if (schemaType.type) {
1553
- if (schemaType.type.name === 'ObjectId' && schemaType.ref === resource.resourceName) {
1554
- fldList.push(prefix + fld);
1555
- }
1556
- else if (_.isArray(schemaType.type)) {
1557
- schemaType.type.forEach(function (t) {
1558
- searchPaths(t, prefix + fld + '.');
1559
- });
1560
- }
1561
- }
1562
- }
1538
+ generateDependencyList(resource) {
1539
+ if (resource.options.dependents === undefined) {
1540
+ let that = this;
1541
+ resource.options.dependents = that.resources.reduce(function (acc, r) {
1542
+ function searchPaths(schema, prefix) {
1543
+ let fldList = [];
1544
+ for (let fld in schema.paths) {
1545
+ if (schema.paths.hasOwnProperty(fld)) {
1546
+ const parts = fld.split('.');
1547
+ let schemaType = schema.tree;
1548
+ while (parts.length > 0) {
1549
+ schemaType = schemaType[parts.shift()];
1563
1550
  }
1564
- if (fldList.length > 0) {
1565
- acc.push({ resource: r, keys: fldList });
1551
+ if (schemaType.type) {
1552
+ if (schemaType.type.name === 'ObjectId' && schemaType.ref === resource.resourceName) {
1553
+ fldList.push(prefix + fld);
1554
+ }
1555
+ else if (_.isArray(schemaType.type)) {
1556
+ schemaType.type.forEach(function (t) {
1557
+ searchPaths(t, prefix + fld + '.');
1558
+ });
1559
+ }
1566
1560
  }
1567
1561
  }
1568
- searchPaths(r.model.schema, '');
1569
- return acc;
1570
- }, []);
1571
- for (let pluginName in that.options.plugins) {
1572
- let thisPlugin = that.options.plugins[pluginName];
1573
- if (thisPlugin.dependencyChecks && thisPlugin.dependencyChecks[resource.resourceName]) {
1574
- resource.options.dependents = resource.options.dependents.concat(thisPlugin.dependencyChecks[resource.resourceName]);
1575
- }
1576
1562
  }
1563
+ if (fldList.length > 0) {
1564
+ acc.push({ resource: r, keys: fldList });
1565
+ }
1566
+ }
1567
+ searchPaths(r.model.schema, '');
1568
+ return acc;
1569
+ }, []);
1570
+ for (let pluginName in that.options.plugins) {
1571
+ let thisPlugin = that.options.plugins[pluginName];
1572
+ if (thisPlugin.dependencyChecks && thisPlugin.dependencyChecks[resource.resourceName]) {
1573
+ resource.options.dependents = resource.options.dependents.concat(thisPlugin.dependencyChecks[resource.resourceName]);
1577
1574
  }
1578
1575
  }
1576
+ }
1577
+ }
1578
+ async getDependencies(resource, id) {
1579
+ this.generateDependencyList(resource);
1580
+ let promises = [];
1581
+ let foreignKeyList = [];
1582
+ resource.options.dependents.forEach(collection => {
1583
+ collection.keys.forEach(key => {
1584
+ promises.push({
1585
+ p: collection.resource.model.find({ [key]: id }).limit(1).exec(),
1586
+ collection,
1587
+ key
1588
+ });
1589
+ });
1590
+ });
1591
+ return Promise.all(promises.map(p => p.p))
1592
+ .then((results) => {
1593
+ results.forEach((r, i) => {
1594
+ if (r.length > 0) {
1595
+ foreignKeyList.push({ resourceName: promises[i].collection.resource.resourceName, key: promises[i].key, id: r[0]._id });
1596
+ }
1597
+ });
1598
+ return foreignKeyList;
1599
+ });
1600
+ }
1601
+ entityDelete() {
1602
+ let that = this;
1603
+ return _.bind(async function (req, res, next) {
1579
1604
  async function removeDoc(doc, resource) {
1580
1605
  switch (resource.options.handleRemove) {
1581
1606
  case 'allow':
1582
1607
  // old behaviour - no attempt to maintain data integrity
1583
1608
  return doc.remove();
1584
1609
  case 'cascade':
1585
- generateDependencyList(resource);
1586
1610
  res.status(400).send('"cascade" option not yet supported');
1587
1611
  break;
1588
1612
  default:
1589
- generateDependencyList(resource);
1590
- let promises = [];
1591
- resource.options.dependents.forEach(collection => {
1592
- collection.keys.forEach(key => {
1593
- promises.push({
1594
- p: collection.resource.model.find({ [key]: doc._id }).limit(1).exec(),
1595
- collection,
1596
- key
1597
- });
1598
- });
1599
- });
1600
- return Promise.all(promises.map(p => p.p))
1601
- .then((results) => {
1602
- results.forEach((r, i) => {
1603
- if (r.length > 0) {
1604
- throw new ForeignKeyError(resource.resourceName, promises[i].collection.resource.resourceName, promises[i].key, r[0]._id);
1605
- }
1606
- });
1613
+ return that.getDependencies(resource, doc._id)
1614
+ .then((dependencies) => {
1615
+ if (dependencies.length > 0) {
1616
+ throw new ForeignKeyError(resource.resourceName, dependencies);
1617
+ }
1607
1618
  return doc.remove();
1608
1619
  });
1609
1620
  }
@@ -1836,8 +1847,8 @@ class FormsAngular {
1836
1847
  }
1837
1848
  exports.FormsAngular = FormsAngular;
1838
1849
  class ForeignKeyError extends global.Error {
1839
- constructor(resourceName, foreignKeyOnResource, foreignItem, id) {
1840
- super(`Cannot delete this ${resourceName}, as it is the ${foreignItem} on ${foreignKeyOnResource} ${id}`);
1850
+ constructor(resourceName, foreignKeys) {
1851
+ super(`Cannot delete this ${resourceName}, as it is: ${foreignKeys.map(d => ` the ${d.key} on ${d.resourceName} ${d.id}`).join("; ")}`);
1841
1852
  this.name = "ForeignKeyError";
1842
1853
  this.stack = new global.Error('').stack;
1843
1854
  }
@@ -1,4 +1,4 @@
1
- import {Error, Model} from "mongoose";
1
+ import { Error, Model, Types } from "mongoose";
2
2
  import {Express} from "express";
3
3
 
4
4
  declare module fngServer {
@@ -43,6 +43,9 @@ declare module fngServer {
43
43
 
44
44
  export type DependencyList = Dependency[];
45
45
 
46
+ export type ForeignKeys = { resourceName: string, key: string, id: Types.ObjectId};
47
+ export type ForeignKeyList = ForeignKeys[];
48
+
46
49
  export interface ResourceOptions {
47
50
  suppressDeprecatedMessage?: boolean;
48
51
  onRemove?: (doc, req, cb) => void;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Mark Chapman <support@forms-angular.org>",
4
4
  "description": "A form builder that sits on top of Angular.js, Twitter Bootstrap, jQuery UI, Angular-UI, Express and Mongoose. Opinionated or what?",
5
5
  "homepage": "http://forms-angular.org",
6
- "version": "0.12.0-beta.210",
6
+ "version": "0.12.0-beta.211",
7
7
  "engines": {
8
8
  "node": ">=8.x",
9
9
  "npm": ">=5.x"