Skip to content

Instantly share code, notes, and snippets.

View skolhustick's full-sized avatar

eshwarenm skolhustick

View GitHub Profile
@skolhustick
skolhustick / users.index.astro
Created September 3, 2022 14:33
astro-mongodob-users.index.astro
// src/pages/users/index.astro
---
import { getAllUsers } from "../../lib/users";
import Layout from "../layouts/Layout.astro";
const users = await getAllUsers();
if (!users) {
return new Response(null, {
status: 404,
statusText: "Not found",
});
@skolhustick
skolhustick / CreateUserForm.svelte
Created September 3, 2022 14:30
astro-mongodob-CreateUserForm.svelte
// src/components/CreateUserForm.svelte
<script>
const onSubmit = async (e) => {
const formData = new FormData(e.target);
const data = {};
for (let field of formData) {
const [key, value] = field;
data[key] = value;
}
@skolhustick
skolhustick / api.users.js
Created September 3, 2022 14:27
astro-mongodob-api.users.js
// src/pages/api/users.js
import { createUser, getAllUsers } from "../../lib/users";
export const get = async () => {
const users = await getAllUsers();
if (!users) {
return new Response(null, {
status: 404,
statusText: "Not found",
@skolhustick
skolhustick / users.js
Created September 3, 2022 14:25
astro-mongodob-users.js
// /src/lib/users.js
import { Users } from "./mongodb";
export const getAllUsers = async () => {
const users = await (await Users()).find({}).toArray();
return users;
};
export const createUser = async (newUser) => {
const user = await (await Users()).insert(newUser);
@skolhustick
skolhustick / mongodb.js
Created September 3, 2022 14:23
astro-mongodob-mongodb.js
// src/lib/mongodb.js
import { MongoClient } from "mongodb";
if (!import.meta.env.MONGODB_URI) {
throw new Error('Invalid environment variable: "MONGODB_URI"');
}
const uri = import.meta.env.MONGODB_URI;
const options = {};
@skolhustick
skolhustick / astro.config.mjs
Created September 3, 2022 14:14
astro-mongodob-astro.config.mjs
// astro.config.mjs
import { defineConfig } from "astro/config";
import node from "@astrojs/node";
import svelte from "@astrojs/svelte";
export default defineConfig({
output: "server",
adapter: node(),
integrations: [svelte()]
@skolhustick
skolhustick / next-js-prisma-orm-mongodb-pages:index.js
Created January 8, 2022 09:13
next-js-prisma-orm-mongodb-pages:index.js
import React from 'react'
import { getAllUsers } from '../prisma/user'
const Homepage = ({ users }) => {
return (
<div>
{users.map(user => (
<div key={user.id}>{user.name}</div>
))}
</div>
@skolhustick
skolhustick / next-js-prisma-orm-mongodb-curl-tests.sh
Created January 8, 2022 09:11
next-js-prisma-orm-mongodb-curl-tests.sh
curl - request GET \
 - url http://localhost:3000/api/users
curl - request POST \
 - url http://localhost:3000/api/users \
 - header 'Content-Type: application/json' \
 - data '{
 "name": "Bill Gates",
 "email": "[email protected]",
 "birthYear": 1955
// pages/api/user
import {
createUser,
deleteUser,
getAllUsers,
getUser,
updateUser
} from '../../prisma/user'
@skolhustick
skolhustick / next-js-prisma-orm-mongodb-prisma:user.js
Created January 8, 2022 09:04
next-js-prisma-orm-mongodb-prisma:user.js
// /prisma/user.js
import prisma from './prisma'
// READ
export const getAllUsers = async () => {
const users = await prisma.user.findMany({})
return users
}
export const getUser = async id => {