# neogen

> Code generation tool for `neogma`

Latest version **1.0.26** (published 2024-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install neogen
pnpm add neogen
yarn add neogen
bun add neogen
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.26 |
| Published | 2024-02-03 |
| First published | 2023-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 11 |
| Unpacked size | 199.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | fulcanelly |
| Keywords | neo4j, ogm, codegen, orm, neogma, typescript, databases, cypher, generation |

## Links

- npm: https://www.npmjs.com/package/neogen
- Repository: https://github.com/fulcanelly/neogen
- npm.io page: https://npm.io/package/neogen

## Dependencies (11)

- [fs](https://npm.io/package/fs.md) ^0.0.1-security
- [ramda](https://npm.io/package/ramda.md) ^0.29.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [neogma](https://npm.io/package/neogma.md) ^1.12.3
- [ts-jest](https://npm.io/package/ts-jest.md) ^29.1.1
- [winston](https://npm.io/package/winston.md) ^3.11.0
- [typescript](https://npm.io/package/typescript.md) ^5.2.2
- [@types/jest](https://npm.io/package/@types/jest.md) ^29.5.8
- [@types/ramda](https://npm.io/package/@types/ramda.md) ^0.29.8
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.201
- [winston-console-format](https://npm.io/package/winston-console-format.md) ^1.0.8

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.26 (latest) — 2024-02-03
- 1.0.25 — 2023-12-07
- 1.0.24 — 2023-11-27
- 1.0.23 — 2023-11-14
- 1.0.22 — 2023-11-14
- 1.0.21 — 2023-11-12
- 1.0.19 — 2023-11-12
- 1.0.18 — 2023-11-12
- 1.0.17 — 2023-11-12
- 1.0.16 — 2023-11-12
- 1.0.15 — 2023-11-11
- 1.0.12 — 2023-11-11
- 1.0.11 — 2023-11-11
- 1.0.10 — 2023-11-11
- 1.0.9 — 2023-11-11
- … 9 more at https://npm.io/package/neogen/versions

## README

# neogen

![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/fulcanelly/neogen/test.yml)
![NPM](https://img.shields.io/npm/l/neogen)

# What is neogen?

neogen is a code generation tool for `neogma`, focused on automating and streamlining the development of Neo4j object-graph mappings (OGMs). It facilitates efficient model generation, relationship setup, and method creation, while ensuring type safety in Neo4j-based projects.

## What problems does it solve ?
 - Relations validation:
 - Efficiency - neogma introduces great type safety, hints but with it requires more editing: if one property changes - you would need to edit two declaration, neogen tries to solve this issue
 - Structure

## Motivation

Developed with inspiration from Ruby on Rails' `rails g`, neogen eliminates the repetitive and error-prone aspects of manual Neo4j OGM setup in `neogma`. Its primary goal is to enhance development efficiency and accuracy in managing Neo4j database schemas and relationships.


## How to use

#### Installing

```shell
yarn add neogen
```

#### Create config file

```shell
touch neogen.config.ts

```

### Update your connection

At this point assuming your project' structure to be like this
```
├── neogen.config.ts
├── package.json
└── src
    ├── index.ts
    ├── neo4j.ts
    └── models
```

Where `neo4j.ts` contains instance of `Neogma` connection:

It's required to add this lines so that neogen could find connection, since models depend on it's value

```typescript
import { neogen } from "neogen";
// ...
const neogma = new Neogma(...
// ...
neogen.setInstance(neogma)

```

#### Write desired schema:

Update `neogen.config.ts` file:

```typescript
import { neogen } from "neogen";

neogen.generateAll({ // settings
    generateBase: true,
    outputFolder: './src/models'
}, [{ // models
    label: 'Post',
    schema: {
        uuid: 'string',
        text: 'string'
    },
    primaryKeyField: 'uuid'
}, {
    label: 'User',
    schema: {
        name: 'string',
        uuid: 'string',
        online: 'boolean'
    },
    primaryKeyField: 'uuid'
}], { // relations
    POST_POSTED_BY: {
        User: 'posted',
        Post: 'of_user',
    },
    POST_LIKED_BY: {
        User: 'likes',
        Post: 'liked_by'
    },
})
```

#### Adjust your `package.json`

```json
"scripts": {
    "neogen": "ts-node neogen.config.ts",
```

And finally run

```shell
yarn run neogen
```

### Result

With this configuration it'll generate 6 files

```
├── neogen.config.ts
├── package.json
└── src
    └── models
        ├── __base.ts
        ├── __relations.ts
        ├── post.ts
        ├── post_.ts
        ├── user.ts
        └── user_.ts

```

- `post`, `user` - the ones containing all type and meta information
- `post_` and `user_` - contains static and instance methods of the models
- `__relations` - contains all relations defenitions, it's important to import it in main file
- `__base` - contains base of instance and statics methods if `generateBase` option is enabled

### Update your main file

It's important to import it in this order in your main/index file before using any models:

```ts
import './neo4j' // or whatever your file with neo4j connection is named
import './models/__relations'
```

## relations DSL explanation

Give this relationship declrartion:
```ts
const rel = {
  POST_POSTED_BY: {
        User: 'posted',
        Post: 'of_user',
    }
}
```
It would translate to

```chyper
(:User)-[:POST_POSTED_BY]->(:Post)
```

Or if you want node relation to itself you can make

```ts
const rel = {
  POST_POSTED_BY: {
    Comment: ['replied_to', 'replies']
  }
}
```

Which would result to

```chyper
(:Comment)-[:POST_POSTED_BY]->(:Comment)
```

Or you can use raw relation notation:
```js

neogen.generateAll({
    rawRelation: [
        {
            from: 'Session',
            to: 'Channel',
            direction: 'in',
            label: 'TEST',
            alias: 'test'
        }
        ...
```

## TODO
- Core
     - [ ] Validate relations
     - [x] Auto instance import
     - [ ] Relation getters
- Organizational
     - [ ] Eslint
     - [x] Use Github workflow for publishing npm
     - [x] Add tests

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