Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
06ed4944
Unverified
Commit
06ed4944
authored
Jan 19, 2023
by
Adil Ansari
Committed by
GitHub
Jan 19, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Latest Tigris sdk and example using decorators (#44256)
parent
98a2448e
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
67 additions
and
101 deletions
+67
-101
.eslintignore
.eslintignore
+2
-1
.env.local.example
examples/with-tigris/.env.local.example
+7
-0
README.md
examples/with-tigris/README.md
+7
-12
EachToDo.tsx
examples/with-tigris/components/EachToDo.tsx
+1
-1
todoItems.ts
examples/with-tigris/db/models/todoItems.ts
+19
-0
tigris.ts
examples/with-tigris/lib/tigris.ts
+3
-24
todoItems.ts
...les/with-tigris/models/tigris/todoStarterApp/todoItems.ts
+0
-22
package.json
examples/with-tigris/package.json
+4
-3
[id].ts
examples/with-tigris/pages/api/item/[id].ts
+7
-9
index.ts
examples/with-tigris/pages/api/items/index.ts
+3
-6
search.ts
examples/with-tigris/pages/api/items/search.ts
+5
-10
index.tsx
examples/with-tigris/pages/index.tsx
+1
-1
setup.ts
examples/with-tigris/scripts/setup.ts
+6
-12
tsconfig.json
examples/with-tigris/tsconfig.json
+2
-0
No files found.
.eslintignore
View file @
06ed4944
...
@@ -11,6 +11,7 @@ examples/with-flow/**
...
@@ -11,6 +11,7 @@ examples/with-flow/**
examples/with-jest/**
examples/with-jest/**
examples/with-mobx-state-tree/**
examples/with-mobx-state-tree/**
examples/with-mobx/**
examples/with-mobx/**
examples/with-tigris/db/models/todoItems.ts
packages/next/src/bundles/webpack/packages/*.runtime.js
packages/next/src/bundles/webpack/packages/*.runtime.js
packages/next/src/bundles/webpack/packages/lazy-compilation-*.js
packages/next/src/bundles/webpack/packages/lazy-compilation-*.js
packages/next/src/compiled/**/*
packages/next/src/compiled/**/*
...
@@ -36,4 +37,4 @@ bench/nested-deps/pages/**
...
@@ -36,4 +37,4 @@ bench/nested-deps/pages/**
bench/nested-deps/components/**
bench/nested-deps/components/**
packages/next-bundle-analyzer/index.d.ts
packages/next-bundle-analyzer/index.d.ts
examples/with-typescript-graphql/lib/gql/
examples/with-typescript-graphql/lib/gql/
test/development/basic/hmr/components/parse-error.js
test/development/basic/hmr/components/parse-error.js
\ No newline at end of file
examples/with-tigris/.env.local.example
View file @
06ed4944
# Enter your tigris uri, ex :- localhost:8081, api.preview.tigrisdata.cloud etc.
# Enter your tigris uri, ex :- localhost:8081, api.preview.tigrisdata.cloud etc.
TIGRIS_URI=
TIGRIS_URI=
# Name of your project
TIGRIS_PROJECT=
# Client credentials, if using auth, can be generated from Tigris cloud console.
# Client credentials, if using auth, can be generated from Tigris cloud console.
# See: https://docs.tigrisdata.com/auth
# See: https://docs.tigrisdata.com/auth
TIGRIS_CLIENT_ID=
TIGRIS_CLIENT_ID=
TIGRIS_CLIENT_SECRET=
TIGRIS_CLIENT_SECRET=
# Optionally specify a database branch, ex :- "staging" or "${VERCEL_GIT_COMMIT_REF}" to dynamically
# create branches for preview deployments (database branch name will match the branch name on github)
TIGRIS_DB_BRANCH=
examples/with-tigris/README.md
View file @
06ed4944
...
@@ -60,7 +60,7 @@ tigris dev start
...
@@ -60,7 +60,7 @@ tigris dev start
npm run dev
npm run dev
```
```
> Note: This step uses a custom dev & build script to initialize Tigris
database and collection
for
> Note: This step uses a custom dev & build script to initialize Tigris
collections
for
> the app and requires [ts-node](https://www.npmjs.com/package/ts-node#installation) to be installed.
> the app and requires [ts-node](https://www.npmjs.com/package/ts-node#installation) to be installed.
:tada: All done. You should be able to use app on
`localhost:3000`
in browser. Feel free to play
:tada: All done. You should be able to use app on
`localhost:3000`
in browser. Feel free to play
...
@@ -95,10 +95,9 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
...
@@ -95,10 +95,9 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
├── package.json
├── package.json
├── lib
├── lib
│ ├── tigris.ts
│ ├── tigris.ts
├── models
├── db
│ └── tigris
│ └── models
│ └── todoStarterApp
│ └── todoItems.ts
│ └── todoItems.ts
└── pages
└── pages
├── index.tsx
├── index.tsx
└── api
└── api
...
@@ -114,13 +113,9 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
...
@@ -114,13 +113,9 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
<details>
<details>
<summary>
🪢 Tigris schema definition
</summary>
<summary>
🪢 Tigris schema definition
</summary>
[
models/tigris/todoStarterApp/todoItems.ts
](
models/tigris/todoStarterApp/todoItems.ts
)
- The to-do list app
[
db/models/todoItems.ts
](
db/models/todoItems.ts
)
- The to-do list app has a single collection
has a single collection
`todoItems`
that stores the to-do items in
`todoStarterApp`
database. The
`todoItems`
that stores the to-do items. The Collection gets automatically provisioned by the
Database and Collection get automatically provisioned by the
[
setup script
](
scripts/setup.ts
)
.
[
setup script
](
scripts/setup.ts
)
.
This is an inspiration from Next.js based file system router. Create a folder or drop a schema file
inside database folder under
`models/tigris/`
, and you're able to instantly create Databases and
Collections in Tigris for your application.
</details>
</details>
...
...
examples/with-tigris/components/EachToDo.tsx
View file @
06ed4944
import
Image
from
'next/image'
import
Image
from
'next/image'
import
React
from
'react'
import
React
from
'react'
import
{
TodoItem
}
from
'../
models/tigris/todoStarterApp
/todoItems'
import
{
TodoItem
}
from
'../
db/models
/todoItems'
import
styles
from
'../styles/EachToDo.module.css'
import
styles
from
'../styles/EachToDo.module.css'
type
Props
=
{
type
Props
=
{
...
...
examples/with-tigris/db/models/todoItems.ts
0 → 100644
View file @
06ed4944
import
{
Field
,
PrimaryKey
,
TigrisCollection
,
TigrisCollectionType
,
TigrisDataTypes
,
}
from
'@tigrisdata/core'
@
TigrisCollection
(
'todoItems'
)
export
class
TodoItem
implements
TigrisCollectionType
{
@
PrimaryKey
(
TigrisDataTypes
.
INT32
,
{
order
:
1
,
autoGenerate
:
true
})
id
!
:
number
@
Field
()
text
!
:
string
@
Field
()
completed
!
:
boolean
}
examples/with-tigris/lib/tigris.ts
View file @
06ed4944
import
{
DB
,
Tigris
}
from
'@tigrisdata/core'
import
{
Tigris
}
from
'@tigrisdata/core'
const
DB_NAME
=
'todoStarterApp'
const
tigrisClient
=
new
Tigris
()
const
tigrisDb
=
tigrisClient
.
getDatabase
()
declare
global
{
// eslint-disable-next-line no-var
var
tigrisDb
:
DB
}
let
tigrisDb
:
DB
// Caching the client because `next dev` would otherwise create a
// new connection on every file save while previous connection is active due to
// hot reloading. However, in production, Next.js would completely tear down before
// restarting, thus, disconnecting and reconnecting to Tigris.
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
!
global
.
tigrisDb
)
{
const
tigrisClient
=
new
Tigris
()
global
.
tigrisDb
=
tigrisClient
.
getDatabase
(
DB_NAME
)
}
tigrisDb
=
global
.
tigrisDb
}
else
{
const
tigrisClient
=
new
Tigris
()
tigrisDb
=
tigrisClient
.
getDatabase
(
DB_NAME
)
}
// export to share DB across modules
// export to share DB across modules
export
default
tigrisDb
export
default
tigrisDb
examples/with-tigris/models/tigris/todoStarterApp/todoItems.ts
deleted
100644 → 0
View file @
98a2448e
import
{
TigrisCollectionType
,
TigrisDataTypes
,
TigrisSchema
,
}
from
'@tigrisdata/core/dist/types'
export
const
COLLECTION_NAME
=
'todoItems'
export
interface
TodoItem
extends
TigrisCollectionType
{
id
?:
number
text
:
string
completed
:
boolean
}
export
const
TodoItemSchema
:
TigrisSchema
<
TodoItem
>
=
{
id
:
{
type
:
TigrisDataTypes
.
INT32
,
primary_key
:
{
order
:
1
,
autoGenerate
:
true
},
},
text
:
{
type
:
TigrisDataTypes
.
STRING
},
completed
:
{
type
:
TigrisDataTypes
.
BOOLEAN
},
}
examples/with-tigris/package.json
View file @
06ed4944
{
{
"private"
:
true
,
"private"
:
true
,
"scripts"
:
{
"scripts"
:
{
"predev"
:
"
APP_ENV=development
npm run setup"
,
"predev"
:
"npm run setup"
,
"dev"
:
"next dev"
,
"dev"
:
"next dev"
,
"build"
:
"next build"
,
"build"
:
"next build"
,
"postbuild"
:
"
APP_ENV=production
npm run setup"
,
"postbuild"
:
"npm run setup"
,
"start"
:
"next start"
,
"start"
:
"next start"
,
"setup"
:
"npx ts-node scripts/setup.ts"
"setup"
:
"npx ts-node scripts/setup.ts"
},
},
...
@@ -12,7 +12,8 @@
...
@@ -12,7 +12,8 @@
"@tigrisdata/core"
:
"latest"
,
"@tigrisdata/core"
:
"latest"
,
"next"
:
"latest"
,
"next"
:
"latest"
,
"react"
:
"18.2.0"
,
"react"
:
"18.2.0"
,
"react-dom"
:
"18.2.0"
"react-dom"
:
"18.2.0"
,
"reflect-metadata"
:
"0.1.13"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"@types/node"
:
"18.11.2"
,
"@types/node"
:
"18.11.2"
,
...
...
examples/with-tigris/pages/api/item/[id].ts
View file @
06ed4944
import
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
{
import
{
TodoItem
}
from
'../../../db/models/todoItems'
COLLECTION_NAME
,
TodoItem
,
}
from
'../../../models/tigris/todoStarterApp/todoItems'
import
tigrisDb
from
'../../../lib/tigris'
import
tigrisDb
from
'../../../lib/tigris'
type
Data
=
{
type
Data
=
{
...
@@ -16,8 +13,8 @@ async function handleGet(
...
@@ -16,8 +13,8 @@ async function handleGet(
itemId
:
number
itemId
:
number
)
{
)
{
try
{
try
{
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
COLLECTION_NAME
)
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
TodoItem
)
const
item
=
await
itemsCollection
.
findOne
({
id
:
itemId
})
const
item
=
await
itemsCollection
.
findOne
({
filter
:
{
id
:
itemId
}
})
if
(
!
item
)
{
if
(
!
item
)
{
res
.
status
(
404
).
json
({
error
:
'No item found'
})
res
.
status
(
404
).
json
({
error
:
'No item found'
})
}
else
{
}
else
{
...
@@ -32,7 +29,7 @@ async function handleGet(
...
@@ -32,7 +29,7 @@ async function handleGet(
async
function
handlePut
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
Data
>
)
{
async
function
handlePut
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
Data
>
)
{
try
{
try
{
const
item
=
JSON
.
parse
(
req
.
body
)
as
TodoItem
const
item
=
JSON
.
parse
(
req
.
body
)
as
TodoItem
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
COLLECTION_NAME
)
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
TodoItem
)
const
updated
=
await
itemsCollection
.
insertOrReplaceOne
(
item
)
const
updated
=
await
itemsCollection
.
insertOrReplaceOne
(
item
)
res
.
status
(
200
).
json
({
result
:
updated
})
res
.
status
(
200
).
json
({
result
:
updated
})
}
catch
(
err
)
{
}
catch
(
err
)
{
...
@@ -47,8 +44,9 @@ async function handleDelete(
...
@@ -47,8 +44,9 @@ async function handleDelete(
itemId
:
number
itemId
:
number
)
{
)
{
try
{
try
{
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
COLLECTION_NAME
)
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
TodoItem
)
const
status
=
(
await
itemsCollection
.
deleteOne
({
id
:
itemId
})).
status
const
status
=
(
await
itemsCollection
.
deleteOne
({
filter
:
{
id
:
itemId
}
}))
.
status
if
(
status
===
'deleted'
)
{
if
(
status
===
'deleted'
)
{
res
.
status
(
200
).
json
({})
res
.
status
(
200
).
json
({})
}
else
{
}
else
{
...
...
examples/with-tigris/pages/api/items/index.ts
View file @
06ed4944
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
{
import
{
TodoItem
}
from
'../../../db/models/todoItems'
COLLECTION_NAME
,
TodoItem
,
}
from
'../../../models/tigris/todoStarterApp/todoItems'
import
tigrisDb
from
'../../../lib/tigris'
import
tigrisDb
from
'../../../lib/tigris'
type
Response
=
{
type
Response
=
{
...
@@ -12,7 +9,7 @@ type Response = {
...
@@ -12,7 +9,7 @@ type Response = {
async
function
handleGet
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
Response
>
)
{
async
function
handleGet
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
Response
>
)
{
try
{
try
{
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
COLLECTION_NAME
)
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
TodoItem
)
const
cursor
=
itemsCollection
.
findMany
()
const
cursor
=
itemsCollection
.
findMany
()
const
items
=
await
cursor
.
toArray
()
const
items
=
await
cursor
.
toArray
()
res
.
status
(
200
).
json
({
result
:
items
})
res
.
status
(
200
).
json
({
result
:
items
})
...
@@ -25,7 +22,7 @@ async function handleGet(req: NextApiRequest, res: NextApiResponse<Response>) {
...
@@ -25,7 +22,7 @@ async function handleGet(req: NextApiRequest, res: NextApiResponse<Response>) {
async
function
handlePost
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
Response
>
)
{
async
function
handlePost
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
Response
>
)
{
try
{
try
{
const
item
=
JSON
.
parse
(
req
.
body
)
as
TodoItem
const
item
=
JSON
.
parse
(
req
.
body
)
as
TodoItem
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
COLLECTION_NAME
)
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
TodoItem
)
const
inserted
=
await
itemsCollection
.
insertOne
(
item
)
const
inserted
=
await
itemsCollection
.
insertOne
(
item
)
res
.
status
(
200
).
json
({
result
:
[
inserted
]
})
res
.
status
(
200
).
json
({
result
:
[
inserted
]
})
}
catch
(
err
)
{
}
catch
(
err
)
{
...
...
examples/with-tigris/pages/api/items/search.ts
View file @
06ed4944
import
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
{
import
{
TodoItem
}
from
'../../../db/models/todoItems'
COLLECTION_NAME
,
TodoItem
,
}
from
'../../../models/tigris/todoStarterApp/todoItems'
import
{
SearchRequest
}
from
'@tigrisdata/core/dist/search/types'
import
tigrisDb
from
'../../../lib/tigris'
import
tigrisDb
from
'../../../lib/tigris'
type
Data
=
{
type
Data
=
{
...
@@ -22,12 +18,11 @@ export default async function handler(
...
@@ -22,12 +18,11 @@ export default async function handler(
return
return
}
}
try
{
try
{
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
COLLECTION_NAME
)
const
itemsCollection
=
tigrisDb
.
getCollection
<
TodoItem
>
(
TodoItem
)
const
searchRequest
:
SearchRequest
<
TodoItem
>
=
{
q
:
query
as
string
}
const
searchResult
=
await
itemsCollection
.
search
({
q
:
query
as
string
})
const
searchResult
=
await
itemsCollection
.
search
(
searchRequest
)
const
items
=
new
Array
<
TodoItem
>
()
const
items
=
new
Array
<
TodoItem
>
()
for
(
const
hit
of
searchResult
.
hits
)
{
for
await
(
const
res
of
searchResult
)
{
items
.
push
(
hit
.
document
)
res
.
hits
.
forEach
((
hit
)
=>
items
.
push
(
hit
.
document
)
)
}
}
res
.
status
(
200
).
json
({
result
:
items
})
res
.
status
(
200
).
json
({
result
:
items
})
}
catch
(
err
)
{
}
catch
(
err
)
{
...
...
examples/with-tigris/pages/index.tsx
View file @
06ed4944
...
@@ -4,7 +4,7 @@ import Head from 'next/head'
...
@@ -4,7 +4,7 @@ import Head from 'next/head'
import
React
,
{
useEffect
,
useState
}
from
'react'
import
React
,
{
useEffect
,
useState
}
from
'react'
import
EachTodo
from
'../components/EachToDo'
import
EachTodo
from
'../components/EachToDo'
import
LoaderWave
from
'../components/LoaderWave'
import
LoaderWave
from
'../components/LoaderWave'
import
{
TodoItem
}
from
'../
models/tigris/todoStarterApp
/todoItems'
import
{
TodoItem
}
from
'../
db/models
/todoItems'
import
styles
from
'../styles/Home.module.css'
import
styles
from
'../styles/Home.module.css'
const
Home
:
NextPage
=
()
=>
{
const
Home
:
NextPage
=
()
=>
{
...
...
examples/with-tigris/scripts/setup.ts
View file @
06ed4944
import
{
Tigris
}
from
'@tigrisdata/core'
import
{
Tigris
}
from
'@tigrisdata/core'
import
{
loadEnvConfig
}
from
'@next/env'
import
{
TodoItem
}
from
'../db/models/todoItems'
// Run the config loader only when not executing within next runtime
if
(
process
.
env
.
NODE_ENV
===
undefined
)
{
if
(
process
.
env
.
APP_ENV
===
'development'
)
{
loadEnvConfig
(
process
.
cwd
(),
true
)
}
else
if
(
process
.
env
.
APP_ENV
===
'production'
)
{
loadEnvConfig
(
process
.
cwd
())
}
}
async
function
main
()
{
async
function
main
()
{
// setup client
and register schemas
// setup client
const
tigrisClient
=
new
Tigris
()
const
tigrisClient
=
new
Tigris
()
await
tigrisClient
.
registerSchemas
(
'models/tigris'
)
// ensure branch exists, create it if it needs to be created dynamically
await
tigrisClient
.
getDatabase
().
initializeBranch
()
// register schemas
await
tigrisClient
.
registerSchemas
([
TodoItem
])
}
}
main
()
main
()
examples/with-tigris/tsconfig.json
View file @
06ed4944
...
@@ -10,6 +10,8 @@
...
@@ -10,6 +10,8 @@
"esModuleInterop"
:
true
,
"esModuleInterop"
:
true
,
"module"
:
"commonjs"
,
"module"
:
"commonjs"
,
"moduleResolution"
:
"node"
,
"moduleResolution"
:
"node"
,
"experimentalDecorators"
:
true
,
"emitDecoratorMetadata"
:
true
,
"resolveJsonModule"
:
true
,
"resolveJsonModule"
:
true
,
"isolatedModules"
:
true
,
"isolatedModules"
:
true
,
"jsx"
:
"preserve"
,
"jsx"
:
"preserve"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment