A Simple Node.JS Example Program for the MySQL Document Store
I have chatting with others who want to use the new X Devapi Document Store features but have not been able to find simple example programs. Finding the bridge between 'Hello World' and something useful can often be discouraging.
Example Code
Here is a very short Node.JS Docstore example program that accesses the 'countryinfo' collection. More on why Node.JS and what software you need below. But for now regard this code to get one specific record from the database.var mysqlx = require('@mysql/xdevapi'); mysqlx.getSession({ //Auth to server host: 'localhost', port: '33060', dbUser: 'root', dbPassword: 'password' }).then(function (session) { // use world_x.country.info var schema = session.getSchema('world_x'); var collection = schema.getCollection('countryinfo'); collection // Get row for 'CAN' .find("$._id == 'CAN'") .limit(1) .execute(doc => console.log(doc)) .then(() => console.log("\n\nAll done")); session.close(); })
Code Explained
On the first line the X Devapi library is loaded. Line three is authentication to the server followed by getting to the world_x/countryinfo schema. The we find the record for Canada. Is this simpler than "SELECT * FROM countryinfo WHERE _ID = 'CAN'"? Maybe, but I also see where this approach has some facets that need exploiting, er, exploring.Results
So what does the code do?:~/xdevn$ node demo.js { GNP: 598862, _id: 'CAN', Name: 'Canada', IndepYear: 1867, geography: { Region: 'North America', Continent: 'North America', SurfaceArea: 9970610 }, government: { HeadOfState: 'Elisabeth II', GovernmentForm: 'Constitutional Monarchy, Federation' }, demographics: { Population: 31147000, LifeExpectancy: 79.4000015258789 } } All done
Nhận xét
Đăng nhận xét