FLUENT
A JS library that gives a readable query builder. It takes care of the heavy work for you to enjoy quering your services. Fluent works for both Local and Remote Storage/API providers
Installing
To install this package in your project, you can use the following command within your terminal.
npm install --save fast-fluent
Config
Fluent offers a handy configuration method that will get you up and running in seconds.
; Fluent;
Connectors
All Fluent connectors are independent libraries, so you will need to download them separetly.
Here is an example of a REMOTE connector using fluent-formio
name: 'formio' baseUrl: 'https://ydrahgggqviwquft.form.io/' connector: formio
You can use as many connectors as you like. All available connectors are listed bellow.
REMOTE
Remote connectors give you access to API's or Databases, such as MongoDB, Formio
Provider | Library |
---|---|
Formio | fluent-formio |
LOCAL
Local connectors give you access to in Browser/Memory DB's such as LokiJs, IndexedDB, LocalStorage
Provider | Library |
---|---|
LockiJS | fluent-loki |
MERGE
Merge connectors will pull from both Local and Remote storage providers and will merge results to give you access to all your data from one call
Provider | Library |
---|---|
Loki-Formio | fluent-loki-formio |
Your first Model
To start using Fluent your will need to create a Fluent Model. Every Fluent Model is a @stampit, so you can further compose it as you like. If you only defined one connector it will be used as default, so no need to configure your models
const Mymodel = Fluent
In case you defined multiple connectors, your model will need to choose where to get its data from
const Mymodel = Fluent
Onother way of defining a connector as default when using multiple connectors, is to set it on the Fluent.config
; Fluent;
Using a Fluent Model
Remote, Local or Merge?
Every Fluent Model can pull/push data from those 3 sources. All Fluent methods are ASYNC functions be patient and AWAIT for them ;)
const Mymodel = Fluent // Option 1let model = await Mymodel// Option 2let model = await Mymodel// Option 3let model = await Mymodel
After deciding where to pull/push the data from you will have access to quite a few helpful methods
Basic CRUD methods
Creating
const Mymodel = Fluentconst myObj = foo : "bar" pin: "pon"const myArrayOfObjs = foo : "bar" pin: "pon" foo : "bar" pin: "pon"// Single insert examplelet inserted = await Mymodel // Multiple insert examplelet inserted = await Mymodel
Updating
All Fluent models identify records by de _id identifier. To update a record you MUST give it the _id
const Mymodel = Fluent// Single insert examplelet updated = await Mymodel
Deleting
To remove a records you must provide its ID
const Mymodel = Fluent// Single insert examplelet updated = await Mymodel
Reading - Query Builder
Here is where we start having fun with Fluent. Fluent offers a powerful QueryBuilder HEAVILY inspired in Laravel (Thanks Taylor Otwell!!)
Get all Objects (rows)
const Users = Fluent// Single insert examplelet users = await Users
Unlike Laravel, Fluent get() Method returns and Array with the requested Objects, ready for you to use.
users
Get a single Object (row)
const Users = Fluent// Single insert examplelet user = await Users
In this case Fluent will return a single Object
username
Get a list of Column Values
const Users = Fluent// Single insert examplelet names = await Users
Select
const Users = Fluent// Single insert examplelet users = await Users // Its also possible to pass it as an Arraylet users = await Users
Ordering, Grouping, Limit & Offset
orderBy
const Users = Fluent// Single insert examplelet users = await Users
skip / take
const Users = Fluent// Single insert examplelet users = await Users
Is the same as
const Users = Fluent// Single insert examplelet users = await Users
Collections
To further operate your data, you can turn your results into Collections (Yes, Thanks again Taylor!) Just make sure to call the collect method instead of the get()
const Users = Fluent// Single insert examplelet users = await Users
You can also turn any array into a Fluent Collection by using the collect method
let users = name: 'John' age: 20 name: 'Michael' age: 40// Single insert examplelet users = Fluent
Now with the collection on hand, you can use the following operators
average
Alias for avg()
users // 30
avg
users // 30
Chunk
Chunk will just get all results and then separate them in smaller Arrays.
users // [[{name: 'John', age: 20}],[{name: 'Michael', age: 40}]]
Collapse
let collection = Fluent;collection// [{name: 'John', age: 20}, {name: 'Michael', age: 40}]