Constructor
new BeanBagDB(db_instance)
Initializes the BeanBagDB instance.
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
db_instance |
object | Database configuration object. Properties
|
Classes
Members
error_codes
Static property containing predefined error codes for common errors. These error codes can be used throughout the class to handle specific exceptions.
Properties:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
error_codes |
Object | An object with key-value pairs representing error codes and their messages. Properties
|
rest_enabled
This is the list of methods that are compatible with JSON-REST API. Each command either has no params or takes just one json param as input
Methods
(async) create(input) → {Promise.<{id: string}>}
Creates a document for the given schema into the database.
This method validates the input data and schema before inserting a new document into the database.
Parameters:
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input |
object | The document details, e.g.,{ schema:"name",data: { "name": "", "mobile": "", ... }}. Properties
|
Throws:
-
- Throws an error if insertion checks fail or if there is an issue with the database operation.
- Type
- Error
Returns:
- A promise that resolves with the newly inserted document's ID.
- Type
- Promise.<{id: string}>
(async) create_edge(node1, node2, edge_name, edge_label) → {Object}
To add an edge between 2 nodes in the system wide simple directed graph.
Parameters:
Name | Type | Description |
---|---|---|
node1 |
object | |
node2 |
object | |
edge_name |
string | |
edge_label |
* |
Returns:
- Type
- Object
(async) delete(doc_id)
Deletes a document from the database by its ID.
Parameters:
Name | Type | Description |
---|---|---|
doc_id |
String | The ID of the document to delete. |
Throws:
-
-
If the document with the specified ID does not exist.
- Type
- DocNotFoundError
-
-
-
If the database is not ready to use.
- Type
- ValidationError
-
(async) get(inputopt) → {Object}
Retrieves special types of documents from the database, such as schema documents or blank documents for a given schema. It handles system-related data and throws errors for invalid document types or if the document is not found.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object |
<optional> |
{} | Criteria used to search for the special document. Properties
|
Throws:
-
-
Throws if the
special_doc_type
is not recognized. - Type
- ValidationError
-
-
-
Throws if the requested document is not found in the database.
- Type
- DocNotFoundError
-
Returns:
The fetched special document based on the type and criteria.
- Type
- Object
(async) initialize() → {Promise.<void>}
Initializes the database with the required schemas.
This method is responsible for:
- Verifying the existence and latest version of the
schema_schema
document. - Upgrading or inserting a new system schema if the version is outdated or missing.
- Logging initialization steps in the system logs.
- Updating the database version if needed.
This method is usually called automatically by 'ready' if required but can be run manually if needed.
Throws:
-
- Throws an error if schema initialization fails.
- Type
- Error
Returns:
- Resolves when the initialization is complete.
- Type
- Promise.<void>
(async) initialize_app(app_data)
Install/Updates an app in the database. This should be called before using any
Parameters:
Name | Type | Description |
---|---|---|
app_data |
Object |
(async) load_plugin(plugin_name, plugin_module)
To load a plugin in the current BeanBagDB instance.
Plug_module has to be loaded manually first. It must export an object containing fields: actions
and schema
.
actions
is an object of methods which can be called after loading the plugin.
schema
is an array of JSON schemas that are required by the plugin. Every time a plugin is loaded, this list is schemas is verified. New updates are added automatically to the database and logged
methods inside actions must be async and must have at least one parameter : db_instance
which is assumed to be the current instance of the BeanBagDB object itself. They ideally must also return some value.
Parameters:
Name | Type | Description |
---|---|---|
plugin_name |
string | |
plugin_module |
object |
metadata() → {DBMetaData}
Retrieves metadata for the current database object.
- To Do:
-
- Include additional metadata: document count, schema count, records for each schema, size of the database.
Returns:
An object containing system metadata.
- Type
- DBMetaData
(async) modify_setting(name, value, mode)
Check if the setting with the given name exists. New record created if not found. If found data is updated based on the updated_mode and the data type of the existing data If existing value is an array, and update_mode is append "value" is appended to the current value array. if existing value is an object, update_mode "append" will update fields that exists in the new object, for both data types, new value is replaced in update_mode : "replace"
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The name of the setting |
value |
object | Value to be modified |
mode |
string |
(async) read(criteria) → {Promise.<Object>}
Reads a document from the database based on the provided criteria.
There are three valid ways to search for one document:
- By
_id
(e.g.,{ "_id": "document_id" }
) - By
link
(e.g.,{ "link": "some_link" }
) - By schema's primary key (e.g.,
{ "schema": "schema_name", "data": { "primary_key_1": "value", "primary_key_2": "value" }}
)
If the document does not exist, an error will be thrown.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
criteria |
Object | The search criteria for the document. Properties
|
Throws:
-
-
If no document is found for the given criteria.
- Type
- DocNotFoundError
-
-
-
If invalid search criteria are provided.
- Type
- ValidationError
-
Returns:
- Returns an object with the document (
doc
) and optionally the schema (schema
).
- Type
- Promise.<Object>
(async) ready() → {Promise.<void>}
Checks if the database is ready to be used. It is important to run this method after the class is initialized.
This method performs the following actions:
- Pings the database.
- Searches the database for the
system_setting.beanbagdb_version
document. - Sets the class state as active if the version matches the current BeanBagDB version.
- If the version does not match, calls
initialize()
to set up the database to the latest version.
- To Do:
-
- Code to ping the DB and throw Connection error if failed to connect
Returns:
- Resolves when the database has been verified and initialized.
- Type
- Promise.<void>
(async) search(criteria)
Searches for documents in the database for the specified query. The query are Mango queries. One field is mandatory : Schema E.g
Parameters:
Name | Type | Description |
---|---|---|
criteria |
Object |
(async) update(params) → {Object}
Updates the data and metadata of a document.
Frequently Asked Questions:
-
Which data fields can be edited?
- All fields except for the ones listed in the schema's
settings.non_editable_fields
can be edited. If this setting is blank, all fields are editable by default.
- All fields except for the ones listed in the schema's
-
Are primary key fields editable?
- Yes, but a validation check ensures that primary key policies are not violated before the update is applied.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object | Object to fetch and update data Properties
|
Throws:
-
-
- If a document with conflicting primary keys already exists.
- Type
- DocUpdateError
-
-
-
- If the provided data or metadata is invalid according to the schema.
- Type
- ValidationError
-
Returns:
The result of the document update operation.
Errors:
- Throws an error if a document with the same primary keys already exists .
- Throws a
DocUpdateError
if a primary key conflict is detected during the update.
- Type
- Object
(async) update_indexes()
Adds indexes for all the schemas in the data base. This is important to make search faster. This must be done every time a new schema is introduced in the database
util_filter_object()
Filters an object, returning a new object that only contains the specified fields. const data = { name: "Alice", age: 25, location: "NY" }; const result = util_filter_object(data, ["name", "age"]); // result: { name: "Alice", age: 25 }
util_generate_random_link() → {String}
Generates a random link composed of four words from a predefined dictionary.
The words are selected randomly, and the resulting link is formatted as a hyphen-separated string. This can be useful for creating link for documents.
Returns:
A hyphen-separated string containing three randomly selected words from the dictionary. For example: "banana-earth-rain".
- Type
- String
util_validate_data(schema_obj, data_obj)
Validates a data object against a provided JSON schema and returns a valid data object (with default value for missing field for which default values are defined in the schema ) It relies on the external API provided by the user
Parameters:
Name | Type | Description |
---|---|---|
schema_obj |
Object | The JSON schema object to validate against |
data_obj |
Object | The data object to validate |
Throws:
-
If the data object does not conform to the schema
- Type
- Error
util_validate_schema_object(schema_doc)
Validates the structure and content of a schema object.
This method checks the following conditions:
- The schema must have a 'type' field, which should be 'object'.
- The 'properties' field must be an object and contain at least one property.
- The 'additionalProperties' field must be present and of type boolean.
- Primary keys must be defined in the schema and cannot be of type 'object' or 'array'.
- Non-editable fields must be defined in the schema.
- Encrypted fields must be defined in the schema, of type 'string', and cannot include primary keys.
If any of these conditions are violated, an array of error messages will be collected and thrown as a ValidationError.
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
schema_doc |
Object | The schema document to validate. Properties
|
Throws:
-
If any validation checks fail, with an array of error messages.
- Type
- ValidationError