Namespace

online

kiss.db.online

Online database wrapper for KissJS server

Standard REST requests are performed for basic CRUD operations, except for:

  • find(modelId, query)
  • findById(modelId, ids, sort, sortSyntax)
  • updateOneDeep(modelId, recordId, update)
  • updateBulk(operations)
  • deleteOne(modelId, recordId, sendToTrash)
  • deleteMany(modelId, query, sendToTrash)

Remember: these custom methods are design for KissJS server, and will not behave the same if used on another REST endpoint.

The find method provides a rich and expressive way of querying the database with a query parameter. The query can be an object with complex filters (multi-level AND / OR) and multi-field sorting, so, we need a way to send that (eventually big) object to the server. As the HTTP GET method has some limitations about the amount of data that can be sent, KissJS uses a POST instead (despite it's a READ operation).

The updateOneDeep method performs a deep update of the record and its relationships. This is the main method used to perform the updates of foreign records in a NoSQL environment.

The updateBulk method offers a convenient way to perform multiple updates in multiple collections at once. The operations parameter is an array of operations, where each operation contains:

  • the target modelId
  • the target recordId
  • the updates to apply to the target record

The deleteOne and deleteMany operations have an extra "sendToTrash" parameter which is useful to implement soft-deletion.

Below is a table of correspondance between the API and how it is converted to HTTP requests:

kiss.db.online HTTP HTTP body
insertOne(modelId, record) POST /modelId record
insertMany(modelId, records) POST /modelId records
updateOne(modelId, recordId, update) PATCH /modelId/recordId update
updateOneDeep(modelId, recordId, update) PATCH /modelId/recordId {operation: "updateOneDeep", update} => update the record and its relationships
updateMany(modelId, query, update) PATCH /modelId query+update
updateBulk(operations) PATCH /data operations
findOne(modelId, recordId) GET /modelId/recordId
findById(modelId, ids, sort, sortSyntax) POST /modelId Array of ids
find(modelId) GET /modelId
find(modelId, query) POST /modelId query
deleteOne(modelId, recordId, sendToTrash) DELETE /modelId/recordId {sendToTrash: true/false}
deleteMany(modelId, query, sendToTrash) POST /modelId {operation: "delete", sendToTrash: true/false}

View Source client/core/db/online.js, line 1

Methods

# async static count(modelId, query) → {number}

Count the number of records that match a query

Parameters:
Name Type Description
modelId string
query object

Use same query format as for find() method

View Source client/core/db/online.js, line 448

The number of records

number

# static deleteMany(modelId, query, sendToTrashopt) → {object}

Delete many records from a collection

Note: REST doesn't have any standard way to perform a bulk delete. For this, we use a custom POST operation, as suggested in the Google API Design Guide

Parameters:
Name Type Attributes Description
modelId string
query object
sendToTrash boolean <optional>

If true, keeps the original record in a "trash" collection

View Source client/core/db/online.js, line 410

The server response

object

# async static deleteOne(modelId, recordId, sendToTrashopt) → {boolean}

Delete an element from a collection. See db.delete

Parameters:
Name Type Attributes Description
modelId string
recordId string
sendToTrash boolean <optional>

If true, keeps the original record in a "trash" collection

View Source client/core/db/online.js, line 374

boolean

# async static find(modelId, queryopt) → {Array.<object>}

Find documents in a collection. See db.find

Important:

  • without query params, it uses standard GET
  • with query params, the only way is to "POST" the request to the server In effet, the HTTP GET query parameters are limited in size, which is blocking to handle complex queries.
Parameters:
Name Type Attributes Description
modelId string
query object <optional>

Optional query object

View Source client/core/db/online.js, line 333

An array containing the records data

Array.<object>

# async static findById(modelId, ids, sortopt, sortSyntaxopt) → {Array.<object>}

Find multiple records by id

Parameters:
Name Type Attributes Description
modelId string
ids Array.<string>

ids of the records to retrieve

sort Array.<object> | object <optional>

Sort options, as a normalized array or a Mongo object. Normalized example: [{fieldA: "asc"}, {fieldB: "desc"}]. Mongo example: {fieldA: 1, fieldB: -1}

sortSyntax string <optional>

Sort syntax: "nomalized" | "mongo". Default is normalized

View Source client/core/db/online.js, line 303

The found records

Array.<object>

# async static findOne(modelId, recordId) → {object}

Find a single record by id. See db.findOne

Parameters:
Name Type Description
modelId string
recordId string

View Source client/core/db/online.js, line 284

The server response

object

# async static insertMany(modelId, records) → {object}

Insert many records in a collection. See db.insertMany

Parameters:
Name Type Description
modelId string
records Array.<object>

An array of records [{...}, {...}] for bulk insert

View Source client/core/db/online.js, line 94

The server response

object

# async static insertOne(modelId, record) → {object}

Insert one record in a collection. See db.insertOne

Parameters:
Name Type Description
modelId string
record object

A single record

View Source client/core/db/online.js, line 62

The server response

object

# async static updateBulk(operations) → {object}

Update multiple records in multiple collections. See db.updateBulk

Parameters:
Name Type Description
operations Array.<object>

The list of updates to perform

View Source client/core/db/online.js, line 254

The server response

object

Update the 2 records connected by a link

Parameters:
Name Type Description
link object

View Source client/core/db/online.js, line 235

The transaction result

# async static updateMany(modelId, query, update) → {object}

Update many records in a single collection. See db.updateMany

Parameters:
Name Type Description
modelId string
query object
update object

View Source client/core/db/online.js, line 161

The server response

TODO: NOT TESTED YET

object

# async static updateOne(modelId, recordId, update) → {object}

Update a record in a collection. See db.updateOne

Parameters:
Name Type Description
modelId string
recordId string
update string

Update to apply to the record. Ex: {firstName: "Bob"}

View Source client/core/db/online.js, line 126

The server response

object

# async static updateOneDeep(modelId, recordId, updateopt) → {boolean}

Update a single field of a record then propagate the triggered mutations to foreign records

IMPORTANT: this method does not broadcast to the active user because all relationships computations are done server-side

Parameters:
Name Type Attributes Description
modelId string
recordId string
update string <optional>

If not specified, re-compute all the computed fields

View Source client/core/db/online.js, line 202

true if the update is successful

boolean
Example
await kiss.db.updateOneDeep("company", "f07xF008d", {"name": "pickaform"})