@1auth/store-sql 0.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/index.js +97 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 will Farrell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const options = {
|
|
2
|
+
log: false,
|
|
3
|
+
query: (sql, parameters) => {
|
|
4
|
+
console.log('sql', sql, parameters)
|
|
5
|
+
return {}
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default (params) => {
|
|
10
|
+
Object.assign(options, params)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const exists = async (table, filters) => {
|
|
14
|
+
const item = await select(table, filters)
|
|
15
|
+
return item?.sub
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const selectList = async (table, filters = {}, fields = []) => {
|
|
19
|
+
const { select, where, parameters } = makeSqlParts(filters, {}, fields)
|
|
20
|
+
let sql = `SELECT ${select} FROM ${table} ${where}`
|
|
21
|
+
return await options.query(sql, parameters)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const select = async (table, filters = {}, fields = []) => {
|
|
25
|
+
const { select, where, parameters } = makeSqlParts(filters, {}, fields)
|
|
26
|
+
let sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
|
|
27
|
+
return await options.query(sql, parameters).then((res) => res[0])
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const insert = async (table, values = {}) => {
|
|
31
|
+
normalizeValues(values)
|
|
32
|
+
const { insert, parameters } = makeSqlParts({}, values)
|
|
33
|
+
let sql = `INSERT INTO ${table} ${insert}`
|
|
34
|
+
return await options.query(sql, parameters).then((res) => res.id)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const update = async (table, filters = {}, values = {}) => {
|
|
38
|
+
normalizeValues(values)
|
|
39
|
+
const { update, where, parameters } = makeSqlParts(filters, values)
|
|
40
|
+
let sql = `UPDATE ${table} SET ${update} ${where}`
|
|
41
|
+
await options.query(sql, parameters)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const remove = async (table, filters = {}) => {
|
|
45
|
+
const { where, parameters } = makeSqlParts(filters)
|
|
46
|
+
let sql = `DELETE FROM ${table} ${where}`
|
|
47
|
+
await options.query(sql, parameters)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const normalizeValues = (values) => {
|
|
51
|
+
values.create &&= new Date(values.create * 1000).toISOString()
|
|
52
|
+
values.update &&= new Date(values.update * 1000).toISOString()
|
|
53
|
+
values.verify &&= new Date(values.verify * 1000).toISOString()
|
|
54
|
+
values.expire &&= new Date(values.expire * 1000).toISOString()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// export for testing
|
|
58
|
+
export const makeSqlParts = (filters = {}, values = {}, fields = []) => {
|
|
59
|
+
let parameters = []
|
|
60
|
+
let idx = 1
|
|
61
|
+
const keys = Object.keys(values)
|
|
62
|
+
|
|
63
|
+
const select = fields.length ? fields.join(', ') : '*'
|
|
64
|
+
|
|
65
|
+
const insertParts = []
|
|
66
|
+
const updateParts = []
|
|
67
|
+
for (const key of keys) {
|
|
68
|
+
insertParts.push('$' + idx)
|
|
69
|
+
updateParts.push(key + ' = $' + idx)
|
|
70
|
+
idx++
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let insert =
|
|
74
|
+
'("' + keys.join('", "') + '") VALUES (' + insertParts.join(', ') + ')'
|
|
75
|
+
|
|
76
|
+
let update = updateParts.join(', ')
|
|
77
|
+
|
|
78
|
+
parameters = parameters.concat(Object.values(values))
|
|
79
|
+
|
|
80
|
+
let where = Object.keys(filters)
|
|
81
|
+
where = where
|
|
82
|
+
.map((key) => {
|
|
83
|
+
if (Array.isArray(filters[key])) {
|
|
84
|
+
let sql = filters[key].map((v, vidx) => '$' + idx++).join(',')
|
|
85
|
+
sql = key + ' IN (' + sql + ')'
|
|
86
|
+
parameters = parameters.concat(filters[key])
|
|
87
|
+
return sql
|
|
88
|
+
}
|
|
89
|
+
const sql = key + ' = $' + idx++
|
|
90
|
+
parameters.push(filters[key])
|
|
91
|
+
return sql
|
|
92
|
+
})
|
|
93
|
+
.join(' AND ')
|
|
94
|
+
where &&= `WHERE ${where}`
|
|
95
|
+
|
|
96
|
+
return { select, insert, update, where, parameters }
|
|
97
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1auth/store-sql",
|
|
3
|
+
"version": "0.0.0-alpha.4",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=16"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"main": "./index.js",
|
|
14
|
+
"module": "./index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"default": "./index.js"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"types": "index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"index.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "npm run test:unit",
|
|
30
|
+
"test:unit": "ava"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"funding": {
|
|
34
|
+
"type": "github",
|
|
35
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [],
|
|
38
|
+
"author": {
|
|
39
|
+
"name": "1auth contributors",
|
|
40
|
+
"url": "https://github.com/willfarrell/1auth/graphs/contributors"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "github:willfarrell/1auth",
|
|
45
|
+
"directory": "packages/store-dynamodb"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/willfarrell/1auth/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/willfarrell/1auth",
|
|
51
|
+
"gitHead": "47894073ee5f1b6754cef5e482f123dc81575b1c",
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@aws-sdk/client-dynamodb": "^3.0.0",
|
|
54
|
+
"@aws-sdk/util-dynamodb": "^3.0.0",
|
|
55
|
+
"uuid": "9.0.0"
|
|
56
|
+
}
|
|
57
|
+
}
|