quickfirestore

1.0.1 • Public • Published

Quick FireBase NPM version

  • Quick FireStore wrapper for beginners that provides key-value based interface.
  • The goal of the package is to facilitate the way to deal with data so that your way of dealing with it is like QuickDB or ST.db

Features

  • Quick to contact and respond
  • Provides you with complete ease in reading and recording data
  • It contains many functions that make it easier for you to perform calculations
  • Key-Value like interface
  • Beginner friendly
  • Easy to use and fast

Content

Getting Started

Installation

You can start install the package on your project:

npm install quickfirestore
yarn add quickfirestore
  • CommonJS
const Database = require('quickfirestore')
  • ESM
import Database from 'quickfirestore';
  • ُExample
const firebaseConfig = {
  //If you do not know what this variable contains, you can read below this paragraph
};
const db = new Database(firebaseConfig,{
  collectionName:"users"
  /*
   Write the collection you want to register with. Note If this option is not written, the default setting for it will be Collection Name "quickfirestore"
  */
})

Important Notes

  • All methods must use then or await with it
// set
await db.set({ key:'Array', value:['eg',  'ps']  });
db.set({ key:'Array', value:['eg',  'ps']  }).then(() => {});

// get
console.log(await db.get({key:'Array'}))
db.get({key:'Array'}).then(console.log)

How to create data base in firebase

  • The first step, go here to this site, click here to go, and then register with your Google account
  • Then click on "Add Project"
  • Then it will ask you the name of the project and some simple things to do and then when finished
  • Note that it will take a minute or two to create your project
  • When finished, click "Continue".
  • Then go to "Project Settings" enter image description here
  • Then click on this icon as shown in the image enter image description here
  • Then fill in the name of the application and make a registration for it enter image description here
  • Don't forget to keep a variable that will appear to you in the code which is a variable named "firebaseConfig" as you will need it to use the package to connect to your database
  • Well, now you have finished creating the project, but now it is the basic and important step for creating the FireStore Database
  • Head to this page as shown in the image with the name "Firestore Database" enter image description here
  • Then click on the "Create Database" button. enter image description here
  • Then do as shown in the image and click "Next" enter image description here
  • Then it will ask you to select the country, select the appropriate region for you
  • Upon completion of the creation, you should take a look at how to give the package permission to connect in Database easily by clicking here to see the method

How to get firebaseConfig

  • When you are finished creating, you will find this code placed in a picture. All you have to do is copy a variable with the name "firebaseConfig" enter image description here
  • You don't know where this page is, you can look at the paragraph above this paragraph

Allow reading and writing in Database

  • First, go to the Cloud "Firestore Database" page enter image description here
  • go to the"Rules"
  • Click on "Edit Rules" and then replace the current code with this code below ↓
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

enter image description here

Debug Mode

  • To know in console everything is added to the data
const Database = require('quickfirestore')
const firebaseConfig =  {   };  
const db =  new  Database(firebaseConfig,{  
    collectionName:"users",
    debug:true
})

Events

  • Events help you know the value of something when a specific event occurs

Data connection event

Parameters:

Name Type Description
value object Information about your Database settings.

Examples

db.on('ready', (database) =>{
   console.log(`The data is working successfully`)
   console.log(database)
})

Add an value in the data

Parameters:

Name Type Description
value object Information about the added value.

Examples

db.on('addElement', (data) =>{
   console.log(`Key => ${data.key}`) // Key => test
   console.log(`Value => ${data.value}`) // Value => true
})

await db.set({key:'test',value: true})

Read a value by Element

Parameters:

Name Type Description
value object Information about the value that was read.

Examples

db.on('getElement', (data) =>{
   console.log(`Key => ${data.key}`) // Key => test
})

await db.get('test')

Sets a value to the specified key on the database!

Parameters:

Name Type Description
key string The key to set.
value all types The value to set on the key.

Examples

await db.set({
    key:"age",value:16
})
// or
await db.set({
    key:{id:"742070589212327947",uuid:"a4s7-4qw7-rq84-5fsd"},value:16
})
// or
await db.put([
   {
      key:"age",value:16
   },
   {
      key:"name",value:"mohamed"
   }
])

Fetches the data from database!

Parameters:

Name Type Description
key string Key

Examples

await db.get({key:'profile'}); // Get the value of the data
await db.fetch({key:'data'}); // Fetches the value of the data

Fetches everything and sorts by given target

Parameters:

Name Type Description
key string The key to set.

Examples

await db.includes({key:"tes"});// It fetches the values ​​containing this value
await db.startsWith({key:"te"});// It fetches values ​​starting with this value
await db.endsWith({key:"st"});// It fetches values ​​ending with this value

Set, get, delete and control the array in the database

Parameters:

Name Type Description
key string The key to set.
value Value to push.

Examples

//"hello":[2020]

//It sets at the end
await db.push({
  key:`hello`,
  value:2021
})// "hello":[2020,2021] 

//Iteratively deletes the value from the array
await db.unpush({
  key:`hello`,
  value:2020
})// "hello":[2021]

//It sets at the start
await db.unshift({
  key:`hello`,
  value:2019
})//"hello":[2019,2020,2021]

//It removes the first value from the array
await db.shift({
  key:`hello`
})//"hello":[2020,2021]

//It removes the last value from the array
await db.pop({
  key:`hello`
})//"hello":[2019,2020]

Deletes a key from the database!

Parameters:

Name Type Description
key string The key to delete.

Examples

 // Removing something from an array using value/index
 await db.delete({key:'Array'});

Delete's all of the data from the database!

Parameters:

Name Type Description
ops object Clear options.

Examples

await db.deleteALL(); // Clears everything from the database

Returns everything from the database

Parameters:

Name Type Description
limit number Define a limit on the values ​​that reads
ops All options

Examples

//Returns everything from the database
await db.all(); || await db.fetchAll()
await db.all(5); || await db.fetchAll(5)//You can select the number you want to read
/*
 => Example
[
  {
    documentId: 'CiuslpcUVnxpHsHR9qXe',
    ID: 'info',
    data: { name: "Mohamed Abdelkarim" },
    typeof: 'object',
    _v: 0
  },
  {
    documentId: 'FcJIsrp01OryzVGdlWaw',
    ID: 'age',
    data: 16,
    typeof: 'number',
    _v: 1
  }
]
*/

//Return all values from the database
await db.valuesAll()

//Return all keys from the database
await db.keysAll()

Does a math calculation and stores the value in the database!

Parameters:

Name Type Description
key string The key to set.
operator string One of +, -, %, * or /
value number The value, must be a number.

Examples

await db.math({
  key:"coins",  
  operator:"+",  
  value:"100", 
  goToNegative:false
})

// To subtract from value
await db.subtract({key:"coins", value:50})

// To add from value
await db.add({key:"coins", value:50})

// To multiply from value
await db.multiply({key:"coins", value:2})

//To double from value
await db.double({key:"coins"})

Checks if there is a data stored with the given key

Parameters:

Name Type Description
key string Key.

Returns:

Examples

await db.has({key:"coins"})//Returns "true" or "false" if the database has the data or not.

Return's the value type of the key!

Parameters:

Name Type Description
key string Key.

Returns:

Examples

await db.type({key:"coins"})//To find out the type of value written in the data

Contact

Any bug or suggestion !

Server Support

License

Dependents (0)

Package Sidebar

Install

npm i quickfirestore

Weekly Downloads

0

Version

1.0.1

License

CC BY-NC-ND 4.0

Unpacked Size

49.6 kB

Total Files

7

Last publish

Collaborators

  • shuruhatik