Skip to content

Commit

Permalink
Merge pull request #4 from AbyssAlora/dev
Browse files Browse the repository at this point in the history
feat: utility methods in arango repository
  • Loading branch information
AbyssAlora committed Jun 2, 2023
2 parents 55c97e6 + 27513ea commit 65f40c4
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 70 deletions.
31 changes: 31 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,31 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'prettier/prettier': [
'error',
{
endOfLine: 'auto',
},
],
},
};
4 changes: 4 additions & 0 deletions .prettierrc
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
49 changes: 22 additions & 27 deletions README.md
Expand Up @@ -73,7 +73,6 @@ import arangoConfig from 'arango.config';
export class AppModule { }
```


### Document entity definition

In ArangoDB, every record is a document stored in a collection. A collection can be defined as a collection of vertices or edges. We can define a document entity as shown below:
Expand All @@ -94,7 +93,6 @@ export class UserEntity extends ArangoDocument {
The `@Collection()` decorator defines the name of the collection containing documents like this one. **In ArangoDB, collection names are case-sensitive.**
Notice that `UserEntity` inherits from `ArangoDocument`. This type contains the standard metadata that ArangoDB uses (`_id`, `_key`, `_rev`). For edges, we can use the `ArangoDocumentEdge` type, which additionally includes edge metadata (`_from`, `to`). For more information about ArangoDB documents, refer to the official [ArangoDB Documentation](https://www.arangodb.com/docs/stable/data-modeling-documents-document-address.html).


### Arango Repository

`ArangoRepository` is a generic wrapper for [ArangoJS](https://www.npmjs.com/package/arangojs) methods that aims to simplify the usage of its CRUD methods, while adding some extra methods to fit our basic needs. To be able to inject the repository into our application, we need to extend `AppModule` from the earlier example with `ArangoModule.forFeature([...])` and register all the entities bound to document collections (see the example below).
Expand Down Expand Up @@ -128,7 +126,7 @@ import { UserEntity } from './entities/user.entity';
export class AppService {
constructor(
@InjectRepository(UserEntity)
private readonly userRepository: ArangoRepository<UserEntity>,
private readonly userRepository: ArangoRepository<UserEntity>
) {}
}
```
Expand All @@ -146,13 +144,13 @@ import { ArangoManager, InjectManager } from 'nest-arango';
@Injectable()
export class AppService {
constructor(
@InjectManager()
@InjectManager()
private databaseManager: ArangoManager;
) {}
}
```

With `ArangoManager` injected, you can directly access the ArangoJS `Database` object and begin transactions.
With `ArangoManager` injected, you can directly access the ArangoJS `Database` object and begin transactions.

### Transactions

Expand All @@ -164,7 +162,7 @@ There are two ways to work with transactions. The first is one is to begin the t
@Injectable()
export class AppService {
constructor(
@InjectManager()
@InjectManager()
private databaseManager: ArangoManager,
@InjectRepository(UserEntity)
private readonly userRepository: ArangoRepository<UserEntity>,
Expand All @@ -180,12 +178,12 @@ export class AppService {
try {
// edge collection => [User -> Knows -> User]
await this.knowsRepository.save(
{
_from: `Users/${user1._key}`,
_to: `Users/${user2._key}`
{
_from: user1._id,
_to: user2._id_
},
{
transaction: trx
{
transaction: trx
}
);
await trx.commit();
Expand Down Expand Up @@ -226,29 +224,28 @@ Currently available listeners:
1. `@BeforeSave()` - executes method before `save` and `saveAll`
2. `@BeforeUpdate()` - executes method before `update` and `updateAll`


### CLI migration tool

The `nest-arango` package also provides an experimental CLI tool to manage database migrations. We can directly use the `cli.js` provided within the package, but first we need to define a configuration file with the name `nest-arango.json` in your root folder. Here is an example:

```json
{
"database": {
"url": "http://localhost:8529",
"databaseName": "env:ARANGO__DATABASE",
"auth": {
"username": "env:ARANGO__USERNAME",
"password": "env:ARANGO__PASSWORD"
},
"agentOptions": {
"rejectUnauthorized": "env:ARANGO__REJECT_UNAUTHORIZED_CERT:boolean"
}
"database": {
"url": "http://localhost:8529",
"databaseName": "env:ARANGO__DATABASE",
"auth": {
"username": "env:ARANGO__USERNAME",
"password": "env:ARANGO__PASSWORD"
},
"migrationsCollection": "Migrations",
"cli": {
"migrationsDir": "migrations"
"agentOptions": {
"rejectUnauthorized": "env:ARANGO__REJECT_UNAUTHORIZED_CERT:boolean"
}
},
"migrationsCollection": "Migrations",
"cli": {
"migrationsDir": "migrations"
}
}
```

- The `database` field has the same structure as the database configuration in `ArangoModule`. We can pass values as plain text, or we can provide a reference to an environment variable from our `.env` file. Optionally, we can also specify a type for the environment variable (see `rejectUnauthorized` in the example above). Currently, we can specify these types: `boolean` | `number` | `string`. By default, all variables are parsed as strings.
Expand All @@ -265,7 +262,6 @@ branko@buzniç:~$ node /path/to/cli.js --revert
2. `--run` is used to run all the unapplied migrations from `migrationDir`
3. `--revert` reverts the last successfuly processed migration


Below is an example output of the **`--create`** migration command.

```typescript
Expand Down Expand Up @@ -297,4 +293,3 @@ export class Migration1679387529350 implements Migration {
}
}
```

2 changes: 1 addition & 1 deletion configs/tsconfig.cjs.json
Expand Up @@ -8,4 +8,4 @@
"outDir": "../lib/cjs",
"declarationDir": "../lib/cjs/types"
}
}
}
4 changes: 2 additions & 2 deletions configs/tsconfig.cli.json
Expand Up @@ -14,8 +14,8 @@
"declaration": true,
"declarationMap": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"experimentalDecorators": true
},
"exclude": ["**/*"],
"include": ["../src/cli/*"]
}
}
2 changes: 1 addition & 1 deletion configs/tsconfig.esm.json
Expand Up @@ -8,4 +8,4 @@
"outDir": "../lib/esm",
"declarationDir": "../lib/esm/types"
}
}
}
12 changes: 10 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "nest-arango",
"version": "0.1.3",
"version": "0.1.4",
"description": "ArangoDB driver module for NestJS with a built-in CLI tool for creating and running migration scripts",
"types": "./lib/cjs/types/index.d.ts",
"main": "./lib/cjs/index.js",
Expand Down Expand Up @@ -28,7 +28,15 @@
}
}
},
"keywords": ["nestjs", "arangodb", "arangojs", "repository", "migrations", "cli", "documents"],
"keywords": [
"nestjs",
"arangodb",
"arangojs",
"repository",
"migrations",
"cli",
"documents"
],
"author": "The Nest-Arango Team",
"repository": {
"type": "git",
Expand Down
5 changes: 3 additions & 2 deletions src/cli/commands/run.command.ts
Expand Up @@ -31,8 +31,9 @@ export class RunCommand {
processedMigrations.push(migration.name);
}

const migrationFiles = readdirSync(migrationsDir)
.sort((a,b) => a.localeCompare(b));
const migrationFiles = readdirSync(migrationsDir).sort((a, b) =>
a.localeCompare(b),
);
for (const migrationFile of migrationFiles) {
try {
const migrationFilePath = join(migrationsDir, migrationFile);
Expand Down
2 changes: 1 addition & 1 deletion src/common/index.ts
@@ -1,3 +1,3 @@
export * from './arango.util';
export * from './constructable.interface';
export * from './deep-partial.type';
export * from './deep-partial.type';
1 change: 0 additions & 1 deletion src/index.ts
Expand Up @@ -9,4 +9,3 @@ export * from './manager';
export * from './repository';
export * from './cli/interfaces/migration.interface';
export * from './arango.module';

2 changes: 1 addition & 1 deletion src/interfaces/index.ts
@@ -1,4 +1,4 @@
export * from './arango-options.interface';
export * from './entity-factory.interface';
export * from './repository-options.types';
export * from './repository-parameters.types';
export * from './repository-parameters.types';
2 changes: 2 additions & 0 deletions src/interfaces/repository-options.types.ts
Expand Up @@ -13,6 +13,8 @@ interface ReturnOldOptions {
returnOld?: boolean;
}

export type GetDocumentCountByOptions = TransactionOptions;

export type FindOneOptions = TransactionOptions;

export type FindOneByOptions = TransactionOptions;
Expand Down

0 comments on commit 65f40c4

Please sign in to comment.