smallapi-js
v1.0.0
Published
smallapi-js
Downloads
998
Maintainers
Readme
Smallapi-js
Smallapi client API wrapper
What is Smallapi-js?
Smallapi-js is a small wrapper wrote in javascript that allows smallapi users to uses their APIs cloud functions from the client side.
Table of contents
Prerequisites
This package requires NodeJS and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following commands:
node --version
# v21.5.0
npm --version
# 10.2.4
Installation
BEFORE YOU INSTALL: please read the prerequisites
Install the package using npm:
npm i -S smallapi-js
Usage
import { smallapi } from 'smallapi-js';
const api = await smallapi('https://my-api-url.com/', {
apiKey: 'my-secret-key',
});
const createdUser = await api.createUser({
firstName: 'John',
lastName: 'Doe',
email: `[email protected]`,
age: 32,
});
Example
A repository with complete examples can be found here: https://github.com/tutanck/small-demo.
Typescript support
This package export a type definition file so you can use it, out of the box, inside your Typescript project.
import { smallapi, Api, Config } from 'smallapi-js';
const myApiUrl: string = 'https://my-api-url.com/';
const myConfig: Config = {
apiKey: 'my-secret-key',
};
const api: Api = await smallapi(myApiUrl, myConfig);
const userInfos: object = {
firstName: 'John',
lastName: 'Doe',
email: `[email protected]`,
age: 32,
};
const createdUser: object = await api.createUser(userInfos);
API
Here is the list of cloud functions generated when you create a model called {Model} on the smallapi platform:
count{Model}Documents(query): number
Counts the number of documents matching the query parameter in the {{modelName}} collection.
Parameters:
query : Object • Indicates how to filter the documents in the collection {{modelName}}.
Returns:
Number • The number of documents matching the query parameter in the collection {{modelName}}
Examples:
const resultsCount = count{Model}Documents(query)
More:
create{Model}(docs): documents
Insert one document or an array of documents to the {{modelName}} collection.
Parameters:
docs : Array|Object • The documents to insert in the collection {{modelName}}.
Returns:
Document|Array<Document> • The list of documents inserted in the collection {{modelName}}.
Examples:
const results = create{Model}(docs)
More:
find{Model}ById(id, [projection], [options]): document
Finds a single document by its _id field in the {{modelName}} collection.
Parameters:
id : ObjectId • Required • value of _id field to query by {{modelName}} collection.
[projection] : Object|String|Array<String> • Optional • fields to return from the document found in the collection {{modelName}}.
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- limit
- skip
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
Returns:
Document • The document identified by its _id in the collection {{modelName}}.
Examples:
const result = find{Model}ById(id)
const result = find{Model}ById(id, { propertyA: 1, propertyB: -1 }, options)
const result = find{Model}ById(id, "propertyA -propertyB", options)
const result = find{Model}ById(id, ["propertyA", "propertyC"], options)
More:
Learn more about field selection
Learn more about the populate option
find{Model}ByQuery(query, [projection], [options]): documents
Find the documents matching the query parameter in the {{modelName}} collection.
Parameters:
query : Object • Indicates how to filter the documents in the collection {{modelName}}.
[projection] : Object|String|Array<String> • Optional • fields to return from the document found in the collection {{modelName}}.
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- limit
- skip
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
- useFindOne : true|false • If set to true the driver will use findOne instead of find • default to false.
Returns:
Array<Document> • The documents matching the query parameter in the collection {{modelName}}
Examples:
const result = find{Model}ByQuery(query)
const result = find{Model}ByQuery(query, { propertyA: 1, propertyB: -1 }, options)
const result = find{Model}ByQuery(query, "propertyA -propertyB", options)
const result = find{Model}ByQuery(query, ["propertyA", "propertyC"], options)
More:
Learn more about field selection
Learn more about the populate option
remove{Model}ById(id, [options]): document
Delete a single document by its _id field in the {{modelName}} collection.
Parameters:
id : ObjectId • Required • value of _id field to query by {{modelName}} collection.
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.- populate
Returns:
Document • The document identified by its _id in the collection {{modelName}}.
Examples:
const result = remove{Model}ById(id, options)
More:
Learn more about the populate option
remove{Model}ByQuery(query, [options]): documents
Delete all the documents matching the query parameter in the {{modelName}} collection.
Parameters:
query : Object • Indicates how to filter the documents in the collection {{modelName}}.
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- sort • Works only if useFindOne is set to true.
- populate • Object|String • Populate the specified path • Works only if useFindOne is set to true.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
- useFindOne : true|false • If set to true the driver will use findOneAndDelete instead of deleteMany • default to false.
Returns:
DeleteResult|Document • an object with the property deletedCount containing the number of documents deleted OR the document matching the query parameter in the collection {{modelName}} if useFindOne is set to true.
Examples:
const result = remove{Model}ByQuery(query, options)
More:
Learn more about the populate option
update{Model}ById(id, update, [options]): document
Updates a single document by its _id field in the {{modelName}} collection.
Parameters:
id : ObjectId • Required • value of _id field to query by {{modelName}} collection.
[update] : Object • Required • The update object to replace the one found while querying the collection {{modelName}}.
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- upsert : true|false • if true, and no documents found, insert a new document • default to false.
- new : true|false • if true, return the modified document rather than the original • default to true.
- runValidators : true|false • if true, validate the update operation against the model's definition first • default to true.
- select Object|String • sets the document fields to return.
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
Returns:
Document • The document identified by its _id in the collection {{modelName}}.
Examples:
const result = update{Model}ById(id, update, options)
More:
Learn more about field selection
Learn more about the populate option
update{Model}ByQuery(query, update, [options]): document
Updates all the documents matching the query parameter in the {{modelName}} collection.
Parameters:
query : Object • Required • value of _id field to query by {{modelName}} collection.
[update] : Object • Required • The update object to replace the one found while querying the collection {{modelName}}.
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- upsert : true|false • if true, and no documents found, insert a new document • default to false.
- new : true|false • if true, return the modified document rather than the original • default to false • Works only if useFindOne is set to true.
- runValidators : true|false • if true, validate the update operation against the model's definition first • default to false • Works only if useFindOne is set to true.
- fields Object|String • sets the document fields to return • Works only if useFindOne is set to true.
- sort• Works only if useFindOne is set to true.
- populate • Object|String • Populate the specified path • Works only if useFindOne is set to true.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
- useFindOne : true|false • If set to true the driver will use findOneAndDelete instead of deleteMany • default to false.
Returns:
UpdateResult|Document • an UpdateResult object OR the document matching the query parameter in the collection {{modelName}} if useFindOne is set to true.
Examples:
const result = update{Model}ByQuery(id, update, options)
More:
Learn more about field selection
Learn more about the populate option
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Add your changes:
git add .
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :sunglasses:
License
MIT License © Anagbla Joan (tutanck)
Contributors ✨
Thanks goes to these wonderful people for their contribution:
This project follows the all-contributors specification. Contributions of any kind welcome!