Model

The Model class has the following methods.

Each Model represents a table.
All methods accepts an optional callback, if not provided, a Promise would be returned.

find([columns] | {options},callback)

Returns all records from the table.

    //using an array with callback

myUsers.find(['age','name'],(err,result)=>{

 if(err) console.log(err)

 console.log(result) 

})

// with options, expecting a promise

 myUsers.find({limit:5, offset:10,getAttributes:['age','name'], orderBy:'name',order:'DESC'})
.then((result)=>console.log(res)).catch(err)

// in an async function

try{

// passing an empty array or object will return everything
// equivalent to ['*']

 const result = await myUsers.find([])

}catch(err){

console.log(err)

}
    
    

findOne({key:value}, callback)

Returns a single record based on the specified key-value

    // this will return a single record where age is 20

myUsers.findOne({age:20},(err,result)=>{

 console.log(result)

 })
    
    

findById([ids] | {options},callback)

Returns one or more records based on the specified array of ids,
Note:the values must be the values of the primaryKey at your Schema config.

    // using an array

myUsers.findById(['user1','user2'],(err,result)=>{

console.log(result)

})

// using an object

try{

 await myUsers.findById({id:['user1','user2'],getAttributes:['age','name']})

} catch{

}
    
    

findByIdAndRemove( [ids] ,callback)

Deletes one or more Records based on the specified values, Note: the values must be the values of your primaryKey.

    // this will removes users with id user3 and user4

myUsers.findByIdAndRemove(['user3','user4'],(err,result)=>{

console.log(result)

})
    
    

findAndRemove({key:[values] },callback)

Deletes one or more Records based on the specified key:values.

    // this will delete records with the following email addresses

myUsers.findAndRemove({email:['user3@mail.org','user4@mail.co']},(err,result)=>{

console.log(result)

})
    
    

findByValue({options}, callback)

Returns one or more records based on the specified values, wildcard is allowed

    myUsers.findByValue({searchAttribute:'name',searchValue:'pa*',getAttributes:['age','email']},(err,result)=>{

console.log(result)

})
    
    

findByConditions( {options}, callback)

Returns one or more records matching the specified conditions. Refer to harperDB docs for a list of available options.

    myUsers.findByConditions({limit:10,offset:20:operator:'and',
conditions:[{
search_attribute:'name',
search_type:'equals',
search_value:'jane'

}]

}, (err,result)=>{

console.log(result)

})
    
    

create({record}, callback)

Inserts a new record into the table.
Note:the data-types specified here must match those specified at Schema fields,otherwise an error would be thrown, you can turn this of by setting silent:true at Schema.

    myUsers.create({
name:'paul',age:34,user_id:'user4'},(err,result)=>{

console.log(result)

})
    
    

createMany( [records], callback)

Inserts one or more records into the table.
Note: The data-types of this method are not validated, unlike the create method.

    myUsers.createMany([{
name:'paul',age:34,user_id:'user4'},{
name:'jane',age:23,user_id:'user5'}],(err,result)=>{

console.log(result)

})
    
    

update([records], callback)

update one or more records.
Note: the records to be updated must include their primary key (id).

    myUsers.update([{name:'paul wilson',user_id:'user4',occupation:'banker'}],(err,result)=>{

console.log(result)

})
    
    

query( sqlQuery, callback)

This methods let's you write custom sql queries.

    myUsers.query('SELECT * FROM schema.table',(err,result)=>{

console.log(result)

})
    
    

describeModel( callback)

Returns information about the table.

    myUsers.describeModel((err,result)=>{

console.log(result)

})
    
    

importFromCsv( {options}, callback)

Load data into the table with a valid CSV

    myUsers.importFromCsv({csv:'a valid CSV string',(err,result)=>{

console.log(result)

})
    
    

importFromCsvFile( {options}, callback)

Load data into the table from a local CSV file.
Note:This does not work for harperdb cloud instance.

    myUsers.importFromCsvFile({filePath:'/path/to/local/file',(err,result)=>{

console.log(result)

})
    
    

importFromCsvUrl( {options}, callback)

Load data into the table from an external CSV file.

    myUsers.importFromCsvUrl({fileUrl:'http://example.com/file.csv',(err,result)=>{

console.log(result)

})
    
    

importFromS3( {options}, callback)

Load data into the table from amazon S3.

    myUsers.importFromS3({
awsAccessKeyId:'your aws access key id',
awsSecretAccessKey:'your aws secret access key',
bucket:'the name of your aws bucket where the file lives',
key:' a .csv or .json file'},(err,result)=>{

console.log(result)

})