rapid-database v1.1.0
Rapid Database
Document based local database for rapid testing
Installation
Rapid database is only supported on Node.js; you can install it with npm install rapid-database
.
Initialization
Include the script at the top of your file.
const RapidDatabase = require('rapid-database')
You can create a new database with the new
class constructor. The database takes one parameter, a file name that should be used when retrieving or downloading instances of the db. It is automatically set to local-db.json
const database = new RapidDatabase('local-db.json')
Retrieving
If you already have a local database in a text file, you can initialize it with database.retrieve()
.
const database = new RapidDatabase('local-db.json').retrieve()
Downloading
You can download you database with db.download()
. This is also synchronous like db.retrieve()
. The file destination is automatically set to the initialized input file, but you can also specify that yourself.
database.download()
Database Collections
Local Database uses a collection/document based system, similar to what you might see in Mongo or Firestore. All of the current database methods accept a "name" parameter, which specifies the name of the collection.
- Using
createCollection
, you can create a collection with a specified name. - Using
getCollection
, you can retrieve a collection. It will throw an error if the collection does not exist. - Using
hasCollection
, you can check if the database has a particular collection. - Using
removeCollection
, you can remove a collection from the database. - Using
getCollectionNames
, you can retrieve all of the collection names within the database. Example:
const db = new RapidDatabase('db2.json')
const users = db.createCollection('users')
Collections
The most important method is createDocument
, which accepts a json object as its parameter. You can interact with all of the documents under documents
.
const db = new RapidDatabase('db2.json')
const users = db.createCollection('users')
users.createDocument({
username: 'Nathan',
details: {
name: 'Nathan Pham',
email: 'lmao'
}
})
console.log(users.documents)
Documents
- Using
setProperty
, you can set a key inside of the document to a new value. - Using
hasProperty
, you can check if a property exists. - Using
getProperty
, you can retrieve a property from the document. - Using
getCollection
, you can find which collection the document originated from.