@platformatic/sql-mapper 0.46.1 → 0.47.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-mapper",
3
- "version": "0.46.1",
3
+ "version": "0.47.0",
4
4
  "description": "A data mapper utility for SQL databases",
5
5
  "main": "mapper.js",
6
6
  "types": "mapper.d.ts",
@@ -31,7 +31,7 @@
31
31
  "camelcase": "^6.3.0",
32
32
  "fastify-plugin": "^4.5.0",
33
33
  "inflected": "^2.1.0",
34
- "@platformatic/utils": "0.46.1"
34
+ "@platformatic/utils": "0.47.0"
35
35
  },
36
36
  "tsd": {
37
37
  "directory": "test/types"
@@ -1,162 +0,0 @@
1
- 'use strict'
2
-
3
- const { clear, connInfo, isSQLite, isMysql, isPg } = require('./helper')
4
- const { test } = require('tap')
5
- const { connect } = require('..')
6
- const fakeLogger = {
7
- trace: () => {},
8
- // trace: console.log,
9
- error: () => {}
10
- }
11
-
12
- test('composite primary keys', async ({ equal, same, teardown, rejects }) => {
13
- /* https://github.com/platformatic/platformatic/issues/299 */
14
- async function onDatabaseLoad (db, sql) {
15
- await clear(db, sql)
16
- teardown(() => db.dispose())
17
-
18
- if (isSQLite) {
19
- await db.query(sql`CREATE TABLE pages (
20
- id INTEGER PRIMARY KEY,
21
- the_title VARCHAR(42)
22
- );`)
23
-
24
- await db.query(sql`CREATE TABLE users (
25
- id INTEGER PRIMARY KEY,
26
- username VARCHAR(255) NOT NULL
27
- );`)
28
-
29
- await db.query(sql`CREATE TABLE editors (
30
- page_id INTEGER NOT NULL,
31
- user_id INTEGER NOT NULL,
32
- role VARCHAR(255) NOT NULL,
33
- CONSTRAINT fk_editor_pages FOREIGN KEY (page_id) REFERENCES pages(id),
34
- CONSTRAINT fk_editor_users FOREIGN KEY (user_id) REFERENCES users(id),
35
- PRIMARY KEY (page_id, user_id)
36
- );`)
37
- } else if (isPg) {
38
- await db.query(sql`CREATE TABLE pages (
39
- id SERIAL PRIMARY KEY,
40
- the_title VARCHAR(255) NOT NULL
41
- );`)
42
-
43
- await db.query(sql`CREATE TABLE users (
44
- id SERIAL PRIMARY KEY,
45
- username VARCHAR(255) NOT NULL
46
- );`)
47
-
48
- await db.query(sql`CREATE TABLE editors (
49
- page_id INTEGER NOT NULL,
50
- user_id INTEGER NOT NULL,
51
- role VARCHAR(255) NOT NULL,
52
- CONSTRAINT fk_editor_pages FOREIGN KEY (page_id) REFERENCES pages(id),
53
- CONSTRAINT fk_editor_users FOREIGN KEY (user_id) REFERENCES users(id),
54
- PRIMARY KEY (page_id, user_id)
55
- );`)
56
- } else if (isMysql) {
57
- await db.query(sql`CREATE TABLE pages (
58
- id INTEGER PRIMARY KEY AUTO_INCREMENT,
59
- the_title VARCHAR(255) NOT NULL
60
- );`)
61
-
62
- await db.query(sql`CREATE TABLE users (
63
- id INTEGER PRIMARY KEY AUTO_INCREMENT,
64
- username VARCHAR(255) NOT NULL
65
- );`)
66
-
67
- await db.query(sql`CREATE TABLE editors (
68
- page_id INTEGER NOT NULL,
69
- user_id INTEGER NOT NULL,
70
- role VARCHAR(255) NOT NULL,
71
- CONSTRAINT \`fk_editor_pages\` FOREIGN KEY (page_id) REFERENCES pages (id) ON DELETE CASCADE ON UPDATE RESTRICT,
72
- CONSTRAINT \`fk_editor_users\` FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT,
73
- PRIMARY KEY (page_id, user_id)
74
- );`)
75
- }
76
- }
77
- const mapper = await connect({
78
- connectionString: connInfo.connectionString,
79
- log: fakeLogger,
80
- onDatabaseLoad,
81
- ignore: {},
82
- hooks: {}
83
- })
84
- const pageEntity = mapper.entities.page
85
- const userEntity = mapper.entities.user
86
- const editorEntity = mapper.entities.editor
87
-
88
- const page = await pageEntity.save({
89
- input: { theTitle: 'foobar' }
90
- })
91
- same(page, { id: '1', theTitle: 'foobar' })
92
-
93
- const user = await userEntity.save({
94
- input: { username: 'mcollina' }
95
- })
96
- same(user, { id: '1', username: 'mcollina' })
97
-
98
- const user2 = await userEntity.save({
99
- input: { username: 'lucamaraschi' }
100
- })
101
- same(user2, { id: '2', username: 'lucamaraschi' })
102
-
103
- const editor1 = await editorEntity.save({
104
- input: {
105
- pageId: '1',
106
- userId: '1',
107
- role: 'admin'
108
- }
109
- })
110
- same(editor1, { pageId: '1', userId: '1', role: 'admin' })
111
-
112
- const editor2 = await editorEntity.save({
113
- input: {
114
- pageId: '1',
115
- userId: '2',
116
- role: 'author'
117
- }
118
- })
119
- same(editor2, { pageId: '1', userId: '2', role: 'author' })
120
-
121
- await editorEntity.save({
122
- input: {
123
- pageId: '1',
124
- userId: '1',
125
- role: 'captain'
126
- }
127
- })
128
-
129
- const editors = await editorEntity.find({ orderBy: [{ field: 'userId', direction: 'ASC' }] })
130
- same(editors, [{
131
- pageId: '1',
132
- userId: '1',
133
- role: 'captain'
134
- }, {
135
- pageId: '1',
136
- userId: '2',
137
- role: 'author'
138
- }])
139
-
140
- await editorEntity.delete({})
141
-
142
- const editorsInserted = await editorEntity.insert({
143
- inputs: [{
144
- pageId: '1',
145
- userId: '1',
146
- role: 'admin'
147
- }, {
148
- pageId: '1',
149
- userId: '2',
150
- role: 'author'
151
- }]
152
- })
153
- same(editorsInserted, [{
154
- pageId: '1',
155
- userId: '1',
156
- role: 'admin'
157
- }, {
158
- pageId: '1',
159
- userId: '2',
160
- role: 'author'
161
- }])
162
- })
@@ -1,39 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { clear, connInfo, isSQLite } = require('./helper')
5
- const { createConnectionPool } = require('..')
6
- const fakeLogger = {
7
- trace: () => {},
8
- error: () => {}
9
- }
10
-
11
- test('createConnectionPool', async ({ equal, same, teardown, rejects }) => {
12
- const { db, sql } = await createConnectionPool({
13
- connectionString: connInfo.connectionString,
14
- log: fakeLogger
15
- })
16
- await clear(db, sql)
17
- teardown(() => db.dispose())
18
- if (isSQLite) {
19
- await db.query(sql`CREATE TABLE pages (
20
- id INTEGER PRIMARY KEY,
21
- the_title VARCHAR(42),
22
- is_published BOOLEAN NOT NULL
23
- );`)
24
- } else {
25
- await db.query(sql`CREATE TABLE pages (
26
- id SERIAL PRIMARY KEY,
27
- the_title VARCHAR(255) NOT NULL,
28
- is_published BOOLEAN NOT NULL
29
- );`)
30
- }
31
- await db.query(sql`INSERT INTO pages (the_title, is_published) VALUES ('foo', true)`)
32
-
33
- const res = await db.query(sql`SELECT * FROM pages`)
34
- same(res, [{
35
- id: 1,
36
- the_title: 'foo',
37
- is_published: true
38
- }])
39
- })