API Docs for: 0.0.3
Show:

Database Class

Extends CoreObject

Available since 0.0.2

The main class of the local-json-db, representing a database with one or more layers

Constructor

Database

()

Defined in src/Database.coffee:13

Available since 0.0.2

Example:

  var db = new Database();
  var record = db.createRecord({name: 'Huafu'});
  record.age = 31;
  db.updateRecord(record);
  console.log(db.find('user', record.id));
  // will output {id: 1, name: 'Huafu', age: 31}
  db.save();

Methods

_createModelStore

(
  • name
)
MergedRecordStore private

Defined in src/Database.coffee:339

Available since 0.0.2

Creates the store for a given model

Parameters:

  • name String

    name of the model

Returns:

MergedRecordStore:

the newly created store for the given model

_modelName

(
  • name
)
String private

Defined in src/Database.coffee:326

Available since 0.0.2

Normalize a model name

Parameters:

  • name String

    name of the model to normalize

Returns:

String:

normalized name

_saveModelStore

(
  • name
  • model
)
String private

Defined in src/Database.coffee:366

Available since 0.0.2

Saves the store of a given model to disk

Parameters:

  • name String

    name of the model

  • model Model

    model instance

Returns:

String:

the full path of the file that has been saved

addOverlay

(
  • path
)
chainable

Defined in src/Database.coffee:90

Available since 0.0.2

Add an overlay on top of all layers (the latest is the one used first, then come the others in order until the base)

Parameters:

  • path String

    the path where to read/write JSON files of records, relative to the base path

assert

(
  • expression
  • message
)
chainable

Inherited from CoreObject: src/CoreObject.coffee:103

Available since 0.0.2

Make an assertion and throw an error if it fails

Parameters:

  • expression Boolean

    the test to assert true

  • message String

    the message of the error if assertion is failing

assertNotLoaded

(
  • msg
)
chainable

Defined in src/Database.coffee:314

Available since 0.0.2

Asserts that the DB hasn't been loaded yet, or throw an error

Parameters:

  • msg String

    additional message to add in the error

className

() String

Inherited from CoreObject: src/CoreObject.coffee:78

Available since 0.0.2

Returns the name of the class for this object

Returns:

String:

class name of this object

constructor

(
  • [basePath="./json.db"]
  • [config={}]
)

Defined in src/Database.coffee:70

Available since 0.0.2

Constructs a new instance of Database

Parameters:

  • [basePath="./json.db"] String optional

    the base path for the db files, hosting base JSON files of any added overlay

  • [config={}] Object optional

    the configuration options

    • [createdAtKey] String optional

      name of the key to use as the createdAt flag for a record

    • [updatedAtKey] String optional

      name of the key to use as the updatedAt flag for a record

    • [deletedAtKey="__deleted"] String optional

      name of the key to use as the deletedAt flag for a record

count

(
  • modelName
)
Number

Defined in src/Database.coffee:202

Available since 0.0.2

Counts all records of a given model

Parameters:

  • modelName String

    name of the model to count records

Returns:

Number:

the total number of records

createRecord

(
  • modelName
  • record
)
Object

Defined in src/Database.coffee:108

Available since 0.0.2

Creates a new record in the database

Parameters:

  • modelName String

    name of the model

  • record Object

    attributes of the record to create

Returns:

Object:

a copy of the newly created model with read-only id

deleteRecord

(
  • modelName
  • id
)
Object

Defined in src/Database.coffee:136

Available since 0.0.2

Deletes a record given its id

Parameters:

Returns:

Object:

a copy of the old record which has been deleted

destroy

()

Inherited from CoreObject but overwritten in src/Database.coffee:273

Available since 0.0.2

Destroy and free the db object

find

(
  • modelName
  • id
)
Object | Undefined

Defined in src/Database.coffee:149

Available since 0.0.2

Finds a record by id

Parameters:

Returns:

Object | Undefined:

copy of the record if found, else undefined

findAll

(
  • modelName
)
Array

Defined in src/Database.coffee:190

Available since 0.0.2

Finds all records in the database

Parameters:

  • modelName String

    name of the model

Returns:

Array:

array containing all records of the given model

findMany

(
  • modelName
  • ids
)
Array

Defined in src/Database.coffee:162

Available since 0.0.2

Finds many record given a list of ids. If some records are not found, they'll just be filtered out of the resulting array

Parameters:

  • modelName String

    name of the model

  • ids Array | String | Number multiple

    id of each record to find, or one array with all record ids

Returns:

Array:

array containing all found records

findQuery

(
  • modelName
  • filter
  • [thisArg]
)
Array

Defined in src/Database.coffee:176

Available since 0.0.2

Finds records using a filter (either function or set of properties to match)

Parameters:

  • modelName String

    name of the model

  • filter Object | Function

    attributes to match or a function used to filter records

  • [thisArg] Object optional

    will be used as the context to run the filter function

Returns:

Array:

array with all records which matched

identify

() String

Inherited from CoreObject: src/CoreObject.coffee:143

Available since 0.0.2

Get a string identification of the object

Returns:

String:

the string identifying the object

isLoaded

() Boolean

Defined in src/Database.coffee:232

Available since 0.0.2

Whether the database has been loaded or not (in that case no overlay can be added)

Returns:

Boolean:

returns true if the db is loaded, else false

load

(
  • force
)
chainable

Defined in src/Database.coffee:243

Available since 0.0.2

Loads the database (you don't need to call this method, it'll be automatically called when needed)

Parameters:

  • force Boolean

    whether to force a reload in case it has already been loaded previously

lockProperties

(
  • names
)
chainable

Inherited from CoreObject: src/CoreObject.coffee:117

Available since 0.0.2

Lock given properties so that setting them will fail

Parameters:

  • names String multiple

    the name of each property to lock

log

(
  • [level="debug"]
  • items
)
chainable

Inherited from CoreObject: src/CoreObject.coffee:89

Available since 0.0.2

Log a string with the class name as a prefix

Parameters:

  • [level="debug"] String optional

    level (debug, notice, info, warning, error, danger or fatal)

  • items Mixed multiple

    anything to give back to the logger

modelFactory

(
  • modelName
)
Model

Defined in src/Database.coffee:284

Available since 0.0.2

Get the Model instance given a model name

Parameters:

  • modelName String

    name of the model to get the instance

Returns:

Model:

the model object

modelNameToFileName

(
  • modelName
)
String

Defined in src/Database.coffee:301

Available since 0.0.2

Used to transform a model name into its store's JSON file name

Parameters:

  • modelName String

    name of the model

Returns:

String:

name of the file, sanitized and normalized

save

() chainable

Defined in src/Database.coffee:214

Available since 0.0.2

Saves in the top overlay's path the records that have been created/modified or deleted

unload

() chainable

Defined in src/Database.coffee:258

Available since 0.0.2

Unloads the database and all the records. This does NOT save anything

updateModel

(
  • modelName
  • [id]
  • record
)
Object

Defined in src/Database.coffee:121

Available since 0.0.2

Updates a record with the given attributes

Parameters:

  • modelName String

    name of the model

  • [id] String | Number optional

    id of the record to update, if not given it must be in record

  • record Object

    attributes of the record to update

Returns:

Object:

a copy of the updated record

uuid

() String

Inherited from CoreObject: src/CoreObject.coffee:131

Available since 0.0.2

Get the UUID of the object or fail if none defined

Returns:

String:

UUID of the object

Properties

_basePath

String private

Defined in src/Database.coffee:32

Available since 0.0.2

The base path

Default: "./json.db"

_config

Object private

Defined in src/Database.coffee:41

Available since 0.0.2

Configuration options

Default: {deletedAtKey: "__deleted"}

_layers

Array private

Defined in src/Database.coffee:50

Available since 0.0.2

Array of all layers (overlays) path

Default: [this._baseBath]

_models

Object private

Defined in src/Database.coffee:59

Available since 0.0.2

Loaded models, indexed by their bare name

Default: null