Comparing version 1.0.4 to 1.0.5
180
index.js
const MongoClient = require('mongodb').MongoClient; | ||
const assert = require('assert'); | ||
// const assert = require('assert'); | ||
@@ -52,2 +52,34 @@ // // Connection URL | ||
/** | ||
* @typedef InitOptions | ||
* @prop {string} [url] (Currently unsued) MongoDB URL to connect to. Defaults to `"mongodb://localhost:27017"`. | ||
* @prop {string} [name] Name of the database to use. Defaults to `"comfyDB"`. | ||
*/ | ||
/** | ||
* @typedef FindOptions | ||
* @prop {string} [id] Find by the `_id` field. | ||
* @prop {string} [key] Find by the `key` field. | ||
* @prop {boolean} [count] Limit the amount of results. | ||
* @prop {string} [sortBy] A field to sort results by. Must also set | ||
* `isOrderDescending`. | ||
* @prop {boolean} [isOrderDescending] If `sortBy` is set, choose to sort by | ||
* descending (`true`) or ascending (`false`). | ||
* @prop {string} [field] A field to compare against. Must also set `compare` and `value`. | ||
* @prop {"=" | "!" | "<" | "<=" | ">" | ">=" | "^" | "$" | "_" | "1" | "0"} compare | ||
* Compare the set `field` against a `value`. Must also set `value` and `field`. | ||
* - Equal: "=", | ||
* - Not equal: "!", | ||
* - Less than: "<", | ||
* - Less than or equal: "<=", | ||
* - Greater than: ">", | ||
* - Greater than or equal: ">=", | ||
* - Starts with: "^", | ||
* - Ends with: "$", | ||
* - Contains: "_", | ||
* - True: "1", | ||
* - False: "0" | ||
* @prop {*} [value] A value to `compare` against at `field`. Must also set `compare` and `field`. | ||
*/ | ||
let comfyDBRunning = false; | ||
@@ -57,4 +89,12 @@ | ||
_mongo: null, | ||
/** @type {import('mongodb').MongoClient} */ | ||
_client: null, | ||
/** @type {import('mongodb').Db} */ | ||
_DB: null, | ||
/** | ||
* Initialize the connection. | ||
* | ||
* @param {InitOptions} options | ||
* @returns {Promise} | ||
*/ | ||
Init: function( { url = "mongodb://localhost:27017", name = "comfyDB" } = {} ) { | ||
@@ -78,3 +118,3 @@ return new Promise( async ( resolve, reject ) => { | ||
comfyDB._mongo.on( "ready", async () => { | ||
console.log( "Ready..." ); | ||
console.log( "[ComfyDB] Ready..." ); | ||
comfyDBRunning = true; | ||
@@ -86,3 +126,3 @@ comfyDB._client = await MongoClient.connect( url, { useNewUrlParser: true } ); | ||
comfyDB._mongo.on( "exit", ( code ) => { | ||
console.log( "Exit:", code ); | ||
console.log( "[ComfyDB] Exit:", code ); | ||
comfyDB.Close(); | ||
@@ -97,2 +137,15 @@ comfyDBRunning = false; | ||
}, | ||
/** | ||
* Check if the database is running. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
IsRunning: function() { | ||
return comfyDBRunning; | ||
}, | ||
/** | ||
* Close the connection and shutdown the server. | ||
* | ||
* @param {boolean} [shouldExit] Whether or not to end the process. Defauls to true. | ||
*/ | ||
Close: function( shouldExit = true ) { | ||
@@ -111,5 +164,11 @@ if( comfyDB._client ) { | ||
}, | ||
/** | ||
* Create a backup of the database. (TODO) | ||
*/ | ||
Backup: function() { | ||
// TODO: Backup Database | ||
}, | ||
/** | ||
* Restore from a backup of the database. (TODO) | ||
*/ | ||
Restore: function() { | ||
@@ -119,2 +178,8 @@ // TODO: Restore Database | ||
Collections: { | ||
/** | ||
* Create a collection in the database. | ||
* | ||
* @param {string} name Name of the collection to create. | ||
* @returns {Promise<string>} | ||
*/ | ||
Create: async function( name ) { | ||
@@ -124,2 +189,7 @@ if( !comfyDB._DB ) { throw new Error( "No Connection" ); } | ||
}, | ||
/** | ||
* List the collections in the database. | ||
* | ||
* @return {Promise<string[]>} | ||
*/ | ||
List: async function() { | ||
@@ -129,2 +199,8 @@ if( !comfyDB._DB ) { throw new Error( "No Connection" ); } | ||
}, | ||
/** | ||
* Delete/drop a collection in the database. | ||
* | ||
* @param {string} name Name of the collection to delete. | ||
* @return {Promise} | ||
*/ | ||
Delete: async function( name ) { | ||
@@ -137,2 +213,8 @@ if( !comfyDB._DB ) { throw new Error( "No Connection" ); } | ||
Data: { | ||
/** | ||
* (TODO) | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {*} options | ||
*/ | ||
Set: function( collection, { options } ) { | ||
@@ -142,2 +224,13 @@ // check if key is a single string or an array for batch update | ||
}, | ||
/** | ||
* Set data in the database by a key name. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string | string[]} key Key or array of keys to save data to. | ||
* @param {* | []} data Data to save to the key. If `key` is an array, | ||
* data must also be an array that matches the length of the `key` array. | ||
* @param {*} [overwrite] Whether or not to overwrite existing data in the | ||
* database. Defaults to true. | ||
* @returns {Promise<import("mongodb").BulkWriteResult | import("mongodb").InsertWriteOpResult>} | ||
*/ | ||
SetByKey: function( collection, key, data, overwrite = true ) { | ||
@@ -177,2 +270,8 @@ // check if key is a single string or an array for batch update | ||
}, | ||
/** | ||
* (TODO) | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {*} options | ||
*/ | ||
Delete: function( collection, { options } ) { | ||
@@ -182,2 +281,9 @@ // check if key is a single string or an array for batch update | ||
}, | ||
/** | ||
* Delete data in the database by key name. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string | string[]} key Key or array of keys to delete data at. | ||
* @returns {Promise<import("mongodb").BulkWriteResult>} | ||
*/ | ||
DeleteByKey: function( collection, key ) { | ||
@@ -199,2 +305,11 @@ // check if key is a single string or an array for batch update | ||
}, | ||
/** | ||
* Increment a value at field of key in the database. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string | string[]} key Key or array of keys to increment. | ||
* @param {string} field Field of key to increment. | ||
* @param {number} amount Amount to increment field. | ||
* @returns {Promise<import("mongodb").BulkWriteResult>} | ||
*/ | ||
Increment: function( collection, key, field, amount ) { | ||
@@ -225,5 +340,21 @@ const set = comfyDB._DB.collection( collection ); | ||
}, | ||
/** | ||
* Decrement a value at field of key in the database. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string | string[]} key Key or array of keys to decrement. | ||
* @param {string} field Field of key to decrement. | ||
* @param {number} amount Amount to decrement field. | ||
* @returns {Promise<import("mongodb").BulkWriteResult>} | ||
*/ | ||
Decrement: function( collection, key, field, amount ) { | ||
comfyDB.Data.Increment( collection, key, field, -amount ); | ||
return comfyDB.Data.Increment( collection, key, field, -amount ); | ||
}, | ||
/** | ||
* Query data in the database by some options. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {FindOptions} options Options for the find query. | ||
* @returns {Promise<any[]>} | ||
*/ | ||
Find: function( collection, options ) { | ||
@@ -285,8 +416,33 @@ // TODO: Add time query for how long it took | ||
}, | ||
/** | ||
* Query data in the database by its document `_id`. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string} id A document _id value. | ||
* @returns {Promise<any[]>} | ||
*/ | ||
FindById: function( collection, id ) { | ||
return comfyDB.Data.Find( collection, { id } ); | ||
}, | ||
/** | ||
* Query data in the database by its document `_id`. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string} id A document _id value. | ||
* @returns {Promise<any[]>} | ||
*/ | ||
FindByKey: function( collection, key ) { | ||
return comfyDB.Data.Find( collection, { key } ); | ||
}, | ||
/** | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string} field Name of the field to find by.() | ||
* @param {"=" | "!" | "<" | "<=" | ">" | ">=" | "^" | "$" | "_" | "1" | "0"} compare Compare the set `field` against a `value`. See Find. Defaults to True. | ||
* @param {*} [value] Value to find by when comparing. Defaults to "". | ||
* @param {number} [count] Limit the amount of results. Defaults to 100. | ||
* @param {boolean} [descending] If the results should be sorted by | ||
* descending order. Defaults to true. | ||
* @returns {Promise<any[]>} | ||
*/ | ||
FindByField: function( collection, field, compare = COMPARE.True, value = "", count = 100, descending = true ) { | ||
@@ -303,2 +459,10 @@ // TODO: Support an array of fields and comparisons | ||
}, | ||
/** | ||
* Get the last `count` items in a collection. | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {number} [count] Limit the amount of results in the response. | ||
* Defaults to 100. | ||
* @returns {Promise<any[]>} | ||
*/ | ||
FindLatest: function( collection, count = 100 ) { | ||
@@ -311,2 +475,10 @@ return comfyDB.Data.Find( collection, { | ||
}, | ||
/** | ||
* | ||
* @param {string} collection Name of the collection in the database. | ||
* @param {string} field Name of the field to count. | ||
* @param {string} [compare] Compare operator. See Find. Defaults to True. | ||
* @param {*} [value] Value to compare against. Defaults to "". | ||
* @returns {Promise<number>} | ||
*/ | ||
Count: function( collection, field, compare = COMPARE.True, value = "" ) { | ||
@@ -313,0 +485,0 @@ // TODO: Optimize!!! |
{ | ||
"name": "comfydb", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Comfiest Way To Use A Database!", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26239
546