Collections

Handle schema-based validation and data creation in a database.

Setup

const Collection = require('dubnium/collection');
const collection = new Collection(db, name, schema)
Parameter
Type
About

db

Object

The database instance this collection is associated with.

name

String

The prefix or name of the collection.

schema

Object | String

Schema definition for validating data. Can be: - A string: any non-null value is valid. - An object: keys are field names and values are either expected types ("string", "number", etc.) or "required" for mandatory fields.

Validate a Record

  • If schema is a string, validation passes if data is not undefined or null.

  • If schema is an object:

    • Each key in the schema must exist in the data.

    • "required" fields must be non-empty.

    • Each field must match the expected type.

collection.validate(data)

Parameters

Name
Type
About

data

Object

The data to validate.

Returns

Type
About

Boolean

true if the data matches the schema, false otherwise.

Creates a new record in the database after validating it against the schema.

Parameters

Name
Type
About

tag

String

Unique identifier for the record.

data

Object |String

Data to be stored.

Returns

  • A promise resolving to the result of db.create(tag, data)

Full Example

Last updated