I am trying to use orbit-db along with the IPFS and I am going through the official documentation of OrbitDB. ATM, I am simply trying to create a db(specifically key-value store) and I have the following code uptil now
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
createDb = async() => {
try {
const ipfsOptions = {
config: {
my-config-options
},
EXPERIMENTAL: {
pubsub: true
}
}
const ipfs = await IPFS.create(ipfsOptions)
const orbitdb = await OrbitDB.createInstance(ipfs)
const db = await orbitdb.create('test', 'keyvalue', {
overwrite: false,
replicate: true,
accessController: {
admin: ['*'],
write: ['*']
}
})
console.log(db)
await db.put('hello', { name: 'world' })
console.log(db.all)
} catch (error) {
console.trace(error)
}
}
But I keep getting the same error no matter what I try
Trace: Error: Could not append entry, key "..." is not allowed to write to the log
So any help would be much appreciated. Also do comment if any information is missing and I would add it as per the requirements
I think there's a small issue with your accessController.
The presence of the admin property indicates that you want to use the orbitdb type like this:
accessController: {
type: 'orbitdb',
admin: ['*'],
write: ['*']
}
Otherwise if you wish to use the default ipfs type, remove the admin property like this:
accessController: {
type: 'ipfs',
write: ['*']
}
Related
I want to compare to ids one in relation and the other given by me in a query and get all information, for example:
async getAllPhoto(id: string) {
var photo = await this._photoRepository.find({
relations: {
catalogue: true,
},
where: { catalogue: { id: Not(id) } },
});
return photo;
}
I tried this but got an empty array.
const ids = 2; // get your id which you request from font-end
const photo = this.repository.find({
relations:['catalogue'],
where:{
catalogue:{
id: Not(ids)
}
}
})
when you develop project in nestjs,
you'd better enable "logging":true in your database config!
you will find all raw sql from ORM.
const user = new mongoose.Schema(
{
nano_id: {
type: String,
required: true,
default: () => nanoid(7),
index: { unique: true },
},
...
}
How to run again nanoid(7) if is not unique? (run automatically and not get any error in console)
There are two ways to do this:
Prevent the error from happening in the first place by searching for a document with a similar Nano ID, if a document exists, regenerate a new Nano ID using a recursive function.
const { customAlphabet } = require('nanoid');
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
const nanoid = customAlphabet(alphabet, 8);
// userRegistration controller or route...
async function uniqueNanoId(query): Promise<string> {
const nanoId = nanoid();
const sameNanoId = await User.findOne({ nano_id:nanoId });
if (sameNanoId) {
return uniqueNanoId(query);
}
return nanoId;
}
const nanoId = await uniqueNanoId();
const user = User.create({...userBody,nanoId});
//...
Catch the error - as #cachius hinted - and regenerate the unique Nano ID accordingly (not tested). Catching a duplicate key has been discussed here
Bonus: Ask yourself the question, do I really need both Default Mongoose IDs and Nano IDs? If not, then this is a simple solution.
// ...
_id: {
type: String,
default: () => nanoid(),
},
// ...
The database will throw an error you have to catch and react to by generating the same record again with a newly generated nanoid.
I'm using Mongoose in NestJs library and want to use mongoose-delete plugin for all of my schemas.
But I don't know how to use it with nestJS And Typescript.
First i installed both mongoose-delete and #Types/mongoose-delete libraries but there is no typescript documentary for This plugin.
This is the recommended method for adding plugin by nest:
MongooseModule.forRoot(MONGO_URI, {
connectionFactory: connection => {
connection.plugin(require('mongoose-delete'));
return connection;
},
}),
And absolutely this generates esLint error:
Require statement not part of import statement.eslint
And I cannot use delete function. It's not defined in the mongoose.Dcoument
export type ChannelDocument = Channel & Document;
constructor(
#InjectModel(Channel.name) private _channelModel: Model<ChannelDocument>,
) {}
async delete(id: string) {
this._channelModel.delete({ id });
// This is undefined -^
}
Try to restart you IDE (vscode if you use) after install this package: #types/mongoose-delete
use soft delete plugin => https://www.npmjs.com/package/soft-delete-mongoose-plugin
A simple and friendly soft delete plugin for mongoose,implementation using TS.
Methods were added and overridden on mongoose model to realize soft deletion logic.
yuo can used as a global plugin:
import { plugin } from 'mongoose';
import { SoftDelete } from 'soft-delete-mongoose-plugin';
// defind soft delete field name
const IS_DELETED_FIELD = 'isDeleted';
const DELETED_AT_FIELD = 'deletedAt';
// use soft delete plugin
plugin(
new SoftDelete({
isDeletedField: IS_DELETED_FIELD,
deletedAtField: DELETED_AT_FIELD,
}).getPlugin(),
);
// other code
// ...
Please take a look at mongoose-softdelete-typescript.
import { Schema, model } from 'mongoose';
import { softDeletePlugin, ISoftDeletedModel, ISoftDeletedDocument } from 'mongoose-softdelete-typescript';
const TestSchema = new Schema({
name: { type: String, default: '' },
description: { type: String, default: 'description' },
});
TestSchema.plugin(softDeletePlugin);
const Test = model<ISoftDeletedDocument, ISoftDeletedModel<ISoftDeletedDocument>>('Test', TestSchema);
const test1 = new Test();
// delete single document
const newTest = await test1.softDelete();
// restore single document
const restoredTest = await test1.restore();
// find many deleted documents
const deletedTests = await Test.findDeleted(true);
// soft delete many documents with conditions
await Test.softDelete({ name: 'test' });
// support mongo transaction
const session = await Test.db.startSession();
session.startTransaction();
try {
const newTest = await test1.softDelete(session);
await session.commitTransaction();
} catch (e) {
console.log('e', e);
await session.abortTransaction();
} finally {
await session.endSession();
}
I'm new to feathersjs. Please help me understand these bits
So, I have this codes (products.services.js)
function mapUserIdToData(context) {
if (context.data && context.params.route.userId) {
context.data.user = context.params.route.userId;
}
}
app.use("/stores", new Stores(options, app));
const service = app.service("stores");
service.hooks(hooks);
// setup our nested routes
app.use("/users/:userId/stores", app.service("stores"));
app.service("users/:userId/stores").hooks({
before: {
find(context) {
if (context.params.route.userId)
context.params.query.user = context.params.route.userId;
},
create: mapUserIdToData,
update: mapUserIdToData,
patch: mapUserIdToData,
},
});
And in my register module (register.service.js), I called these logic to create store for new user
const users = this.app.service("users");
const user = await users.create(userData);
// save store
if (storeTitle) {
const stores = this.app.service("stores");
await stores.create({ user: user._id, title: storeTitle });
}
What I don't understand is : Why the line await stores.create({ user: user._id, title: storeTitle }); also trigger hooks logic in app.service("users/:userId/stores") ? I know because it run the mapUserIdToData function, which will be failed because there is no route param for userId.
Thank You
I want to understand how to switch between databases within mongoose global promise connection.
My current connection is established this way app.ts
import * as mongoose from 'mongoose';
...
try {
await mongoose.createConnection(`mongodb://localhost:27017/db1`, {
useNewUrlParser: true,
})
console.log("Connected")
} catch (error) {
console.log(error)
}
And then I am accessing it in different files some.model.ts
import { Schema, Document, model } from 'mongoose';
const SomeSchema: Schema = new Schema({
name: { type: String, required: true },
owner: { type: string, required: true }
});
export default model('Some', SomeSchema);
According to documentation.
So far we've seen how to connect to MongoDB using Mongoose's default connection. At times we may need multiple connections open to Mongo, each with different read/write settings, or maybe just to different databases for example. In these cases we can utilize mongoose.createConnection() which accepts all the arguments already discussed and returns a fresh connection for you.
const conn = mongoose.createConnection('mongodb://[username:password#]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);
I can create multiple database connections like this
try {
const db1 = await mongoose.createConnection(`mongodb://localhost:27017/db1`, {
useNewUrlParser: true,
})
const db2 = await mongoose.createConnection(`mongodb://localhost:27017/db2`, {
useNewUrlParser: true,
})
console.log("Connected")
} catch (error) {
console.log(error)
}
I can see both connection in console.log(mongoose.connections)
But how can I specify what database should be used for the Model in some.model.ts?
import { Schema, Document, model } from 'mongoose';
const SomeSchema: Schema = new Schema({
name: { type: String, required: true },
owner: { type: string, required: true }
});
export default SPECIFY_DATABASE.model('Some', SomeSchema);
I have found other questions like this but there are connections created "localy", I need to use mongoose connection across many different files.
Thank you for answers, if you need more explanation please let me now.
You need to actually return the connection, and then register a given model to each of the connections. To clarify, you need:
something to create a (named, specific) connection
schemas
you create models by registering schemas to the given connections,
you also need something to orchestrate it.
Example, lets have a "db.js" file (I call mine "repo.js" usually) with a single export, a function that returns the initialized database Promise.
You'd use it by importing the function and awaiting for the db.
I have a bit of a longer example, so error handling etc is ommited for brevity.
import { createConnections } from './create-connections';
import { UsersSchema } from './users-schema';
import { PostsSchema } from './posts-schema';
let db: any;
export function getDatabase(): Promise<any> {
if (this.db) return Promise.resolve(db);
return createDatabases();
}
async function createDatabases() {
const { db1, db2 } = await createConnections('mongodb://localhost/db1', 'mongodb://localhost/db2');
const UserModel = db1.model('users', UserSchema);
const PostModel = db2.model('posts', PostSchema);
db = {
UserModel,
PostModel,
// also if you need this
connections: {
db1,
db2,
}
}
return db;
}
Now, I've used './create-connections' here, which is almost what you have:
// create-connection.js
const { createConnection } = require('mongoose');
// You create connections by calling this function and giving it the URL to the server
export function createConnections(url1, url2) {
const db1 = await createConnection(url1);
const db2 = await createConnection(url2);
return {
db1,
db2
}
}
Now, let's say you have two models: users and posts, let's have their schemas.
// users schema
import { Schema, Document } from 'mongoose';
export const UserSchema: Schema = new Schema({
name: { type: String, required: true },
});
// posts schema
import { Schema, Document } from 'mongoose';
export const PostSchema: Schema = new Schema({
text: { type: String, required: true },
owner: { type: SchemaID, required: true }
});
So now you need to bind it all in that fdirst file.
But how to use it? As I've said, since it's async, you always import it and use it as a simple async getDB:
// some controller, route handler, service etc.
import { getDatabase } from './get-database';
router.get('/users', async (req, res) => {
const User = await getDatabase().UserModel;
const users = await User.find();
return res.json(users);
});
router.post('/posts', async (req, res) {
const { text } = req.body;
const owner = req.user.id;
const Post = await getDatabase().PostModel;
const post = await Post.create({ text, owner });
return res.json(post);
});