2.0.1 • Published 1 year ago

ccdb-commonjs v2.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Constructable DB

npm npm

image

Simple data storage based on JSON format.

This is CommonJS package. if you want to use version for ECMAScript, so welcome

Content

Install

npm i ccdb

Quick start to CCDB:

const {SchemaTypes, Schema, Model, Database} = require("ccdb");

const path = require("path");

const db = new Database(path.join(__dirname, "db"));

const userModelTemp = {
	id: {
		type: SchemaTypes.number,
		required: true
	},
	name: {
		type: SchemaTypes.string,
		required: true
	}
}

const userSchema = new Schema(userModelTemp);

const Users = new Model("users", userSchema, db.path);

db.addModel(Users.name, Users);

const users = [
	{
		id: 1,
		name: "Joe"
	},
	{
		id: 2,
		name: "Steve"
	}
]

const writed = Users.write(users);

const getByManyParams = Users.get({id: 1});

console.log(getByManyParams);

Examples

You can see in folder lib/examples

API reference

Database

new Database(dbPath: string) - main class of database

Parameters

  • Database.fs - instance of FS (FileSystem) class

  • Database.path - path where database placed

  • Database.models - object, which contains all added models

Methods

  • Database.addModel(modelName: String, model: ccdb.Model) - adds a new model to database and creates a file of model

Model

new Model(name: String, schema: ccdb.Schema, dbPath: String) - class for making models.

Parameters

  • Model.fs - instance of FS class

  • Model.name - name of model

  • Model.path - path where placed database

  • Model.schema - instance of Schema

  • Model.fullPath - path where placed database file

Methods

  • Model.read() - reads all database file content, includes name and schema of database

  • Model.write(data: Array) - write array of data to database

  • Model.get(options: Object) - get document from database by object of parameters

  • Model.update(options: Object, data: Object, params: {count: Number}) - update document in database, which goted by few parameters

  • Model.delete(options: Object, params: {deleteAll: Boolean, count: Number}) - delete doc/docs by special parameter

Schema

new Schema(template: Object) - class which makes schemas for models

Parameters

  • Schema.schema - template of schema

SchemaField

new SchemaField(field: Object) - class of schema field

Parameters

  • SchemaField.type - value type for validating data; required option

  • SchemaField.required - value requires to insert or not; in default false

  • SchemaField.defaultValue - vvalue will insert when no have value to insert; in default null

  • SchemaField.maxStringLength - string value length don't be more that specified; default 2**53-1

  • SchemaField.minStringLength - string value length don't be less that specified; default 0

  • SchemaField.maxNumValue - number value don't be more that specified; default 2**53-1

  • SchemaField.minNumValue - number value don't be less that specified; default 2**53-1

  • SchemaField.unique - one value for one field if true; default false

FS

new FS()/*(FileSystem)*/ - class for direct control on the database files

Parameters

  • FS.dbPath - path where database placed

Methods

  • FS.createDatabaseFolder(dbPath: String) - makes a folder, where will be placed db

  • FS.readDatabaseFile(modelPath: String) - reads all database file

  • FS.writeDataToDatabase(modelPath: string, data: Array) - writes data to database file

  • FS.addDatabaseFile(modelName: String, modelSchema: ccdb.Schema) - adds new database file

  • FS.clearDatabaseData(modelPath: string) - clears all data in database file

  • FS.removeDocs(modelPath: string, doc: Object) - delete all docs from database

  • FS.removeDoc(modelPath: string, doc: Object, params: {count: Number}) - delete few documents from database

2.0.1

1 year ago

2.0.0

1 year ago

1.5.0

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago