Resource accessor for ClayDB
- Functions
ClayResource
Class- new ClayResource(nameString, bounds, options)
- resource.one(id, options, )
- resource.list(condition, options, )
- resource.create(attributes, options)
- resource.update(id, attributes, options)
- resource.destroy(id, options)
- resource.drop(options)
- resource.oneBulk(ids, options)
- resource.listBulk(conditionArray)
- resource.createBulk(attributesArray, options)
- resource.updateBulk(attributesHash, options)
- resource.destroyBulk(ids, options)
- resource.cursor(condition, options)
- resource.each(handler, condition, options)
- resource.first(filter, options)
- resource.last(filter, options)
- resource.only(filter, options)
- resource.seal(privateKey, options)
- resource.has(id)
- resource.exists(filter)
- resource.count(filter)
- resource.of(attributes)
- resource.all(filter, options, )
- resource.refOf(id)
- resource.toggleAnnotate()
- resource.clone()
- resource.addInbound(name, inbound)
- resource.hasInbound(name)
- resource.removeInbound(name)
- resource.applyInbound(attributesArray, actionContext)
- resource.inboundAttributes(attributes, actionContext)
- resource.inboundAttributesArray(attributesArray, actionContext)
- resource.inboundAttributesHash(attributesHash, actionContext)
- resource.addOutbound(name, handler)
- resource.hasOutbound(name)
- resource.removeOutbound(name)
- resource.applyOutbound(entities, actionContext)
- resource.outboundEntity(entity, actionContext)
- resource.outboundEntityArray(entityArray, actionContext)
- resource.outboundCollection(collection, actionContext)
- resource.outboundEntityHash(entityHash, actionContext)
- resource.outboundCollectionArray(collectionArray, actionContext)
- resource.getPolicy()
- resource.setPolicy(policy)
- resource.removePolicy()
- resource.fetchPolicy(digest)
- resource.savePolicy(policy)
- resource.addRef(resourceName, resource)
- resource.hasRef(resourceName)
- resource.getRef(resourceName)
- resource.removeRef(resourceName)
- resource.sub(name)
- resource.subNames()
- resource.throwEntityNotFoundError(id)
- resource.internal(name)
- resource.internalNames()
- resource.prepares(prepares)
- resource.prepareIfNeeded()
- resource.prepare()
- resource.addPrepareTask(name, task)
- resource.hasPrepareTask(name)
- resource.removePrepareTask(name)
- resource.setNeedsPrepare(needsPrepare)
- resource.decorate(methodName, decorate)
- resource.caches(caches)
- resource.storeCache(entity)
- resource.gainCache(id)
- resource.removeCache(id)
- resource.requestCacheClear(ids)
- resource.parseCondition(condition)
- resource.parseConditionArray(conditionArray)
Create a ClayResource instance
Param | Type | Description |
---|---|---|
args | * |
Create clayResource class from driver
Param | Type | Description |
---|---|---|
driver | Driver | Driver to bind |
nameString | string | Resource name string |
options | Object | Optional settings |
Example:
const { fromDriver } = require('clay-resource')
const { SqliteDriver } = require('clay-driver-sqlite')
{
let driver = new SqliteDriver('var/test.db')
let resource = fromDriver(driver)
}
Resource accessor
Extends:
AnnotateMixed
CloneMixed
InboundMixed
OutboundMixed
PolicyMixed
RefMixed
SubMixed
ThrowMixed
InternalMixed
PrepareMixed
DecorateMixed
CacheMixed
ConditionMixed
ClusterMixed
Constructor of ClayResource class
Param | Type | Description |
---|---|---|
nameString | string | Name string |
bounds | Object.<string, function()> | Method bounds |
options | Object | Optional settings |
options.annotates | boolean | Enable annotation |
options.refs | Array.<ClayResource> | Add resource refs |
Get a resource
Param | Type | Description |
---|---|---|
id | ClayId | Id of the entity |
options | Object | Optional settings |
options.ignoreCached | boolean | Ignore cached data |
options.strict | boolean | If true, throws an error when not found |
options.plain | boolean | Mark result as plain object |
Example:
const Product = lump.resource('Product')
async function tryOne () {
const product = await Product.one(1) // Find by id
console.log(product)
}
List entities from resource
Param | Type | Description |
---|---|---|
condition | ListCondition | List condition query |
condition.filter | FilterTerm | Filter condition |
condition.page | PagerTerm | Page condition |
condition.page.number | number | Number of page, start with 1 |
condition.page.size | number | Number of resources per page |
condition.sort | SortTerm | Sort condition |
options | Object | Optional settings |
Example:
const Product = lump.resource('Product')
async function tryList () {
const products = await Product.list({
filter: { type: 'Vehicle' }, // Filter condition
page: { number: 1, size: 25 }, // Paginate
sort: [ 'createdAt', '-name' ] // Sort condition
})
console.log(products)
}
tryList()
Create a new entity with resource
Param | Type | Description |
---|---|---|
attributes | Object | Resource attributes to create |
options | Object | Optional settings |
options.allowReserved | boolean | Arrow to set reserved attributes (like "id") |
options.errorNamespace | string | Namespace for error fields |
options.waitListeners | boolean | Wait for listeners |
Example:
const Product = lump.resource('Product')
async function tryCreate () {
const product = await Product.create({
name: 'Super Car',
type: 'Vehicle'
})
console.log(product)
}
tryCreate()
Update an existing entity in resource
Param | Type | Description |
---|---|---|
id | ClayId | Resource id |
attributes | Object | Resource attributes to update |
options | Object | Optional settings |
options.allowReserved | boolean | Arrow to set reserved attributes (like "id") |
options.errorNamespace | string | Namespace for error fields |
options.waitListeners | boolean | Wait for listeners |
Example:
const Product = lump.resource('Product')
async function tryUpdate () {
const product = await Product.update(1, {
name: 'Super Super Car'
})
console.log(product)
}
tryUpdate()
Delete a entity resource
Param | Type | Description |
---|---|---|
id | ClayId | Resource id |
options | Object | Optional settings |
options.waitListeners | boolean | Wait for listeners |
Example:
const Product = lump.resource('Product')
async function tryDestroy () {
await Product.destroy(1)
}
tryDestroy()
Drop resource
Param | Type | Description |
---|---|---|
options | Object | Optional settings |
options.waitListeners | boolean | Wait for listeners |
Example:
const Product = lump.resource('Product')
async function tryDrop () {
await Product.drop()
}
tryDrop()
One as bulk
Param | Type | Description |
---|---|---|
ids | Array.<ClayId> | Resource ids |
options | Object | Optional settings |
Example:
const Product = lump.resource('Product')
async function tryOneBulk () {
const products = await Product.oneBulk([ 1, 5, 10 ])
console.log(products)
}
tryOneBulk()
List with multiple conditions
Param | Type | Description |
---|---|---|
conditionArray | Array.<ListCondition> |
Example:
const Product = lump.resource('Product')
async function tryListBulk () {
const [ cars, ships ] = await Product.listBulk([
{ filter: { type: 'CAR' } },
{ filter: { type: 'SHIP' } },
])
console.log(cars)
console.log(ships)
}
tryListBulk()
Create multiple resources
Param | Type | Description |
---|---|---|
attributesArray | Array.<Object> | List of attributes |
options | Object | Optional settings |
options.allowReserved | boolean | Arrow to set reserved attributes (like "id") |
options.errorNamespace | string | Namespace for error fields |
options.waitListeners | boolean | Wait for listeners |
Example:
const Product = lump.resource('Product')
async function tryCreateBulk () {
const products = await Product.createBulk([
{ name: 'Super Orange', type: 'CAR' },
{ name: 'Ultra Green', type: 'CAR' },
])
console.log(products)
}
tryCreateBulk()
Update multiple resources
Param | Type | Description |
---|---|---|
attributesHash | Object.<ClayId, Object> | Hash of attributes |
options | Object | Optional settings |
options.allowReserved | boolean | Arrow to set reserved attributes (like "id") |
options.errorNamespace | string | Namespace for error fields |
options.waitListeners | boolean | Wait for listeners |
Example:
const Product = lump.resource('Product')
async function tryUpdateBulk () {
const products = await Product.updateBulk({
'1': { name: 'Super Super Orange' },
'2': { name: 'Ultra Ultra Green' },
})
console.log(products)
}
tryUpdateBulk()
Update multiple resources
Param | Type | Description |
---|---|---|
ids | Array.<ClayId> | Ids to destroy |
options | Object | Optional settings |
Example:
const Product = lump.resource('Product')
async function tryDestroyBulk () {
await Product.destroyBulk([1, 2])
})
tryDestroyBulk()
Create cursor to cursor
Param | Type | Description |
---|---|---|
condition | Object | Search condition |
condition.filter | FilterTerm | Filter condition |
condition.sort | SortTerm | Sort condition |
options | Object | Optional settings |
Example:
const Product = lump.resource('Product')
async function tryCursor () {
const cursor = await Product.cursor({
filter: { type: 'CAR' }
})
console.log(cursor.length) // Number of entities matches the condition
for (const fetch of cursor) {
const car = await fetch() // Fetch the pointed entity
console.log(car)
}
}
tryCursor()
Iterate entities with handler
Param | Type | Description |
---|---|---|
handler | function | Entity handler |
condition | Object | Optional settings |
condition.filter | FilterTerm | Filter condition |
condition.sort | SortTerm | Sort condition |
options | Object | Optional settings |
Example:
const Product = lump.resource('Product')
async function tryEach () {
await Product.each(async (product) => {
// Do something with each entity
}, {
filter: {price: {$gt: 100}}
})
}
tryEach()
Get the first entity matches filter
Param | Type | Description |
---|---|---|
filter | FilterTerm | Listing filter |
options | Object | Optional settings |
options.sort | Object | Sort conditions |
Example:
const Product = lump.resource('Product')
async function tryFirst () {
const product = Product.first({ name: 'Super Super Orange' })
console.log(product)
}
tryFirst()
Get the last entity matches filter
Param | Type | Description |
---|---|---|
filter | FilterTerm | Listing filter |
options | Object | Optional settings |
options.sort | Object | Sort conditions |
Almost same with .first()
, but throws an error if multiple record hits, or no record found
Param | Type | Description |
---|---|---|
filter | FilterTerm | Listing filter |
options | Object | Optional settings |
options.strict | boolean | If true, throws an error when not found |
Example:
const Product = lump.resource('Product')
async function tryOnly () {
const product = Product.only({ name: 'Super Super Orange' })
console.log(product)
}
tryOnly()
Seal resources
Param | Type | Description |
---|---|---|
privateKey | string | RSA Private key |
options | Object | Optional settings |
options.by | string | For $$by |
Example:
const Product = lump.resource('Product')
const privateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
async function trySeal () {
await Product.seal(privateKey)
}
trySeal()
Check entity with id exists
Param | Type | Description |
---|---|---|
id | ClayId | Id of the entity |
Example:
const Product = lump.resource('Product')
async function tryHas () {
const has = await Product.has(1)
console.log(has)
}
tryHas()
Check data exists with filter
Param | Type | Description |
---|---|---|
filter | FilterTerm | List filter |
Example:
const Product = lump.resource('Product')
async function tryExists () {
const exists = await Product.exists({ name: 'Super Super Orange' })
console.log(exists)
}
tryExists()
Count data matches filter
Param | Type | Description |
---|---|---|
filter | FilterTerm | List filter |
Example:
const Product = lump.resource('Product')
async function tryCount () {
const count = await Product.count({ type: 'CAR' })
console.log(count)
}
tryCount()
Find entity with attributes and returns if found. If not found, create and return the one.
Param | Type | Description |
---|---|---|
attributes | Object | Attributes |
Example:
const Product = lump.resource('Product')
async function tryOf () {
const values = await Product.of({ code: '#1234' })
console.log(values)
}
tryOf()
Get all entities inside resource which matches the filter
Param | Type | Description |
---|---|---|
filter | FilterTerm | Listing filter |
options | Object | Optional settings |
options.sort | string,Array.<string> | Sort attribute |
options.iterateSize | number | Size to iterate |
Get resource ref string for this resource
Param | Type | Description |
---|---|---|
id | ClayId,string | Resource id |
Clone the instance
Add inbound
Param | Type | Description |
---|---|---|
name | string | Name of inbound |
inbound | function | Inbound function |
Check if has inbound
Param | Type | Description |
---|---|---|
name | string | Name of inbound |
Remove inbound
Param | Type | Description |
---|---|---|
name | string | Name of inbound |
Apply inbound to array of attributes
Param | Type | Description |
---|---|---|
attributesArray | Array.<EntityAttributes> | Array of attributes |
actionContext | ActionContext | Context for resource action |
Inbound attributes
Param | Type | Description |
---|---|---|
attributes | EntityAttributes | Attributes to inbound |
actionContext | ActionContext | Context for resource action |
resource.inboundAttributesArray(attributesArray, actionContext) -> Promise.<Array.<EntityAttributes>>
Inbound attributes array
Param | Type | Description |
---|---|---|
attributesArray | Array.<EntityAttributes> | Attributes array to inbound |
actionContext | ActionContext | Context for resource action |
Inbound attributes hash
Param | Type | Description |
---|---|---|
attributesHash | AttributesHash | |
actionContext | ActionContext | Context for resource action |
Add outbound
Param | Type | Description |
---|---|---|
name | string | Name of outbound |
handler | function | Format handler function |
Check if has outbound
Param | Type | Description |
---|---|---|
name | string | Name of outbound |
Remove outbound
Param | Type | Description |
---|---|---|
name | string | Name of outbound |
Apply outbound to entities
Param | Type | Description |
---|---|---|
entities | Array.<Entity> | Entities to outbound |
actionContext | Object |
Format entity
Param | Type | Description |
---|---|---|
entity | Entity | |
actionContext | Object |
Proses entity array
Param | Type | Description |
---|---|---|
entityArray | Array.<Entity> | |
actionContext | Object |
Format entity collection
Param | Type | Description |
---|---|---|
collection | Collection | |
actionContext | Object |
Format entity hash
Param | Type | Description |
---|---|---|
entityHash | EntityHash | |
actionContext | Object |
Format collection array
Param | Type | Description |
---|---|---|
collectionArray | CollectionArray | |
actionContext | Object |
Get the policy
Set policy
Param | Type | Description |
---|---|---|
policy |
Remove policy
Fetch policy from db
Param | Type | Description |
---|---|---|
digest | string |
Save policy
Param | Type | Description |
---|---|---|
policy | ClayPolicy |
Add resource ref
Param | Type | Description |
---|---|---|
resourceName | string | |
resource | ClayResource |
Has resources ref
Param | Type | Description |
---|---|---|
resourceName | string |
Get resource ref
Param | Type | Description |
---|---|---|
resourceName | string |
Remove resource ref
Param | Type | Description |
---|---|---|
resourceName | string |
Get sub resource
Param | Type | Description |
---|---|---|
name | string |
Get names of sub resources
Throw entity not found error
Param | Type | Description |
---|---|---|
id | ClayId |
Get internal resource
Param | Type | Description |
---|---|---|
name | string |
Get names of internal resources
Toggle prepare support
Param | Type | Description |
---|---|---|
prepares | boolean | Should prepare or not |
Do prepare if needed
Do preparing
Add prepare task
Param | Type | Description |
---|---|---|
name | string | Name of task |
task | function | Task function |
Check if has task
Param | Type | Description |
---|---|---|
name | string |
Remove a task
Param | Type | Description |
---|---|---|
name | string | Name of task |
Set needs prepare
Param | Type | Description |
---|---|---|
needsPrepare | boolean | Needs preparing |
Decorate a method
Param | Type | Description |
---|---|---|
methodName | string | Name of method |
decorate | function | Decorate function |
Toggle caching
Param | Type | Description |
---|---|---|
caches | boolean | Should cache or not |
Store an entity into cache
Param | Type | Description |
---|---|---|
entity |
Gain entity from cache
Param | Type | Description |
---|---|---|
id | ClayId |
Remove entity from cache
Param | Type | Description |
---|---|---|
id | ClayId |
Request cache clear
Param | Type | Description |
---|---|---|
ids | ClayId,Array.<ClayId> | Ids to clear |
Parse list condition
Param | Type | Description |
---|---|---|
condition | ListCondition |
Parse list condition array
Param | Type | Description |
---|---|---|
conditionArray | Array.<ListCondition> |