Skip to content

Commit 35484c0

Browse files
author
Snowflake107
committed
deprecate db#table and fix db#get and db#has
1 parent 2573e1a commit 35484c0

File tree

8 files changed

+191
-162
lines changed

8 files changed

+191
-162
lines changed

Diff for: .npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.github/
2-
test/
2+
test/
3+
docs/

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mongo.on("ready", () => importData());
5252
# Links
5353
- **[Discord Support Server](https://discord.gg/2SUybzb)**
5454
- **[Documentation](https://quickmongo.js.org)**
55-
- **[GitHub](https://github.com/Snowflake107/quickmongo)**
55+
- **[GitHub](https://github.com/DevSnowflake/quickmongo)**
5656

5757
# Example
5858

Diff for: index.d.ts

-151
This file was deleted.

Diff for: package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "quickmongo",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"description": "Quick mongodb wrapper for beginners.",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"types": "typings/index.d.ts",
77
"exports": {
88
".": [
99
{
@@ -21,12 +21,12 @@
2121
},
2222
"repository": {
2323
"type": "git",
24-
"url": "git+https://github.com/Snowflake107/quickmongo.git"
24+
"url": "git+https://github.com/DevSnowflake/quickmongo.git"
2525
},
2626
"author": "Snowflake107",
2727
"license": "Apache License 2.0",
2828
"bugs": {
29-
"url": "https://github.com/Snowflake107/quickmongo/issues"
29+
"url": "https://github.com/DevSnowflake/quickmongo/issues"
3030
},
3131
"homepage": "https://quickmongo.js.org",
3232
"funding": "https://www.paypal.me/devsnowflake",

Diff for: src/Main.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Schema = require("./Schema");
33
const Error = require("./Error");
44
const fs = require("fs");
55
const Util = require("./Util");
6+
const { deprecate } = require("util");
67

78
/**
89
* Quick mongodb wrapper
@@ -95,8 +96,17 @@ class Database extends Base {
9596
*/
9697
async exists(key) {
9798
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
98-
let get = await this.get(key);
99-
return !!get;
99+
const parsed = Util.parseKey(key);
100+
101+
let get = await this.schema.findOne({ ID: parsed.key })
102+
.catch(e => {
103+
return this.emit("error", e);
104+
});
105+
if (!get) return null;
106+
let item;
107+
if (parsed.target) item = Util.getData(key, Object.assign({}, get.data));
108+
else item = get.data;
109+
return item === undefined ? false : true;
100110
}
101111

102112
/**
@@ -127,7 +137,7 @@ class Database extends Base {
127137
let item;
128138
if (parsed.target) item = Util.getData(key, Object.assign({}, get.data));
129139
else item = get.data;
130-
return item ? item : null;
140+
return item !== undefined ? item : null;
131141
}
132142

133143
/**
@@ -577,13 +587,25 @@ class Database extends Base {
577587
* This method acts like `quick.db#table`. It will return new instance of itself.
578588
* @param {string} name Model name
579589
* @returns {Database}
590+
* @deprecated
580591
*/
581592
table(name) {
582593
if (!name || typeof name !== "string") throw new Error("Invalid model name");
583594
const CustomModel = new Database(this.dbURL, name, this.options);
584595
return CustomModel;
585596
}
586597

598+
/**
599+
* This method acts like `quick.db#table`. It will return new instance of itself.
600+
* @param {string} name Model name
601+
* @returns {Database}
602+
*/
603+
createModel(name) {
604+
if (!name || typeof name !== "string") throw new Error("Invalid model name");
605+
const CustomModel = new Database(this.dbURL, name, this.options);
606+
return CustomModel;
607+
}
608+
587609
/**
588610
* This method exports **QuickMongo** data to **Quick.db**
589611
* @param {any} quickdb Quick.db instance
@@ -641,4 +663,6 @@ class Database extends Base {
641663

642664
}
643665

666+
Database.prototype.table = deprecate(Database.prototype.table, "db#table is deprecated and will be removed in future update, please use db#createModel instead!");
667+
644668
module.exports = Database;

Diff for: src/Schema.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const { Schema, model } = require("mongoose");
33
const Default = new Schema({
44
ID: {
55
type: Schema.Types.String,
6-
required: true
6+
required: true,
7+
unique: true
78
},
89
data: {
910
type: Schema.Types.Mixed,

Diff for: test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function execute() {
2727
console.log('[13] Fetching deleted prop:', (await db.get('myObj.prop')) === null);
2828

2929
// Fetching properties from specific tables
30-
const test = db.table('test');
30+
const test = db.createModel('test');
3131
test.set('data', 'hello world').then(console.log);
3232
console.log(await db.get('data')); // -> null
3333
console.log(test.get('data')); // -> 'hello world'

0 commit comments

Comments
 (0)