NoSQL
NoSQL is a flat file JSON database management system which is a fork of LaciDb.
Overview
NoSQL is a flat file Database with a JSON storage format. Due to the JSON format, NoSQL is schemaless like any other NoSQLs. A record can have different columns.
In NoSQL there is no table, it is a collection. A collection in NoSQL represents a file that holds multiple records (in JSON format).
In NoSQL, each query will open file => query execution (select|insert|update|delete) => file is closed.
NoSQL is not for:
- Saving a large database with lots of data.
- Storing databases that require a high level of security.
NoSQL created for:
- Handling small data such as settings, queues or other small data.
- For those of you who want a portable database that is easy to import/export, version control and backup.
- For those of you who want a database that is easy to edit yourself without using special software.
Instantiate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
How it Works?
The way NoSQL works is basically just flowing the array of json_decode
results into the 'pipes' which functions as filtering, mapping, sorting and limiting until finally the results will be executed to retrieve the value, change its value or discard (read: deleted).
The following is an explanation of the process:
Filtering
To do filtering you can use the where
andorWhere
methods. Both methods can accept the Closure
parameter or some key, operator, value
parameter.
Mapping
Mapping is used to form a new value on each filtered record.
Here are some methods for mapping records:
map(Closure $mapper)
For mapping records in the filtered collection.
select(array $columns)
Mapping records to retrieve only certain columns.
withOne(Collection|Query $relation, $key, $otherKey, $operator, $thisKey)
To take a 1:1 relation.
withMany(Collection|Query $relation, $key, $otherKey, $operator, $thisKey)
To take a 1:n relation.
Sorting
Sorting is used to sort data that has been filtered and mapped. To do the sorting you can use the sortBy($key, $ascending)
method. The parameter $key
can be a string key/column to sort or Closure
if you want to sort based on the computed value first.
Limiting/Taking
After the data has been filtered, mapped, and sorted, you can cut and retrieve some of the data using the skip($offset)
or take($limit, $offset)
method.
Executing
After filtering, mapping, sorting, and setting aside, the next step is to execute the results.
Here are some methods for executing:
get(array $select = [])
Fetching a set of records in collection. If you want to retrieve a specific column define the column in the $select
array.
first(array $select = [])
Fetch (one) record in a collection. If you want to retrieve a specific column define the column in the $select
array.
count()
Count all elements in the collection based on mapping criteria.
sum($key)
Fetch the total key specified in the collection.
avg($key)
Take the average of certain keys in a collection.
min($key)
Fetches the lowest value of a specific key in a collection.
max($key)
Fetches the highest value of a specific key in a collection.
lists($key, $resultKey = null)
Collect and retrieve specific keys into the array on the collection.
insert(array $data)
Insert new data into the collection.
inserts(array $listData)
Batch insert new data into the collection. Note: insert
and inserts
cannot be performed after the query is filtered or mapped.
update(array $newData)
Updates the data on records in a filtered and mapped collection.
save()
Similar to update
. Except that save
will save records based on the mapping results, not based on $newData
as in update
.
delete()
Clears data in filtered and mapped collections.
truncate()
Erases all data. No need for filtering and mapping beforehand.
Examples
Insert Data
1 2 3 4 5 |
|
$user
will return an array like this:
1 2 3 4 5 6 |
|
'_id' is
uniqid()
Find Single Record By ID
1 |
|
Find One
1 |
|
Select All
1 |
|
Update
1 2 3 4 |
|
Return value is count affected records
Delete
1 |
|
Return value is count affected records
Multiple Inserts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Find Where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Operator can be '=', '<', '<=', '>', '>=', 'in', 'not in', 'between', 'match'.
Fetching Specific Columns/Keys
1 2 |
|
Alias Column/Key
1 2 |
|
Mapping
1 2 3 4 5 6 |
|
Sorting
1 2 3 4 5 6 7 8 9 10 |
|
Limit & Offset
1 2 3 4 5 |
|
Join
1 2 3 4 5 6 7 8 |
|
Map & Save
1 2 3 4 |
|
Transaction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Macro Query
The query macro allows us to add a new method to the Qubus\NoSql\Collection
instance so that we can use it repeatedly in a more fluid manner.
For example, we want to retrieve active user data, if in the usual way we can do a query like this:
1 |
|
If used repeatedly, sometimes we forget to recognize the active user whose active
value is 1
, or true
, or yes
, or YES
, or Yes
, or y
, or Y
, etc.?
So to make things easier, we can use the following macros:
1 2 3 |
|
So that we can get active users in this way:
1 |
|