1
- import { Handler } from "@netlify/functions" ;
2
- import { Response } from "@netlify/functions/dist/function/response" ;
3
- import Airtable from "airtable" ;
1
+ import { Handler , HandlerResponse } from "@netlify/functions" ;
2
+ import { MongoClient } from "mongodb" ;
4
3
import invariant from "tiny-invariant" ;
5
4
import { State } from "../../src/components/TraitsBuilderTab/index.d" ;
6
5
6
+ invariant ( process . env . MONGODB_URI , "MongoDB URI is not defined." ) ;
7
+ const mongoClient = new MongoClient ( process . env . MONGODB_URI ) ;
8
+ const clientPromise = mongoClient . connect ( ) ;
9
+
7
10
const saveLocatorsInteraction = async ( type : "PNG" | "DDS" , state : State ) => {
8
- const { AIRTABLE_API_KEY , AIRTABLE_BASE , AIRTABLE_TRAITS_TABLE } =
11
+ const { MONGODB_DATABASE , MONGODB_TRAITS_COLLECTION } =
9
12
process . env ;
10
13
const {
11
14
name,
@@ -17,37 +20,24 @@ const saveLocatorsInteraction = async (type: "PNG" | "DDS", state: State) => {
17
20
iconYOffset,
18
21
} = state ;
19
22
20
- invariant ( AIRTABLE_API_KEY , "Airtable API key is not defined." ) ;
21
- invariant ( AIRTABLE_BASE , "Airtable base is not defined." ) ;
22
- invariant ( AIRTABLE_TRAITS_TABLE , "Airtable table id is not defined." ) ;
23
+ invariant ( MONGODB_DATABASE , "Mongo DB name is not defined." ) ;
24
+ invariant ( MONGODB_TRAITS_COLLECTION , "Mongo DB collection is not defined." ) ;
23
25
24
- Airtable . configure ( {
25
- apiKey : AIRTABLE_API_KEY ,
26
- endpointUrl : "https://api.airtable.com" ,
27
- } ) ;
28
- const base = Airtable . base ( AIRTABLE_BASE ) ;
26
+ const database = ( await clientPromise ) . db ( MONGODB_DATABASE ) ;
27
+ const collection = database . collection ( MONGODB_TRAITS_COLLECTION ) ;
29
28
30
- return new Promise ( ( resolve , reject ) => {
31
- base ( AIRTABLE_TRAITS_TABLE ) . create (
32
- {
33
- name,
34
- format : type ,
35
- bg_color : bgColor ,
36
- recolor_icon : recolorIcon ,
37
- icon_color : iconColor ,
38
- icon_scale : iconScale ,
39
- icon_x_offset : iconXOffset ,
40
- icon_y_offset : iconYOffset ,
41
- created_at : new Date ( ) . toISOString ( ) ,
42
- } ,
43
- ( err ) => {
44
- if ( err ) {
45
- reject ( err ) ;
46
- }
47
- resolve ( true ) ;
48
- }
49
- ) ;
29
+ await collection . insertOne ( {
30
+ name,
31
+ format : type ,
32
+ bg_color : bgColor ,
33
+ recolor_icon : recolorIcon ,
34
+ icon_color : iconColor ,
35
+ icon_scale : iconScale ,
36
+ icon_x_offset : iconXOffset ,
37
+ icon_y_offset : iconYOffset ,
38
+ created_at : new Date ( ) ,
50
39
} ) ;
40
+
51
41
} ;
52
42
53
43
const headers = {
@@ -57,7 +47,7 @@ const headers = {
57
47
} ;
58
48
59
49
const handler : Handler = async ( event , _context ) => {
60
- const response : Response = {
50
+ const response : HandlerResponse = {
61
51
statusCode : 200 ,
62
52
headers,
63
53
} ;
0 commit comments