Node JS

Brightlever has a valuable Nodejs module that can be used to read and manage Brightlever data.

import Client, { Timestamp } from '@brightlever/client';

const client= new Client({
    environment_id:"YOUR_ENVIRONMENT_ID",
    access_token:"YOUR_ACCESS_TOKEN"
})

const response=await client
    .count()
    .sum('donation')
    .avg('age')
    .min('age')
    .max('age')
    .from('widget')
    .where('name','==','test')
    .orderBy('name','asc')
    .pageSize(100)
    .page(1)
    .get();

response.items.forEach(...)

const widget=await client.with('widget').id('asd').get();
const widget=await client.with('widget').id('asd').delete();
const widget=await client.with('widget').id('asd').rollback();
const widget=await client.with('widget').save(data);
const widget=await client.with('widget').publish(data);
const widget=await client.with('widget').validate(data);

const widgetStorage=client.with('widget').getStorage();

const newAssetMetadata=await widgetStorage.upload({
    file:file,
    overwrite:false
})

const assetMetadata=await await widgetStorage.metadata(newAssetMetadata.id)

const duplicatesMetadata=await await widgetStorage.duplicate(newAssetMetadata.id)

await await widgetStorage.delete(duplicatesMetadata.id)

When using the select method to explicitly pull specific document attributes you do not need to use the populate method. It automatically assumes that you want any selected relationships populated.

  const response=await client
    .select('name,address')
    .from('widget')
    .where('name','==','test')
    .orderBy('name','asc')
    .pageSize(100)
    .page(1)
    .get();

Otherwise you need to specifiy that you want any document relationships poplulated.

   const response=await client
    .from('widget')
    .where('name','==','test')
    .populate('address')
    .orderBy('name','asc')
    .pageSize(100)
    .page(1)
    .get();

Dates in Brightlever are store as objects with _seconds and _nanoseconds.

{
    _seconds:number
    _nanoseconds:number
}

So the node module supplies a Timestamp object that can be use to both serialize and deserialize native JS Date instances.

import Client, { Timestamp } from '@brightlever/client';

const blDateNow=Timestamp.fromDate(new Date());

const jsDateNow=Timestamp.toDate(blDateNow);