@1auth/store-sql 0.0.0-alpha.10

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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/index.js +101 -0
  3. 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,101 @@
1
+ const options = {
2
+ log: false,
3
+ useGeneratedIdentityAsId: false, // aka SERIAL
4
+ query: (sql, parameters) => {
5
+ console.log('sql', sql, parameters)
6
+ return {}
7
+ }
8
+ }
9
+
10
+ export default (params) => {
11
+ Object.assign(options, params)
12
+ }
13
+
14
+ export const exists = async (table, filters) => {
15
+ const item = await select(table, filters)
16
+ return item?.sub
17
+ }
18
+
19
+ export const selectList = async (table, filters = {}, fields = []) => {
20
+ const { select, where, parameters } = makeSqlParts(filters, {}, fields)
21
+ const sql = `SELECT ${select} FROM ${table} ${where}`
22
+ return await options.query(sql, parameters)
23
+ }
24
+
25
+ export const select = async (table, filters = {}, fields = []) => {
26
+ const { select, where, parameters } = makeSqlParts(filters, {}, fields)
27
+ const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
28
+ return await options.query(sql, parameters).then((res) => res[0])
29
+ }
30
+
31
+ export const insert = async (table, values = {}) => {
32
+ if (options.useGeneratedIdentityAsId) {
33
+ delete values.id
34
+ }
35
+ normalizeValues(values)
36
+ const { insert, parameters } = makeSqlParts({}, values)
37
+ const sql = `INSERT INTO ${table} ${insert}`
38
+ return await options.query(sql, parameters)
39
+ }
40
+
41
+ export const update = async (table, filters = {}, values = {}) => {
42
+ normalizeValues(values)
43
+ const { update, where, parameters } = makeSqlParts(filters, values)
44
+ const sql = `UPDATE ${table} SET ${update} ${where}`
45
+ await options.query(sql, parameters)
46
+ }
47
+
48
+ export const remove = async (table, filters = {}) => {
49
+ const { where, parameters } = makeSqlParts(filters)
50
+ const sql = `DELETE FROM ${table} ${where}`
51
+ await options.query(sql, parameters)
52
+ }
53
+
54
+ const normalizeValues = (values) => {
55
+ values.create &&= new Date(values.create * 1000).toISOString()
56
+ values.update &&= new Date(values.update * 1000).toISOString()
57
+ values.verify &&= new Date(values.verify * 1000).toISOString()
58
+ values.expire &&= new Date(values.expire * 1000).toISOString()
59
+ }
60
+
61
+ // export for testing
62
+ export const makeSqlParts = (filters = {}, values = {}, fields = []) => {
63
+ let parameters = []
64
+ let idx = 1
65
+ const keys = Object.keys(values)
66
+
67
+ const select = fields.length ? fields.join(', ') : '*'
68
+
69
+ const insertParts = []
70
+ const updateParts = []
71
+ for (const key of keys) {
72
+ insertParts.push('$' + idx)
73
+ updateParts.push(key + ' = $' + idx)
74
+ idx++
75
+ }
76
+
77
+ const insert =
78
+ '("' + keys.join('", "') + '") VALUES (' + insertParts.join(', ') + ')'
79
+
80
+ const update = updateParts.join(', ')
81
+
82
+ parameters = parameters.concat(Object.values(values))
83
+
84
+ let where = Object.keys(filters)
85
+ where = where
86
+ .map((key) => {
87
+ if (Array.isArray(filters[key])) {
88
+ let sql = filters[key].map((v, vidx) => '$' + idx++).join(',')
89
+ sql = key + ' IN (' + sql + ')'
90
+ parameters = parameters.concat(filters[key])
91
+ return sql
92
+ }
93
+ const sql = key + ' = $' + idx++
94
+ parameters.push(filters[key])
95
+ return sql
96
+ })
97
+ .join(' AND ')
98
+ where &&= `WHERE ${where}`
99
+
100
+ return { select, insert, update, where, parameters }
101
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@1auth/store-sql",
3
+ "version": "0.0.0-alpha.10",
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": "40d015ce640f9813d809fddb847f60974c596b46",
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
+ }