Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
What is light service
Light service is a future-oriented cloud service platform produced by Bytedance. It provides cloud services such as database, page hosting and resource storage for developers, allowing developers to quickly develop web pages and small programs without building servers…
To get started
Enter the homepage of the light service official website, register and pass the real-name authentication, and create the first service on the homepage
Create the first service
- Click Create Service
- Enter a service name in the dialog box that is displayed
project
After a few moments we are in the control panel for the service we created
Cloud function
Cloud functions are the building blocks of light service applications and are where developers write the main business and functionality.
Quickly create a cloud function
- Go to the sidebar – Cloud Functions
- I’m gonna hit Create function
3. In the dialog box that is displayed, enter the cloud function name
myFunction
, click OK
Debug cloud functions
The light service provides the function of debugging functions online, so that developers can quickly view the results of functions during development.
After entering the debugging parameter Params, click start debugging to debug the function, and you can see the running result on the console after running
On-line cloud function
After writing the cloud function, you need to click online to use it in the real environment
After the cloud function is online, click the copy call address above to copy the call address to the clipboard
Calling the cloud function
axios.post('https://qcvrso.fn.thelarkcloud.com/myFunction', params)
Copy the code
Installing dependent libraries
In the lower left cornerNPM rely on
Click on the+
In the dialog box that is displayed, enter the NPM package to be installed, select the corresponding version number, and clickThe installation
Quick installation
Uninstalling dependent libraries
To uninstall installed packages, move the mouse pointer to the delete button on the right of the package in the lower right corner
The database
Create the first table quickly
Click on the sidebarData management
Enter the page and clickcreate
Enter the table name in the pop-up box
books
, click on thedetermine
- The table name must contain digits, letters, and underscores (_) and cannot start with underscores (_).
- The maximum number of service tables is 100.
Insert data
- Use Inspirecloud.db.table to get the data table
const table = inspirecloud.db.table('books');
Copy the code
- Add a record using the save method
const result = await tableable.save({name: 'The Little Prince'.author: 'Antoine DE Saint Exupery -'});
return result;
Copy the code
After clicking start debugging, you can see the results in the console
Re-enter the database and you can see
books
Add a new record to the table:
Query data
- Query data based on a field
Query fineOne that meets the conditions
const table = inspirecloud.db.table('books');
const result = await table.where({name: 'The Little Prince'}).findOne()
return result
Copy the code
Find Queries all data that meet the criteria
const table = inspirecloud.db.table('books');
const result = await table.where({name: 'The Little Prince'}).find()
return result
Copy the code
Click Start Debug to see the console output
If you want to query data using _id, run the where({_id: ObjectId(‘ XXXXX ‘)} command.
- Queries data based on a field value greater than (less than) a value
The value of the price field is greater than 100db.gt().
const book = await BooksTable.where({ price: db.gt(100) }).find();
Copy the code
Price field value < 100db.lt()
const book = await BooksTable.where({ price: db.lt(100) }).find();
Copy the code
Price field value greater than or equal to 100db.gte()
const book = await BooksTable.where({ price: db.gte(100) }).find();
Copy the code
Price field value less than or equal to 100db.lte()
const book = await BooksTable.where({ price: db.lte(100) }).find();
Copy the code
- Query data by field range
Price field value range: [100,200]
const book = await BooksTable.where({ price: db.gte(100).lte(200) }).find();
Copy the code
- Query data for multiple conditions
Select * from books where the name of the Book is Prince and the price range is between [100,200]
const book = await BooksTable.where({ name: 'The Little Prince'.price: db.gte(100).lte(200) }).find();
Copy the code
Change the data
Query the corresponding data first, use the save method to update to the database
// Change the name of the book
module.exports = async function(params, context) {
// Use inspirecloud.db.table to get the data table
const BookTable = inspirecloud.db.table('books');
// Use where to specify that the query condition is _id
let book = await BookTable.where({_id: ObjectId(params.id)}).findOne();
// If no record is found, initialize a record
if(! book){ book = BookTable.create({name: params.name, author: params.author});
}else {
book.name = params.name
}
await BookTable.save(book);
return {
result: book,
};
}
Copy the code
Delete the data
Query the corresponding data and delete the data from the database using the delete method
module.exports = async function(params, context) {
// Use inspirecloud.db.table to get the data table
const BookTable = inspirecloud.db.table('books');
// Use where to specify that the number of inventories is less than or equal to 100
const book = await BookTable.where({ name: 'The Little Prince' }).find();
// Use delete to delete these records
const result = await BookTable.delete(book);
return {
result,
};
}
Copy the code
There are several simple ways to manipulate data. For more details, see the official documentation of the Light Service
Page hosting
The light service provides page deployment capabilities that allow developers to quickly deploy pages in just a few steps and provide an accessible HTTPS domain name
Run the NPM Run build package in your Vue or React project and zip the generated dist folder into a. Zip file and upload it to the light service for quick deployment