quickmongo
Advanced tools
Comparing version 5.0.0 to 5.0.1
@@ -1,2 +0,3 @@ | ||
import mongoose from 'mongoose'; | ||
import mongoose, { NativeError } from 'mongoose'; | ||
import * as mongodb from 'mongodb'; | ||
import { TypedEmitter } from 'tiny-typed-emitter'; | ||
@@ -35,3 +36,3 @@ | ||
sort?: string; | ||
filter?: (data: T, idx: number) => boolean; | ||
filter?: (data: AllData<T>, idx: number) => boolean; | ||
} | ||
@@ -56,2 +57,14 @@ /** | ||
ready: (db: Database<V>) => unknown; | ||
connecting: () => unknown; | ||
connected: () => unknown; | ||
open: () => unknown; | ||
disconnecting: () => unknown; | ||
disconnected: () => unknown; | ||
close: () => unknown; | ||
reconnected: () => unknown; | ||
error: (error: NativeError) => unknown; | ||
fullsetup: () => unknown; | ||
all: () => unknown; | ||
reconnectFailed: () => unknown; | ||
reconnectTries: () => unknown; | ||
} | ||
@@ -109,2 +122,8 @@ /** | ||
/** | ||
* Get item from the database | ||
* @param {string} key The key | ||
* @returns {Promise<any>} | ||
*/ | ||
fetch<V = T>(key: string): Promise<V>; | ||
/** | ||
* Set item in the database | ||
@@ -159,2 +178,7 @@ * @param {string} key The key | ||
/** | ||
* Drops this database | ||
* @returns {Promise<boolean>} | ||
*/ | ||
drop(): Promise<boolean>; | ||
/** | ||
* Identical to quick.db push | ||
@@ -193,2 +217,16 @@ * @param {string} key The key | ||
/** | ||
* The db metadata | ||
* @type {?Object} | ||
*/ | ||
get metadata(): { | ||
name: string; | ||
db: string; | ||
namespace: string; | ||
}; | ||
/** | ||
* Returns database statistics | ||
* @returns {Promise<CollStats>} | ||
*/ | ||
stats(): Promise<mongodb.CollStats>; | ||
/** | ||
* Close the database connection | ||
@@ -199,2 +237,3 @@ * @param {?boolean} [force=false] Close forcefully | ||
close(force?: boolean): Promise<void>; | ||
private __applyEventsBinding; | ||
/** | ||
@@ -201,0 +240,0 @@ * Formats document data |
@@ -1,1 +0,1 @@ | ||
var M=Object.create;var h=Object.defineProperty,P=Object.defineProperties,R=Object.getOwnPropertyDescriptor,S=Object.getOwnPropertyDescriptors,k=Object.getOwnPropertyNames,b=Object.getOwnPropertySymbols,B=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var T=(i,t,e)=>t in i?h(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,A=(i,t)=>{for(var e in t||(t={}))_.call(t,e)&&T(i,e,t[e]);if(b)for(var e of b(t))K.call(t,e)&&T(i,e,t[e]);return i},D=(i,t)=>P(i,S(t)),E=i=>h(i,"__esModule",{value:!0});var U=(i,t)=>{for(var e in t)h(i,e,{get:t[e],enumerable:!0})},C=(i,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of k(t))!_.call(i,r)&&(e||r!=="default")&&h(i,r,{get:()=>t[r],enumerable:!(n=R(t,r))||n.enumerable});return i},m=(i,t)=>C(E(h(i!=null?M(B(i)):{},"default",!t&&i&&i.__esModule?{get:()=>i.default,enumerable:!0}:{value:i,enumerable:!0})),i),j=(i=>(t,e)=>i&&i.get(t)||(e=C(E({}),t,1),i&&i.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var a=(i,t,e)=>new Promise((n,r)=>{var s=u=>{try{l(e.next(u))}catch(g){r(g)}},c=u=>{try{l(e.throw(u))}catch(g){r(g)}},l=u=>u.done?n(u.value):Promise.resolve(u.value).then(s,c);l((e=e.apply(i,t)).next())});var V={};U(V,{Database:()=>w,Util:()=>o,docSchema:()=>O});var p=m(require("mongoose")),O=new p.default.Schema({ID:{type:p.default.SchemaTypes.String,required:!0,unique:!0},data:{type:p.default.SchemaTypes.Mixed,required:!1}},{timestamps:!0});function f(i,t="JSON"){return i.model(t,O)}var I=m(require("mongoose"));var x=require("tiny-typed-emitter");var N=m(require("lodash")),o=class extends null{constructor(){}static v(t,e,n){return typeof t===e&&!!t?t:n}static pick(t,e){if(!t||typeof t!="object"||!e||typeof e!="string"||!e.includes("."))return t;let n=o.getKeyMetadata(e);return N.default.get(Object.assign({},t),n.target)}static getKey(t){return t.split(".").shift()}static getKeyMetadata(t){let[e,...n]=t.split(".");return{master:e,child:n,target:n.join(".")}}};var d=m(require("lodash")),w=class extends x.TypedEmitter{constructor(t,e={}){super();this.url=t;this.options=e;this.parent=null;this.__child__=!1;this.model=null;Object.defineProperty(this,"__child__",{writable:!0,enumerable:!1,configurable:!0})}isChild(){return!this.isParent()}isParent(){return!this.__child__}get ready(){return!!(this.model&&this.connection)}get readyState(){var t,e;return(e=(t=this.connection)==null?void 0:t.readyState)!=null?e:0}getRaw(t){return a(this,null,function*(){return this.__readyCheck(),yield this.model.findOne({ID:o.getKey(t)})})}get(t){return a(this,null,function*(){let e=yield this.getRaw(t),n=this.__formatData(e);return o.pick(n,t)})}set(t,e){return a(this,null,function*(){if(this.__readyCheck(),t.includes(".")){let n=o.getKeyMetadata(t),r=yield this.model.findOne({ID:n.master});if(!r)return yield this.model.create({ID:n.master,data:d.default.set({},n.target,e)}),yield this.get(t);if(r.data!==null&&typeof r.data!="object")throw new Error("CANNOT_TARGET_NON_OBJECT");let s=Object.assign({},r.data),c=d.default.set(s,n.target,e);return yield r.updateOne({$set:{data:c}}),yield this.get(n.master)}else return yield this.model.findOneAndUpdate({ID:t},{$set:{data:e}},{upsert:!0}),yield this.get(t)})}has(t){return a(this,null,function*(){return(yield this.get(t))!=null})}delete(t){return a(this,null,function*(){this.__readyCheck();let e=o.getKeyMetadata(t);if(!e.target)return(yield this.model.deleteOne({ID:e.master})).deletedCount>0;let n=yield this.model.findOne({ID:e.master});if(!n)return!1;if(n.data!==null&&typeof n.data!="object")throw new Error("CANNOT_TARGET_NON_OBJECT");let r=Object.assign({},n.data);return d.default.unset(r,e.target),yield n.updateOne({$set:{data:r}}),!0})}deleteAll(){return a(this,null,function*(){return(yield this.model.deleteMany()).deletedCount>0})}count(){return a(this,null,function*(){return yield this.model.estimatedDocumentCount()})}ping(){return a(this,null,function*(){let t=Date.now();return yield this.get("SOME_RANDOM_KEY"),Date.now()-t})}instantiateChild(t,e){return a(this,null,function*(){return yield new w(e||this.url,D(A({},this.options),{child:!0,parent:this,collectionName:t,shareConnectionFromParent:!!e||!0})).connect()})}all(t){return a(this,null,function*(){this.__readyCheck();let n=(yield this.model.find()).map(r=>({ID:r.ID,data:this.__formatData(r)})).filter((r,s)=>(t==null?void 0:t.filter)?t.filter(r.data,s):!0);if(typeof(t==null?void 0:t.sort)=="string"){t.sort.startsWith(".")&&(t.sort=t.sort.slice(1));let r=t.sort.split(".");n=d.default.sortBy(n,r).reverse()}return typeof(t==null?void 0:t.limit)=="number"&&t.limit>0?n.slice(0,t.limit):n})}push(t,e){return a(this,null,function*(){let n=yield this.get(t);if(n==null)return Array.isArray(e)?yield this.set(t,e):yield this.set(t,[e]);if(!Array.isArray(n))throw new Error("TARGET_EXPECTED_ARRAY");return Array.isArray(e)?yield this.set(t,n.concat(e)):(n.push(e),yield this.set(t,n))})}pull(t,e,n=!0){return a(this,null,function*(){let r=yield this.get(t);if(r==null)return!1;if(!Array.isArray(r))throw new Error("TARGET_EXPECTED_ARRAY");if(Array.isArray(e))return r=r.filter(s=>!e.includes(s)),yield this.set(t,r);if(n)return r=r.filter(s=>s!==e),yield this.set(t,r);{if(!r.some(l=>l===e))return!1;let c=r.findIndex(l=>l===e);return r=r.splice(c,1),yield this.set(t,r)}})}add(t,e){return a(this,null,function*(){if(typeof e!="number")throw new TypeError("VALUE_MUST_BE_NUMBER");let n=yield this.get(t);return yield this.set(t,(typeof n=="number"?n:0)+e)})}subtract(t,e){return a(this,null,function*(){if(typeof e!="number")throw new TypeError("VALUE_MUST_BE_NUMBER");let n=yield this.get(t);return yield this.set(t,(typeof n=="number"?n:0)-e)})}connect(){return new Promise((t,e)=>{if(typeof this.url!="string"||!this.url)return e(new Error("MISSING_MONGODB_URL"));this.__child__=Boolean(this.options.child),this.parent=this.options.parent||null;let n=this.options.collectionName,r=!!this.options.shareConnectionFromParent;if(delete this.options.collectionName,delete this.options.child,delete this.options.parent,delete this.options.shareConnectionFromParent,r&&this.__child__&&this.parent)return this.parent.connection?(this.connection=this.parent.connection,this.model=f(this.connection,o.v(n,"string","JSON")),t(this)):e(new Error("PARENT_HAS_NO_CONNECTION"));I.default.createConnection(this.url,this.options,(s,c)=>{if(s)return e(s);this.connection=c,this.model=f(this.connection,o.v(n,"string","JSON")),this.emit("ready",this),t(this)})})}close(t=!1){return a(this,null,function*(){return yield this.connection.close(t)})}__formatData(t){return(t==null?void 0:t.data)?t.data:null}__readyCheck(){if(!this.model)throw new Error("DATABASE_NOT_READY")}};module.exports=j(V);0&&(module.exports={Database,Util,docSchema}); | ||
var x=Object.create;var d=Object.defineProperty,P=Object.defineProperties,M=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,S=Object.getOwnPropertyNames,_=Object.getOwnPropertySymbols,B=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var T=(i,t,e)=>t in i?d(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,D=(i,t)=>{for(var e in t||(t={}))b.call(t,e)&&T(i,e,t[e]);if(_)for(var e of _(t))v.call(t,e)&&T(i,e,t[e]);return i},A=(i,t)=>P(i,R(t)),E=i=>d(i,"__esModule",{value:!0});var K=(i,t)=>{for(var e in t)d(i,e,{get:t[e],enumerable:!0})},C=(i,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of S(t))!b.call(i,r)&&(e||r!=="default")&&d(i,r,{get:()=>t[r],enumerable:!(n=M(t,r))||n.enumerable});return i},p=(i,t)=>C(E(d(i!=null?x(B(i)):{},"default",!t&&i&&i.__esModule?{get:()=>i.default,enumerable:!0}:{value:i,enumerable:!0})),i),V=(i=>(t,e)=>i&&i.get(t)||(e=C(E({}),t,1),i&&i.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var a=(i,t,e)=>new Promise((n,r)=>{var s=u=>{try{l(e.next(u))}catch(g){r(g)}},c=u=>{try{l(e.throw(u))}catch(g){r(g)}},l=u=>u.done?n(u.value):Promise.resolve(u.value).then(s,c);l((e=e.apply(i,t)).next())});var U={};K(U,{Database:()=>w,Util:()=>o,docSchema:()=>O});var m=p(require("mongoose")),O=new m.default.Schema({ID:{type:m.default.SchemaTypes.String,required:!0,unique:!0},data:{type:m.default.SchemaTypes.Mixed,required:!1}},{timestamps:!0});function f(i,t="JSON"){return i.model(t,O)}var k=p(require("mongoose"));var I=require("tiny-typed-emitter");var N=p(require("lodash")),o=class extends null{constructor(){}static v(t,e,n){return typeof t===e&&!!t?t:n}static pick(t,e){if(!t||typeof t!="object"||!e||typeof e!="string"||!e.includes("."))return t;let n=o.getKeyMetadata(e);return N.default.get(Object.assign({},t),n.target)}static getKey(t){return t.split(".").shift()}static getKeyMetadata(t){let[e,...n]=t.split(".");return{master:e,child:n,target:n.join(".")}}};var h=p(require("lodash")),w=class extends I.TypedEmitter{constructor(t,e={}){super();this.url=t;this.options=e;this.parent=null;this.__child__=!1;this.model=null;Object.defineProperty(this,"__child__",{writable:!0,enumerable:!1,configurable:!0})}isChild(){return!this.isParent()}isParent(){return!this.__child__}get ready(){return!!(this.model&&this.connection)}get readyState(){var t,e;return(e=(t=this.connection)==null?void 0:t.readyState)!=null?e:0}getRaw(t){return a(this,null,function*(){return this.__readyCheck(),yield this.model.findOne({ID:o.getKey(t)})})}get(t){return a(this,null,function*(){let e=yield this.getRaw(t),n=this.__formatData(e);return o.pick(n,t)})}fetch(t){return a(this,null,function*(){return yield this.get(t)})}set(t,e){return a(this,null,function*(){if(this.__readyCheck(),t.includes(".")){let n=o.getKeyMetadata(t),r=yield this.model.findOne({ID:n.master});if(!r)return yield this.model.create({ID:n.master,data:h.default.set({},n.target,e)}),yield this.get(t);if(r.data!==null&&typeof r.data!="object")throw new Error("CANNOT_TARGET_NON_OBJECT");let s=Object.assign({},r.data),c=h.default.set(s,n.target,e);return yield r.updateOne({$set:{data:c}}),yield this.get(n.master)}else return yield this.model.findOneAndUpdate({ID:t},{$set:{data:e}},{upsert:!0}),yield this.get(t)})}has(t){return a(this,null,function*(){return(yield this.get(t))!=null})}delete(t){return a(this,null,function*(){this.__readyCheck();let e=o.getKeyMetadata(t);if(!e.target)return(yield this.model.deleteOne({ID:e.master})).deletedCount>0;let n=yield this.model.findOne({ID:e.master});if(!n)return!1;if(n.data!==null&&typeof n.data!="object")throw new Error("CANNOT_TARGET_NON_OBJECT");let r=Object.assign({},n.data);return h.default.unset(r,e.target),yield n.updateOne({$set:{data:r}}),!0})}deleteAll(){return a(this,null,function*(){return(yield this.model.deleteMany()).deletedCount>0})}count(){return a(this,null,function*(){return yield this.model.estimatedDocumentCount()})}ping(){return a(this,null,function*(){let t=Date.now();return yield this.get("SOME_RANDOM_KEY"),Date.now()-t})}instantiateChild(t,e){return a(this,null,function*(){return yield new w(e||this.url,A(D({},this.options),{child:!0,parent:this,collectionName:t,shareConnectionFromParent:!!e||!0})).connect()})}all(t){return a(this,null,function*(){this.__readyCheck();let n=(yield this.model.find()).map(r=>({ID:r.ID,data:this.__formatData(r)})).filter((r,s)=>(t==null?void 0:t.filter)?t.filter({ID:r.ID,data:r.data},s):!0);if(typeof(t==null?void 0:t.sort)=="string"){t.sort.startsWith(".")&&(t.sort=t.sort.slice(1));let r=t.sort.split(".");n=h.default.sortBy(n,r).reverse()}return typeof(t==null?void 0:t.limit)=="number"&&t.limit>0?n.slice(0,t.limit):n})}drop(){return a(this,null,function*(){return this.__readyCheck(),yield this.model.collection.drop()})}push(t,e){return a(this,null,function*(){let n=yield this.get(t);if(n==null)return Array.isArray(e)?yield this.set(t,e):yield this.set(t,[e]);if(!Array.isArray(n))throw new Error("TARGET_EXPECTED_ARRAY");return Array.isArray(e)?yield this.set(t,n.concat(e)):(n.push(e),yield this.set(t,n))})}pull(t,e,n=!0){return a(this,null,function*(){let r=yield this.get(t);if(r==null)return!1;if(!Array.isArray(r))throw new Error("TARGET_EXPECTED_ARRAY");if(Array.isArray(e))return r=r.filter(s=>!e.includes(s)),yield this.set(t,r);if(n)return r=r.filter(s=>s!==e),yield this.set(t,r);{if(!r.some(l=>l===e))return!1;let c=r.findIndex(l=>l===e);return r=r.splice(c,1),yield this.set(t,r)}})}add(t,e){return a(this,null,function*(){if(typeof e!="number")throw new TypeError("VALUE_MUST_BE_NUMBER");let n=yield this.get(t);return yield this.set(t,(typeof n=="number"?n:0)+e)})}subtract(t,e){return a(this,null,function*(){if(typeof e!="number")throw new TypeError("VALUE_MUST_BE_NUMBER");let n=yield this.get(t);return yield this.set(t,(typeof n=="number"?n:0)-e)})}connect(){return new Promise((t,e)=>{if(typeof this.url!="string"||!this.url)return e(new Error("MISSING_MONGODB_URL"));this.__child__=Boolean(this.options.child),this.parent=this.options.parent||null;let n=this.options.collectionName,r=!!this.options.shareConnectionFromParent;if(delete this.options.collectionName,delete this.options.child,delete this.options.parent,delete this.options.shareConnectionFromParent,r&&this.__child__&&this.parent)return this.parent.connection?(this.connection=this.parent.connection,this.model=f(this.connection,o.v(n,"string","JSON")),t(this)):e(new Error("PARENT_HAS_NO_CONNECTION"));k.default.createConnection(this.url,this.options,(s,c)=>{if(s)return e(s);this.connection=c,this.model=f(this.connection,o.v(n,"string","JSON")),this.emit("ready",this),this.__applyEventsBinding(),t(this)})})}get metadata(){return this.model?{name:this.model.collection.name,db:this.model.collection.dbName,namespace:this.model.collection.namespace}:null}stats(){return a(this,null,function*(){return this.__readyCheck(),yield this.model.collection.stats()})}close(t=!1){return a(this,null,function*(){return yield this.connection.close(t)})}__applyEventsBinding(){this.__readyCheck();let t=["connecting","connected","open","disconnecting","disconnected","close","reconnected","error","fullsetup","all","reconnectFailed","reconnectTries"];for(let e of t)this.connection.prependListener(e,(...n)=>{this.emit(e,...n)})}__formatData(t){return(t==null?void 0:t.data)?t.data:null}__readyCheck(){if(!this.model)throw new Error("DATABASE_NOT_READY")}};module.exports=V(U);0&&(module.exports={Database,Util,docSchema}); |
{ | ||
"name": "quickmongo", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "Quick Mongodb wrapper for beginners that provides key-value based interface.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -26,5 +26,5 @@ # QuickMongo | ||
```js | ||
const { Database } = require("quickmongo"); | ||
import { Database } from "quickmongo"; | ||
const db = new Database("mongodb://localhost/quickmongo"); | ||
const db = new Database("mongodb://localhost:27017/quickmongo"); | ||
@@ -36,5 +36,6 @@ db.on("ready", () => { | ||
db.connect(); | ||
// top-level awaits | ||
await db.connect(); | ||
function doStuff() { | ||
async function doStuff() { | ||
// Setting an object in the database: | ||
@@ -63,3 +64,3 @@ await db.set("userInfo", { difficulty: "Easy" }); | ||
// remove item | ||
db.pull("userInfo", "Sword", "items").then(console.log); | ||
await db.pull("userInfo.items", "Sword"); | ||
// -> { difficulty: 'Easy', items: ['Watch'], balance: 1000 } | ||
@@ -66,0 +67,0 @@ } |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
28360
340
71
0