Database Class
The main class of the local-json-db
, representing a database with one or more layers
Constructor
Database
()
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();
Item Index
Methods
Methods
_createModelStore
-
name
Creates the store for a given model
Parameters:
-
name
Stringname of the model
Returns:
the newly created store for the given model
_modelName
-
name
Normalize a model name
Parameters:
-
name
Stringname of the model to normalize
Returns:
normalized name
_saveModelStore
-
name
-
model
Saves the store of a given model to disk
Returns:
the full path of the file that has been saved
addOverlay
-
path
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
Stringthe path where to read/write JSON files of records, relative to the base path
assert
-
expression
-
message
Make an assertion and throw an error if it fails
assertNotLoaded
-
msg
Asserts that the DB hasn't been loaded yet, or throw an error
Parameters:
-
msg
Stringadditional message to add in the error
className
()
String
Returns the name of the class for this object
Returns:
class name of this object
constructor
-
[basePath="./json.db"]
-
[config={}]
Constructs a new instance of Database
count
-
modelName
Counts all records of a given model
Parameters:
-
modelName
Stringname of the model to count records
Returns:
the total number of records
createRecord
-
modelName
-
record
Creates a new record in the database
Returns:
a copy of the newly created model with read-only id
deleteRecord
-
modelName
-
id
Deletes a record given its id
Returns:
a copy of the old record which has been deleted
destroy
()
Destroy and free the db object
find
-
modelName
-
id
Finds a record by id
Returns:
copy of the record if found, else undefined
findAll
-
modelName
Finds all records in the database
Parameters:
-
modelName
Stringname of the model
Returns:
array containing all records of the given model
findMany
-
modelName
-
ids
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:
Returns:
array containing all found records
findQuery
-
modelName
-
filter
-
[thisArg]
Finds records using a filter (either function or set of properties to match)
Parameters:
Returns:
array with all records which matched
identify
()
String
Get a string identification of the object
Returns:
the string identifying the object
isLoaded
()
Boolean
Whether the database has been loaded or not (in that case no overlay can be added)
Returns:
returns true
if the db is loaded, else false
load
-
force
Loads the database (you don't need to call this method, it'll be automatically called when needed)
Parameters:
-
force
Booleanwhether to force a reload in case it has already been loaded previously
lockProperties
-
names
Lock given properties so that setting them will fail
Parameters:
-
names
String multiplethe name of each property to lock
log
-
[level="debug"]
-
items
Log a string with the class name as a prefix
Parameters:
-
[level="debug"]
String optionallevel (debug, notice, info, warning, error, danger or fatal)
-
items
Mixed multipleanything to give back to the logger
modelFactory
-
modelName
Get the Model instance given a model name
Parameters:
-
modelName
Stringname of the model to get the instance
Returns:
the model object
modelNameToFileName
-
modelName
Used to transform a model name into its store's JSON file name
Parameters:
-
modelName
Stringname of the model
Returns:
name of the file, sanitized and normalized
save
()
chainable
Saves in the top overlay's path the records that have been created/modified or deleted
unload
()
chainable
Unloads the database and all the records. This does NOT save anything
Properties
_layers
ArrayArray of all layers (overlays) path
Default: [this._baseBath]
_models
ObjectLoaded models, indexed by their bare name
Default: null