项目作者: aloshai

项目描述 :
A fast, optimized and easy-to-use database manager. MongoDB has never been easier! Perfect for beginners!
高级语言: JavaScript
项目地址: git://github.com/aloshai/mongosha.git
创建时间: 2020-12-26T14:14:28Z
项目社区:https://github.com/aloshai/mongosha

开源协议:MIT License

下载


MONGOSHA

Mongosha is a simple to use database manager. It allows you to do your transactions in a very short way and all you have to do is specify path!

Installation

Download the package npm i mongosha or yarn add mongosha

Quickstart

  1. const { Mongosha } = require("mongosha");
  2. const client = await Mongosha.connect("MONGODB_CONNECTION_STRING");
  3. const db = client.database("example_database");
  4. const collection = db.collection("users");
  5. const data = collection.data("user_id_0");
  6. // set and delete
  7. data.set("identify", { "name": "Mert", "surname": "Yılmaz" }); // => { "name": "Mert", "surname": "Yılmaz" }
  8. data.set("wallet", { price: 1500 }); // => { price: 1500 }
  9. data.set("adress", "Europe/Istanbul"); // => "Europe/Istanbul"
  10. data.set("mail", "aloshai@aloshai.com"); // => "aloshai@aloshai.com"
  11. data.delete("mail"); // => Promise<void>
  12. // addition and subtraction
  13. data.add("wallet", 10); // => 1510
  14. data.subtract("wallet", 1000); // => 510
  15. // array operations
  16. data.push("inventory", "Sword"); // => "Sword"
  17. data.pushRange("inventory", ["Shield", "Gum", "Gum", "Phone"]); // => ["Shield", "Gum", "Gum", "Phone"]
  18. data.pushRange("favorite_numbers", [1, 2, 3, 4, 10, 5, 30, 6, 10]);
  19. data.pull("inventory", "Sword"); // Promise<void>
  20. data.pullAll("inventory", "Gum"); // Promise<void>
  21. // sort
  22. data.sort("favorite_numbers", "ASC"); // => [1, 2, 3, 4, 5, 6, 10, 10, 30]
  23. data.sort("favorite_numbers", "DESC"); // => [30, 10, 10, 6, 5, 4, 3, 2, 1]
  24. // has path any value?
  25. data.has("wallet"); // => true
  26. data.has("mail"); // => false
  27. data.has("otherField"); // => false

API Documentation

Here are our APIs.

  • Mongosha
  • Client
  • Database
  • Collection
  • Data

Mongosha

Mongosha is a model. Use Mongosha like this.

  1. var { Mongosha } = require("mongosha");
  2. const options = {
  3. useUnifiedTopology: true
  4. };
  5. // We connect to a new address:
  6. const client = Mongosha.connect("mongodb://localhost:27017", options);

API References

  • .connect(url, options?): Promise<void>: Creates a new client.

Client

Client is a class. Use Client like this.

  1. const client = Mongosha.connect(url, options);
  2. // To create a database:
  3. var database = client.database("my_good_database"); // => Returns new 'Database' class template
  4. // To drop:
  5. client.dropDatabase("my_bad_database"); // => Promise<void>

API References

  • .database(databaseName): Database: If database does not exists, creates database then returns the database. Otherwise just returns database.
  • .dropDatabase(databaseName): Promise<void>: Drops the Database.

Database

Database is a class. Use Database like this.

  1. var database = client.database("my_good_database");
  2. // To create a collection:
  3. var collection = database.collection("my_users"); // => Returns new 'Collection' class template
  4. // To Drop:
  5. database.dropCollection("my_bad_users"); // => Promise<void>

API References

  • .collection(databaseName): Collection: If collection does not exists, creates collection then returns the collection. Otherwise just returns collection.
  • .dropCollection(databaseName): Promise<void>: Drops the Collection.

Collection

Collection is a class. Use Collection like this.

  1. var collection = database.collection("my_users");
  2. // Created a data named user_0. Here user_0 is a key. Here you can enter a user-specific ID or something else.
  3. var data = collection.data("user_0"); // => Returns new 'Data' class template
  4. // Have assigned a value of 'Alosha' to the 'nickname' field of all the data in the collection.
  5. collection.set("nickname", "Alosha");
  6. // The 'notes' field has been removed for all data in the collection.
  7. collection.delete("notes");
  8. // Added 500 to the 'score' field of all the data in the collection.
  9. collection.add("score", 500);
  10. // Subtracted 500 from the 'score' field for all data in the collection.
  11. collection.subtract("score", 250);
  12. // 'Gum' has been pushed to the 'items' field for all data in the collection.
  13. collection.push("items", "Gum");
  14. // 'Gum' and 'Axe' has been pushed to the 'items' field for all data in the collection.
  15. collection.pushRange("items", ["Gum", "Axe"]);
  16. // 'Axe' is pulled from 'items' field for all data in the collection.
  17. collection.pull("items", "Axe");

API References

  • .set(path, value): Promise<UpdateWriteOpResult>: In all data, the value assigns to the specified path.
  • .delete(path): Promise<UpdateWriteOpResult>: Removes the specified path in all data.
  • .add(path, value): Promise<UpdateWriteOpResult>: Performs mathematical addition for the specified path in all data.
  • .subtract(path, value): Promise<UpdateWriteOpResult>: Performs mathematical subtraction for the specified path in all data.
  • .push(path, value): Promise<UpdateWriteOpResult>: In all data, an array element is added to the specified path.
  • .pushRange(path, values): Promise<UpdateWriteOpResult>: In all data, more than one array element is added to the specified path.
  • .pull(path, value): Promise<UpdateWriteOpResult>: It extracts one or more elements from the specified path in all data.
  • .sort(path, orderType, limit?): Sorts all data in descending or ascending order based on the value in the specified path.

Data

Data is a class. You can use like it this:

  1. var data = collection.data("user_0");
  2. // Assigns the value 'Mongosha' to the specified path.
  3. data.set("name", "Mongosha");
  4. // Assigns the object `{ id: 500, phone: "+123456789" }` to the specified path.
  5. data.set("identify", { id: 500, phone: "+123456789" });
  6. // Assigns the value '499' to the specified path.
  7. data.set("identify.id", 499);
  8. // Assigns the value 0 to the specified path.
  9. data.set("wallet", 0);
  10. // Get the value in path "name".
  11. data.get("name"); // => "Mongosha"
  12. // Get the value in path "identify".
  13. data.get("identify"); // => { id: 400, phone: "+123456789" }
  14. // Get the value in path "identify.id".
  15. data.get("identify.id"); // => 400
  16. // Check if Field exists to specified path.
  17. data.has("identify"); // => true
  18. // Mathematical addition to the "wallet" path.
  19. data.add("wallet", 100);
  20. // Mathematical substract to the "wallet" path.
  21. data.subtract("wallet", 25);
  22. // Added a "Gum" array element to the "inventory" path.
  23. data.push("inventory", "Gum");
  24. // Added an "Ax", "Backpack", "Pencil" and "Gum" array elements to the "Inventory" path.
  25. data.pushRange("inventory", ["Axe", "Backpack", "Pencil", "Gum"]);
  26. // Pulled an element from array in the specified path.
  27. data.pull("inventory", "Axe");
  28. // Pulled all items with the same value from the array in the specified path.
  29. data.pullAll("inventory", "Gum");
  30. // We add numbers for sorting:
  31. data.pushRange("numbers", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  32. // Sorted the array in the specified path in descending order.
  33. data.sort("numbers", "DESC"); // => [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  34. // Sorted the array in the specified path in ascending order.
  35. data.sort("numbers", "ASC", 6); // => [1, 2, 3, 4, 5, 6]

API References

  • .set(path, value): Promise<any>: Check if Field exists to specified path.
  • .get(path, value): Promise<any>: Returns the value/object at the specified path.
  • .has(path): Promise<Boolean>: Check if Field exists to specified path.
  • .add(path, value): Promise<Number>: Do mathematical addition to specified path.
  • .subtract(path, value): Promise<Number>: Do mathematical subtraction to specified path.
  • .push(path, value): Promise<void>: Push to value an array to specified path.
  • .pushRange(path, values): Promise<void>: Push to multiple values an array to specified path.
  • .pull(path, value): Promise<void>: Extract element from Array to specified path.
  • .pullAll(path, values): Promise<void>: Extract all elements from Array to specified path.
  • .sort(path, orderType, limit?): Promise<any[]>: Sorts the array/values ​​in path.

Contact

Discord