@cocreate/indexeddb 1.8.7 → 1.9.1

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 (3) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +3 -2
  3. package/src/index.js +108 -117
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.9.1](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.9.0...v1.9.1) (2023-05-10)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * duplicate keys ([e87668f](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/e87668f17412e08a1497a53acbf7ba8592b53756))
7
+
8
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.8.7...v1.9.0) (2023-05-10)
9
+
10
+
11
+ ### Features
12
+
13
+ * generateDB, added @cocreate/uuid ([28be996](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/28be9960f64cac3fc72afcf2b8025431be9e8640))
14
+
1
15
  ## [1.8.7](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.8.6...v1.8.7) (2023-05-06)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/indexeddb",
3
- "version": "1.8.7",
3
+ "version": "1.9.1",
4
4
  "description": "A simple indexeddb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "indexeddb",
@@ -61,6 +61,7 @@
61
61
  "dependencies": {
62
62
  "@cocreate/docs": "^1.7.8",
63
63
  "@cocreate/hosting": "^1.10.0",
64
- "@cocreate/utils": "^1.20.6"
64
+ "@cocreate/utils": "^1.20.6",
65
+ "@cocreate/uuid": "^1.4.7"
65
66
  }
66
67
  }
package/src/index.js CHANGED
@@ -5,6 +5,8 @@
5
5
  * SPDX-License-Identifier: MIT
6
6
  ********************************************************************************/
7
7
  import {ObjectId, dotNotationToObject, searchData, sortData, queryData} from '@cocreate/utils'
8
+ import uuid from '@cocreate/uuid'
9
+
8
10
  let status = true;
9
11
 
10
12
  function createDatabase(data){
@@ -1014,125 +1016,114 @@ async function readDocs(data, database, collection, objectStore, direction) {
1014
1016
  });
1015
1017
  }
1016
1018
 
1017
- function generateDB(data) {
1018
- const organization_id = data.organization_id;
1019
- const apiKey = data.apiKey;
1020
- const user_id = data.user_id;
1019
+ async function generateDB(organization = {document: {}}, user = {document: {}}) {
1020
+ const organization_id = organization.document._id || ObjectId();
1021
+ const primaryKey = organization.document.key || uuid.generate();
1022
+ const user_id = user.document._id || ObjectId();
1023
+
1024
+ let apiKey = await readDocument({database: organization_id, collection: 'keys', organization_id})
1025
+ if (apiKey && apiKey.document && apiKey.document[0])
1026
+ return
1027
+
1028
+ try {
1029
+ // Create organization
1030
+ organization.database = organization_id
1031
+ organization.collection = 'organizations'
1032
+ organization.document._id = organization_id
1033
+ organization.document.key = primaryKey
1034
+ organization.document.name = organization.document.name || 'untitiled',
1035
+ organization.organization_id = organization_id
1036
+ createDocument(organization);
1037
+
1038
+ // Create primary key
1039
+ let key = {
1040
+ database: organization_id,
1041
+ collection: 'keys',
1042
+ document: {
1043
+ _id: ObjectId(),
1044
+ type: "apikey",
1045
+ key: primaryKey,
1046
+ hosts: [
1047
+ "*"
1048
+ ],
1049
+ collections: {
1050
+ "organizations": ["read"],
1051
+ "files": ["read"]
1052
+ },
1053
+ documents: {
1054
+ "someid": {
1055
+ "permissions": [""],
1056
+ "fieldNames": {
1057
+ "name": ["read"]
1058
+ }
1059
+ }
1060
+ },
1061
+ actions: {
1062
+ "signIn": "",
1063
+ "signUp": "",
1064
+ "createOrg": "",
1065
+ "runIndustry": "",
1066
+ "sendgrid": ["sendEmail"]
1067
+ },
1068
+ admin: "false",
1069
+ primary: true,
1070
+ },
1071
+ organization_id
1072
+ }
1073
+ createDocument(key);
1074
+
1075
+ // Create user
1076
+ user.database = organization_id
1077
+ user.collection = 'users'
1078
+ user.document._id = user_id
1079
+ user.document.name = user.document.name || 'untitiled',
1080
+ user.document.password = user.document.password || btoa('0000'),
1081
+ user.document.connected_orgs = [organization_id],
1082
+ user.document.current_org = organization_id,
1083
+ user.organization_id = organization_id
1084
+ createDocument(user);
1085
+
1086
+ // Create role permission
1087
+ let role_id = ObjectId();
1088
+ let role = {
1089
+ database: organization_id,
1090
+ collection: 'keys',
1091
+ document: {
1092
+ _id: role_id,
1093
+ "type": "role",
1094
+ "name": "admin",
1095
+ "collections": {
1096
+ "*": ["*"]
1097
+ },
1098
+ "modules": {
1099
+ "*": ""
1100
+ },
1101
+ "admin": "true",
1102
+ "hosts": ["*"],
1103
+ },
1104
+ organization_id
1105
+ };
1106
+ createDocument(role);
1021
1107
 
1022
- try {
1023
- // Create organization
1024
- let organization = {
1108
+ // Create user permission
1109
+ let userKey = {
1025
1110
  database: organization_id,
1026
- collection: 'organizatons',
1027
- document: {
1028
- _id: organization_id,
1029
- name: 'untitled',
1030
- organization_id
1031
- }
1032
- }
1033
- createDocument(organization);
1034
-
1035
- // Create apiKey permission
1036
- if (organization_id && apiKey) {
1037
- let permissions = {
1038
- database: organization_id,
1039
- collection: 'permissions',
1040
- document: {
1041
- _id: ObjectId(),
1042
- type: "apikey",
1043
- key: apiKey,
1044
- hosts: [
1045
- "*"
1046
- ],
1047
- collections: {
1048
- "organizations": ["read"],
1049
- "files": ["read"]
1050
- },
1051
- documents: {
1052
- "someid": {
1053
- "permissions": [""],
1054
- "fieldNames": {
1055
- "name": ["read"]
1056
- }
1057
- }
1058
- },
1059
- actions: {
1060
- "signIn": "",
1061
- "signUp": "",
1062
- "createOrg": "",
1063
- "runIndustry": "",
1064
- "sendgrid": ["sendEmail"]
1065
- },
1066
- admin: "false",
1067
- primary: true,
1068
- organization_id
1069
- }
1070
- }
1071
-
1072
- createDocument(permissions);
1073
- }
1074
-
1075
- // Create user
1076
- if (organization_id) {
1077
- let user = {
1078
- database: organization_id,
1079
- collection: 'users',
1080
- document: {
1081
- _id: user_id,
1082
- password: btoa('0000'),
1083
- connected_orgs: [organization_id],
1084
- current_org: organization_id,
1085
- organization_id
1086
- }
1087
- }
1088
- createDocument(user);
1089
- }
1090
-
1091
- // Create role permission
1092
- if (user_id) {
1093
- let role = {
1094
- database: organization_id,
1095
- collection: 'permissions',
1096
- document: {
1097
- _id: ObjectId(),
1098
- "type": "role",
1099
- "name": "admin",
1100
- "collections": {
1101
- "*": ["*"]
1102
- },
1103
- "modules": {
1104
- "*": ""
1105
- },
1106
- "admin": "true",
1107
- "hosts": ["*"],
1108
- organization_id,
1109
- }
1110
- };
1111
-
1112
- createDocument(role);
1113
- let role_id = role.document._id;
1114
-
1115
- // Create user permission
1116
- if (role_id) {
1117
- let userPermission = {
1118
- database: organization_id,
1119
- collection: 'permissions',
1120
- document: {
1121
- _id: ObjectId(),
1122
- organization_id,
1123
- "type": "user_id",
1124
- "key": user_id,
1125
- "roles": [role_id]
1126
- }
1127
- };
1128
- createDocument(userPermission);
1129
- }
1130
- }
1131
- return true
1132
-
1133
- } catch (error) {
1134
- errorHandler(data, error)
1135
- }
1111
+ collection: 'keys',
1112
+ document: {
1113
+ _id: ObjectId(),
1114
+ "type": "user_id",
1115
+ "key": user_id,
1116
+ "roles": [role_id]
1117
+ },
1118
+ organization_id
1119
+ };
1120
+ createDocument(userKey);
1121
+
1122
+ return [organization, key, user, role, userKey]
1123
+
1124
+ } catch (error) {
1125
+ return false
1126
+ }
1136
1127
  }
1137
1128
 
1138
1129
  function errorHandler(data, error, database, collection){