GithubHelp home page GithubHelp logo

odoo-await's Introduction

Odoo Await

Simple Odoo API client using async await. Features CRUD and extra methods. Odoo API docs: Odoo External API

Contributing

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 version

Node 11.16+

Installation

$ npm install odoo-await

Usage

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}`);

Methods

odoo.connect()

Must be called before other methods.

odoo.execute_kw(model,method,params)

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

CRUD

odoo.create(model, params)

Returns the ID of the created record.

const partnerId = await odoo.create('res.partner', {name: 'Kool Keith'})

many2many fields

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]})

odoo.read(model, recordId)

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]' } ];

odoo.update(model, recordId, params)

Returns true if successful

const updated = await odoo.update('res.partner', 54, {street: '334 Living Astro Blvd.'})
console.log(updated); // true

odoo.delete(model, recordId)

Returns true if successful.

const deleted = await odoo.delete('res.partner', 54);

Other Methods

odoo.search(model, domain)

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, ... ]

odoo.searchRead(model, domain, fields, opts)

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});

odoo.getFields(model, attributes)

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);

odoo.createOrderLine(orderId, productId, opts)

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'} );

Complete Example

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);
}

Testing

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 

License

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.

odoo-await's People

Contributors

vettloffah avatar debugsito avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.