3.0.0 • Published 11 months ago
ccdb v3.0.0
CCDB - Simple data storage tool, based on JSON
About
CCDB is a JSON-based storage tool, created for simplest data storaging. You can modify database file personally, because CCDB database file just JSON doc; CCDB works by scheme "one model - one database file". It's made to prevent main database file bloat; CCDB have simple API for working with database data. Also, you can make model schemas for tipyzing your model data; And this package wroten on Typescript, so you can use this package in Typescript too;
Examples
// typescript or javascript (ECMAScript)
import { Database, Model, Schema, SchemaType } from "ccdb";
import path from "path";
const dbPath = path.join(process.cwd(), "db");
const db = new Database({ path: dbPath });
const SimpleSchema = new Schema({
name: {
type: SchemaType.string
}
});
const SimpleModel = new Model({name: "simple", dbPath: dbPath, schema: SimpleSchema});
db.addModel(SimpleModel);
SimpleModel.write([{name: "Alex"}, {name: "Joe"}, {name: "Alice"}]);
SimpleModel.delete({name: "Alex"}) // or SimpleModel.delete() for full data drop
SimpleModel.update({name: "Joe"}, {name: "John"});
SimpleModel.get() // reads all database data
// javascript (CommonJS)
const {Database, Model, Schema, SchemaType} = require("ccdb")
const path = require("path");
const dbPath = path.join(process.cwd(), "db");
const db = new Database({ path: dbPath });
const SimpleSchema = new Schema({
name: {
type: SchemaType.string
}
});
const SimpleModel = new Model({name: "simple", dbPath: dbPath, schema: SimpleSchema});
db.addModel(SimpleModel);
SimpleModel.write([{name: "Alex"}, {name: "Joe"}, {name: "Alice"}]);
SimpleModel.delete({name: "Alex"}) // or SimpleModel.delete() for full data drop
SimpleModel.update({name: "Joe"}, {name: "John"});
SimpleModel.get() // reads all database data
API Reference
Types
ModelData
Array<{[name: string]: any}>
Enumerates
SchemaType
string = 'string',
number = 'number',
array = 'object',
object = 'object'
Classes
Database
- Params
public readonly path: string
- path where database placedpublic readonly fs: FileSystem
- instance of FileSystem class; for work with database filespublic readonly models: Models[]
- a list of exist models
- Methods
constructor({ path }: DatabaseInitOptions): Database
- constructor of Database classaddModel(model: Model): void
- adds new model to Database instance and makes file of the model
Model
- Params
public readonly name: string
- name of the modelpublic readonly dbPath: string
- path where database placedpublic readonly schema: Schema
- schema of the modelprivate fs: FileSystem
- instance of FileSystem class; for work with model file
- Methods
constructor({ name, dbPath, schema }: ModelInitOptions): Model
- constructor of Model classget(params?: ModelMethodParams): ModelData
- returns docs filtered via provided parameters; returns all docs without parameterswrite(data: ModelData): void
- writes some data to model file if data valids via schemadelete(params?: ModelMethodParams, options?: ModelMethodOptions): void
- deletes some docs from model file; removes all docs if parameters not providedupdate(params: ModelMethodParams, dataToUpdate: ModelMethodParams, options?: ModelMethodOptions): void
- updates some docs via provided params and data; updates few documents, if options provided
Schema
- Params
public readonly schema: SchemaInitOptions
- main schema template
- Methods
constructor(schema: SchemaInitOptions): Schema
- constructor of Schema classcheckByScheme(data: ModelMethodParams, model: Model): void
- checks data compliance by schema; returns error when data don't matchs schema
SchemaField
- Params
public readonly type: SchemaType
public readonly max?: number
public readonly min?: number
public readonly maxLen?: number
public readonly minLen?: number
public readonly default?: any
public readonly required?: boolean = false
public readonly unique?: boolean = false
- Methods
constructor(field: SchemaFieldInitOptions): SchemaField
- constructor of SchemaField
FileSystem
- Params
private dbPath: string
- path where database placed
- Methods
constructor({ dbPath }: FileSystemInitOptions): FileSystem
- constructor of FileSystemOptionscreateDatabaseFolder(): void
- creates database folder, where will contain model filescreateDatabaseModel(model: Model): void
- creates databse model filereadModelData(modelName: string): ModelRawData
- returns all data about modelwriteDataToModel(modelName: string, data: ModelData): void
- writes provided data to modeldropModelData(modelName: string): void
- drops all docs in modeldeleteDataFromModel(modelName: string, data: ModelData): void
- removes provided docs from model
Interfaces
DatabaseInitOptions
path: string;
FileSystemInitOptions
dbPath: string;
ModelRawData
name: string,
path: string,
schema: SchemaInitOptions,
data: Array<{[name: string]: any}>
ModelInitOptions
name: string;
dbPath: string;
schema: Schema;
ModelMethodParams
[name: string]: any
ModelMethodOptions
count?: number;
deleteMany?: boolean;
SchemaInitOptions
[name: string]: SchemaField
SchemaFieldInitOptions
type: SchemaType;
max?: number;
min?: number;
maxLen?: number;
minLen?: number;
default?: any;
required?: boolean;
unique?: boolean;