# @4djs/client

> Retrieve the function location from it reference in NodeJS.

Latest version **0.3.6** (published 2025-05-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @4djs/client
pnpm add @4djs/client
yarn add @4djs/client
bun add @4djs/client
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.6 |
| Published | 2025-05-06 |
| First published | 2020-06-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8.0.0 |
| Dependencies | 5 |
| Unpacked size | 326.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | 4D SAS |
| Maintainers | idrissi, nasser_setti |
| Keywords | 4d, client, nodejs, node, promise |

## Links

- npm: https://www.npmjs.com/package/@4djs/client
- Repository: https://git-ps.wakanda.io/wakanda-ps/misc/4djs/client
- npm.io page: https://npm.io/package/@4djs/client

## Dependencies (5)

- [ajv](https://npm.io/package/ajv.md) ^6.12.4
- [glob](https://npm.io/package/glob.md) ^7.1.6
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [cookie](https://npm.io/package/cookie.md) ^0.4.1
- [form-data](https://npm.io/package/form-data.md) ^4.0.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.3.6 (latest) — 2025-05-06
- 0.3.5 — 2025-05-05
- 0.3.4 — 2025-04-17
- 0.3.3 — 2025-04-14
- 0.3.2 — 2023-11-07
- 0.3.1 — 2023-11-07
- 0.3.0 — 2023-11-07
- 0.2.9 — 2023-10-26
- 0.2.8 — 2023-10-26
- 0.2.7 — 2023-10-26
- 0.2.6 — 2023-04-12
- 0.2.5 — 2023-04-06
- 0.2.4 — 2021-02-18
- 0.2.3 — 2021-02-17
- 0.2.2 — 2021-02-07
- … 29 more at https://npm.io/package/@4djs/client/versions

## README

# @4djs/client

A NodeJS connector for 4D bases

## How to use

### Create a client

```javascript
// Import the Client contructor from the module
const { Client } = require('@4djs/client');

// Create a client instance with basic credentials
const c = new m.Client(
  'http://localhost/rest', {
    type: 'basic',
    user: 'admin',
    pass: 'admin'
  },
  { 
    ctx: { req, res },
  },
);

// Create a client instance with token credentials
const c = new m.Client(
  'http://localhost/rest', {
    type: 'token',
    token: 'admin',
    authUrl: '/your-api/login'
  },
  { 
    ctx: { req, res },
  },
);

// Get the catalog from client
const { ds } = await c.getCatalog();
```

### Querying a table

Suppose that we have a catalog containing a DataClass named `Employee`.

Running this Query:

```javascript
const collection = await ds.Employee.query('firstname == :1', 'Employee');
```

Looks for employees having `Employee` as a firstname.

### Requests chaining

The NodeJS Client allows request chaining. As an example, if you run this request:

```javascript
const array = await ds.Employee
  .query('firstname == :1', 'Employee')
  .orderBy('firstname asc')
  .skip(20).limit(5)
  .toArray('ID, firstname, lastname, company.name');
```

It will:

* Sort the result in ascending order by `firstname`
* Skip `20 entities`
* Limit the result to `5 entities`
* Converts the result to an array
* Selects the specified attributes

To optimize performance, the client will prepare the request in `the client side` (NodeJS), and sends only one request to 4D.

### Create entity

Creating an entity is as simple as:

```javascript
const entity = new ds.Employee({ firstname: 'Employee' });
await entity.save();
```

### execute 4D methods

To run a DataClass method:

```javascript
const result = await ds.Employee.dataclassMethod('param');
```

And this is how we can execute an `entity method`

```javascript
const entity = await ds.Employee.find('ID = :1', 10108, {
  $attributes: 'ID',
});
const result = await entity.getFullName();
```

In the example above:

* We retrieve an employee having the ID `10108` and we select only its `ID` attribute
* We execute the entity method named `getFullName`

### working with the related Entity/Entities

```javascript
const entity = await ds.Employee.find('ID = :1', 10108);
const employer = entity.employer;
await employer.refresh();
```
In the example above :

* We retrieve an employee having the ID `10108` and we select only its `ID` attribute
* We get the related entity/entities attribute (we get an instance of Employee / EmployeeCollection)
* We load the related entity/entities from database 

## Compiling the typescript code

```shell
npm i # Will install the NodeJS dependencies
npm run build # Will transpile the code into javascript code
```

Another option is to run this command

```shell
npm run dev
```

This command will watch the typescript changes, then it will transpile the code to javascript

---
_Source: https://npm.io/package/@4djs/client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
