Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10 implement feed #14

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a083481
feat(feed drop): add command
Saverio976 Jul 10, 2023
66cc2c5
docs(utils opendDB+doesDBExists): add docs
Saverio976 Jul 11, 2023
4a0ac35
feat(package.json): add script lint-fix
Saverio976 Jul 12, 2023
baf02aa
feat(lint): run eslint fix
Saverio976 Jul 12, 2023
a5ac40c
Merge remote-tracking branch 'origin/10-implement-feed-drop' into 10-…
YlanGarnier Jul 17, 2023
87d4f06
feat(feed add): command added
YlanGarnier Jul 18, 2023
5b0fa77
feat(feed add): fixed database retrieving
YlanGarnier Jul 19, 2023
1579f83
feat(feed del): added command to delete entries from a feed database
YlanGarnier Jul 19, 2023
5cc9556
feat(orbitdb): start options to start node if not offline
Saverio976 Jul 19, 2023
5f4ea78
feat(utils): use option type (too many params look so bad)
Saverio976 Jul 19, 2023
eab6b01
refactor(lint): run yarn lint-fix
Saverio976 Jul 19, 2023
f658da9
refactor(lint): use prettier
Saverio976 Jul 19, 2023
13be35f
refactor(commands feed): change name of class to Feed{Command}
Saverio976 Jul 21, 2023
81f73b3
feat(orbitdb): add func to save the db; and open will now try to rest…
Saverio976 Jul 21, 2023
3754ff0
feat(feed info): add command to get informations about a feed database
YlanGarnier Jul 22, 2023
1ddd266
feat(feed add): feed now stores strings instead of files
YlanGarnier Jul 22, 2023
c89a88e
feat(feed info): command to list the entries of a feed database
YlanGarnier Jul 22, 2023
298aaf0
Merge branch '10-implement-feed-info' into 10-implement-feed
YlanGarnier Jul 22, 2023
68a3724
feat(feed): fixed not working asynchronous tasks
YlanGarnier Jul 22, 2023
ff5fbb3
fix(feed): run yarn lint-fix
Jul 25, 2023
df56838
feat(docs): usage feed
Saverio976 Aug 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

6 changes: 0 additions & 6 deletions .eslintrc

This file was deleted.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@
"@types/mocha": "^9.0.0",
"@types/node": "^16.18.29",
"chai": "^4",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4",
"eslint-config-oclif-typescript": "^1.0.3",
"mocha": "^9",
"oclif": "^3",
"prettier": "^3.0.0",
"shx": "^0.3.3",
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
Expand All @@ -63,7 +61,8 @@
},
"scripts": {
"build": "shx rm -rf dist && tsc -b",
"lint": "eslint . --ext .ts --config .eslintrc",
"lint": "prettier --check src",
"lint-fix": "prettier --write src",
"postpack": "shx rm -f oclif.manifest.json",
"posttest": "yarn lint",
"prepack": "yarn build && oclif manifest && oclif readme",
Expand Down
57 changes: 57 additions & 0 deletions src/commands/feed/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Command, Flags } from "@oclif/core";
import { startOrbitDB } from "../../services/start-OrbitDB";
import { stopOrbitDB } from "../../services/stop-OrbitDB";
import { doesDBExists } from "../../utils/does-DBExists";
import { openDB } from "../../utils/open-DB";
import { resolveDBIdByName } from "../../utils/resolve-DBIdByName";
import { saveDB } from "../../utils/save-DB";

export default class FeedAdd extends Command {
static description = "Add a file to a feed type database";

static examples: Command.Example[] = [
"<%= config.bin %> <%= command.id %> --name=myFeedDbName --file=myFile",
];

static flags = {
// flag with a value (-n VALUE, --name=VALUE)
dbName: Flags.string({
char: "n",
description: "name of the database",
required: true,
}),
// flag with a value (-n VALUE, --name=VALUE)
data: Flags.string({
char: "f",
description: "file to add into db",
required: true,
multiple: true,
}),
};

public async run(): Promise<void> {
const { flags } = await this.parse(FeedAdd);
const orbitdb = await startOrbitDB(true);
const dbAdress = await resolveDBIdByName(orbitdb, flags.dbName, "feed");
const DBExists = await doesDBExists(orbitdb, dbAdress);

if (!DBExists) {
this.error(
`database '${flags.dbName}' (or '${dbAdress}') does not exist`,
);
}

const db = await openDB(orbitdb, dbAdress, "feed");
flags.data.forEach(async (data) => {
try {
const hash = await db.add(data);
this.log(`added data: '${data}' to feed '${flags.dbName}' database : ${hash}`);
} catch (error) {
this.log(`Error occured while adding entry: ${error}`);
}
});
await saveDB(db);
await db.close();
await stopOrbitDB(orbitdb);
}
}
38 changes: 23 additions & 15 deletions src/commands/feed/create.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import { startOrbitDB } from '../../services/start-OrbitDB';
import { stopOrbitDB } from '../../services/stop-OrbitDB';
import { createDB } from '../../utils/create-DB';
import { Command, Flags } from '@oclif/core';
import { Command, Flags } from "@oclif/core";
import { startOrbitDB } from "../../services/start-OrbitDB";
import { stopOrbitDB } from "../../services/stop-OrbitDB";
import { createDB } from "../../utils/create-DB";


export default class Create extends Command {
static description = 'Create a feed type database'
export default class FeedCreate extends Command {
static description = "Create a feed type database";

static examples = [
'<%= config.bin %> <%= command.id %> --name=myFeedDbLOL',
'<%= config.bin %> <%= command.id %> --name=myFeedDbLOL --force',
]
"<%= config.bin %> <%= command.id %> --name=myFeedDbLOL",
"<%= config.bin %> <%= command.id %> --name=myFeedDbLOL --force",
];

static flags = {
// flag with a value (-n VALUE, --name=VALUE)
name: Flags.string({ char: 'n', description: 'name of the database', required: true }),
name: Flags.string({
char: "n",
description: "name of the database",
required: true,
}),
// flag with no value (-f, --force)
force: Flags.boolean({ char: 'f', description: 'force overwrite if DB already exists' }),
}
force: Flags.boolean({
char: "f",
description: "force overwrite if DB already exists",
}),
};

public async run(): Promise<void> {
const { flags } = await this.parse(Create);
const { flags } = await this.parse(FeedCreate);
const orbitdb = await startOrbitDB(true);

this.log(`creating database name: ${flags.name} ...`);
const db = await createDB(orbitdb, flags.name, 'feed', flags.force);
const db = await createDB(orbitdb, flags.name, "feed", {
overwrite: flags.force,
});
this.log(`created database: ${db.address}`);
await stopOrbitDB(orbitdb);
}
Expand Down
55 changes: 55 additions & 0 deletions src/commands/feed/del.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Command, Flags } from "@oclif/core";
import { startOrbitDB } from "../../services/start-OrbitDB";
import { stopOrbitDB } from "../../services/stop-OrbitDB";
import { doesDBExists } from "../../utils/does-DBExists";
import { openDB } from "../../utils/open-DB";
import { resolveDBIdByName } from "../../utils/resolve-DBIdByName";
import { saveDB } from "../../utils/save-DB";

export default class FeedDel extends Command {
static description = "Delete a file to a feed type database";

static examples: Command.Example[] = [
"<%= config.bin %> <%= command.id %> --name=myFeedDbName --file=myFile",
];

static flags = {
// flag with a value (-n VALUE, --name=VALUE)
dbName: Flags.string({
char: "n",
description: "name of the database",
required: true,
}),
// flag with a value (-n VALUE, --name=VALUE)
entries: Flags.integer({
char: "e",
description: "sequence number of the entry you want to delete",
required: true,
multiple: true,
}),
};

public async run(): Promise<void> {
const { flags } = await this.parse(FeedDel);
const orbitdb = await startOrbitDB(true);
const dbAdress = await resolveDBIdByName(orbitdb, flags.dbName, "feed");
const DBExists = await doesDBExists(orbitdb, dbAdress);

if (!DBExists) {
this.error(
`database '${flags.dbName}' (or '${dbAdress}') does not exist`,
);
}

const db = await openDB(orbitdb, dbAdress, "feed");
flags.entries.forEach(async (entry) => {
await db.del(entry);
this.log(
`deleted entry number ${entry} from feed '${flags.dbName}' database`,
);
});
await saveDB(db);
await db.close();
await stopOrbitDB(orbitdb);
}
}
73 changes: 73 additions & 0 deletions src/commands/feed/drop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Command, Flags, ux } from "@oclif/core";
import { rm } from "node:fs/promises";
import * as path from "node:path";
import { startOrbitDB } from "../..//services/start-OrbitDB";
import { stopOrbitDB } from "../../services/stop-OrbitDB";
import { doesDBExists } from "../../utils/does-DBExists";
import { openDB } from "../../utils/open-DB";
import { resolveDBIdByName } from "../../utils/resolve-DBIdByName";

export default class FeedDrop extends Command {
static description =
"Delete a feed type database locally (This doesn't remove data on other nodes that have the removed database replicated.)";

static examples = [
"<%= config.bin %> <%= command.id %> --name=myFeedDbLOL",
"<%= config.bin %> <%= command.id %> --name=myFeedDbLOL -y",
];

static flags = {
// flag with a value (-n VALUE, --name=VALUE)
name: Flags.string({
char: "n",
description: "name of the database",
required: true,
}),
// flag with no value (-y, --yes)
yes: Flags.boolean({
char: "y",
description:
"confirm drop (without this option you will be asked to confirm)",
}),
};

public async run(): Promise<void> {
const { flags } = await this.parse(FeedDrop);
const orbitdb = await startOrbitDB(true);
const name = await resolveDBIdByName(orbitdb, flags.name, "feed");
const DBExists = await doesDBExists(orbitdb, name);

if (!DBExists) {
this.error(`database '${flags.name}' (or '${name}') does not exist`);
}

const db = await openDB(orbitdb, name, "feed");

if (!flags.yes) {
const confirm = await ux.prompt(
`Are you sure you want to drop the database '${name}' (y/n)?`,
);
if (confirm !== "y") {
this.log('aborting (response was not "y")');
await stopOrbitDB(orbitdb);
return;
}
}

// this was in https://github.com/orbitdb/orbit-db-cli/blob/f70880de90e8d1005a936bea93736ef762f25321/src/commands/drop.js#L35C5-L36C1
const dbCachePath = path.join(
"./",
db._cache.path +
"/" +
db.address.toString().replace("/orbitdb/", "") +
".orbitdb",
);

this.log(`droping database name: ${name} ...`);
await db.drop();
await db.close();
await rm(dbCachePath, { recursive: true, force: true });
this.log(`droped database: ${name}`);
await stopOrbitDB(orbitdb);
}
}
21 changes: 6 additions & 15 deletions src/commands/feed/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { Command } from '@oclif/core'
import { Command } from "@oclif/core";

export default class Feed extends Command {
static description = 'feed related commands'
static description = "feed related commands (see examples below)";

static examples = [
'$ orbitdb feed --help',
]
static examples = ["<%= config.bin %> <%= command.id %> --help"];

static flags = {
// flag with a value (-n, --name=VALUE)
// name: Flags.string({char: 'n', description: 'name to print'}),
// flag with no value (-f, --force)
// force: Flags.boolean({char: 'f'}),
}
static flags = {};

static args = {
// databaseName: Args.string({description: 'name of the database to create'}),
}
static args = {};

public async run(): Promise<void> {
this.log(`feed basic command`)
this.error("try 'feed --help' for more information");
}
}
51 changes: 51 additions & 0 deletions src/commands/feed/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Command, Flags } from "@oclif/core";
import { startOrbitDB } from "../../services/start-OrbitDB";
import { stopOrbitDB } from "../../services/stop-OrbitDB";
import { doesDBExists } from "../../utils/does-DBExists";
import { openDB } from "../../utils/open-DB";
import { resolveDBIdByName } from "../../utils/resolve-DBIdByName";
import { saveDB } from "../../utils/save-DB";

export default class FeedInfo extends Command {
static description = "Show informations about a feed type database";

static examples: Command.Example[] = [
"<%= config.bin %> <%= command.id %> --name=myFeedDbName --file=myFile",
];

static flags = {
// flag with a value (-n VALUE, --name=VALUE)
dbName: Flags.string({
char: "n",
description: "name of the database",
required: true,
}),
};

public async run(): Promise<void> {
const { flags } = await this.parse(FeedInfo);
const orbitdb = await startOrbitDB(true);
const dbAdress = await resolveDBIdByName(orbitdb, flags.dbName, "feed");
const DBExists = await doesDBExists(orbitdb, dbAdress);

if (!DBExists) {
this.error(
`database '${flags.dbName}' (or '${dbAdress}') does not exist`,
);
}

const db = await openDB(orbitdb, dbAdress, "feed")
this.log('--- Database informations ---\n')
this.log(`Name: ${db.dbname}`)
this.log(`Type: ${db._type}`)
Saverio976 marked this conversation as resolved.
Show resolved Hide resolved
this.log(`Adress: ${db.address.toString()}`)
this.log(`Owner: ${db.id}`)
this.log(`Data file: ./${db._cache.path}`)
this.log(`Entries: ${db._oplog.length}`)
this.log(`Write-Access: ${db.access.write}`)

await saveDB(db);
Saverio976 marked this conversation as resolved.
Show resolved Hide resolved
await db.close();
await stopOrbitDB(orbitdb);
}
}
Loading
Loading