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
|
Methods
(async) create(schema, data, metaopt, settingsopt) → {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 | Attributes | Default | Description |
---|---|---|---|---|
schema |
string | The schema name for the document, e.g., "contact". |
||
data |
object | The document data, e.g., { "name": "", "mobile": "", ... }. |
||
meta |
object |
<optional> |
{} | Optional metadata associated with the document. |
settings |
object |
<optional> |
{} | Optional settings that may affect document creation behavior. |
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) 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(special_doc_type, criteriaopt) → {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 |
---|---|---|---|---|
special_doc_type |
String | The type of special document to fetch. Supported types include: - 'schema': Retrieves a schema document based on the criteria provided. |
||
criteria |
Object |
<optional> |
{} | Criteria used to search for the special document. For example, to search for a schema, the criteria should include the name. |
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>
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) read(criteria, include_schemaopt) → {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 | Attributes | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
criteria |
Object | The search criteria for the document. Properties
|
||||||||||||||||||||||
include_schema |
boolean |
<optional> |
false | Whether to include the schema object in the returned result. |
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) save_setting_doc(name, new_data, schemaopt) → {Promise.<object>}
Updates a setting document if it already exists in the database or creates a new document Inserts or updates a setting in the system settings schema.
This method either:
- Updates an existing document if the setting with the given
name
already exists in the database. - Inserts a new document if no matching setting is found.
If the setting exists and the value
is an array, the behavior depends on the on_update_array
key:
"append"
: Appends the new value to the existing array."update"
: Replaces the current array with the new value.
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name |
string | The name of the setting to insert or update. |
||||||||||||||
new_data |
object | The new data to insert or update. Properties
|
||||||||||||||
schema |
object |
<optional> |
{} | Optional schema to validate the data against (currently not implemented). |
Throws:
-
- Throws an error if
new_data
ornew_data.value
is not provided, or ifon_update_array
is invalid.
- Throws an error if
- Type
- Error
Returns:
- The updated or newly inserted document.
- Type
- Promise.<object>
(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(doc_search_criteria, rev_id, updates, update_sourceopt, save_conflictopt) → {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 | Attributes | Default | Description |
---|---|---|---|---|
doc_search_criteria |
Object | The criteria used to search for the document (e.g., {"_id": "document_id"}, {"link": "some_link"}, {"schema": "schema_name", "data": {primary_key_fields}}). |
||
rev_id |
String | The document's revision ID ( |
||
updates |
Object | The updated values for the document, structured as |
||
update_source |
String |
<optional> |
"api" | Identifies the source of the update (default: "api"). |
save_conflict |
Boolean |
<optional> |
true | If Behavior:
Returns: |
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_check_required_fields(requiredFields, obj)
Validates that the required fields are present in the provided object.
Parameters:
Name | Type | Description |
---|---|---|
requiredFields |
Array.<string> | An array of field names that are required. |
obj |
object | The object to check for the required fields. |
Throws:
-
If any of the required fields are missing, an error is thrown.
- Type
- ValidationError
util_filter_object(obj, fields) → {Object}
Filters an object, returning a new object that only contains the specified fields.
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | The object to filter. |
fields |
Array.<String> | An array of field names to retain in the filtered object. |
Returns:
- A new object containing only the fields that exist in
obj
from thefields
array.
Example:
const data = { name: "Alice", age: 25, location: "NY" }; const result = util_filter_object(data, ["name", "age"]); // result: { name: "Alice", age: 25 }
- Type
- Object
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 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