Collections
Handle schema-based validation and data creation in a database.
Setup
const Collection = require('dubnium/collection');
const collection = new Collection(db, name, schema)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
datais notundefinedornull.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
data
Object
The data to validate.
Returns
Boolean
true if the data matches the schema, false otherwise.
Creates a new record in the database after validating it against the schema.
Parameters
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