Simple Odoo API client using async await. Features CRUD and extra methods. Odoo API docs: Odoo External API
Happy to merge all useful features and bug fixes. Just start an 'issue' regarding the update, fork the repo, commit your changes, and submit a pull request.
Node 11.16+
$ npm install odoo-await
const Odoo = require('odoo-await');
const odoo = new Odoo({
baseUrl: 'http://localhost',
port: 8069,
db: 'odoo_db',
username: 'admin',
password: 'admin'
});
await odoo.connect();
const partnerId = await odoo.create('res.partner', {name: 'Kool Keith', email: '[email protected]'});
console.log(`Partner created with ID ${partnerId}`);
Must be called before other methods.
This method is wrapped inside the below methods. If below methods don't do what you need, you can use this method. Docs: https://www.odoo.com/documentation/13.0/webservices/odoo.html
Returns the ID of the created record.
const partnerId = await odoo.create('res.partner', {name: 'Kool Keith'})
Provide an array of record ID's for many2many fields
const partnerId = await odoo.create('res.partner', {name: 'Kool Keith', tag_ids: [1,3,5]})
Takes an array of record ID's and fetches the record data. Returns an array. Optionally, you can specify which fields to return. This is usually a good idea, since there tends to be a lot of fields on the base models (like over 100).
const records = await odoo.read('res.partner', [54, 1568], ['name', 'email']);
console.log(records);
// [ { name: 'Kool Keith', email: '[email protected] }, { name: 'Jack Dorsey', email: '[email protected]' } ];
Returns true if successful
const updated = await odoo.update('res.partner', 54, {street: '334 Living Astro Blvd.'})
console.log(updated); // true
Returns true if successful.
const deleted = await odoo.delete('res.partner', 54);
Searches and returns record ID's for all records that match the model and domain.
const recordIds = await odoo.search(`res.partner`, {country_id: 'United States'});
console.log(recordIds); // [14,26,33, ... ]
Searches for matching records and returns record data. Provide an array of field names if you only want certain fields returned.
const records = await odoo.searchRead(`res.partner`, {country_id: 'United States'}, ['name', 'city'], {limit: 5, offset: 10});
console.log(records); // [ {id: 5, name: 'Kool Keith', city: 'Los Angeles' }, ... ]
If you just want to do a basic search_read you can omit the extra args:
const records = await odoo.searchRead(`res.partner`, {country_id: 'United States'});
Empty args can be used if you need some type of filtering but not others:
const records = await odoo.searchRead(`res.partner`, {}, [], {limit: 10, offset: 20});
Returns detailed list of fields for a model, filtered by attributes. i.e. if you only want to know if fields are required you could call:
const fields = await odoo.getFields('res.partner', ['required']);
console.log(fields);
const orderId = await odoo.create('sale.order', {partner_id: 54});
await odoo.createOrderLine(orderId, 47, { qty: 2, price: 45.55, name: 'Dehydrated space food capsule'} );
This example creates a partner (customer), creates an order (quote) for the customer, and finally ads a line item to that order. You might use this if integrating with an ecomm platform.
const Odoo = require('odoo-await');
const odoo = new Odoo({
baseUrl: 'http://localhost',
port: 8069,
db: 'odoo_db',
username: 'admin',
password: 'admin'
});
try {
await odoo.connect();
const partnerId = await odoo.create('res.partner', {name: 'Kool Keith', email: '[email protected]'});
const orderId = await odoo.create('sale.order', {partner_id: partnerId});
await odoo.createOrderLine(orderId, 47, { qty: 2, price: 45.55, name: 'Dehydrated space food capsule'} );
} catch(e) {
console.log(e);
}
The default test will run through basic CRUD functions, creating a res.partner
record, updating it, reading it, and deleting it. Uses Mocha and Should as dependencies.
If you are using default db name "odoo_db"
, username "admin"
, password "admin"
, and port 8069
on "http://localhost"
:
$ npm test
If you aren't using the defaults, pass the variables in command line with environment variables:
$ ODOO_DB=mydatabase ODOO_USER=myusername ODOO_PW=mypassword ODOO_PORT=8080 ODOO_BASE_URL=https://myodoo.com npm test
ISC
Copyright 2020 Charlie Wettlaufer
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.