@cocreate/mongodb
Advanced tools
Comparing version 1.4.18 to 1.5.0
@@ -0,1 +1,16 @@ | ||
# [1.5.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.4.18...v1.5.0) (2023-08-16) | ||
### Bug Fixes | ||
* crud attributes renamed ([08ecbf2](https://github.com/CoCreate-app/CoCreate-mongodb/commit/08ecbf218203a2d08dad7ac298aa8b4feeb09452)) | ||
* Refactored *-target to *-selector ([9fa3212](https://github.com/CoCreate-app/CoCreate-mongodb/commit/9fa3212a4994c08b58c978620ed3f763ba8ea8db)) | ||
* startIndex variable renamed to index and comment out unnecessary code. ([b20bf7a](https://github.com/CoCreate-app/CoCreate-mongodb/commit/b20bf7a1beae3345e64af432c9651e686b78690f)) | ||
* webpack.config and package.json make use of mode=production instead of process.env ([22a2454](https://github.com/CoCreate-app/CoCreate-mongodb/commit/22a24544bc29be1d83cb6acae750619a6b14cdb3)) | ||
### Features | ||
* name attribute and variable renamed to key ([79e1c03](https://github.com/CoCreate-app/CoCreate-mongodb/commit/79e1c039924b2a013d8c2de2e83eb807443e0d65)) | ||
## [1.4.18](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.4.17...v1.4.18) (2023-06-14) | ||
@@ -2,0 +17,0 @@ |
@@ -7,4 +7,4 @@ module.exports = { | ||
"sources": [{ | ||
"collection": "files", | ||
"document": { | ||
"array": "files", | ||
"object": { | ||
"_id": "60145dc49f64ba1680b86693", | ||
@@ -11,0 +11,0 @@ "name": "index.html", |
{ | ||
"name": "@cocreate/mongodb", | ||
"version": "1.4.18", | ||
"version": "1.5.0", | ||
"description": "A simple mongodb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"start": "npx webpack --config webpack.config.js", | ||
"build": "NODE_ENV=production npx webpack --config webpack.config.js", | ||
"build": "npx webpack --mode=production --config webpack.config.js", | ||
"dev": "npx webpack --config webpack.config.js --watch", | ||
@@ -49,5 +49,5 @@ "postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\"" | ||
"dependencies": { | ||
"@cocreate/utils": "^1.21.15", | ||
"@cocreate/utils": "^1.21.16", | ||
"mongodb": "^4.12.1" | ||
} | ||
} |
232
src/index.js
@@ -92,21 +92,21 @@ const { MongoClient, ObjectId } = require('mongodb'); | ||
function createCollection(data) { | ||
return collection('createCollection', data) | ||
return array('createCollection', data) | ||
} | ||
function readCollection(data) { | ||
return collection('readCollection', data) | ||
return array('readCollection', data) | ||
} | ||
function updateCollection(data) { | ||
return collection('updateCollection', data) | ||
return array('updateCollection', data) | ||
} | ||
function deleteCollection(data) { | ||
return collection('deleteCollection', data) | ||
return array('deleteCollection', data) | ||
} | ||
function collection(action, data) { | ||
function array(action, data) { | ||
return new Promise(async (resolve, reject) => { | ||
let type = 'collection' | ||
let collectionArray = []; | ||
let type = 'array' | ||
let arrayArray = []; | ||
@@ -118,3 +118,3 @@ try { | ||
if (data.request) | ||
data.collection = data.request | ||
data.array = data.request | ||
@@ -142,5 +142,5 @@ let databases = data.database; | ||
if (isFilter) | ||
collectionArray.push({ name: res.name, database, db: 'mongodb' }) | ||
arrayArray.push({ name: res.name, database, db: 'mongodb' }) | ||
} else | ||
collectionArray.push({ name: res.name, database, db: 'mongodb' }) | ||
arrayArray.push({ name: res.name, database, db: 'mongodb' }) | ||
} | ||
@@ -151,3 +151,3 @@ } | ||
if (!databasesLength) { | ||
data = createData(data, collectionArray, type) | ||
data = createData(data, arrayArray, type) | ||
resolve(data) | ||
@@ -157,29 +157,29 @@ } | ||
} else { | ||
let collections | ||
let arrays | ||
let value | ||
if (action == 'updateCollection') | ||
collections = Object.entries(data.collection) | ||
arrays = Object.entries(data.array) | ||
else | ||
collections = data.collection; | ||
arrays = data.array; | ||
if (!Array.isArray(collections)) | ||
collections = [collections] | ||
if (!Array.isArray(arrays)) | ||
arrays = [arrays] | ||
let collectionsLength = collections.length | ||
for (let collection of collections) { | ||
let arraysLength = arrays.length | ||
for (let array of arrays) { | ||
if (action == 'createCollection') { | ||
db.createCollection(collection, function (error, result) { | ||
db.createCollection(array, function (error, result) { | ||
if (error) | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
if (result) | ||
collectionArray.push({ name: collection, database, db: 'mongodb' }) | ||
arrayArray.push({ name: array, database, db: 'mongodb' }) | ||
collectionsLength -= 1 | ||
if (!collectionsLength) | ||
arraysLength -= 1 | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
data = createData(data, collectionArray, type) | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, arrayArray, type) | ||
resolve(data) | ||
@@ -190,21 +190,21 @@ } | ||
if (action == 'updateCollection') { | ||
[collection, value] = collection | ||
[array, value] = array | ||
} | ||
const collectionObj = db.collection(collection); | ||
const arrayObj = db.array(array); | ||
if (action == 'updateCollection') { | ||
collectionObj.rename(value, function (error, result) { | ||
arrayObj.rename(value, function (error, result) { | ||
if (error) | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
if (result) | ||
collectionArray.push({ name: value, oldName: collection, database, db: 'mongodb' }) | ||
arrayArray.push({ name: value, oldName: array, database, db: 'mongodb' }) | ||
collectionsLength -= 1 | ||
if (!collectionsLength) | ||
arraysLength -= 1 | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
data = createData(data, collectionArray, type) | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, arrayArray, type) | ||
resolve(data) | ||
@@ -217,15 +217,15 @@ } | ||
if (action == 'deleteCollection') { | ||
collectionObj.drop(function (error, result) { | ||
arrayObj.drop(function (error, result) { | ||
if (error) | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
if (result) | ||
collectionArray.push({ name: collection, database, db: 'mongodb' }) | ||
arrayArray.push({ name: array, database, db: 'mongodb' }) | ||
collectionsLength -= 1 | ||
if (!collectionsLength) | ||
arraysLength -= 1 | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
data = createData(data, collectionArray, type) | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, arrayArray, type) | ||
resolve(data) | ||
@@ -254,16 +254,16 @@ } | ||
function createDocument(data) { | ||
return document('createDocument', data) | ||
function createObject(data) { | ||
return document('createObject', data) | ||
} | ||
function readDocument(data) { | ||
return document('readDocument', data) | ||
function readObject(data) { | ||
return document('readObject', data) | ||
} | ||
function updateDocument(data) { | ||
return document('updateDocument', data) | ||
function updateObject(data) { | ||
return document('updateObject', data) | ||
} | ||
function deleteDocument(data) { | ||
return document('deleteDocument', data) | ||
function deleteObject(data) { | ||
return document('deleteObject', data) | ||
} | ||
@@ -298,10 +298,10 @@ | ||
for (let database of databases) { | ||
let collections = data.collection; | ||
if (!Array.isArray(collections)) | ||
collections = [collections] | ||
let arrays = data.array; | ||
if (!Array.isArray(arrays)) | ||
arrays = [arrays] | ||
let collectionsLength = collections.length | ||
for (let collection of collections) { | ||
let arraysLength = arrays.length | ||
for (let array of arrays) { | ||
const db = client.db(database); | ||
const collectionObj = db.collection(collection); | ||
const arrayObj = db.array(array); | ||
@@ -325,3 +325,3 @@ let { query, sort } = getFilters(data); | ||
if (action == 'createDocument') { | ||
if (action == 'createObject') { | ||
data[type][i] = dotNotationToObject(data[type][i]) | ||
@@ -335,6 +335,6 @@ | ||
} | ||
if (action == 'readDocument' && data[type][i]._id) { | ||
if (action == 'readObject' && data[type][i]._id) { | ||
_ids.push(ObjectId(data[type][i]._id)) | ||
} | ||
if (action == 'updateDocument') { | ||
if (action == 'updateObject') { | ||
if (data[type][i]._id) | ||
@@ -349,6 +349,6 @@ update_ids.push({ _id: data[type][i]._id, updateDoc: data[type][i], updateType: '_id' }) | ||
} | ||
if (action == 'deleteDocument') { | ||
if (action == 'deleteObject') { | ||
if (data[type][i]._id) { | ||
_ids.push(ObjectId(data[type][i]._id)) | ||
documents.push({ _id: data[type][i]._id, db: 'mongodb', database, collection }) | ||
documents.push({ _id: data[type][i]._id, db: 'mongodb', database, array }) | ||
} | ||
@@ -364,15 +364,15 @@ } | ||
if (action == 'createDocument') { | ||
collectionObj.insertMany(data[type], function (error, result) { | ||
if (action == 'createObject') { | ||
arrayObj.insertMany(data[type], function (error, result) { | ||
if (error) | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
for (let i = 0; i < data[type].length; i++) | ||
documents.push({ db: 'mongodb', database, collection, ...data[type][i] }) | ||
documents.push({ db: 'mongodb', database, array, ...data[type][i] }) | ||
collectionsLength -= 1 | ||
if (!collectionsLength) | ||
arraysLength -= 1 | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, documents, type) | ||
@@ -384,10 +384,10 @@ resolve(data) | ||
if (action == 'readDocument') { | ||
if (action == 'readObject') { | ||
let index = 0, limit = 0 | ||
if (data.filter) { | ||
const count = await collectionObj.estimatedDocumentCount() | ||
const count = await arrayObj.estimatedDocumentCount() | ||
data.filter.count = count | ||
if (data.filter.startIndex) | ||
index = data.filter.startIndex | ||
if (data.filter.index) | ||
index = data.filter.index | ||
if (data.filter.limit) | ||
@@ -399,5 +399,5 @@ limit = data.filter.limit | ||
collectionObj.find(query).limit(limit).sort(sort).toArray(function (error, result) { | ||
arrayObj.find(query).skip(index).limit(limit).sort(sort).toArray(function (error, result) { | ||
if (error) | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
@@ -413,6 +413,6 @@ if (result) { | ||
doc.database = database | ||
doc.collection = collection | ||
doc.array = array | ||
doc._id = doc._id.toString() | ||
if (data.returnDocument == false) { | ||
if (data.returnObject == false) { | ||
let tempDoc = {}; | ||
@@ -434,12 +434,12 @@ let docs = new Map(data[type].map((obj) => [obj._id, obj])); | ||
if (index && limit) { | ||
documents = documents.slice(index, limit) | ||
} | ||
// if (index && limit) { | ||
// documents = documents.slice(index, limit) | ||
// } | ||
} | ||
collectionsLength -= 1 | ||
if (!collectionsLength) | ||
arraysLength -= 1 | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, documents, type) | ||
@@ -451,9 +451,9 @@ resolve(data) | ||
if (action == 'updateDocument' || action == 'deleteDocument') { | ||
if (action == 'updateObject' || action == 'deleteObject') { | ||
const queryDocs = () => { | ||
return new Promise(async (resolve, reject) => { | ||
collectionObj.find(query).sort(sort).toArray(function (error, result) { | ||
arrayObj.find(query).sort(sort).toArray(function (error, result) { | ||
if (error) | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
@@ -479,4 +479,4 @@ if (data.filter && data.filter.search) { | ||
if (isFilter && data.returnDocument != false) | ||
if (action == 'deleteDocument' || action == 'updateDocument' && updateData.update) | ||
if (isFilter && data.returnObject != false) | ||
if (action == 'deleteObject' || action == 'updateObject' && updateData.update) | ||
Result = await queryDocs() | ||
@@ -486,4 +486,4 @@ | ||
for (let doc of Result) { | ||
if (action == 'deleteDocument') | ||
documents.push({ _id: doc._id, db: 'mongodb', database, collection }) | ||
if (action == 'deleteObject') | ||
documents.push({ _id: doc._id, db: 'mongodb', database, array }) | ||
else | ||
@@ -497,3 +497,3 @@ doc['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId } | ||
if (action == 'updateDocument') { | ||
if (action == 'updateObject') { | ||
let docsLength = update_ids.length | ||
@@ -508,3 +508,3 @@ for (let { updateDoc, updateType } of update_ids) { | ||
projection = $update.projection | ||
documents.push({ _id: update_id, db: 'mongodb', database, collection, ...update['$set'] }) | ||
documents.push({ _id: update_id, db: 'mongodb', database, array, ...update['$set'] }) | ||
} | ||
@@ -518,3 +518,3 @@ | ||
for (let _id of _ids) | ||
documents.push({ _id, db: 'mongodb', database, collection, ...update['$set'] }) | ||
documents.push({ _id, db: 'mongodb', database, array, ...update['$set'] }) | ||
@@ -525,3 +525,3 @@ } | ||
collectionObj.updateMany(query, update, { | ||
arrayObj.updateMany(query, update, { | ||
upsert: data.upsert, | ||
@@ -532,3 +532,3 @@ projection | ||
}).catch((error) => { | ||
errorHandler(data, error, database, collection) | ||
errorHandler(data, error, database, array) | ||
console.log(action, 'error', error); | ||
@@ -538,8 +538,8 @@ }).finally((error) => { | ||
if (!docsLength) | ||
collectionsLength -= 1 | ||
arraysLength -= 1 | ||
if (!collectionsLength) | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, documents, type) | ||
@@ -554,8 +554,8 @@ resolve(data) | ||
if (!docsLength) | ||
collectionsLength -= 1 | ||
arraysLength -= 1 | ||
if (!collectionsLength) | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, documents, type) | ||
@@ -568,3 +568,3 @@ resolve(data) | ||
if (action == 'deleteDocument') { | ||
if (action == 'deleteObject') { | ||
if (_ids.length == 1) | ||
@@ -574,8 +574,8 @@ query['_id'] = ObjectId(_ids[0]) | ||
query['_id'] = { $in: _ids } | ||
collectionObj.deleteMany(query, function (error, result) { | ||
collectionsLength -= 1 | ||
if (!collectionsLength) | ||
arrayObj.deleteMany(query, function (error, result) { | ||
arraysLength -= 1 | ||
if (!arraysLength) | ||
databasesLength -= 1 | ||
if (!databasesLength && !collectionsLength) { | ||
if (!databasesLength && !arraysLength) { | ||
data = createData(data, documents, type) | ||
@@ -681,3 +681,3 @@ resolve(data) | ||
}, | ||
startIndex: 0, | ||
index: 0, | ||
...data.filter | ||
@@ -697,3 +697,3 @@ }; | ||
sort[filter.sort[i].name] = filter.sort[i].direction | ||
sort[filter.sort[i].key] = filter.sort[i].direction | ||
} | ||
@@ -711,6 +711,6 @@ } | ||
if (!item.name) | ||
if (!item.key) | ||
continue | ||
if (item.name == "_id") { | ||
if (item.key == "_id") { | ||
if (item.value) | ||
@@ -722,3 +722,3 @@ item.value = ObjectId(item.value) | ||
let key = item.name; | ||
let key = item.key; | ||
if (!query[key]) { | ||
@@ -786,3 +786,3 @@ query[key] = {}; | ||
function errorHandler(data, error, database, collection) { | ||
function errorHandler(data, error, database, array) { | ||
if (typeof error == 'object') | ||
@@ -795,4 +795,4 @@ error['storage'] = 'mongodb' | ||
error['database'] = database | ||
if (collection) | ||
error['collection'] = collection | ||
if (array) | ||
error['array'] = array | ||
if (data.error) | ||
@@ -836,6 +836,6 @@ data.error.push(error) | ||
createDocument, | ||
readDocument, | ||
updateDocument, | ||
deleteDocument, | ||
createObject, | ||
readObject, | ||
updateObject, | ||
deleteObject, | ||
} |
const path = require("path") | ||
const TerserPlugin = require("terser-webpack-plugin") | ||
const MiniCssExtractPlugin = require("mini-css-extract-plugin") | ||
let isProduction = process.env.NODE_ENV === "production" | ||
const { CleanWebpackPlugin } = require("clean-webpack-plugin") | ||
module.exports = { | ||
entry: { | ||
"CoCreate-mongodb": "./src/index.js", | ||
}, | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: isProduction ? "[name].min.js" : "[name].js", | ||
libraryTarget: "umd", | ||
libraryExport: "default", | ||
library: ["CoCreate", "mongodb"], | ||
globalObject: "this", | ||
}, | ||
module.exports = (env, argv) => { | ||
let isProduction = false | ||
if (argv.mode === 'production') | ||
isProduction = true | ||
plugins: [ | ||
new CleanWebpackPlugin(), | ||
new MiniCssExtractPlugin({ | ||
filename: "[name].css", | ||
}), | ||
], | ||
// Default mode for Webpack is production. | ||
// Depending on mode Webpack will apply different things | ||
// on final bundle. For now we don't need production's JavaScript | ||
// minifying and other thing so let's set mode to development | ||
mode: isProduction ? "production" : "development", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /.js$/, | ||
exclude: /(node_modules)/, | ||
use: { | ||
loader: "babel-loader", | ||
options: { | ||
plugins: ["@babel/plugin-transform-modules-commonjs"], | ||
}, | ||
const config = { | ||
entry: { | ||
"CoCreate-mongodb": "./src/index.js", | ||
}, | ||
}, | ||
{ | ||
test: /.css$/i, | ||
use: [ | ||
{ loader: "style-loader", options: { injectType: "linkTag" } }, | ||
"file-loader", | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: isProduction ? "[name].min.js" : "[name].js", | ||
libraryTarget: "umd", | ||
libraryExport: "default", | ||
library: ["CoCreate", "mongodb"], | ||
globalObject: "this", | ||
}, | ||
plugins: [ | ||
new CleanWebpackPlugin(), | ||
new MiniCssExtractPlugin({ | ||
filename: "[name].css", | ||
}), | ||
], | ||
}, | ||
], | ||
}, | ||
// Default mode for Webpack is production. | ||
// Depending on mode Webpack will apply different things | ||
// on final bundle. For now we don't need production's JavaScript | ||
// minifying and other thing so let's set mode to development | ||
mode: isProduction ? "production" : "development", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /.js$/, | ||
exclude: /(node_modules)/, | ||
use: { | ||
loader: "babel-loader", | ||
options: { | ||
plugins: ["@babel/plugin-transform-modules-commonjs"], | ||
}, | ||
}, | ||
}, | ||
{ | ||
test: /.css$/i, | ||
use: [ | ||
{ loader: "style-loader", options: { injectType: "linkTag" } }, | ||
"file-loader", | ||
], | ||
}, | ||
], | ||
}, | ||
// add source map | ||
...(isProduction ? {} : { devtool: "eval-source-map" }), | ||
// add source map | ||
...(isProduction ? {} : { devtool: "eval-source-map" }), | ||
optimization: { | ||
minimize: true, | ||
minimizer: [ | ||
new TerserPlugin({ | ||
extractComments: true, | ||
// cache: true, | ||
parallel: true, | ||
// sourceMap: true, // Must be set to true if using source-maps in production | ||
terserOptions: { | ||
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions | ||
// extractComments: 'all', | ||
compress: { | ||
drop_console: true, | ||
}, | ||
optimization: { | ||
minimize: true, | ||
minimizer: [ | ||
new TerserPlugin({ | ||
extractComments: true, | ||
// cache: true, | ||
parallel: true, | ||
// sourceMap: true, // Must be set to true if using source-maps in production | ||
terserOptions: { | ||
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions | ||
// extractComments: 'all', | ||
compress: { | ||
drop_console: true, | ||
}, | ||
}, | ||
}), | ||
], | ||
splitChunks: { | ||
chunks: "all", | ||
minSize: 200, | ||
// maxSize: 99999, | ||
//minChunks: 1, | ||
cacheGroups: { | ||
defaultVendors: false, | ||
}, | ||
}, | ||
}, | ||
}), | ||
], | ||
splitChunks: { | ||
chunks: "all", | ||
minSize: 200, | ||
// maxSize: 99999, | ||
//minChunks: 1, | ||
cacheGroups: { | ||
defaultVendors: false, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
return config | ||
} |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
78397
782
0
Updated@cocreate/utils@^1.21.16