Scandit.Db.BaseModel

Base class for all database objects

Constructor

new BaseModel(data?: object)

Creates a new instance of a BaseModel

Parameters:
Name Type Attributes Default Description
data object <optional>
{}

Object data

Examples
Create new product and save it to Cloud DB. Assuming that Product was set up before on the web dashboard.
  • JavaScript
  • ES6
var product = new Scandit.Db.Product({name: 'New product', price: 1.23});
product.save()
 .then(function(product) {
    alert('New product has been created. Id: ' + product.id);
 })
 .catch(function(reason) {
    alert('Product could not be created because of: ' + reason);
 });
let product = new Scandit.Db.Product({name: 'New product', price: 1.23});
product.save()
 .then(product => {
    alert(`New product has been created. Id: ${product.id}`);
 })
 .catch(reason => {
    alert(`Product could not be created because of: ${reason}`);
 });

Methods

all(options?: object): Promise<Scandit.Db.BaseModel[]>

Finds all objects

Parameters:
Name Type Attributes Default Description
options object <optional>
{}
Properties
Name Type Attributes Default Description
limit integer <optional>
1000

Max number of objects to return (max: 5000).

skip integer <optional>
0

Number of objects to skip.

Returns:
Promise<Scandit.Db.BaseModel[]>

Promise which is resolved to the array of model instances.

Can return an error with code:

Examples
Assuming that the Product class was set up on the web dashboard.
  • JavaScript
  • ES6
Scandit.Db.Product.all().then(function(products) {
  products.forEach(function(product) {
    console.log(product.name);
  });
});
Scandit.Db.Product.all().then(products => {
  products.forEach(product => {
    console.log(product.name);
  });
});

count(query: Scandit.Db.Query): Promise

Performs a database count query

Parameters:
Name Type Description
query Scandit.Db.Query

database query

Returns:
Promise

Promise which is resolved to the result of the count.

Can return an error with code:

Examples
Assuming that the Product class was set up on the web dashboard.
  • JavaScript
  • ES6
Scandit.Db.Product.count().then(function(productCount) {
  console.log('There were ' + productCount + ' products found');
});
Scandit.Db.Product.count().then(productCount => {
  console.log(`There were ${productCount} products found`);
});

delete(): Promise

Deletes the object permanently

Returns:
Promise

Promise which succeeds or fails if the object doesn't exist.

Can return an error with code:

Examples
Search for specific products and delete them. Assuming that the Product class was previously set up on the web dashboard and there is an index for the name field.
  • JavaScript
  • ES6
Scandit.Db.Product.find({name: {$eq: 'unwanted object'}}).then(function(products) {
  products.forEach(function(product) {
    product.delete().then(function() {
      console.log('Product ' + product.id + ' was deleted');
    });
  });
});
Scandit.Db.Product.find({name: {$eq: 'unwanted object'}}).then(products => {
  products.forEach(product => {
    product.delete().then(() => {
      console.log(`Product ${product.id} was deleted`);
    });
  });
});

find(query: Scandit.Db.Query, options?: object): Promise<Scandit.Db.BaseModel[]>

Performs a database query

Parameters:
Name Type Attributes Default Description
query Scandit.Db.Query

database query

options object <optional>
{}
Properties
Name Type Attributes Default Description
limit integer <optional>
1000

Max number of objects to return.

skip integer <optional>
0

Number of objects to skip.

Returns:
Promise<Scandit.Db.BaseModel[]>

Promise which is resolved to the array of model instances.

Can return an error with code:

Examples
Assuming that you have previously set up Product on the web dashboard and that you have an index on the name field (an index can be added on the web dashboard as well).
  • JavaScript
  • ES6
 Scandit.Db.Product.find({name: {$eq: 'New product'}})
 .then(function(products) {
    alert('Found ' + products.length + ' products');
    products.forEach(function(product) {
      console.log(product.name);
    });
 })
 .catch(function(reason) {
    alert('Products query failed: ' + reason);
 });
 Scandit.Db.Product.find({name: {$eq: 'New product'}})
 .then(products => {
    alert(`Found ${products.length} products`);
    products.forEach(product => {
      console.log(product.name);
    });
 })
 .catch(function(reason) {
    alert(`Products query failed: ${reason}`);
 });

get(id: string): Promise<Scandit.Db.BaseModel>

Performs a database query and returns a single object. If there are more than one objects matching, it returns the first one.

Every object has a property _id which can be passed.

Parameters:
Name Type Description
id string

Object id

Returns:
Promise<Scandit.Db.BaseModel>

Promise which is resolved to the model instance or fails if it doesn't exist.

Can return an error with code:

Examples
Assuming that the Product class was set up on the web dashboard, has the field name and that productId was previously defined and corresponds to a database object id.
  • JavaScript
  • ES6
Scandit.Db.get(productId).then(function(product) {
  console.log(product.name);
});
Scandit.Db.get(productId).then(product => {
  console.log(product.name);
});

save(): Promise

Performs save on the object, updates if it exists, creates it otherwise

Returns:
Promise

Promise which is resolved to the model instance.

Can return an error with code:

Examples
Create new product and save it to Cloud DB. Assuming that Product was set up before on the web dashboard.
  • JavaScript
  • ES6
var product = new Scandit.Db.Product({name: 'New product', price: 1.23});
product.save()
 .then(function(product) {
    alert('New product has been created. Id: ' + product.id);
 })
 .catch(function(reason) {
    alert('Product could not be created because of: ' + reason);
 });
let product = new Scandit.Db.Product({name: 'New product', price: 1.23});
product.save()
 .then(product => {
    alert(`New product has been created. Id: ${product.id}`);
 })
 .catch(reason => {
    alert(`Product could not be created because of: ${reason}`);
 });

setAttachment(name: string, reference: string, mimeType: string): Promise

Adds an attachment to the object

Parameters:
Name Type Description
name string

Name of the attachment (if an attachment with the given name already exists, it will be overwritten)

reference string

Url to the local file

mimeType string

Mime type of the file

Returns:
Promise

Promise which succeeds or fails if the object doesn't exist.

Can return an error with code:

Examples
Assuming that product and path are set.
  • JavaScript
  • ES6
product.setAttachment('attachment01', path, 'image/jpeg').then(function() {
  console.log('Attachment set');
});
product.setAttachment('attachment01', path, 'image/jpeg').then(() => {
  console.log('Attachment set');
});