@cocreate/indexeddb 1.8.7 → 1.9.0

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 +7 -0
  2. package/package.json +3 -2
  3. package/src/index.js +104 -117
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.8.7...v1.9.0) (2023-05-10)
2
+
3
+
4
+ ### Features
5
+
6
+ * generateDB, added @cocreate/uuid ([28be996](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/28be9960f64cac3fc72afcf2b8025431be9e8640))
7
+
1
8
  ## [1.8.7](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.8.6...v1.8.7) (2023-05-06)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/indexeddb",
3
- "version": "1.8.7",
3
+ "version": "1.9.0",
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,110 @@ 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
+ function generateDB(organization = {document: {}}, user = {document: {}}) {
1020
+ const organization_id = organization._id || ObjectId();
1021
+ const primaryKey = organization.key || uuid.generate();
1022
+ const user_id = user._id || ObjectId();
1023
+
1024
+ try {
1025
+ // Create organization
1026
+ organization.database = organization_id
1027
+ organization.collection = 'organizations'
1028
+ organization.document._id = organization_id
1029
+ organization.document.key = primaryKey
1030
+ organization.document.name = organization.document.name || 'untitiled',
1031
+ organization.organization_id = organization_id
1032
+ createDocument(organization);
1033
+
1034
+ // Create primary key
1035
+ let key = {
1036
+ database: organization_id,
1037
+ collection: 'keys',
1038
+ document: {
1039
+ _id: ObjectId(),
1040
+ type: "apikey",
1041
+ key: primaryKey,
1042
+ hosts: [
1043
+ "*"
1044
+ ],
1045
+ collections: {
1046
+ "organizations": ["read"],
1047
+ "files": ["read"]
1048
+ },
1049
+ documents: {
1050
+ "someid": {
1051
+ "permissions": [""],
1052
+ "fieldNames": {
1053
+ "name": ["read"]
1054
+ }
1055
+ }
1056
+ },
1057
+ actions: {
1058
+ "signIn": "",
1059
+ "signUp": "",
1060
+ "createOrg": "",
1061
+ "runIndustry": "",
1062
+ "sendgrid": ["sendEmail"]
1063
+ },
1064
+ admin: "false",
1065
+ primary: true,
1066
+ },
1067
+ organization_id
1068
+ }
1069
+ createDocument(key);
1070
+
1071
+ // Create user
1072
+ user.database = organization_id
1073
+ user.collection = 'users'
1074
+ user.document._id = user_id
1075
+ user.document.name = user.document.name || 'untitiled',
1076
+ user.document.password = user.document.password || btoa('0000'),
1077
+ user.document.connected_orgs = [organization_id],
1078
+ user.document.current_org = organization_id,
1079
+ user.organization_id = organization_id
1080
+ createDocument(user);
1081
+
1082
+ // Create role permission
1083
+ let role_id = ObjectId();
1084
+ let role = {
1085
+ database: organization_id,
1086
+ collection: 'keys',
1087
+ document: {
1088
+ _id: role_id,
1089
+ "type": "role",
1090
+ "name": "admin",
1091
+ "collections": {
1092
+ "*": ["*"]
1093
+ },
1094
+ "modules": {
1095
+ "*": ""
1096
+ },
1097
+ "admin": "true",
1098
+ "hosts": ["*"],
1099
+ },
1100
+ organization_id
1101
+ };
1102
+ createDocument(role);
1021
1103
 
1022
- try {
1023
- // Create organization
1024
- let organization = {
1104
+ // Create user permission
1105
+ let userKey = {
1025
1106
  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
- }
1107
+ collection: 'keys',
1108
+ document: {
1109
+ _id: ObjectId(),
1110
+ "type": "user_id",
1111
+ "key": user_id,
1112
+ "roles": [role_id]
1113
+ },
1114
+ organization_id
1115
+ };
1116
+ createDocument(userKey);
1117
+
1118
+ return [organization, key, user, role, userKey]
1119
+
1120
+ } catch (error) {
1121
+ return false
1122
+ }
1136
1123
  }
1137
1124
 
1138
1125
  function errorHandler(data, error, database, collection){