not-options 0.2.2 → 0.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-options",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "not-* family options model in not- environment",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  import CRUDGenericActionCreate from "not-bulma/src/frame/crud/actions/generic/create";
2
2
  import UIImportFromJSON from "./import.from.json.svelte";
3
- import { MODULE_NAME,DATA_MODEL_NAME } from "./../../../../const.js";
3
+ import { MODULE_NAME, DATA_MODEL_NAME } from "./../../../../const.js";
4
4
  import notCommon from "not-bulma/src/frame/common";
5
5
 
6
6
  const DEFAULT_BREADCRUMB_TAIL = `${MODULE_NAME}:action_import_from_json_title`;
@@ -115,9 +115,9 @@ class ncaImportFromJSON extends CRUDGenericActionCreate {
115
115
  try {
116
116
  ncaImportFromJSON.setUILoading(controller);
117
117
  const res = await controller
118
- .getModel(DATA_MODEL_NAME, {
119
- moduleName: controller.MODULE_NAME,
120
- options: jsonAsText
118
+ .getModel(DATA_MODEL_NAME, {
119
+ moduleName: controller.getModuleName(),
120
+ options: jsonAsText,
121
121
  })
122
122
  [`$${ncaImportFromJSON.MODEL_ACTION_PUT}`]();
123
123
  if (this.isResponseBad(res)) {
@@ -1,193 +1,201 @@
1
- const log = require('not-log')(module, 'Options Model');
1
+ const log = require("not-log")(module, "Options Model");
2
2
  try {
3
- const MODEL_NAME = 'Options';
4
- const Increment = require('not-node').Increment;
5
- const notError = require('not-error').notError;
3
+ const MODEL_NAME = "Options";
4
+ const Increment = require("not-node").Increment;
5
+ const notError = require("not-error").notError;
6
6
 
7
- const FIELDS = [
8
- 'id',
9
- 'value',
10
- [
11
- 'active',
12
- {
13
- default: true
14
- }
15
- ],
16
- 'createdAt',
17
- 'updatedAt'
18
- ];
7
+ const FIELDS = [
8
+ "id",
9
+ "value",
10
+ [
11
+ "active",
12
+ {
13
+ default: true,
14
+ },
15
+ ],
16
+ "createdAt",
17
+ "updatedAt",
18
+ ];
19
19
 
20
- exports.FIELDS = FIELDS;
20
+ exports.FIELDS = FIELDS;
21
21
 
22
- exports.keepNotExtended = false;
23
- exports.thisModelName = MODEL_NAME;
22
+ exports.keepNotExtended = false;
23
+ exports.thisModelName = MODEL_NAME;
24
24
 
25
- exports.enrich = {
26
- versioning: true,
27
- increment: true,
28
- validators: true
29
- };
25
+ exports.enrich = {
26
+ versioning: true,
27
+ increment: true,
28
+ validators: true,
29
+ };
30
30
 
31
- const metaExtend = require('not-meta').extend;
32
- const metaModel = require('not-meta').Model;
31
+ const metaExtend = require("not-meta").extend;
32
+ const metaModel = require("not-meta").Model;
33
33
 
34
- const ActionList = ['search'];
35
- const MODULE_OPTIONS_PREFIX = 'MODULE_';
34
+ const ActionList = ["search"];
35
+ const MODULE_OPTIONS_PREFIX = "MODULE_";
36
36
 
37
- exports.thisStatics = {
38
- getModuleOptionsName(moduleName) {
39
- return MODULE_OPTIONS_PREFIX + moduleName.toLowerCase();
40
- },
41
- readModuleOptions(moduleName) {
42
- let name = this.getModuleOptionsName(moduleName);
43
- return this.findOne({
44
- 'id': name,
45
- 'active': true,
46
- __latest: true,
47
- __closed: false
48
- }).exec()
49
- .then((result) => {
50
- if (result) {
51
- try {
52
- return JSON.parse(result.value);
53
- } catch (e) {
54
- log.error(e);
55
- return {};
37
+ exports.thisStatics = {
38
+ getModuleOptionsName(moduleName) {
39
+ return MODULE_OPTIONS_PREFIX + moduleName.toLowerCase();
40
+ },
41
+ readModuleOptions(moduleName) {
42
+ let name = this.getModuleOptionsName(moduleName);
43
+ return this.findOne({
44
+ id: name,
45
+ active: true,
46
+ __latest: true,
47
+ __closed: false,
48
+ })
49
+ .exec()
50
+ .then((result) => {
51
+ if (result) {
52
+ try {
53
+ return JSON.parse(result.value);
54
+ } catch (e) {
55
+ log.error(e);
56
+ return {};
57
+ }
58
+ } else {
59
+ return {};
60
+ }
61
+ });
62
+ },
63
+ async writeModuleOptions(moduleName, options) {
64
+ let name = this.getModuleOptionsName(moduleName);
65
+ let value =
66
+ typeof options === "string" ? options : JSON.stringify(options);
67
+ let exists = await this.countWithFilter({
68
+ id: name,
69
+ __latest: true,
70
+ __closed: false,
71
+ });
72
+ if (exists) {
73
+ await this.updateOne(
74
+ {
75
+ id: name,
76
+ __latest: true,
77
+ __closed: false,
78
+ },
79
+ {
80
+ value,
81
+ updatedAt: Date.now(),
82
+ }
83
+ ).exec();
84
+ let item = await this.findOne({
85
+ id: name,
86
+ __latest: true,
87
+ __closed: false,
88
+ }).exec();
89
+ if (typeof item !== "undefined" && item !== null) {
90
+ return this.saveVersion(item._id);
91
+ } else {
92
+ throw new notError(
93
+ "-options for module version not saved, empty response",
94
+ {
95
+ id: name,
96
+ item,
97
+ }
98
+ );
99
+ }
100
+ } else {
101
+ return this.add({
102
+ id: name,
103
+ value,
104
+ active: true,
105
+ updatedAt: Date.now(),
106
+ });
56
107
  }
57
- } else {
58
- return {};
59
- }
60
- });
61
- },
62
- async writeModuleOptions(moduleName, options) {
63
- let name = this.getModuleOptionsName(moduleName);
64
- let value = JSON.stringify(options);
65
- let exists = await this.countWithFilter({
66
- id: name,
67
- __latest: true,
68
- __closed: false
69
- });
70
- if (exists) {
71
- await this.updateOne({
72
- id: name,
73
- __latest: true,
74
- __closed: false
75
- }, {
76
- value,
77
- updatedAt: Date.now()
78
- }).exec();
79
- let item = await this.findOne({
80
- id: name,
81
- __latest: true,
82
- __closed: false
83
- }).exec();
84
- if (typeof item !== 'undefined' && item !== null) {
85
- return this.saveVersion(item._id);
86
- } else {
87
- throw new notError('-options for module version not saved, empty response', {
88
- id: name,
89
- item
90
- });
91
- }
92
- } else {
93
- return this.add({
94
- id: name,
95
- value,
96
- active: true,
97
- updatedAt: Date.now()
98
- });
99
- }
100
- },
101
- getAllAsObject(whitelist = false) {
102
- return this.find({
103
- 'active': true,
104
- __latest: true,
105
- __closed: false
106
- }).exec()
107
- .then((results) => {
108
- let options = {};
109
- if (Array.isArray(results)) {
110
- results.forEach((item) => {
111
- if (whitelist && Array.isArray(whitelist)) {
112
- if (!whitelist.includes(item.id)) {
113
- return;
108
+ },
109
+ getAllAsObject(whitelist = false) {
110
+ return this.find({
111
+ active: true,
112
+ __latest: true,
113
+ __closed: false,
114
+ })
115
+ .exec()
116
+ .then((results) => {
117
+ let options = {};
118
+ if (Array.isArray(results)) {
119
+ results.forEach((item) => {
120
+ if (whitelist && Array.isArray(whitelist)) {
121
+ if (!whitelist.includes(item.id)) {
122
+ return;
123
+ }
124
+ }
125
+ options[item.id] = item.value;
126
+ });
127
+ }
128
+ return options;
129
+ });
130
+ },
131
+ async initIfNotExists(opts) {
132
+ let names = Object.keys(opts);
133
+ let res = await this.find({
134
+ __closed: false,
135
+ __latest: true,
136
+ id: {
137
+ $in: names,
138
+ },
139
+ }).exec();
140
+ let existed = res.map((itm) => itm.id);
141
+ for (let name of names) {
142
+ if (!existed.includes(name)) {
143
+ await this.add({
144
+ id: name,
145
+ ...opts[name],
146
+ });
114
147
  }
115
- }
116
- options[item.id] = item.value;
117
- });
118
- }
119
- return options;
120
- });
121
- },
122
- async initIfNotExists(opts) {
123
- let names = Object.keys(opts);
124
- let res = await this.find({
125
- __closed: false,
126
- __latest: true,
127
- id: {
128
- $in: names
129
- }
130
- }).exec();
131
- let existed = res.map(itm => itm.id);
132
- for (let name of names) {
133
- if (!existed.includes(name)) {
134
- await this.add({
135
- id: name,
136
- ...opts[name]
137
- });
138
- }
139
- }
140
- },
141
- async bulkExport(){
142
- return this.listAll();
143
- },
144
- async closeExisting(){
145
- //closing existing records
146
- let all = await this.listAll();
147
- for (let rec of Array.from(all)) {
148
- await rec.close();
149
- }
150
- log.log(`Existing records closed`);
151
- },
152
- /**
153
- * import of many docs in one go,
154
- * if close==true, all existing will be closed
155
- * @param {array} data array of docs
156
- * @param {boolean} close close existing documents
157
- * @returns {Promise}
158
- */
159
- async bulkImport(data, close = false) {
160
- let maxID = 0;
161
- data = Array.from(data);
162
- let len = data.length;
163
- if (close) {
164
- await this.closeExisting();
165
- }
166
- //adding new or modifing existing
167
- for (let itm of Array.from(data)) {
168
- //keep track of maximum ID, to later change in 'inc' collection
169
- if (itm.optionsID > maxID) {
170
- maxID = itm.optionsID;
171
- }
172
- delete itm._id;
173
- itm._versions;
174
- }
175
- await this.insertMany(data);
176
- log.log(`Imported ${len}`);
177
- await Increment.rebase(MODEL_NAME, maxID + 1);
178
- log.log(`ID cursor moved to ${maxID}`);
179
- },
180
- async importAsObject(obj, close = false){
181
- if (close) {
182
- await this.closeExisting();
183
- }
184
- }
185
- };
186
-
187
- metaExtend(metaModel, exports.thisStatics, ActionList, {
188
- MODEL_NAME
189
- });
148
+ }
149
+ },
150
+ async bulkExport() {
151
+ return this.listAll();
152
+ },
153
+ async closeExisting() {
154
+ //closing existing records
155
+ let all = await this.listAll();
156
+ for (let rec of Array.from(all)) {
157
+ await rec.close();
158
+ }
159
+ log.log(`Existing records closed`);
160
+ },
161
+ /**
162
+ * import of many docs in one go,
163
+ * if close==true, all existing will be closed
164
+ * @param {array} data array of docs
165
+ * @param {boolean} close close existing documents
166
+ * @returns {Promise}
167
+ */
168
+ async bulkImport(data, close = false) {
169
+ let maxID = 0;
170
+ data = Array.from(data);
171
+ let len = data.length;
172
+ if (close) {
173
+ await this.closeExisting();
174
+ }
175
+ //adding new or modifing existing
176
+ for (let itm of Array.from(data)) {
177
+ //keep track of maximum ID, to later change in 'inc' collection
178
+ if (itm.optionsID > maxID) {
179
+ maxID = itm.optionsID;
180
+ }
181
+ delete itm._id;
182
+ itm._versions;
183
+ }
184
+ await this.insertMany(data);
185
+ log.log(`Imported ${len}`);
186
+ await Increment.rebase(MODEL_NAME, maxID + 1);
187
+ log.log(`ID cursor moved to ${maxID}`);
188
+ },
189
+ async importAsObject(obj, close = false) {
190
+ if (close) {
191
+ await this.closeExisting();
192
+ }
193
+ },
194
+ };
190
195
 
196
+ metaExtend(metaModel, exports.thisStatics, ActionList, {
197
+ MODEL_NAME,
198
+ });
191
199
  } catch (e) {
192
- log.error(e);
200
+ log.error(e);
193
201
  }