Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda