I'm trying to create a container and define the composite indexes that I want using Azure Cosmos DB, Azure functions, and node with typescript. I have a config file that defines my options which I read from. I then try to pass in the composite index json object into my createIfNotExists containers function, but no luck. Any help would be appreciated!
config.ts
export const config = {
endpoint: <MyUrl>,
key: <MyKey>,
databaseId: MyDBId,
myContainerId: <MyContainerId>,
myPartitionKey: { kind: "Hash", paths: ["/id"] },
myContainerOptions: {
"compositeIndexes":[
[
{
"path":"/myId",
},
{
"path":"/myOtherId",
}
]
]
}
};
my azure function code:
const myId= (req.query.myId || (req.body && req.body.myId));
const myOtherId = (req.query.myOtherId || (req.body && req.body.myOtherId));
const { endpoint, key, databaseId, myContainerId, myPartitionKey, myContainerOptions } = config;
const client = new CosmosClient({ endpoint, key});
const database = client.database(databaseId);
const container = database.container(myContainerId);
await create(client, databaseId, myContainerId, myPartitionKey, myContainerOptions);
DbContext.ts
export async function create(client, databaseId, containerId, partitionKey, options?) {
const { database } = await client.databases.createIfNotExists({ id: databaseId });
const { container } = await database.containers.createIfNotExists({ id: containerId, partitionKey}, options);
console.log(`Created Database: ${database.id}\n Created container:\n${container.id}`);
}
Composite indexes should be part of container definition and not part of options.
Can you try with the following:
const containerDefinition = {
id: containerId,
partitionKey,
indexingPolicy: {
compositeIndexes: [
[
{ "path":"/myId"},
{ "path":"/myOtherId"}
]
]
}
};
const { container } = await database.containers.createIfNotExists(containerDefinition, options);
Related
I have the following code:
const { query1 } = require('query1')
const { query2 } = require('query2')
const { query3 } = require('query3')
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
query1,
query2,
query3
}
})
});
const permissions = shield(
{
Query: {
query1: user,
query2: user,
query3: admin
}
}
)
(much longer in the reality)
And I'm looking for a way to make it clearer, like:
const { query1 } = require('query1')
const { query2 } = require('query2')
const { query3 } = require('query3')
const declaration = {
query1: user,
query2: user,
query3: admin
}
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: someMagic(declaration)
})
});
const permissions = shield(
{
Query: declaration
}
)
But here declaration keys are the strings "query1", "query2" and "query3". Not the objects.
With a WeakMap we could have something like:
const declaration = new WeakMap();
declaration.set(query1, user);
declaration.set(query2, user);
declaration.set(query3, admin);
But I find it much less elegant. Is there another way ?
Hope this might help:
/*
queries['query1'] = require('query1')
queries['query2'] = require('query2')
queries['query3'] = require('query3')
*/
let queries = {
query1: { a: { $eq: "I am Query 1" }, permission: "user" }, //user can be String or object or whatever !
query2: { b: { $eq: "I am Query 2" }, permission: "user" },
query3: { c: { $eq: "I am Query 3" }, permission: "admin" },
};
let declaration = {};
let queryNames = Object.keys(queries);
for (let i in queryNames) {
let curQueryName = queryNames[i];
declaration[curQueryName] = queries[curQueryName]["permission"];
//delete queries[curQueryName]["permission"]
}
console.log(JSON.stringify(declaration, null, 2));
Instead of an object, you could have an array of objects, something like this:
const { query1 } = require('query1')
const { query2 } = require('query2')
const { query3 } = require('query3')
var declarations = [
{ query1, permissions: user },
{ query2, permissions: user },
{ query3, permissions: admin }
];
Then to extract fields and permissions objects:
var fields = {};
var queryPermissions = {};
for (let declaration of declarations) {
for (let key of Object.keys(declaration)) {
if (key !== 'permissions') {
fields[key] = declaration[key];
queryPermissions[key] = declaration.permissions;
}
}
}
For example:
const query1 = { query: 'sample query 1' };
const query2 = { query: 'sample query 2' };
const query3 = { query: 'sample query 3' };
const user = 'user';
const admin = 'admin';
var declarations = [
{ query1, permissions: user },
{ query2, permissions: user },
{ query3, permissions: admin }
];
var fields = {};
var queryPermissions = {};
for (let declaration of declarations) {
for (let key of Object.keys(declaration)) {
if (key !== 'permissions') {
fields[key] = declaration[key];
queryPermissions[key] = declaration.permissions;
}
}
}
console.log(fields);
console.log(queryPermissions);
Side Note:
Another advantage of doing it this way is that you can group queries by permissions, for instance:
var declarations = [
{ query1, query2, permissions: user },
{ query3, permissions: admin }
];
It's not possible to construct an object that uses a variable's name as the property name but something else than the variable's value as the value. You'll have to spell them out twice, once for the permissions once for the resolvers:
const queryPermissions: {
'query1': user,
'query2': user,
'query3': admin,
};
const queryResolvers: {
'query1': require('query1').query1,
'query2': require('query2').query2,
'query3': require('query3').query3,
};
The destructured variables from the imports don't really help with anything here. However, if your module structure is really like this, and you're still using Common.js modules, then you can actually derive the queryResolvers object from the property names of the queryPermissions object:
const queryResolvers = Object.fromEntries(Object.keys(queryPermissions).map(fieldName =>
[fieldName, require(fieldName)[fieldName]]
));
This should do the trick:
((function(t,d){
var fields = {}
for (const key in d) {
fields[key] = t[key]
}
return fields
})(this, declaration)
{a, b, c}
Is just shorthand for {a: a, b: b, c: c}
However, Global constants do not become properties of the window object, unlike var variables, so you might need to fiddle a bit with it.
I have the cars collection in firebase with the following format and examples:
{
{
make: BMW,
model: X7,
color: white
},
{
make: BMW,
model: X7,
color: white
},
{
make: BMW,
model: X7,
color: black
},
{
make: Audi,
model: Q7,
color: gray
}
}
What I would like to receive from my query is something like this:
[
{
make: BMW,
model: X7,
colors: [white, black]
},
{
make: Audi,
model: Q7,
colors: [gray]
}
]
It doesn't have to be exactly at this format but I hope that I made my purpose clear. How can I achieve this efficiently using firebase?
Firestore screenshot
My code to receive all the documents:
const admin = require('firebase-admin')
const db = admin.firestore()
module.exports.getVehicles = async (data, context) => {
const vehiclesQuery = db.collection('vehicles').get()
const vehicles = []
vehiclesQuery.forEach(doc => {
vehicles.push(doc.data())
})
return vehicles
}
There isn't any direct way to get data in that format. You would have to modify the data using Javascript after fetching all the documents. Also you are missing the await before get() statement:
module.exports.getVehicles = async (data, context) => {
const vehiclesQuery = await db.collection('vehicles').get()
const res = {}
vehiclesQuery.docs.forEach(doc => {
const { color, make, model } = doc
if (!res[make+'-'+model]) {
res[make+'-'+model] = [color]
} else {
res[make+'-'+model].push(color)
}
})
const vehicles = []
Object.entries(res).forEach((v) => {
const [make, model] = v[0].split('-')
vehicles.push({make, model, colors: v[1]})
})
console.log(vehicles)
return vehicles
}
Subscriptions with Nexus are undocumented but I searched Github and tried every example in the book. It's just not working for me.
I have cloned Prisma2 GraphQL boilerplate project & my files are as follows:
prisma/schema.prisma
datasource db {
provider = "sqlite"
url = "file:dev.db"
default = true
}
generator photon {
provider = "photonjs"
}
generator nexus_prisma {
provider = "nexus-prisma"
}
model Pokemon {
id String #default(cuid()) #id #unique
number Int #unique
name String
attacks PokemonAttack?
}
model PokemonAttack {
id Int #id
special Attack[]
}
model Attack {
id Int #id
name String
damage String
}
src/index.js
const { GraphQLServer } = require('graphql-yoga')
const { join } = require('path')
const { makeSchema, objectType, idArg, stringArg, subscriptionField } = require('#prisma/nexus')
const Photon = require('#generated/photon')
const { nexusPrismaPlugin } = require('#generated/nexus-prisma')
const photon = new Photon()
const nexusPrisma = nexusPrismaPlugin({
photon: ctx => ctx.photon,
})
const Attack = objectType({
name: "Attack",
definition(t) {
t.model.id()
t.model.name()
t.model.damage()
}
})
const PokemonAttack = objectType({
name: "PokemonAttack",
definition(t) {
t.model.id()
t.model.special()
}
})
const Pokemon = objectType({
name: "Pokemon",
definition(t) {
t.model.id()
t.model.number()
t.model.name()
t.model.attacks()
}
})
const Query = objectType({
name: 'Query',
definition(t) {
t.crud.findManyPokemon({
alias: 'pokemons'
})
t.list.field('pokemon', {
type: 'Pokemon',
args: {
name: stringArg(),
},
resolve: (parent, { name }, ctx) => {
return ctx.photon.pokemon.findMany({
where: {
name
}
})
},
})
},
})
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.crud.createOnePokemon({ alias: 'addPokemon' })
},
})
const Subscription = subscriptionField('newPokemon', {
type: 'Pokemon',
subscribe: (parent, args, ctx) => {
return ctx.photon.$subscribe.pokemon()
},
resolve: payload => payload
})
const schema = makeSchema({
types: [Query, Mutation, Subscription, Pokemon, Attack, PokemonAttack, nexusPrisma],
outputs: {
schema: join(__dirname, '/schema.graphql')
},
typegenAutoConfig: {
sources: [
{
source: '#generated/photon',
alias: 'photon',
},
],
},
})
const server = new GraphQLServer({
schema,
context: request => {
return {
...request,
photon,
}
},
})
server.start(() => console.log(`🚀 Server ready at http://localhost:4000`))
The related part is the Subscription which I don't know why it's not working or how it's supposed to work.
I searched Github for this query which results in all projects using Subscriptions.
I also found out this commit in this project to be relevant to my answer. Posting the related code here for brevity:
import { subscriptionField } from 'nexus';
import { idArg } from 'nexus/dist/core';
import { Context } from './types';
export const PollResultSubscription = subscriptionField('pollResult', {
type: 'AnswerSubscriptionPayload',
args: {
pollId: idArg(),
},
subscribe(_: any, { pollId }: { pollId: string }, context: Context) {
// Subscribe to changes on answers in the given poll
return context.prisma.$subscribe.answer({
node: { poll: { id: pollId } },
});
},
resolve(payload: any) {
return payload;
},
});
Which is similar to what I do. But they do have AnswerSubscriptionPayload & I don't get any generated type that contains Subscription in it.
How do I solve this? I think I am doing everything right but it's still not working. Every example on GitHub is similar to above & even I am doing the same thing.
Any suggestions?
Edit: Subscriptions aren't implemented yet :(
I seem to have got this working despite subscriptions not being implemented. I have a working pubsub proof of concept based off the prisma2 boilerplate and Ben Awad's video tutorial https://youtu.be/146AypcFvAU . Should be able to get this up and running with redis and websockets to handle subscriptions until the prisma2 version is ready.
https://github.com/ryanking1809/prisma2_subscriptions
Subscriptions aren't implemented yet.
I've opened up an issue to track it.
I'll edit this answer as soon as it's implemented in Prisma 2.
I am new to Vuex and Nuxt.
I would like to use vuex to fetch dropbox filestructure and store them.
Here is my code. the console.log seems to work fine. it prints out something like below.
But the structure still turns out to be [] when i use in index.vue
[ { '.tag': 'file',
name: 'Document.docx',
path_lower: '/posts/document.docx',
path_display: '/posts/Document.docx',
id: 'id:H_6Dhj1r7cEAAAAAAAAXlQ',
client_modified: '2018-09-02T14:23:05Z',
server_modified: '2018-09-02T14:23:06Z',
rev: '5e5cab150',
size: 11366,
content_hash: 'd26bb0382752820694d31f42e82e31ef72bed683b90e02952ea09125264d4124' },
{ '.tag': 'file',
name: '2013-5-17-first-post.md',
path_lower: '/posts/2013-5-17-first-post.md',
path_display: '/posts/2013-5-17-first-post.md',
id: 'id:H_6Dhj1r7cEAAAAAAAAXlg',
client_modified: '2018-09-02T14:25:38Z',
server_modified: '2018-09-02T14:25:38Z',
rev: '6e5cab150',
size: 136,
content_hash: '3b8d60de425e8280d55e45d7359cd3290abc5bc3b0bb6831b09a6da0d3cb6a12' } ]
the code is like below
import "isomorphic-fetch"
import {
Dropbox
} from "dropbox";
import {
DropboxTeam
} from "dropbox";
export const state = () => ({
structure: []
});
export const mutations = {
setStucture(state, structure) {
state.structure = structure.slice();
console.log(state.structure);
// console.log(structure.slice());
}
};
export const actions = {
async nuxtServerInit({ commit }) {
let accessToken = "XXXXX"
let dropbox = new Dropbox({
accessToken: accessToken
});
dropbox.filesListFolder({path: '/posts'})
.then(response => {
const structure = response.entries;
commit("setStucture", structure);
})
.catch(error => {
console.log(error);
});
}
};
Can I get some help. Thank you!
add await to dropbox.filesListFolder({path: '/posts'}) like below turned out to be the right answer.
await dropbox.filesListFolder({path: '/posts'})
I have a collection and a mutation to add a new item to it. I haven't been able to get Relay Modern to update the UI after a successful mutation.
I've got a PaginationContainer setup with the following query: prop
{
query: graphql`
fragment ContactsList_query on WPQuery {
id
contacts: posts(
first: $count,
after: $cursor
post_type: $postType,
order: $order,
category_name: $categoryName
) #connection(key: "ContactsList_contacts" ) {
edges {
node {
id
...ContactListItem_contact
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`
},
That fetches correctly. I've then got a mutation to add a contact to this list.
Neither the config RANGE_ADD or the updater: callback techniques work at all.
I'm triggering this mutation like so
onSave = (fields) => {
insertPost(
fields.toJS(),
this.props.query.id,
this.props.relay.environment
);
}
No errors, just nothing updates.
const mutation = graphql`
mutation InsertPostMutation(
$data: InsertPostInput!
) {
insert_post(input: $data) {
wp_query {
id
}
postEdge {
node {
id
title
}
}
}
}
`;
export default function insertPost(data, id, environment) {
const variables = {
data,
};
commitMutation(
environment,
{
mutation,
variables,
onCompleted: (response, errors) => {
console.log('Response received from server.')
},
onError: err => console.error(err),
configs: [{
type: 'RANGE_ADD',
parentID: id,
connectionInfo: [{
key: 'ContactsList_contacts',
rangeBehavior: 'append',
}],
edgeName: 'postEdge'
}],
// updater: (store) => {
// // const inspector = new RecordSourceInspector(store)
// const payload = store.getRootField('insert_post')
// const newEdge = payload.getLinkedRecord('postEdge')
// const proxy = store.get(id)
// // Conn is always undefined here
// const conn = ConnectionHandler.getConnection(proxy, 'ContactsList_contacts')
// ConnectionHandler.insertEdgeAfter(conn, newEdge)
// }
},
);
}
Well, I was able to fix this by changing the line
#connection(key: "ContactsList_contacts")
To
#connection(key: "ContactsList_contacts", filters: [])
Seems it couldn't find the connection otherwise...
https://facebook.github.io/relay/docs/pagination-container.html#connection-directive
Then using the updater function the connection was found.