@cocreate/crud-server
Advanced tools
Comparing version 1.24.7 to 1.25.0
@@ -0,1 +1,15 @@ | ||
# [1.25.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.24.7...v1.25.0) (2023-08-16) | ||
### Bug Fixes | ||
* crud attributes renamed ([757fa5a](https://github.com/CoCreate-app/CoCreate-crud-server/commit/757fa5a51bc511be8fd428ae9d64a1456cc536e8)) | ||
* Refactored *-target to *-selector ([fab5662](https://github.com/CoCreate-app/CoCreate-crud-server/commit/fab5662929f208bb5da7c47f0a10d673ef261fb7)) | ||
### Features | ||
* Add sorting functionality for returning sorted values from multiple storages. ([667ad70](https://github.com/CoCreate-app/CoCreate-crud-server/commit/667ad70f8704de0fc8ff504e831fa64d954da012)) | ||
* name attribute and variable renamed to key ([d5cf8bc](https://github.com/CoCreate-app/CoCreate-crud-server/commit/d5cf8bcd1bea68a480a9a8dec6cf129e6942c8ed)) | ||
## [1.24.7](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.24.6...v1.24.7) (2023-06-16) | ||
@@ -2,0 +16,0 @@ |
@@ -7,4 +7,4 @@ module.exports = { | ||
{ | ||
"collection": "files", | ||
"document": { | ||
"array": "files", | ||
"object": { | ||
"_id": "6204253480b409001727b73e", | ||
@@ -11,0 +11,0 @@ "name": "index.html", |
{ | ||
"name": "@cocreate/crud-server", | ||
"version": "1.24.7", | ||
"version": "1.25.0", | ||
"description": "CoCreate-crud-server", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
141
src/index.js
@@ -46,83 +46,20 @@ 'use strict'; | ||
if (this.wsManager) { | ||
this.wsManager.on('createDatabase', (socket, data) => this.crud(socket, 'createDatabase', data)) | ||
this.wsManager.on('readDatabase', (socket, data) => this.crud(socket, 'readDatabase', data)) | ||
this.wsManager.on('updateDatabase', (socket, data) => this.crud(socket, 'updateDatabase', data)) | ||
this.wsManager.on('deleteDatabase', (socket, data) => this.crud(socket, 'deleteDatabase', data)) | ||
this.wsManager.on('createCollection', (socket, data) => this.crud(socket, 'createCollection', data)) | ||
this.wsManager.on('readCollection', (socket, data) => this.crud(socket, 'readCollection', data)) | ||
this.wsManager.on('updateCollection', (socket, data) => this.crud(socket, 'updateCollection', data)) | ||
this.wsManager.on('deleteCollection', (socket, data) => this.crud(socket, 'deleteCollection', data)) | ||
this.wsManager.on('createDocument', (socket, data) => this.crud(socket, 'createDocument', data)) | ||
this.wsManager.on('readDocument', (socket, data) => this.crud(socket, 'readDocument', data)) | ||
this.wsManager.on('updateDocument', (socket, data) => this.crud(socket, 'updateDocument', data)) | ||
this.wsManager.on('deleteDocument', (socket, data) => this.crud(socket, 'deleteDocument', data)) | ||
const method = ['create', 'read', 'update', 'delete']; | ||
const type = ['storage', 'database', 'array', 'index', 'object']; | ||
for (let i = 0; i < method.length; i++) { | ||
for (let j = 0; j < type.length; j++) { | ||
const action = method[i] + '.' + type[j]; | ||
this.wsManager.on(action, (socket, data) => this.crud(socket, data)) | ||
} | ||
} | ||
} | ||
} | ||
async databaseStats(data) { | ||
data = await this.crud('', 'databaseStats', data) | ||
async send(data) { | ||
data = await this.crud('', data) | ||
return data | ||
} | ||
async createDatabase(data) { | ||
data = await this.crud('', 'createDatabase', data) | ||
return data | ||
} | ||
async readDatabase(data) { | ||
data = await this.crud('', 'readDatabase', data) | ||
return data | ||
} | ||
async updateDatabase(data) { | ||
data = await this.crud('', 'updateDatabase', data) | ||
return data | ||
} | ||
async deleteDatabase(data) { | ||
data = await this.crud('', 'deleteDatabase', data) | ||
return data | ||
} | ||
async createCollection(data) { | ||
data = await this.crud('', 'createCollection', data) | ||
return data | ||
} | ||
async readCollection(data) { | ||
data = await this.crud('', 'readCollection', data) | ||
return data | ||
} | ||
async updateCollection(data) { | ||
data = await this.crud('', 'updateCollection', data) | ||
return data | ||
} | ||
async deleteCollection(data) { | ||
data = await this.crud('', 'deleteCollection', data) | ||
return data | ||
} | ||
async createDocument(data) { | ||
data = await this.crud('', 'createDocument', data) | ||
return data | ||
} | ||
async readDocument(data) { | ||
data = await this.crud('', 'readDocument', data) | ||
return data | ||
} | ||
async updateDocument(data) { | ||
data = await this.crud('', 'updateDocument', data) | ||
return data | ||
} | ||
async deleteDocument(data) { | ||
data = await this.crud('', 'deleteDocument', data) | ||
return data | ||
} | ||
async crud(socket, action, data) { | ||
async crud(socket, data) { | ||
return new Promise(async (resolve) => { | ||
@@ -159,10 +96,11 @@ try { | ||
} else { | ||
let organization = await this.readDocument({ | ||
let organization = await this.send({ | ||
method: 'read.object', | ||
database: this.config.organization_id, | ||
collection: 'organizations', | ||
document: [{ _id: data.organization_id }], | ||
array: 'organizations', | ||
object: [{ _id: data.organization_id }], | ||
organization_id: this.config.organization_id | ||
}) | ||
if (organization && organization.document && organization.document[0]) | ||
organization = organization.document[0] | ||
if (organization && organization.object && organization.object[0]) | ||
organization = organization.object[0] | ||
if (organization && organization.storage) { | ||
@@ -184,15 +122,16 @@ storage = organization.storage | ||
if (action == 'updateDocument' && data.upsert != false) | ||
if (data.method.startsWith('update') && data.upsert != false) | ||
data.upsert = true | ||
let action = data.method.replace(/\.([a-z])/g, (_, match) => match.toUpperCase()); | ||
// TODO: support stats from multiple dbs | ||
if (data.collection || action === 'databaseStats') { | ||
if (data.array || data.method === 'databaseStats') { | ||
if (!data.database) | ||
data['database'] = data.organization_id | ||
if (action === 'updateDocument' && data.organization_id !== this.config.organization_id) { | ||
if (data.method.startsWith('update.object') && data.organization_id !== this.config.organization_id) { | ||
let syncKeys | ||
if (data.collection === 'organizations') | ||
if (data.array === 'organizations') | ||
syncKeys = ['name', 'logo', 'databases', 'host', 'apis'] | ||
else if (data.collection === 'users') | ||
else if (data.array === 'users') | ||
syncKeys = ['name', 'email', 'password', 'avatar'] | ||
@@ -203,12 +142,12 @@ | ||
database: this.config.organization_id, | ||
collection: data.collection, | ||
document: [{}], | ||
array: data.array, | ||
object: [{}], | ||
organization_id: this.config.organization_id | ||
} | ||
let document = data.document[0] || data.document | ||
if (document) { | ||
let object = data.object[0] || data.object | ||
if (object) { | ||
for (let key of syncKeys) { | ||
if (document[key]) | ||
platformUpdate.document[0][key] = document[key] | ||
if (object[key]) | ||
platformUpdate.object[0][key] = object[key] | ||
} | ||
@@ -241,3 +180,11 @@ } | ||
//TODO: sorting should take place here in order to return sorted values from multiple dbs | ||
if (data.filter) { | ||
if (data.filter.sort && data.filter.sort.length) | ||
data[data.type] = sortData(array, data.filter.sort) | ||
if (data.filter.index && data.filter.limit) { | ||
data[data.type] = data[data.type].slice(data.filter.index, data.filter.limit) | ||
} | ||
data.filter.count = data[data.type].length | ||
} | ||
} | ||
@@ -254,8 +201,8 @@ } | ||
} | ||
}, action, { ...data }); | ||
}, { ...data }); | ||
data.organization_id = socket.config.organization_id | ||
} | ||
this.wsManager.broadcast(socket, action, data); | ||
process.emit('changed-document', data) | ||
this.wsManager.broadcast(socket, data); | ||
process.emit('changed-object', data) | ||
resolve() | ||
@@ -268,3 +215,3 @@ } else { | ||
this.errorHandler(data, error) | ||
this.wsManager.send(socket, action, data); | ||
this.wsManager.send(socket, data); | ||
resolve() | ||
@@ -278,3 +225,3 @@ } else { | ||
errorHandler(data, error, database, collection) { | ||
errorHandler(data, error, database, array) { | ||
if (typeof error == 'object') | ||
@@ -281,0 +228,0 @@ error['storage'] = 'mongodb' |
Sorry, the diff of this file is not supported yet
110518
13
244