@pcassima/odoo-jsonrpc
v1.0.3
Published
JSON-RPC client for Odoo, using Typescript
Downloads
29
Readme
ODOO JSON-RPC
Simple package to connect to a Odoo database and access its data over JSON-RPC.
The package has full Typescript support.
Installation
npm i @pcassima/odoo-jsonrpcUsage
Creating a new client
Assuming all credentials have been defined using environment variables, a client can be created as follows:
import { Client } from "@pcassima/odoo-jsonrpc";
const client = new Client({
host: process.env.ODOO_HOST,
db: process.env.ODOO_DB,
port: process.env.ODOO_PORT,
username: process.env.ODOO_USER,
password: process.env.ODOO_PASS,
});Searching for records
Search for records can easily be done with the search method:
const recordIds = await client.search(
'product.template',
[['sale_ok', '=', true]],
);Additionally a limit, offset and order can be passed in:
const recordIds = await client.search(
'product.template',
[['sale_ok', '=', true]],
80, // limit
40, // offset
"name DESC", // order
);Typing
Types have been created for the following objects:
- Odoo Domains
- Odoo
web_readspecification
These can easily be imported using the following code:
import { type OdooDomain, type OdooWebReadSpecification } from "@pcassima/odoo-jsonrpc";Astrojs
This package was intended to be used with Astrojs and specifically its object loaders.
An Odoo loader can easily be defined as follows
export function odooDataLoader(options: OdooDataLoaderOptions): Loader {
return {
name: "odoo-data-loader",
async load({ store, logger, parseData }) {
logger.info(`Fetching data from Odoo model: ${options.model}`);
try {
const client = new Client(options.config);
const recordIds = await client.search(
options.model,
options.domain,
options.limit,
options.offset,
options.order
);
const records = await client.webRead(options.model, recordIds, options.specification);
logger.info(`Fetched ${records.length} from Odoo.`);
(options.transform ? records.map(options.transform) : records).map((record: any) =>
parseData({ id: String(record.id), data: record })
.then((data) => {
store.set({ id: String(data.id), data });
})
.catch((error) => logger.info(error))
);
} catch (error) {
logger.error("Failed to fetch data from Odoo.");
if (error instanceof Error) {
logger.error(error.message);
}
}
},
};
}Where the OdooDataLoaderOptions type is defined as follows:
export type OdooDataLoaderOptions = {
config: Config;
model: string;
domain: OdooDomain;
specification: OdooWebReadSpecification;
limit?: number;
offset?: number;
order?: string;
transform?: (record: any) => any;
};The credentials can easily be provided using a .env file and used as follows:
export const odooConfig = {
host: import.meta.env.ODOO_HOST,
db: import.meta.env.ODOO_DB,
port: import.meta.env.ODOO_PORT,
username: import.meta.env.ODOO_USER,
password: import.meta.env.ODOO_PASS,
};