Massive-Collections
Collections wrapper for Massive-JS. You must already have a connection and Massive-Collections work with your database connection object.
Dependencies
- bluebird
- massive (connection)
Let's admit we have a users table :
( id serial primary key name varchar(255), age smallint, infos jsonb not null, created timestamp with time zone default now(), modified timestamp with time zone default now())
Create a collection file : users.js
const ColumnMissing = ;const Collection = ; // Get the connection from massivemodule { const UsersCollection = 'users' db; return UsersCollection;};
Methods
Now you can use following methods :
- get (id)
- count
- find
- insert
- update
- updateAll
- remove
- removeAll
- flush
Each method returns a Promise.
Count method
Purpose: Count database row.
Returns: {Number}
Parameter | Type | Description | Example |
---|---|---|---|
conditions | Object | WHERE conditions | { "name ~~": "jo%" } // name like |
Collection.count(conditions)
Users;
Get method
Purpose: Get a specific row.
Returns: {Object}
Parameter | Type | Description | Example |
---|---|---|---|
id | Number | ID | 5 |
Collection.get(id)
Users;
Find method
Purpose: Get an array of rows.
Returns: {Array}
Parameter | Type | Description | Example |
---|---|---|---|
conditions | Object | WHERE conditions | { "name ~~": "jo%" } // name like |
options | Object | Other options, like limit, offset, etc... | { columns: ["name", "price", "description"], order: {field: "price", direction: "desc"}, offset: 20, limit: 10 } |
Collection.find(conditions, options)
Users;
Insert method
Purpose: Insert an new row in our table.
Returns: {Object}
Parameter | Type | Description | Example |
---|---|---|---|
data | Object | Values | { name: "John Doe", infos: { email: "john.doe@domain.tld" } } |
Collection.insert(data)
UsersCollection;
Update method
Purpose: Update a row where id = ...
Returns: {Object} (updated row)
Parameter | Type | Description | Example |
---|---|---|---|
id | Number | id of the item | 3 |
data | Object | new data to set | { name: "bobby" } |
Collection.update(id, data)
UsersCollection;
UpdateAll method
Purpose: Update any rows where conditions match
Returns: {Array} (updated rows)
Parameter | Type | Description | Example |
---|---|---|---|
conditions | Object | WHERE conditions | { "name ~~": "jo%" } // name like |
data | Object | new data to set | { name: "bobby" } |
Collection.updateAll(conditions, data)
UsersCollection;
Remove method
Purpose: Remove a row where id = ...
Returns: {Object} (deleted item)
Parameter | Type | Description | Example |
---|---|---|---|
id | Number | id of the item | 5 |
Collection.remove(id)
UsersCollection;
RemoveAll method
Purpose: Remove any rows that match conditions
Returns: {Array} (deleted items)
Parameter | Type | Description | Example |
---|---|---|---|
conditions | Object | WHERE conditions | { "name ~~": "jo%" } // name like |
Collection.removeAll(conditions)
UsersCollection;
Flush method
Purpose: Remove any rows in that table
Parameter | Type | Description | Example |
---|---|---|---|
reset_seq | Boolean | (Optionnal) Describe if we need to reset the linked sequence or not | true |
Collection.flush(reset_seq)
UsersCollection; // Reset sequence UsersCollection
Formatters
You can format data when you read/write.
dbFormat before any write jsFormat before any read
This will be helpful with custom format or extra libs (password hash, etc...).
UsersCollection;
Hooks
You can hook methods call and change data value before any database write.
To assign a pre hook, we use the following method : Collection.preHook(hookName, callback)
UsersCollection;
To assign a post hook, we use the following method : Collection.postHook(hookName, callback)
UsersCollection;
List of Hooks
- pre->count(next)
- pre->get(next)
- pre->find(next)
- pre->flush(next)
- pre->insert(next, data)
- pre->update(next, data)
- pre->updateAll(next, data)
- pre->remove(next)
- post->get(data)
- post->count(data)
- post->find(data)
- post->flush()
- post->insert(data)
- post->update(data)
- post->updateAll(data)
- post->remove(data)
For Pre Insert and Pre Update, you must pass data
through the next
callback.
Custom queries
Like : ~~ :
UsersCollection;
Not Like : !~~ :
UsersCollection;
iLike (case insensitive) :
UsersCollection;
Not iLike (case insensitive) :
UsersCollection;
Compare : > < <= >= :
UsersCollection;
JSONB queries :
Let's assume that we have a columns infos
:
{ "email": "...", "followers": 5000 }
// Value in object likeUsersCollection; // Get value then castUsersCollection; // SortUsersCollection;
CLI
You can create table from a terminal with massive-collections-cli. Let's assume that you already have a connection to a postgresql database (user, password, etc.).
You need to connect first (in a terminal) :
node_modules/.bin/massive-collections-cli connect --h=localhost:5432 --db=test_db --u=root --p=root
Once you are connected, you generate automatically a new file : massive-collections_credentials.json
that should automatically be added to your .gitignore.
Then, you can create tables (in a terminal) :
# Note that we use double quote to prevent bash errors
node_modules/.bin/massive-collections-cli createTable posts "title:varchar(255):unique:notnull" "content:text" "picture:integer" "author:integer" "details:jsonb" "created:timestampz:noindex:null:now()"
Do not add an id column, this is automatic.
If you want to remove properly your credentials, you can disconnect (in a terminal) :
node_modules/.bin/massive-collections-cli disconnect
Please read the documentation first (in a terminal) :
node_modules/.bin/massive-collections-cli help