@bbn/bbn 1.0.57 → 1.0.59

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/dist/db.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ interface Db {
2
+ _structures: object;
3
+ _connections: object;
4
+ _stores: object;
5
+ ok: boolean;
6
+ open(name: string): Promise<object>;
7
+ add(db: string, name: string, structure: object): void;
8
+ }
9
+ declare const db: Db;
10
+ export { db };
package/dist/db.js ADDED
@@ -0,0 +1,183 @@
1
+ import { _ } from './_';
2
+ import { each } from './fn/each';
3
+ import { iterate } from './fn/iterate';
4
+ import { log } from './fn/log';
5
+ const idb = indexedDB || window['webkitIndexedDB'] || window['mozIndexedDB'] || window['OIndexedDB'] || window['msIndexedDB'];
6
+ const dbObject = function (dbName) {
7
+ const conn = db._connections[dbName];
8
+ const structure = db._structures[dbName];
9
+ this.insert = (table, data) => {
10
+ if (!Array.isArray(data)) {
11
+ data = [data];
12
+ }
13
+ return new Promise(resolve => {
14
+ const tx = conn.transaction(table, "readwrite");
15
+ const store = tx.objectStore(table);
16
+ let res = data.length;
17
+ each(data, a => {
18
+ const request = store.put(a);
19
+ request.onerror = () => {
20
+ log(request.error);
21
+ res--;
22
+ };
23
+ });
24
+ tx.onabort = () => {
25
+ throw new Error(tx.error);
26
+ };
27
+ tx.oncomplete = () => {
28
+ resolve(res);
29
+ };
30
+ });
31
+ };
32
+ this.update = (table, data, where) => {
33
+ return new Promise(resolve => {
34
+ const tx = conn.transaction(table, "readwrite");
35
+ const store = tx.objectStore(table);
36
+ const arch = structure[table];
37
+ const primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
38
+ if (!where[primary]) {
39
+ throw new Error(_("No "));
40
+ }
41
+ let res = 1;
42
+ const request = store.put(data, where[primary]);
43
+ request.onerror = () => {
44
+ log(request.error);
45
+ res--;
46
+ };
47
+ tx.onabort = () => {
48
+ throw new Error(tx.error);
49
+ };
50
+ tx.oncomplete = () => {
51
+ resolve(res);
52
+ };
53
+ });
54
+ };
55
+ this.delete = (table, where) => {
56
+ return new Promise(resolve => {
57
+ const tx = conn.transaction(table, "readwrite");
58
+ const store = tx.objectStore(table);
59
+ const arch = structure[table];
60
+ const primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
61
+ if (!where[primary]) {
62
+ throw new Error(_("No "));
63
+ }
64
+ let res = 1;
65
+ const request = store.delete(where[primary]);
66
+ request.onerror = () => {
67
+ log(request.error);
68
+ res--;
69
+ };
70
+ tx.onabort = () => {
71
+ throw new Error(tx.error);
72
+ };
73
+ tx.oncomplete = () => {
74
+ resolve(res);
75
+ };
76
+ });
77
+ };
78
+ this.selectOne = (table, field, where, order, start, limit) => {
79
+ return new Promise(resolve => {
80
+ this.select(table, [field], where, order, start, limit).then(d => {
81
+ resolve(d[field] ?? undefined);
82
+ });
83
+ });
84
+ };
85
+ this.select = (table, fields, where, order, start, limit) => {
86
+ const tx = conn.transaction(table, "readonly");
87
+ const store = tx.objectStore(table);
88
+ const arch = structure[table];
89
+ const primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
90
+ if (!where[primary]) {
91
+ throw new Error(_("No "));
92
+ }
93
+ return new Promise(resolve => {
94
+ const req = store.get(where[primary]);
95
+ req.onsuccess = () => {
96
+ let obj = req.result;
97
+ if (fields.length) {
98
+ let res = {};
99
+ iterate(obj, (v, n) => {
100
+ if (fields.indexOf(n) > -1) {
101
+ res[n] = v;
102
+ }
103
+ });
104
+ return resolve(res);
105
+ }
106
+ else {
107
+ resolve(obj);
108
+ }
109
+ };
110
+ });
111
+ };
112
+ this.selectAll = (table, fields, where, order, start, limit) => {
113
+ const tx = conn.transaction(table, "read");
114
+ const store = tx.objectStore(table);
115
+ const arch = structure[table];
116
+ const primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
117
+ if (!where[primary]) {
118
+ throw new Error(_("No "));
119
+ }
120
+ return new Promise(resolve => {
121
+ const req = store.get(structure.keys.PRIMARY);
122
+ });
123
+ };
124
+ this.getColumnValues = (table, field, where, order, start, limit) => {
125
+ return new Promise(resolve => {
126
+ const tx = conn.transaction(table, "read");
127
+ const store = tx.objectStore(table);
128
+ });
129
+ };
130
+ return this;
131
+ };
132
+ const db = {
133
+ _structures: {},
134
+ /* This variable should be set to true in debugging mode only */
135
+ _connections: {},
136
+ /* Address of the CDN (where this file should be hosted) */
137
+ _stores: {},
138
+ ok: idb !== undefined,
139
+ open(name) {
140
+ return new Promise((resolve) => {
141
+ if (!db._connections[name]) {
142
+ if (!db._structures[name]) {
143
+ throw new Error(_("Impossible to find a structure for the database %s", name));
144
+ }
145
+ const conn = idb.open(name);
146
+ conn.onupgradeneeded = () => {
147
+ log("UPGRADE NEEDED");
148
+ const res = conn.result;
149
+ iterate(db._structures[name], (structure, storeName) => {
150
+ const primary = structure.keys.PRIMARY.columns.length > 1 ? structure.keys.PRIMARY.columns : structure.keys.PRIMARY.columns[0];
151
+ const store = res.createObjectStore(storeName, { keyPath: primary });
152
+ iterate(structure.keys, (a, n) => {
153
+ if (n !== 'PRIMARY') {
154
+ store.createIndex(n, a.columns.length > 1 ? a.columns : a.columns[0], {
155
+ unique: !!a.unique
156
+ });
157
+ }
158
+ });
159
+ });
160
+ };
161
+ conn.onsuccess = () => {
162
+ db._connections[name] = conn.result;
163
+ let obj = dbObject(name);
164
+ resolve(obj);
165
+ };
166
+ return;
167
+ }
168
+ resolve(dbObject(db._connections[name]));
169
+ });
170
+ },
171
+ add(database, name, structure) {
172
+ if (structure?.keys?.PRIMARY && structure?.fields) {
173
+ if (!db._structures[database]) {
174
+ db._structures[database] = {};
175
+ }
176
+ db._structures[database][name] = structure;
177
+ }
178
+ else {
179
+ throw new Error(_("The database structure for %s is not valid (are there keys and field? Is there a primary?", name));
180
+ }
181
+ }
182
+ };
183
+ export { db };
package/dist/fn/ajax.js CHANGED
@@ -67,9 +67,9 @@ const ajax = function (url, datatype, data, success, failure, abort) {
67
67
  args.push(data);
68
68
  }
69
69
  args.push(options);
70
- let loader = axios[args.length === 2 ? 'get' : 'post']
71
- .apply(axios, ...args)
72
- .then((res) => {
70
+ const axiosMethod = args.length === 2 ? 'get' : 'post';
71
+ let loader = axios[axiosMethod].apply(null, args)
72
+ .then(res => {
73
73
  _deleteLoader(requestId, res);
74
74
  defaultEndLoadingFunction(url, tst, data, res);
75
75
  switch (res.status) {
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ import { $ } from './$';
3
3
  import { lng } from './lng';
4
4
  import { vars } from './vars';
5
5
  import { env } from './env';
6
+ import { db } from './db';
6
7
  import { fn } from './fn';
7
8
  const bbn = {
8
9
  version: "1.0.1",
@@ -15,6 +16,7 @@ const bbn = {
15
16
  lng,
16
17
  vars,
17
18
  env,
19
+ db,
18
20
  fn
19
21
  };
20
22
  window.bbn = bbn;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",