Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
RavenDB: How to create a collection?

I set up a free Database "TestDB" in ravendb and there is the default @empty collection when you create some documents. But how can I create my own collection? There is no documentation about that, and no API apparently to do this from their npm ravendb package...?

Here is my code:

import { DocumentStore } from 'ravendb'
import * as fs from 'fs'

// load certificate and prepare authentication options
const authOptions = {
    certificate: fs.readFileSync(
        'C:/Users/asdasd/Desktop/testravendb/free.asdasdasd.client.certificate.pfx'
    ),
    type: 'pfx', // or "pem"
    password: '',
}

const store = new DocumentStore(
    ['https://a.free.asdasdasd.ravendb.cloud'],
    'TestDB',
    authOptions
)
store.initialize()

console.log(store)
const session = store.openSession()
console.log(session)

let product = {
    title: 'iPhone X',
    price: 999.99,
    currency: 'USD',
    storage: 64,
    manufacturer: 'Apple',
    in_stock: true,
    last_update: new Date('2017-10-01T00:00:00'),
}
async function save() {
    await session.store(product, 'products/')
    console.log(product.id) // Products/1-A
    await session.saveChanges()
}
save()

async function getData() {
    const query = session.query({ collection: '@empty' })
    const results = await query.all()
    //let product2 = await session.load('products')
    console.log(results) // iPhone X
}

getData()

It works as long as I use the @empty collection..

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use a field from the document in order to set the collection.

const store = new DocumentStore(urls, database);
store.conventions.findCollectionNameForObjectLiteral = entity => 
entity["collection"];
// ...
store.initialize();

https://ravendb.net/docs/article-page/5.2/nodejs/client-api/session/storing-entities

over 4 years ago · Santiago Trujillo Denunciar

0

To create a collection you need to create an entity with instance of a Class as parameter.

More than that, if you pass an intance of a class it will generate a uniqe slug for you instead of long UUID.

const { User } = require('./user.js')

const store = new DocumentStore(url, db);
store.initialize();

const add = async () => {
    const user = new User({ email@gmail.com, John, 32 })
    await session.store(user);
    await session.saveChanges();
}

execute add() to add a new document inside Users collection.

It will even name the collection as Users although the class you created is User.

user.js:

class User {
    constructor({ email, name, age}) {
        this.email = email;
        this.name= name;
        this.age= age;
    }
}

module.exports {
    User
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda